2018-05-31 23:53:01 +03:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2018-12-22 14:16:02 +03:00
|
|
|
"path/filepath"
|
2019-04-20 21:14:28 +03:00
|
|
|
"strings"
|
2018-05-31 23:53:01 +03:00
|
|
|
"testing"
|
|
|
|
|
2018-12-22 14:16:02 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-09-27 20:58:49 -04:00
|
|
|
_ "github.com/valyala/quicktemplate"
|
2018-10-28 18:47:56 +03:00
|
|
|
|
2018-06-13 22:37:48 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/exitcodes"
|
2019-09-27 20:58:49 -04:00
|
|
|
"github.com/golangci/golangci-lint/test/testshared"
|
2018-05-31 23:53:01 +03:00
|
|
|
)
|
|
|
|
|
2018-12-22 14:16:02 +03:00
|
|
|
func getCommonRunArgs() []string {
|
2019-03-17 22:47:29 +03:00
|
|
|
return []string{"--skip-dirs", "testdata_etc/,pkg/golinters/goanalysis/(checker|passes)"}
|
2018-12-22 14:16:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func withCommonRunArgs(args ...string) []string {
|
|
|
|
return append(getCommonRunArgs(), args...)
|
2018-06-02 19:42:29 +03:00
|
|
|
}
|
|
|
|
|
2018-06-11 00:50:31 +03:00
|
|
|
func TestAutogeneratedNoIssues(t *testing.T) {
|
2018-11-07 21:55:26 +03:00
|
|
|
testshared.NewLintRunner(t).Run(getTestDataDir("autogenerated")).ExpectNoIssues()
|
2018-06-11 00:50:31 +03:00
|
|
|
}
|
|
|
|
|
2018-10-21 17:06:51 +03:00
|
|
|
func TestEmptyDirRun(t *testing.T) {
|
2019-06-04 21:55:20 -04:00
|
|
|
testshared.NewLintRunner(t, "GO111MODULE=off").Run(getTestDataDir("nogofiles")).
|
2018-11-07 21:55:26 +03:00
|
|
|
ExpectExitCode(exitcodes.NoGoFiles).
|
|
|
|
ExpectOutputContains(": no go files to analyze")
|
2018-06-10 17:10:56 +03:00
|
|
|
}
|
|
|
|
|
2018-10-21 17:06:51 +03:00
|
|
|
func TestNotExistingDirRun(t *testing.T) {
|
2019-06-04 21:55:20 -04:00
|
|
|
testshared.NewLintRunner(t, "GO111MODULE=off").Run(getTestDataDir("no_such_dir")).
|
2019-04-21 10:33:39 +03:00
|
|
|
ExpectExitCode(exitcodes.Failure).
|
2019-09-09 21:54:56 +03:00
|
|
|
ExpectOutputContains("cannot find package").
|
|
|
|
ExpectOutputContains("/testdata/no_such_dir")
|
2018-10-21 17:06:51 +03:00
|
|
|
}
|
2018-06-13 22:37:48 +03:00
|
|
|
|
2018-10-21 17:06:51 +03:00
|
|
|
func TestSymlinkLoop(t *testing.T) {
|
2018-11-07 21:55:26 +03:00
|
|
|
testshared.NewLintRunner(t).Run(getTestDataDir("symlink_loop", "...")).ExpectNoIssues()
|
2018-06-13 22:37:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:53:01 +03:00
|
|
|
func TestDeadline(t *testing.T) {
|
2018-11-07 21:55:26 +03:00
|
|
|
testshared.NewLintRunner(t).Run("--deadline=1ms", getProjectRoot()).
|
|
|
|
ExpectExitCode(exitcodes.Timeout).
|
2019-11-07 23:42:30 +11:00
|
|
|
ExpectOutputContains(`Timeout exceeded: try increase it by passing --timeout option`)
|
2019-10-08 09:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTimeout(t *testing.T) {
|
|
|
|
testshared.NewLintRunner(t).Run("--timeout=1ms", getProjectRoot()).
|
|
|
|
ExpectExitCode(exitcodes.Timeout).
|
|
|
|
ExpectOutputContains(`Timeout exceeded: try increase it by passing --timeout option`)
|
2018-06-05 23:54:05 +03:00
|
|
|
}
|
|
|
|
|
2019-10-15 13:11:14 +02:00
|
|
|
func TestTimeoutInConfig(t *testing.T) {
|
|
|
|
type tc struct {
|
|
|
|
cfg string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []tc{
|
|
|
|
{
|
|
|
|
cfg: `
|
|
|
|
run:
|
|
|
|
deadline: 1ms
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cfg: `
|
|
|
|
run:
|
|
|
|
timeout: 1ms
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// timeout should override deadline
|
|
|
|
cfg: `
|
|
|
|
run:
|
|
|
|
deadline: 100s
|
|
|
|
timeout: 1ms
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r := testshared.NewLintRunner(t)
|
|
|
|
for _, c := range cases {
|
|
|
|
// Run with disallowed option set only in config
|
|
|
|
r.RunWithYamlConfig(c.cfg, withCommonRunArgs(minimalPkg)...).ExpectExitCode(exitcodes.Timeout).
|
|
|
|
ExpectOutputContains(`Timeout exceeded: try increase it by passing --timeout option`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 18:13:55 +03:00
|
|
|
func TestTestsAreLintedByDefault(t *testing.T) {
|
2018-11-07 21:55:26 +03:00
|
|
|
testshared.NewLintRunner(t).Run(getTestDataDir("withtests")).
|
2019-02-16 21:23:57 +03:00
|
|
|
ExpectHasIssue("`if` block ends with a `return`")
|
2018-06-02 18:13:55 +03:00
|
|
|
}
|
2018-06-02 19:42:29 +03:00
|
|
|
|
2018-06-30 09:13:13 +03:00
|
|
|
func TestCgoOk(t *testing.T) {
|
2019-09-09 21:54:56 +03:00
|
|
|
testshared.NewLintRunner(t).Run("--no-config", "--enable-all", getTestDataDir("cgo")).ExpectNoIssues()
|
2018-06-30 09:13:13 +03:00
|
|
|
}
|
|
|
|
|
2018-10-21 17:06:51 +03:00
|
|
|
func TestCgoWithIssues(t *testing.T) {
|
2019-09-20 12:05:01 -04:00
|
|
|
testshared.NewLintRunner(t).Run("--no-config", "--disable-all", "-Egovet", getTestDataDir("cgo_with_issues")).
|
2018-11-07 21:55:26 +03:00
|
|
|
ExpectHasIssue("Printf format %t has arg cs of wrong type")
|
2019-09-20 12:05:01 -04:00
|
|
|
testshared.NewLintRunner(t).Run("--no-config", "--disable-all", "-Estaticcheck", getTestDataDir("cgo_with_issues")).
|
|
|
|
ExpectHasIssue("SA5009: Printf format %t has arg #1 of wrong type")
|
2018-10-21 17:06:51 +03:00
|
|
|
}
|
|
|
|
|
2018-06-30 09:13:13 +03:00
|
|
|
func TestUnsafeOk(t *testing.T) {
|
2019-09-09 21:54:56 +03:00
|
|
|
testshared.NewLintRunner(t).Run("--no-config", "--enable-all", getTestDataDir("unsafe")).ExpectNoIssues()
|
2018-06-30 09:13:13 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 00:31:30 +03:00
|
|
|
func TestGovetCustomFormatter(t *testing.T) {
|
|
|
|
testshared.NewLintRunner(t).Run(getTestDataDir("govet_custom_formatter")).ExpectNoIssues()
|
|
|
|
}
|
|
|
|
|
2019-04-20 21:14:28 +03:00
|
|
|
func TestLineDirectiveProcessedFilesLiteLoading(t *testing.T) {
|
|
|
|
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config",
|
|
|
|
"--exclude-use-default=false", "-Egolint", getTestDataDir("quicktemplate"))
|
|
|
|
|
|
|
|
output := strings.Join([]string{
|
|
|
|
"testdata/quicktemplate/hello.qtpl.go:26:1: exported function `StreamHello` should have comment or be unexported (golint)",
|
|
|
|
"testdata/quicktemplate/hello.qtpl.go:50:1: exported function `Hello` should have comment or be unexported (golint)",
|
|
|
|
"testdata/quicktemplate/hello.qtpl.go:39:1: exported function `WriteHello` should have comment or be unexported (golint)",
|
|
|
|
}, "\n")
|
|
|
|
r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputEq(output + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLineDirectiveProcessedFilesFullLoading(t *testing.T) {
|
|
|
|
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config",
|
|
|
|
"--exclude-use-default=false", "-Egolint,govet", getTestDataDir("quicktemplate"))
|
|
|
|
|
|
|
|
output := strings.Join([]string{
|
|
|
|
"testdata/quicktemplate/hello.qtpl.go:26:1: exported function `StreamHello` should have comment or be unexported (golint)",
|
|
|
|
"testdata/quicktemplate/hello.qtpl.go:50:1: exported function `Hello` should have comment or be unexported (golint)",
|
|
|
|
"testdata/quicktemplate/hello.qtpl.go:39:1: exported function `WriteHello` should have comment or be unexported (golint)",
|
|
|
|
}, "\n")
|
|
|
|
r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputEq(output + "\n")
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:11:37 +03:00
|
|
|
func TestSkippedDirsNoMatchArg(t *testing.T) {
|
|
|
|
dir := getTestDataDir("skipdirs", "skip_me", "nested")
|
|
|
|
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "--skip-dirs", dir, "-Egolint", dir)
|
2018-11-07 21:55:26 +03:00
|
|
|
|
|
|
|
r.ExpectExitCode(exitcodes.IssuesFound).
|
2019-02-16 21:23:57 +03:00
|
|
|
ExpectOutputEq("testdata/skipdirs/skip_me/nested/with_issue.go:8:9: `if` block ends with " +
|
|
|
|
"a `return` statement, so drop this `else` and outdent its block (golint)\n")
|
2018-10-21 17:06:51 +03:00
|
|
|
}
|
|
|
|
|
2018-12-22 12:11:37 +03:00
|
|
|
func TestSkippedDirsTestdata(t *testing.T) {
|
|
|
|
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "-Egolint", getTestDataDir("skipdirs", "..."))
|
|
|
|
|
|
|
|
r.ExpectNoIssues() // all was skipped because in testdata
|
|
|
|
}
|
|
|
|
|
2018-06-30 09:13:13 +03:00
|
|
|
func TestDeadcodeNoFalsePositivesInMainPkg(t *testing.T) {
|
2018-11-07 21:55:26 +03:00
|
|
|
testshared.NewLintRunner(t).Run("--no-config", "--disable-all", "-Edeadcode", getTestDataDir("deadcode_main_pkg")).ExpectNoIssues()
|
2018-11-05 14:00:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentifierUsedOnlyInTests(t *testing.T) {
|
2018-11-07 21:55:26 +03:00
|
|
|
testshared.NewLintRunner(t).Run("--no-config", "--disable-all", "-Eunused", getTestDataDir("used_only_in_tests")).ExpectNoIssues()
|
2018-06-30 09:13:13 +03:00
|
|
|
}
|
|
|
|
|
2019-09-09 09:10:49 -04:00
|
|
|
func TestUnusedCheckExported(t *testing.T) {
|
|
|
|
testshared.NewLintRunner(t).Run("-c", "testdata_etc/unused_exported/golangci.yml", "testdata_etc/unused_exported/...").ExpectNoIssues()
|
|
|
|
}
|
|
|
|
|
2018-06-02 19:42:29 +03:00
|
|
|
func TestConfigFileIsDetected(t *testing.T) {
|
2018-11-07 21:55:26 +03:00
|
|
|
checkGotConfig := func(r *testshared.RunResult) {
|
|
|
|
r.ExpectExitCode(exitcodes.Success).
|
|
|
|
ExpectOutputEq("test\n") // test config contains InternalTest: true, it triggers such output
|
2018-06-02 19:42:29 +03:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:55:26 +03:00
|
|
|
r := testshared.NewLintRunner(t)
|
|
|
|
checkGotConfig(r.Run(getTestDataDir("withconfig", "pkg")))
|
|
|
|
checkGotConfig(r.Run(getTestDataDir("withconfig", "...")))
|
2018-06-05 23:54:05 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 13:31:00 +10:00
|
|
|
func TestEnableAllFastAndEnableCanCoexist(t *testing.T) {
|
2018-11-07 21:55:26 +03:00
|
|
|
r := testshared.NewLintRunner(t)
|
2019-09-09 21:54:56 +03:00
|
|
|
r.Run(withCommonRunArgs("--no-config", "--fast", "--enable-all", "--enable=typecheck", minimalPkg)...).
|
|
|
|
ExpectExitCode(exitcodes.Success, exitcodes.IssuesFound)
|
|
|
|
r.Run(withCommonRunArgs("--no-config", "--enable-all", "--enable=typecheck", minimalPkg)...).
|
|
|
|
ExpectExitCode(exitcodes.Failure)
|
2018-06-05 23:54:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnabledPresetsAreNotDuplicated(t *testing.T) {
|
2019-09-09 21:54:56 +03:00
|
|
|
testshared.NewLintRunner(t).Run("--no-config", "-v", "-p", "style,bugs", minimalPkg).
|
2018-11-07 21:55:26 +03:00
|
|
|
ExpectOutputContains("Active presets: [bugs style]")
|
2018-06-05 23:54:05 +03:00
|
|
|
}
|
|
|
|
|
2018-12-22 14:16:02 +03:00
|
|
|
func TestAbsPathDirAnalysis(t *testing.T) {
|
|
|
|
dir := filepath.Join("testdata_etc", "abspath") // abs paths don't work with testdata dir
|
|
|
|
absDir, err := filepath.Abs(dir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "-Egolint", absDir)
|
2019-02-16 21:23:57 +03:00
|
|
|
r.ExpectHasIssue("`if` block ends with a `return` statement")
|
2018-12-22 14:16:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAbsPathFileAnalysis(t *testing.T) {
|
|
|
|
dir := filepath.Join("testdata_etc", "abspath", "with_issue.go") // abs paths don't work with testdata dir
|
|
|
|
absDir, err := filepath.Abs(dir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "-Egolint", absDir)
|
2019-02-16 21:23:57 +03:00
|
|
|
r.ExpectHasIssue("`if` block ends with a `return` statement")
|
2018-12-22 14:16:02 +03:00
|
|
|
}
|
|
|
|
|
2018-06-05 23:54:05 +03:00
|
|
|
func TestDisallowedOptionsInConfig(t *testing.T) {
|
|
|
|
type tc struct {
|
|
|
|
cfg string
|
|
|
|
option string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []tc{
|
|
|
|
{
|
|
|
|
cfg: `
|
|
|
|
ruN:
|
|
|
|
Args:
|
|
|
|
- 1
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cfg: `
|
|
|
|
run:
|
|
|
|
CPUProfilePath: path
|
|
|
|
`,
|
|
|
|
option: "--cpu-profile-path=path",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cfg: `
|
|
|
|
run:
|
|
|
|
MemProfilePath: path
|
|
|
|
`,
|
|
|
|
option: "--mem-profile-path=path",
|
|
|
|
},
|
2019-09-21 13:50:23 +03:00
|
|
|
{
|
|
|
|
cfg: `
|
|
|
|
run:
|
|
|
|
TracePath: path
|
|
|
|
`,
|
|
|
|
option: "--trace-path=path",
|
|
|
|
},
|
2018-06-05 23:54:05 +03:00
|
|
|
{
|
|
|
|
cfg: `
|
|
|
|
run:
|
|
|
|
Verbose: true
|
|
|
|
`,
|
|
|
|
option: "-v",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:55:26 +03:00
|
|
|
r := testshared.NewLintRunner(t)
|
2018-06-05 23:54:05 +03:00
|
|
|
for _, c := range cases {
|
|
|
|
// Run with disallowed option set only in config
|
2019-09-09 21:54:56 +03:00
|
|
|
r.RunWithYamlConfig(c.cfg, withCommonRunArgs(minimalPkg)...).ExpectExitCode(exitcodes.Failure)
|
2018-06-05 23:54:05 +03:00
|
|
|
|
|
|
|
if c.option == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:54:56 +03:00
|
|
|
args := []string{c.option, "--fast", minimalPkg}
|
2018-06-05 23:54:05 +03:00
|
|
|
|
|
|
|
// Run with disallowed option set only in command-line
|
2018-12-22 14:16:02 +03:00
|
|
|
r.Run(withCommonRunArgs(args...)...).ExpectExitCode(exitcodes.Success)
|
2018-06-05 23:54:05 +03:00
|
|
|
|
|
|
|
// Run with disallowed option set both in command-line and in config
|
2018-12-22 14:16:02 +03:00
|
|
|
r.RunWithYamlConfig(c.cfg, withCommonRunArgs(args...)...).ExpectExitCode(exitcodes.Failure)
|
2018-06-05 23:54:05 +03:00
|
|
|
}
|
|
|
|
}
|