1
1
package test
2
2
3
3
import (
4
+ "fmt"
5
+ "io/ioutil"
6
+ "log"
7
+ "os"
4
8
"os/exec"
5
9
"path/filepath"
10
+ "sort"
11
+ "strings"
6
12
"sync"
7
13
"syscall"
8
14
"testing"
9
15
16
+ "github.com/golangci/golangci-lint/pkg/lint/lintersdb"
17
+
10
18
"github.com/stretchr/testify/assert"
11
19
)
12
20
@@ -32,15 +40,17 @@ func TestCongratsMessageIfNoIssues(t *testing.T) {
32
40
func TestDeadline (t * testing.T ) {
33
41
out , exitCode := runGolangciLint (t , "--deadline=1ms" , "../..." )
34
42
assert .Equal (t , 4 , exitCode )
35
- assert .Equal (t , "" , out ) // no 'Congrats! No issues were found.'
43
+ assert .Contains (t , out , "deadline exceeded: try increase it by passing --deadline option" )
44
+ assert .NotContains (t , out , "Congrats! No issues were found." )
36
45
}
37
46
38
47
func runGolangciLint (t * testing.T , args ... string ) (string , int ) {
39
48
installBinary (t )
40
49
41
50
runArgs := append ([]string {"run" }, args ... )
51
+ log .Print (runArgs )
42
52
cmd := exec .Command ("golangci-lint" , runArgs ... )
43
- out , err := cmd .Output ()
53
+ out , err := cmd .CombinedOutput ()
44
54
if err != nil {
45
55
if exitError , ok := err .(* exec.ExitError ); ok {
46
56
t .Logf ("stderr: %s" , exitError .Stderr )
@@ -57,6 +67,30 @@ func runGolangciLint(t *testing.T, args ...string) (string, int) {
57
67
return string (out ), ws .ExitStatus ()
58
68
}
59
69
70
+ func runGolangciLintWithYamlConfig (t * testing.T , cfg string , args ... string ) string {
71
+ f , err := ioutil .TempFile ("" , "golangci_lint_test" )
72
+ assert .NoError (t , err )
73
+ f .Close ()
74
+
75
+ cfgPath := f .Name () + ".yml"
76
+ err = os .Rename (f .Name (), cfgPath )
77
+ assert .NoError (t , err )
78
+
79
+ defer os .Remove (cfgPath )
80
+
81
+ cfg = strings .TrimSpace (cfg )
82
+ cfg = strings .Replace (cfg , "\t " , " " , - 1 )
83
+
84
+ err = ioutil .WriteFile (cfgPath , []byte (cfg ), os .ModePerm )
85
+ assert .NoError (t , err )
86
+
87
+ pargs := append ([]string {"-c" , cfgPath }, args ... )
88
+ out , ec := runGolangciLint (t , pargs ... )
89
+ assert .Equal (t , 0 , ec )
90
+
91
+ return out
92
+ }
93
+
60
94
func TestTestsAreLintedByDefault (t * testing.T ) {
61
95
out , exitCode := runGolangciLint (t , "./testdata/withtests" )
62
96
assert .Equal (t , 0 , exitCode , out )
@@ -74,3 +108,119 @@ func TestConfigFileIsDetected(t *testing.T) {
74
108
out , exitCode := runGolangciLint (t ) // doesn't detect when no args
75
109
checkNoIssuesRun (t , out , exitCode )
76
110
}
111
+
112
+ func inSlice (s []string , v string ) bool {
113
+ for _ , sv := range s {
114
+ if sv == v {
115
+ return true
116
+ }
117
+ }
118
+
119
+ return false
120
+ }
121
+
122
+ func getEnabledByDefaultFastLintersExcept (except ... string ) []string {
123
+ ebdl := lintersdb .GetAllEnabledByDefaultLinters ()
124
+ ret := []string {}
125
+ for _ , linter := range ebdl {
126
+ if linter .DoesFullImport {
127
+ continue
128
+ }
129
+
130
+ if ! inSlice (except , linter .Linter .Name ()) {
131
+ ret = append (ret , linter .Linter .Name ())
132
+ }
133
+ }
134
+
135
+ return ret
136
+ }
137
+
138
+ func getEnabledByDefaultFastLintersWith (with ... string ) []string {
139
+ ebdl := lintersdb .GetAllEnabledByDefaultLinters ()
140
+ ret := append ([]string {}, with ... )
141
+ for _ , linter := range ebdl {
142
+ if linter .DoesFullImport {
143
+ continue
144
+ }
145
+
146
+ ret = append (ret , linter .Linter .Name ())
147
+ }
148
+
149
+ return ret
150
+ }
151
+
152
+ func mergeMegacheck (linters []string ) []string {
153
+ if inSlice (linters , "staticcheck" ) &&
154
+ inSlice (linters , "gosimple" ) &&
155
+ inSlice (linters , "unused" ) {
156
+ ret := []string {"megacheck" }
157
+ for _ , linter := range linters {
158
+ if ! inSlice ([]string {"staticcheck" , "gosimple" , "unused" }, linter ) {
159
+ ret = append (ret , linter )
160
+ }
161
+ }
162
+
163
+ return ret
164
+ }
165
+
166
+ return linters
167
+ }
168
+
169
+ func TestEnabledLinters (t * testing.T ) {
170
+ type tc struct {
171
+ name string
172
+ cfg string
173
+ el []string
174
+ args string
175
+ }
176
+
177
+ cases := []tc {
178
+ {
179
+ name : "disable govet in config" ,
180
+ cfg : `
181
+ linters:
182
+ disable:
183
+ - govet
184
+ ` ,
185
+ el : getEnabledByDefaultFastLintersExcept ("govet" ),
186
+ },
187
+ {
188
+ name : "enable golint in config" ,
189
+ cfg : `
190
+ linters:
191
+ enable:
192
+ - golint
193
+ ` ,
194
+ el : getEnabledByDefaultFastLintersWith ("golint" ),
195
+ },
196
+ {
197
+ name : "disable govet in cmd" ,
198
+ args : "-Dgovet" ,
199
+ el : getEnabledByDefaultFastLintersExcept ("govet" ),
200
+ },
201
+ {
202
+ name : "enable gofmt in cmd and enable golint in config" ,
203
+ args : "-Egofmt" ,
204
+ cfg : `
205
+ linters:
206
+ enable:
207
+ - golint
208
+ ` ,
209
+ el : getEnabledByDefaultFastLintersWith ("golint" , "gofmt" ),
210
+ },
211
+ }
212
+
213
+ for _ , c := range cases {
214
+ t .Run (c .name , func (t * testing.T ) {
215
+ runArgs := []string {"-v" , "--fast" }
216
+ if c .args != "" {
217
+ runArgs = append (runArgs , strings .Split (c .args , " " )... )
218
+ }
219
+ out := runGolangciLintWithYamlConfig (t , c .cfg , runArgs ... )
220
+ el := mergeMegacheck (c .el )
221
+ sort .StringSlice (el ).Sort ()
222
+ expectedLine := fmt .Sprintf ("Active linters: [%s]" , strings .Join (el , " " ))
223
+ assert .Contains (t , out , expectedLine )
224
+ })
225
+ }
226
+ }
0 commit comments