github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/okgo/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  	"io/ioutil"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/nmiyake/pkg/dirs"
    24  	"github.com/palantir/pkg/matcher"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  
    28  	"github.com/palantir/godel/apps/okgo/config"
    29  )
    30  
    31  func TestLoadRawConfig(t *testing.T) {
    32  	for i, currCase := range []struct {
    33  		yml  string
    34  		json string
    35  		want config.OKGo
    36  	}{
    37  		{
    38  			yml: `
    39  			checks:
    40  			  errcheck:
    41  			    args:
    42  			      - "-ignore"
    43  			      - "github.com/seelog:(Info|Warn|Error|Critical)f?"
    44  			    filters:
    45  			      - type: "message"
    46  			        value: "\\w+"
    47  			exclude:
    48  			  names:
    49  			    - "m?cks"
    50  			  paths:
    51  			    - "vendor"
    52  			`,
    53  			json: `{"exclude":{"names":["exclude.*"],"paths":["generated_src"]}}`,
    54  			want: config.OKGo{
    55  				Checks: map[string]config.Checker{
    56  					"errcheck": {
    57  						Args: []string{
    58  							"-ignore",
    59  							"github.com/seelog:(Info|Warn|Error|Critical)f?",
    60  						},
    61  						Filters: []config.Filter{
    62  							{
    63  								Type:  "message",
    64  								Value: `\w+`,
    65  							},
    66  						},
    67  					},
    68  				},
    69  				Exclude: matcher.NamesPathsCfg{
    70  					Names: []string{
    71  						"m?cks",
    72  						"exclude.*",
    73  					},
    74  					Paths: []string{
    75  						"vendor",
    76  						"generated_src",
    77  					},
    78  				},
    79  			},
    80  		},
    81  		{
    82  			yml: `
    83  			checks:
    84  			  errcheck:
    85  			    filters:
    86  			      - value: "\\w+"
    87  			`,
    88  			want: config.OKGo{
    89  				Checks: map[string]config.Checker{
    90  					"errcheck": {
    91  						Filters: []config.Filter{
    92  							{
    93  								Value: `\w+`,
    94  							},
    95  						},
    96  					},
    97  				},
    98  			},
    99  		},
   100  	} {
   101  		got, err := config.LoadRawConfig(unindent(currCase.yml), currCase.json)
   102  		assert.Equal(t, currCase.want, got, "Case %d", i)
   103  		require.NoError(t, err, "Case %d", i)
   104  	}
   105  }
   106  
   107  func TestLoadBadConfig(t *testing.T) {
   108  	tmpDir, cleanup, err := dirs.TempDir("", "")
   109  	defer cleanup()
   110  	require.NoError(t, err)
   111  
   112  	for i, currCase := range []struct {
   113  		yml       string
   114  		json      string
   115  		wantError string
   116  	}{
   117  		{
   118  			yml: `
   119  			checks:
   120  			  errcheck:
   121  			    filters:
   122  			      - type: "unknown"
   123  			`,
   124  			wantError: `failed to parse filter: {unknown }: unknown filter type: unknown`,
   125  		},
   126  		{
   127  			json:      `[}`,
   128  			wantError: `failed to parse JSON [}: invalid character '}' looking for beginning of value`,
   129  		},
   130  	} {
   131  		var path string
   132  		if currCase.yml != "" {
   133  			tmpFile, err := ioutil.TempFile(tmpDir, "")
   134  			require.NoError(t, err, "Case %d", i)
   135  			err = ioutil.WriteFile(tmpFile.Name(), []byte(unindent(currCase.yml)), 0644)
   136  			require.NoError(t, err, "Case %d", i)
   137  			path = tmpFile.Name()
   138  		}
   139  
   140  		_, err = config.Load(path, currCase.json)
   141  		require.Error(t, err, fmt.Sprintf("Case %d", i))
   142  		require.EqualError(t, err, currCase.wantError, "Case %d", i)
   143  	}
   144  }
   145  
   146  func unindent(input string) string {
   147  	return strings.Replace(input, "\n\t\t\t", "\n", -1)
   148  }