From b0826710b53345dedd3ab4d753e7fc48786a0c92 Mon Sep 17 00:00:00 2001 From: Denis Isaev Date: Sat, 2 Jun 2018 18:13:55 +0300 Subject: [PATCH] #52: #36: lint test files by default: set --tests=false to disable it --- .golangci.yml | 3 --- README.md | 5 +---- pkg/commands/run.go | 2 +- test/run_test.go | 11 +++++++---- test/testdata/withtests/p.go | 23 +++++++++++++++++++++++ test/testdata/withtests/p_test.go | 14 ++++++++++++++ 6 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 test/testdata/withtests/p.go create mode 100644 test/testdata/withtests/p_test.go diff --git a/.golangci.yml b/.golangci.yml index ff625fbc..f60eb893 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,3 @@ -run: - tests: true - linters-settings: govet: check-shadowing: true diff --git a/README.md b/README.md index 65be3505..9903c92d 100644 --- a/README.md +++ b/README.md @@ -223,7 +223,7 @@ Flags: --issues-exit-code int Exit code when issues were found (default 1) --build-tags strings Build tags (not all linters support them) --deadline duration Deadline for total work (default 1m0s) - --tests Analyze tests (*_test.go) + --tests Analyze tests (*_test.go) (default true) --print-resources-usage Print avg and max memory usage of golangci-lint and total time -c, --config PATH Read config from file path PATH --no-config Don't read config @@ -293,9 +293,6 @@ There is a [`.golangci.yml`](https://github.com/golangci/golangci-lint/blob/mast It's a [.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml) of this repo: we enable more linters than by default and make their settings more strict: ```yaml -run: - tests: true - linters-settings: govet: check-shadowing: true diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 4d2578d8..dcc7ac66 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -69,7 +69,7 @@ func (e *Executor) initFlagSet(fs *pflag.FlagSet) { 1, wh("Exit code when issues were found")) fs.StringSliceVar(&rc.BuildTags, "build-tags", []string{}, wh("Build tags (not all linters support them)")) fs.DurationVar(&rc.Deadline, "deadline", time.Minute, wh("Deadline for total work")) - fs.BoolVar(&rc.AnalyzeTests, "tests", false, wh("Analyze tests (*_test.go)")) + fs.BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)")) fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false, wh("Print avg and max memory usage of golangci-lint and total time")) fs.StringVarP(&rc.Config, "config", "c", "", wh("Read config from file path `PATH`")) fs.BoolVar(&rc.NoConfig, "no-config", false, wh("Don't read config")) diff --git a/test/run_test.go b/test/run_test.go index e73a2708..2f15f312 100644 --- a/test/run_test.go +++ b/test/run_test.go @@ -20,22 +20,20 @@ func installBinary(t assert.TestingT) { } func TestCongratsMessageIfNoIssues(t *testing.T) { - installBinary(t) - out, exitCode := runGolangciLint(t, "../...") assert.Equal(t, 0, exitCode) assert.Equal(t, "Congrats! No issues were found.\n", out) } func TestDeadline(t *testing.T) { - installBinary(t) - out, exitCode := runGolangciLint(t, "--no-config", "--deadline=1ms", "../...") assert.Equal(t, 4, exitCode) assert.Equal(t, "", out) // no 'Congrats! No issues were found.' } func runGolangciLint(t *testing.T, args ...string) (string, int) { + installBinary(t) + runArgs := append([]string{"run"}, args...) cmd := exec.Command("golangci-lint", runArgs...) out, err := cmd.Output() @@ -54,3 +52,8 @@ func runGolangciLint(t *testing.T, args ...string) (string, int) { ws := cmd.ProcessState.Sys().(syscall.WaitStatus) return string(out), ws.ExitStatus() } + +func TestTestsAreLintedByDefault(t *testing.T) { + out, exitCode := runGolangciLint(t, "--no-config", "./testdata/withtests") + assert.Equal(t, 0, exitCode, out) +} diff --git a/test/testdata/withtests/p.go b/test/testdata/withtests/p.go new file mode 100644 index 00000000..ce0e6b49 --- /dev/null +++ b/test/testdata/withtests/p.go @@ -0,0 +1,23 @@ +package withtests + +import "fmt" + +var varUsedOnlyInTests bool + +func usedOnlyInTests() {} + +type someType struct { + fieldUsedOnlyInTests bool + fieldUsedHere bool +} + +func usedHere() { + v := someType{ + fieldUsedHere: true, + } + fmt.Println(v) +} + +func init() { + usedHere() +} diff --git a/test/testdata/withtests/p_test.go b/test/testdata/withtests/p_test.go new file mode 100644 index 00000000..eaaba6a0 --- /dev/null +++ b/test/testdata/withtests/p_test.go @@ -0,0 +1,14 @@ +package withtests + +import ( + "fmt" + "testing" +) + +func TestSomething(t *testing.T) { + v := someType{ + fieldUsedOnlyInTests: true, + } + fmt.Println(v, varUsedOnlyInTests) + usedOnlyInTests() +}