Skip to content

Commit 8a09f69

Browse files
committed
#52: #36: lint test files by default
1 parent 594197d commit 8a09f69

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

.golangci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
run:
2-
tests: true
3-
41
linters-settings:
52
govet:
63
check-shadowing: true

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ Flags:
219219
--issues-exit-code int Exit code when issues were found (default 1)
220220
--build-tags strings Build tags (not all linters support them)
221221
--deadline duration Deadline for total work (default 1m0s)
222-
--tests Analyze tests (*_test.go)
222+
--tests Analyze tests (*_test.go) (default true)
223223
--print-resources-usage Print avg and max memory usage of golangci-lint and total time
224224
-c, --config PATH Read config from file path PATH
225225
--no-config Don't read config
@@ -289,9 +289,6 @@ There is a [`.golangci.yml`](https://github.com/golangci/golangci-lint/blob/mast
289289

290290
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:
291291
```yaml
292-
run:
293-
tests: true
294-
295292
linters-settings:
296293
govet:
297294
check-shadowing: true

pkg/commands/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (e *Executor) initRun() {
7979
1, wh("Exit code when issues were found"))
8080
runCmd.Flags().StringSliceVar(&rc.BuildTags, "build-tags", []string{}, wh("Build tags (not all linters support them)"))
8181
runCmd.Flags().DurationVar(&rc.Deadline, "deadline", time.Minute, wh("Deadline for total work"))
82-
runCmd.Flags().BoolVar(&rc.AnalyzeTests, "tests", false, wh("Analyze tests (*_test.go)"))
82+
runCmd.Flags().BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)"))
8383
runCmd.Flags().BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false, wh("Print avg and max memory usage of golangci-lint and total time"))
8484
runCmd.Flags().StringVarP(&rc.Config, "config", "c", "", wh("Read config from file path `PATH`"))
8585
runCmd.Flags().BoolVar(&rc.NoConfig, "no-config", false, wh("Don't read config"))

test/run_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,20 @@ func installBinary(t assert.TestingT) {
2020
}
2121

2222
func TestCongratsMessageIfNoIssues(t *testing.T) {
23-
installBinary(t)
24-
2523
out, exitCode := runGolangciLint(t, "../...")
2624
assert.Equal(t, 0, exitCode)
2725
assert.Equal(t, "Congrats! No issues were found.\n", out)
2826
}
2927

3028
func TestDeadline(t *testing.T) {
31-
installBinary(t)
32-
3329
out, exitCode := runGolangciLint(t, "--no-config", "--deadline=1ms", "../...")
3430
assert.Equal(t, 4, exitCode)
3531
assert.Equal(t, "", out) // no 'Congrats! No issues were found.'
3632
}
3733

3834
func runGolangciLint(t *testing.T, args ...string) (string, int) {
35+
installBinary(t)
36+
3937
runArgs := append([]string{"run"}, args...)
4038
cmd := exec.Command("golangci-lint", runArgs...)
4139
out, err := cmd.Output()
@@ -54,3 +52,8 @@ func runGolangciLint(t *testing.T, args ...string) (string, int) {
5452
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
5553
return string(out), ws.ExitStatus()
5654
}
55+
56+
func TestTestsAreLintedByDefault(t *testing.T) {
57+
out, exitCode := runGolangciLint(t, "--no-config", "./testdata/withtests")
58+
assert.Equal(t, 0, exitCode, out)
59+
}

test/testdata/withtests/p.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package withtests
2+
3+
import "fmt"
4+
5+
var varUsedOnlyInTests bool
6+
7+
func usedOnlyInTests() {}
8+
9+
type someType struct {
10+
fieldUsedOnlyInTests bool
11+
fieldUsedHere bool
12+
}
13+
14+
func usedHere() {
15+
v := someType{
16+
fieldUsedHere: true,
17+
}
18+
fmt.Println(v)
19+
}
20+
21+
func init() {
22+
usedHere()
23+
}

test/testdata/withtests/p_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package withtests
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestSomething(t *testing.T) {
9+
v := someType{
10+
fieldUsedOnlyInTests: true,
11+
}
12+
fmt.Println(v, varUsedOnlyInTests)
13+
usedOnlyInTests()
14+
}

0 commit comments

Comments
 (0)