Skip to content

#52: #36: lint test files by default #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
run:
tests: true

linters-settings:
govet:
check-shadowing: true
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
11 changes: 7 additions & 4 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
23 changes: 23 additions & 0 deletions test/testdata/withtests/p.go
Original file line number Diff line number Diff line change
@@ -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()
}
14 changes: 14 additions & 0 deletions test/testdata/withtests/p_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package withtests

import (
"fmt"
"testing"
)

func TestSomething(t *testing.T) {
v := someType{
fieldUsedOnlyInTests: true,
}
fmt.Println(v, varUsedOnlyInTests)
usedOnlyInTests()
}