Skip to content

Commit ac77eaa

Browse files
committed
Fix #282, fix #209: revert goimports commit
ed64e33c8c8bc9a919e2b85a1a08225b5ae59d70. Also add tests for local mode of goimports and do refactoring of tests.
1 parent 7836034 commit ac77eaa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+581
-12170
lines changed

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ require (
3737
github.com/golangci/misspell v0.0.0-20180809174111-950f5d19e770
3838
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21
3939
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0
40-
github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b
40+
github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b
4141
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
4242
github.com/golangci/unparam v0.0.0-20180902112548-7ad9dbcccc16
4343
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
@@ -68,6 +68,7 @@ require (
6868
golang.org/x/tools v0.0.0-20180831211245-6c7e314b6563
6969
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
7070
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
71+
gopkg.in/yaml.v2 v2.2.1
7172
sourcegraph.com/sourcegraph/go-diff v0.0.0-20171119081133-3f415a150aec
7273
sourcegraph.com/sqs/pbtypes v0.0.0-20160107090929-4d1b9dc7ffc3 // indirect
7374
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21 h1:leSNB7iYzLYSS
7575
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21/go.mod h1:tf5+bzsHdTM0bsB7+8mt0GUMvjCgwLpTapNZHU8AajI=
7676
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0 h1:HVfrLniijszjS1aiNg8JbBMO2+E1WIQ+j/gL4SQqGPg=
7777
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0/go.mod h1:qOQCunEYvmd/TLamH+7LlVccLvUH5kZNhbCgTHoBbp4=
78-
github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b h1:3a73k6ptEhiI9YA8cEe4i6nsLWQRjtBG97+tjhS+QBs=
79-
github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b/go.mod h1:zgj6NOYXOC1cexsdtDceI4/mj3aXK4JOVg9AV3C5LWI=
78+
github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b h1:3hI7NZ9D3edEBVbN6V1urHWbFKJfcIlOFvX5m10jB88=
79+
github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b/go.mod h1:zgj6NOYXOC1cexsdtDceI4/mj3aXK4JOVg9AV3C5LWI=
8080
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 h1:zwtduBRr5SSWhqsYNgcuWO2kFlpdOZbP0+yRjmvPGys=
8181
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4/go.mod h1:Izgrg8RkN3rCIMLGE9CyYmU9pY2Jer6DgANEnZ/L/cQ=
8282
github.com/golangci/unparam v0.0.0-20180902112548-7ad9dbcccc16 h1:QURX/XMP2uJUzzEvfJ291v1snmbJuyznAJLSQVnPyko=

pkg/golinters/gocritic.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import (
1414
_ "github.com/go-critic/checkers" // this import register checkers
1515

1616
"github.com/go-lintpack/lintpack"
17+
"golang.org/x/tools/go/loader"
18+
1719
"github.com/golangci/golangci-lint/pkg/lint/linter"
1820
"github.com/golangci/golangci-lint/pkg/result"
19-
"golang.org/x/tools/go/loader"
2021
)
2122

2223
type Gocritic struct{}

pkg/golinters/gofmt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"go/token"
88

9-
"golang.org/x/tools/imports"
9+
"github.com/golangci/tools/imports" // TODO: replace with x/tools when use it in golangci/gofmt/gofmt
1010

1111
gofmtAPI "github.com/golangci/gofmt/gofmt"
1212
goimportsAPI "github.com/golangci/gofmt/goimports"

test/bench_test.go renamed to test/bench/bench_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test
1+
package bench
22

33
import (
44
"bytes"
@@ -13,6 +13,8 @@ import (
1313
"testing"
1414
"time"
1515

16+
"github.com/golangci/golangci-lint/test/testshared"
17+
1618
gops "github.com/mitchellh/go-ps"
1719
"github.com/shirou/gopsutil/process"
1820

@@ -239,7 +241,7 @@ func runOne(b *testing.B, run func(*testing.B), progName string) *runResult {
239241
}
240242

241243
func BenchmarkWithGometalinter(b *testing.B) {
242-
installBinary(b)
244+
testshared.NewLintRunner(b).Install()
243245

244246
type bcase struct {
245247
name string

test/data.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test
2+
3+
import "path/filepath"
4+
5+
const testdataDir = "testdata"
6+
const binName = "golangci-lint"
7+
8+
func getProjectRoot() string {
9+
return filepath.Join("..", "...")
10+
}
11+
12+
func getTestDataDir(names ...string) string {
13+
parts := append([]string{testdataDir}, names...)
14+
return filepath.Join(parts...)
15+
}

test/enabled_linters.go

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package test
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"strings"
7+
"testing"
8+
9+
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
10+
"github.com/golangci/golangci-lint/test/testshared"
11+
)
12+
13+
func inSlice(s []string, v string) bool {
14+
for _, sv := range s {
15+
if sv == v {
16+
return true
17+
}
18+
}
19+
20+
return false
21+
}
22+
23+
func getEnabledByDefaultFastLintersExcept(except ...string) []string {
24+
m := lintersdb.NewManager()
25+
ebdl := m.GetAllEnabledByDefaultLinters()
26+
ret := []string{}
27+
for _, lc := range ebdl {
28+
if lc.NeedsSSARepr {
29+
continue
30+
}
31+
32+
if !inSlice(except, lc.Name()) {
33+
ret = append(ret, lc.Name())
34+
}
35+
}
36+
37+
return ret
38+
}
39+
40+
func getAllFastLintersWith(with ...string) []string {
41+
linters := lintersdb.NewManager().GetAllSupportedLinterConfigs()
42+
ret := append([]string{}, with...)
43+
for _, lc := range linters {
44+
if lc.NeedsSSARepr {
45+
continue
46+
}
47+
ret = append(ret, lc.Name())
48+
}
49+
50+
return ret
51+
}
52+
53+
func getEnabledByDefaultLinters() []string {
54+
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
55+
ret := []string{}
56+
for _, lc := range ebdl {
57+
ret = append(ret, lc.Name())
58+
}
59+
60+
return ret
61+
}
62+
63+
func getEnabledByDefaultFastLintersWith(with ...string) []string {
64+
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
65+
ret := append([]string{}, with...)
66+
for _, lc := range ebdl {
67+
if lc.NeedsSSARepr {
68+
continue
69+
}
70+
71+
ret = append(ret, lc.Name())
72+
}
73+
74+
return ret
75+
}
76+
77+
func mergeMegacheck(linters []string) []string {
78+
if inSlice(linters, "staticcheck") &&
79+
inSlice(linters, "gosimple") &&
80+
inSlice(linters, "unused") {
81+
ret := []string{"megacheck"}
82+
for _, linter := range linters {
83+
if !inSlice([]string{"staticcheck", "gosimple", "unused"}, linter) {
84+
ret = append(ret, linter)
85+
}
86+
}
87+
88+
return ret
89+
}
90+
91+
return linters
92+
}
93+
94+
func TestEnabledLinters(t *testing.T) {
95+
type tc struct {
96+
name string
97+
cfg string
98+
el []string
99+
args string
100+
noImplicitFast bool
101+
}
102+
103+
cases := []tc{
104+
{
105+
name: "disable govet in config",
106+
cfg: `
107+
linters:
108+
disable:
109+
- govet
110+
`,
111+
el: getEnabledByDefaultFastLintersExcept("govet"),
112+
},
113+
{
114+
name: "enable golint in config",
115+
cfg: `
116+
linters:
117+
enable:
118+
- golint
119+
`,
120+
el: getEnabledByDefaultFastLintersWith("golint"),
121+
},
122+
{
123+
name: "disable govet in cmd",
124+
args: "-Dgovet",
125+
el: getEnabledByDefaultFastLintersExcept("govet"),
126+
},
127+
{
128+
name: "enable gofmt in cmd and enable golint in config",
129+
args: "-Egofmt",
130+
cfg: `
131+
linters:
132+
enable:
133+
- golint
134+
`,
135+
el: getEnabledByDefaultFastLintersWith("golint", "gofmt"),
136+
},
137+
{
138+
name: "fast option in config",
139+
cfg: `
140+
linters:
141+
fast: true
142+
`,
143+
el: getEnabledByDefaultFastLintersWith(),
144+
noImplicitFast: true,
145+
},
146+
{
147+
name: "explicitly unset fast option in config",
148+
cfg: `
149+
linters:
150+
fast: false
151+
`,
152+
el: getEnabledByDefaultLinters(),
153+
noImplicitFast: true,
154+
},
155+
{
156+
name: "set fast option in command-line",
157+
args: "--fast",
158+
el: getEnabledByDefaultFastLintersWith(),
159+
noImplicitFast: true,
160+
},
161+
{
162+
name: "fast option in command-line has higher priority to enable",
163+
cfg: `
164+
linters:
165+
fast: false
166+
`,
167+
args: "--fast",
168+
el: getEnabledByDefaultFastLintersWith(),
169+
noImplicitFast: true,
170+
},
171+
{
172+
name: "fast option in command-line has higher priority to disable",
173+
cfg: `
174+
linters:
175+
fast: true
176+
`,
177+
args: "--fast=false",
178+
el: getEnabledByDefaultLinters(),
179+
noImplicitFast: true,
180+
},
181+
{
182+
name: "fast option combined with enable and enable-all",
183+
args: "--enable-all --fast --enable=megacheck",
184+
el: getAllFastLintersWith("megacheck"),
185+
noImplicitFast: true,
186+
},
187+
}
188+
189+
for _, c := range cases {
190+
c := c
191+
t.Run(c.name, func(t *testing.T) {
192+
runArgs := []string{"-v"}
193+
if !c.noImplicitFast {
194+
runArgs = append(runArgs, "--fast")
195+
}
196+
if c.args != "" {
197+
runArgs = append(runArgs, strings.Split(c.args, " ")...)
198+
}
199+
r := testshared.NewLintRunner(t).RunWithYamlConfig(c.cfg, runArgs...)
200+
el := mergeMegacheck(c.el)
201+
sort.StringSlice(el).Sort()
202+
203+
expectedLine := fmt.Sprintf("Active %d linters: [%s]", len(el), strings.Join(el, " "))
204+
r.ExpectOutputContains(expectedLine)
205+
})
206+
}
207+
}

0 commit comments

Comments
 (0)