Skip to content

Commit

Permalink
Improve error message in the case of an unknown linter (alecthomas#370)
Browse files Browse the repository at this point in the history
* Fix error message in the case of an unknown linter

Discovered in alecthomas#369

When an unknown linter is enabled, raise an error identifying which linter(s) are unknown, rather than failing with:

    WARNING: invalid command ""

Note: linters defined with the --linter flag are not considered unknown and will not fail with this error.

* address feedback

- move validateLinters into linters.go
- test the happy path too
- remove unnecessary setup in test

* clean up error handling
  • Loading branch information
zmb3 authored and dnephin committed Oct 11, 2017
1 parent 33811f3 commit cdf1c4a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions linters.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ func defaultEnabled() []string {
return enabled
}

func validateLinters(linters map[string]*Linter, config *Config) error {
var unknownLinters []string
for name := range linters {
if _, isDefault := defaultLinters[name]; !isDefault {
if _, isCustom := config.Linters[name]; !isCustom {
unknownLinters = append(unknownLinters, name)
}
}
}
if len(unknownLinters) > 0 {
return fmt.Errorf("unknown linters: %s", strings.Join(unknownLinters, ", "))
}
return nil
}

const vetPattern = `^(?:vet:.*?\.go:\s+(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*))|(?:(?P<path>.*?\.go):(?P<line>\d+):\s*(?P<message>.*))$`

var defaultLinters = map[string]LinterConfig{
Expand Down
18 changes: 18 additions & 0 deletions linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ func TestGetLinterByName(t *testing.T) {
assert.Equal(t, config.IsFast, overrideConfig.IsFast)
}

func TestValidateLinters(t *testing.T) {
originalConfig := *config
defer func() { config = &originalConfig }()

config = &Config{
Enable: []string{"_dummylinter_"},
}

err := validateLinters(lintersFromConfig(config), config)
require.Error(t, err, "expected unknown linter error for _dummylinter_")

config = &Config{
Enable: defaultEnabled(),
}
err = validateLinters(lintersFromConfig(config), config)
require.NoError(t, err)
}

func functionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ Severity override map (default is "warning"):
paths := resolvePaths(*pathsArg, config.Skip)

linters := lintersFromConfig(config)
err := validateLinters(linters, config)
kingpin.FatalIfError(err, "")

issues, errch := runLinters(linters, paths, config.Concurrency, exclude, include)
status := 0
if config.JSON {
Expand Down

0 comments on commit cdf1c4a

Please sign in to comment.