Skip to content

Commit defc4a9

Browse files
committed
#52: #36: lint test files by default
1 parent 7dfc117 commit defc4a9

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

.golangci.yml

-3
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

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

294294
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:
295295
```yaml
296-
run:
297-
tests: true
298-
299296
linters-settings:
300297
govet:
301298
check-shadowing: true

pkg/commands/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (e *Executor) initFlagSet(fs *pflag.FlagSet) {
6969
1, wh("Exit code when issues were found"))
7070
fs.StringSliceVar(&rc.BuildTags, "build-tags", []string{}, wh("Build tags (not all linters support them)"))
7171
fs.DurationVar(&rc.Deadline, "deadline", time.Minute, wh("Deadline for total work"))
72-
fs.BoolVar(&rc.AnalyzeTests, "tests", false, wh("Analyze tests (*_test.go)"))
72+
fs.BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)"))
7373
fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false, wh("Print avg and max memory usage of golangci-lint and total time"))
7474
fs.StringVarP(&rc.Config, "config", "c", "", wh("Read config from file path `PATH`"))
7575
fs.BoolVar(&rc.NoConfig, "no-config", false, wh("Don't read config"))

test/run_test.go

+7-4
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

+23
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

+14
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)