Skip to content

Commit

Permalink
Fix #282, fix #209: revert goimports commit
Browse files Browse the repository at this point in the history
ed64e33c8c8bc9a919e2b85a1a08225b5ae59d70. Also add tests
for local mode of goimports and do refactoring of tests.
  • Loading branch information
jirfag committed Nov 10, 2018
1 parent 7836034 commit ac77eaa
Show file tree
Hide file tree
Showing 47 changed files with 581 additions and 12,170 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
github.com/golangci/misspell v0.0.0-20180809174111-950f5d19e770
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0
github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b
github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
github.com/golangci/unparam v0.0.0-20180902112548-7ad9dbcccc16
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
Expand Down Expand Up @@ -68,6 +68,7 @@ require (
golang.org/x/tools v0.0.0-20180831211245-6c7e314b6563
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
gopkg.in/yaml.v2 v2.2.1
sourcegraph.com/sourcegraph/go-diff v0.0.0-20171119081133-3f415a150aec
sourcegraph.com/sqs/pbtypes v0.0.0-20160107090929-4d1b9dc7ffc3 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21 h1:leSNB7iYzLYSS
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21/go.mod h1:tf5+bzsHdTM0bsB7+8mt0GUMvjCgwLpTapNZHU8AajI=
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0 h1:HVfrLniijszjS1aiNg8JbBMO2+E1WIQ+j/gL4SQqGPg=
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0/go.mod h1:qOQCunEYvmd/TLamH+7LlVccLvUH5kZNhbCgTHoBbp4=
github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b h1:3a73k6ptEhiI9YA8cEe4i6nsLWQRjtBG97+tjhS+QBs=
github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b/go.mod h1:zgj6NOYXOC1cexsdtDceI4/mj3aXK4JOVg9AV3C5LWI=
github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b h1:3hI7NZ9D3edEBVbN6V1urHWbFKJfcIlOFvX5m10jB88=
github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b/go.mod h1:zgj6NOYXOC1cexsdtDceI4/mj3aXK4JOVg9AV3C5LWI=
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 h1:zwtduBRr5SSWhqsYNgcuWO2kFlpdOZbP0+yRjmvPGys=
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4/go.mod h1:Izgrg8RkN3rCIMLGE9CyYmU9pY2Jer6DgANEnZ/L/cQ=
github.com/golangci/unparam v0.0.0-20180902112548-7ad9dbcccc16 h1:QURX/XMP2uJUzzEvfJ291v1snmbJuyznAJLSQVnPyko=
Expand Down
3 changes: 2 additions & 1 deletion pkg/golinters/gocritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
_ "github.com/go-critic/checkers" // this import register checkers

"github.com/go-lintpack/lintpack"
"golang.org/x/tools/go/loader"

"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
"golang.org/x/tools/go/loader"
)

