github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/apps/gunit/config/config_test.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package config_test
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/palantir/pkg/matcher"
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/palantir/godel/apps/gunit/config"
    27  )
    28  
    29  func TestLoadConfig(t *testing.T) {
    30  	for i, currCase := range []struct {
    31  		yml           string
    32  		json          string
    33  		want          config.GUnit
    34  		wantParamKeys map[string]struct{}
    35  	}{
    36  		{
    37  			yml: `
    38  			tags:
    39  			  integration:
    40  			    names:
    41  			      - "integration_tests"
    42  			    paths:
    43  			      - "test"
    44  			    exclude:
    45  			      names:
    46  			        - "ignore"
    47  			      paths:
    48  			        - "test/foo"
    49  			exclude:
    50  			  names:
    51  			    - ".*test"
    52  			    - "m?cks"
    53  			  paths:
    54  			    - "vendor"
    55  			`,
    56  			json: `{"exclude":{"names":["gunit"],"paths":["generated_src"]}}`,
    57  			want: config.GUnit{
    58  				Tags: map[string]matcher.NamesPathsWithExcludeCfg{
    59  					"integration": {
    60  						NamesPathsCfg: matcher.NamesPathsCfg{
    61  							Names: []string{`integration_tests`},
    62  							Paths: []string{`test`},
    63  						},
    64  						Exclude: matcher.NamesPathsCfg{
    65  							Names: []string{`ignore`},
    66  							Paths: []string{`test/foo`},
    67  						},
    68  					},
    69  				},
    70  				Exclude: matcher.NamesPathsCfg{
    71  					Names: []string{`.*test`, `m?cks`, `gunit`},
    72  					Paths: []string{`vendor`, `generated_src`},
    73  				},
    74  			},
    75  			wantParamKeys: map[string]struct{}{
    76  				"integration": {},
    77  			},
    78  		},
    79  		{
    80  			yml: `
    81  			tags:
    82  			  integration:
    83  			    names:
    84  			      - "integration_tests"
    85  			  mixedCasing:
    86  			    paths:
    87  			      - "test"
    88  			`,
    89  			want: config.GUnit{
    90  				Tags: map[string]matcher.NamesPathsWithExcludeCfg{
    91  					"integration": {
    92  						NamesPathsCfg: matcher.NamesPathsCfg{
    93  							Names: []string{`integration_tests`},
    94  						},
    95  					},
    96  					"mixedCasing": {
    97  						NamesPathsCfg: matcher.NamesPathsCfg{
    98  							Paths: []string{`test`},
    99  						},
   100  					},
   101  				},
   102  			},
   103  			wantParamKeys: map[string]struct{}{
   104  				"integration": {},
   105  				"mixedcasing": {},
   106  			},
   107  		},
   108  	} {
   109  		got, err := config.LoadRawConfig(unindent(currCase.yml), currCase.json)
   110  		require.NoError(t, err, "Case %d", i)
   111  		p := got.ToParams()
   112  		err = p.Validate()
   113  		require.NoError(t, err, "Case %d", i)
   114  		assert.Equal(t, currCase.want, got, "Case %d", i)
   115  
   116  		gotParamKeys := make(map[string]struct{})
   117  		for k := range p.Tags {
   118  			gotParamKeys[k] = struct{}{}
   119  		}
   120  		assert.Equal(t, currCase.wantParamKeys, gotParamKeys, "Case %d", i)
   121  	}
   122  }
   123  
   124  func TestLoadInvalidConfig(t *testing.T) {
   125  	for i, currCase := range []struct {
   126  		name      string
   127  		yml       string
   128  		wantError string
   129  	}{
   130  		{
   131  			name: "tags cannot contain illegal characters",
   132  			yml: `
   133  			tags:
   134  			  integration:
   135  			    names:
   136  			      - "integration_tests"
   137  			  foo-bar:
   138  			    paths:
   139  			     - "foo-bar"
   140  			  foo_bar:
   141  			    names:
   142  			      - "foo_bar"
   143  			  "invalid,entry":
   144  			    names:
   145  			      - "invalid"
   146  			  "another bad":
   147  			    names:
   148  			      - "another bad"
   149  			`,
   150  			wantError: "invalid tag names: [another bad invalid,entry]",
   151  		},
   152  		{
   153  			name: "tags must be unique in a case-insensitive manner",
   154  			yml: `
   155  			tags:
   156  			  integration:
   157  			    names:
   158  			      - "integration_tests"
   159  			  INTEGRATION:
   160  			    paths:
   161  			     - "foo-bar"
   162  			`,
   163  			wantError: "tag names were defined multiple times (names must be unique in case-insensitive manner): [integration]",
   164  		},
   165  		{
   166  			name: `"all" is a reserved tag name`,
   167  			yml: `
   168  			tags:
   169  			  all:
   170  			    names:
   171  			      - "integration_tests"
   172  			`,
   173  			wantError: `"all" is a reserved name that cannot be used as a tag name`,
   174  		},
   175  	} {
   176  		got, err := config.LoadRawConfig(unindent(currCase.yml), "")
   177  		require.NoError(t, err, fmt.Sprintf("Case %d: %s", i, currCase.name))
   178  		p := got.ToParams()
   179  		err = p.Validate()
   180  		require.Error(t, err, fmt.Sprintf("Case %d: %s", i, currCase.name))
   181  		assert.Equal(t, currCase.wantError, err.Error(), "Case %d: %s", i, currCase.name)
   182  	}
   183  }
   184  
   185  func unindent(input string) string {
   186  	return strings.Replace(input, "\n\t\t\t", "\n", -1)
   187  }