Skip to content

Commit

Permalink
#52: #36: lint test files by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jirfag committed Jun 2, 2018
1 parent 7dfc117 commit defc4a9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
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()
}

0 comments on commit defc4a9

Please sign in to comment.