From a22b7c5858ccb545f932ef1b9530fbea09c0e09d Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Sat, 26 Mar 2022 16:50:28 +0900 Subject: [PATCH] add staticcheck GoTool test --- check/testfiles/d.go | 12 ++++++++++++ check/utils.go | 10 ++++++++-- check/utils_test.go | 22 +++++++++++++++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 check/testfiles/d.go diff --git a/check/testfiles/d.go b/check/testfiles/d.go new file mode 100644 index 0000000..d1a1033 --- /dev/null +++ b/check/testfiles/d.go @@ -0,0 +1,12 @@ +package testfiles + +import ( + "fmt" + "time" +) + +func foo() { + a := time.Now() + b := a.Sub(time.Now()) + fmt.Println(a, b) +} diff --git a/check/utils.go b/check/utils.go index ae92001..72e8183 100644 --- a/check/utils.go +++ b/check/utils.go @@ -335,7 +335,10 @@ outer: // GoTool runs a given go command (for example gofmt, go tool vet) // on a directory func GoTool(dir string, filenames, command []string) (float64, []FileSummary, error) { - enabledCheck := command[len(command)-1] + var enabledCheck = command[0] + if command[0] == "gometalinter" { + enabledCheck = command[len(command)-1] + } // temporary disabling of misspell as it's the slowest // command right now @@ -350,7 +353,10 @@ func GoTool(dir string, filenames, command []string) (float64, []FileSummary, er } params := command[1:] - params = addSkipDirs(params) + + if command[0] == "gometalinter" { + params = addSkipDirs(params) + } switch { case strings.Contains(enabledCheck, "cyclo"): diff --git a/check/utils_test.go b/check/utils_test.go index 641f4cf..5b94458 100644 --- a/check/utils_test.go +++ b/check/utils_test.go @@ -10,7 +10,7 @@ func TestGoFiles(t *testing.T) { if err != nil { t.Fatal(err) } - want := []string{"testfiles/a.go", "testfiles/b.go", "testfiles/c.go"} + want := []string{"testfiles/a.go", "testfiles/b.go", "testfiles/c.go", "testfiles/d.go"} if !reflect.DeepEqual(files, want) { t.Errorf("GoFiles(%q) = %v, want %v", "testfiles/", files, want) } @@ -30,7 +30,23 @@ var goToolTests = []struct { failed []FileSummary wantErr bool }{ - {"go vet", "testfiles/", []string{"testfiles/a.go", "testfiles/b.go", "testfiles/c.go"}, []string{"go", "tool", "vet"}, 1, []FileSummary{}, false}, + {"go vet", "testfiles", []string{"testfiles/a.go", "testfiles/b.go", "testfiles/c.go"}, []string{"go", "vet"}, 1, []FileSummary{}, false}, + { + "staticcheck", + "testfiles/", + []string{"testfiles/a.go", "testfiles/b.go", "testfiles/c.go", "testfiles/d.go"}, + []string{"staticcheck", "./..."}, + 0.75, + []FileSummary{ + { + Filename: "testfiles/d.go", FileURL: "", + Errors: []Error{ + {LineNumber: 8, ErrorString: " func foo is unused (U1000)"}, + {LineNumber: 10, ErrorString: " should use time.Until instead of t.Sub(time.Now()) (S1024)"}}, + }, + }, + false, + }, } func TestGoTool(t *testing.T) { @@ -43,7 +59,7 @@ func TestGoTool(t *testing.T) { t.Errorf("[%s] GoTool percent = %f, want %f", tt.name, f, tt.percent) } if !reflect.DeepEqual(fs, tt.failed) { - t.Errorf("[%s] GoTool failed = %v, want %v", tt.name, fs, tt.failed) + t.Errorf("[%s] GoTool failed = %#v, want %v", tt.name, fs, tt.failed) } } }