type Gocritic struct{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"go/token"

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

gofmtAPI "github.com/golangci/gofmt/gofmt"
goimportsAPI "github.com/golangci/gofmt/goimports"
Expand Down
6 changes: 4 additions & 2 deletions test/bench_test.go → test/bench/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package bench

import (
"bytes"
Expand All @@ -13,6 +13,8 @@ import (
"testing"
"time"

"github.com/golangci/golangci-lint/test/testshared"

gops "github.com/mitchellh/go-ps"
"github.com/shirou/gopsutil/process"

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

func BenchmarkWithGometalinter(b *testing.B) {
installBinary(b)
testshared.NewLintRunner(b).Install()

type bcase struct {
name string
Expand Down
15 changes: 15 additions & 0 deletions test/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test

import "path/filepath"

const testdataDir = "testdata"
const binName = "golangci-lint"

func getProjectRoot() string {
return filepath.Join("..", "...")
}

func getTestDataDir(names ...string) string {
parts := append([]string{testdataDir}, names...)
return filepath.Join(parts...)
}
207 changes: 207 additions & 0 deletions test/enabled_linters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package test

import (
"fmt"
"sort"
"strings"
"testing"

"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/test/testshared"
)

func inSlice(s []string, v string) bool {
for _, sv := range s {
if sv == v {
return true
}
}

return false
}

func getEnabledByDefaultFastLintersExcept(except ...string) []string {
m := lintersdb.NewManager()
ebdl := m.GetAllEnabledByDefaultLinters()
ret := []string{}
for _, lc := range ebdl {
if lc.NeedsSSARepr {
continue
}

if !inSlice(except, lc.Name()) {
ret = append(ret, lc.Name())
}
}

return ret
}

func getAllFastLintersWith(with ...string) []string {
linters := lintersdb.NewManager().GetAllSupportedLinterConfigs()
ret := append([]string{}, with...)
for _, lc := range linters {
if lc.NeedsSSARepr {
continue
}
ret = append(ret, lc.Name())
}

return ret
}

func getEnabledByDefaultLinters() []string {
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
ret := []string{}
for _, lc := range ebdl {
ret = append(ret, lc.Name())
}

return ret
}

func getEnabledByDefaultFastLintersWith(with ...string) []string {
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
ret := append([]string{}, with...)
for _, lc := range ebdl {
if lc.NeedsSSARepr {
continue
}

ret = append(ret, lc.Name())
}

return ret
}

func mergeMegacheck(linters []string) []string {
if inSlice(linters, "staticcheck") &&
inSlice(linters, "gosimple") &&
inSlice(linters, "unused") {
ret := []string{"megacheck"}
for _, linter := range linters {
if !inSlice([]string{"staticcheck", "gosimple", "unused"}, linter) {
ret = append(ret, linter)
}
}

return ret
}

return linters
}

func TestEnabledLinters(t *testing.T) {
type tc struct {
name string
cfg string
el []string
args string
noImplicitFast bool
}

cases := []tc{
{
name: "disable govet in config",
cfg: `
linters:
disable:
- govet
`,
el: getEnabledByDefaultFastLintersExcept("govet"),
},
{
name: "enable golint in config",
cfg: `
linters:
enable:
- golint
`,
el: getEnabledByDefaultFastLintersWith("golint"),
},
{
name: "disable govet in cmd",
args: "-Dgovet",
el: getEnabledByDefaultFastLintersExcept("govet"),
},
{
name: "enable gofmt in cmd and enable golint in config",
args: "-Egofmt",
cfg: `
linters:
enable:
- golint
`,
el: getEnabledByDefaultFastLintersWith("golint", "gofmt"),
},
{
name: "fast option in config",
cfg: `
linters:
fast: true
`,
el: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
name: "explicitly unset fast option in config",
cfg: `
linters:
fast: false
`,
el: getEnabledByDefaultLinters(),
noImplicitFast: true,
},
{
name: "set fast option in command-line",
args: "--fast",
el: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
name: "fast option in command-line has higher priority to enable",
cfg: `
linters:
fast: false
`,
args: "--fast",
el: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
name: "fast option in command-line has higher priority to disable",
cfg: `
linters:
fast: true
`,
args: "--fast=false",
el: getEnabledByDefaultLinters(),
noImplicitFast: true,
},
{
name: "fast option combined with enable and enable-all",
args: "--enable-all --fast --enable=megacheck",
el: getAllFastLintersWith("megacheck"),
noImplicitFast: true,
},
}

for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
runArgs := []string{"-v"}
if !c.noImplicitFast {
runArgs = append(runArgs, "--fast")
}
if c.args != "" {
runArgs = append(runArgs, strings.Split(c.args, " ")...)
}
r := testshared.NewLintRunner(t).RunWithYamlConfig(c.cfg, runArgs...)
el := mergeMegacheck(c.el)
sort.StringSlice(el).Sort()

expectedLine := fmt.Sprintf("Active %d linters: [%s]", len(el), strings.Join(el, " "))
r.ExpectOutputContains(expectedLine)
})
}
}
Loading

0 comments on commit ac77eaa

Please sign in to comment.