Skip to content

Commit

Permalink
Fix default linelint path and gitignore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandrone committed Oct 9, 2020
1 parent 5680d8c commit 4396f2b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion linelint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions linter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
64 changes: 41 additions & 23 deletions linter/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
})
}
Expand Down

0 comments on commit 4396f2b

Please sign in to comment.