diff --git a/linelint.go b/linelint.go index 30ea59e..5609810 100644 --- a/linelint.go +++ b/linelint.go @@ -14,7 +14,7 @@ import ( ) const ( - configFile = "./linelint.yml" + configFile = "./.linelint.yml" helpMsg = `usage of %s [-a] [FILE_OR_DIR [FILE_OR_DIR ...]] Validates simple newline and whitespace rules in all sorts of files. diff --git a/linter/config.go b/linter/config.go index fa09b22..9e04f95 100644 --- a/linter/config.go +++ b/linter/config.go @@ -44,19 +44,20 @@ func NewConfigFromFile(path string) Config { // check if config file exists if _, err := os.Stat(path); err != nil { + fmt.Printf("No configuration file found at %s (will use default configuration)\n", path) return NewDefaultConfig() } // if config file does exist, read it data, err := ioutil.ReadFile(path) if err != nil { - fmt.Printf("Error reading YAML file %s: %s (will use default configuration)\n", path, err) + fmt.Printf("Error reading configuration file %s: %s (will use default configuration)\n", path, err) return NewDefaultConfig() } var config Config if err := yaml.Unmarshal(data, &config); err != nil { - fmt.Printf("Error parsing YAML file: %s (will use default configuration)\n", err) + fmt.Printf("Error parsing configuration file: %s (will use default configuration)\n", err) return NewDefaultConfig() } diff --git a/linter/ignore_test.go b/linter/ignore_test.go index 1ed1de6..10710c6 100644 --- a/linter/ignore_test.go +++ b/linter/ignore_test.go @@ -6,55 +6,73 @@ import ( "gopkg.in/yaml.v2" ) -var ignoreTests = []struct { - file string - ignore bool -}{ - {"README", false}, - {".git/objects/04/9f2973ffc85f71da1fd5a", true}, -} +func TestShouldIgnore_DefaultConf(t *testing.T) { + c := Config{} -var yamlAutofixTestConfig = ` -autofix: true + err := yaml.Unmarshal([]byte(`ignore: [ ".git/" ]`), &c) -ignore: - - .git/ + if err != nil { + t.Fatalf("yaml.Unmarshal(Config): %v", err) + } -rules: - end-of-file: - enable: true - disable-autofix: false - single-new-line: true -` + ignoreTests := []struct { + file string + ignore bool + }{ + {".git/objects/04/9f2973ffc85f71da1fd5a", true}, + {"README", false}, + {"clusters/mycluster/applications/app.yml", false}, + {"java/bin/myclass.class", false}, + } -func TestShouldIgnore_DefaultConf(t *testing.T) { for _, tt := range ignoreTests { t.Run(tt.file, func(t *testing.T) { - got := NewEndOfFileRule(autofixTestConf).ShouldIgnore(tt.file) + got := NewEndOfFileRule(c).ShouldIgnore(tt.file) want := tt.ignore if got != want { - t.Errorf("NewEndOfFileRule(defaultTestConf).ShouldIgnore(%q):\n\tExpected %v, got %v", tt.file, want, got) + t.Errorf( + "NewEndOfFileRule(c).ShouldIgnore(%q):\n\tExpected %v, got %v", + tt.file, want, got, + ) } }) } } -func TestShouldIgnore_YAMLParsedConf(t *testing.T) { +func TestShouldIgnore_MoreComplexConf(t *testing.T) { c := Config{} - err := yaml.Unmarshal([]byte(yamlAutofixTestConfig), &c) + err := yaml.Unmarshal( + []byte(`ignore: [ ".git/", "**/bin/", "applications", "/projects/" ]`), &c, + ) + if err != nil { t.Fatalf("yaml.Unmarshal(Config): %v", err) } + ignoreTests := []struct { + file string + ignore bool + }{ + {".git/objects/04/9f2973ffc85f71da1fd5a", true}, + {"README", false}, + {"clusters/mycluster/applications/app.yml", true}, + {"home/projects/data.md", false}, + {"projects/data.md", true}, + {"java/bin/myclass.class", true}, + } + for _, tt := range ignoreTests { t.Run(tt.file, func(t *testing.T) { got := NewEndOfFileRule(c).ShouldIgnore(tt.file) want := tt.ignore if got != want { - t.Errorf("NewEndOfFileRule(defaultTestConf).ShouldIgnore(%q):\n\tExpected %v, got %v", tt.file, want, got) + t.Errorf( + "NewEndOfFileRule(c).ShouldIgnore(%q):\n\tExpected %v, got %v", + tt.file, want, got, + ) } }) }