From 8d9f470ed25e38678b5fa859e7166ed27255fd84 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 28 Feb 2021 23:14:05 +0100 Subject: [PATCH] pkg/analysis/messagefmt: add tests Signed-off-by: Fernandez Ludovic --- .gitignore | 2 +- pkg/analysis/messagefmt/analyzer.go | 8 +-- pkg/analysis/messagefmt/analyzer_test.go | 52 +++++++++++++++++++ pkg/analysis/messagefmt/testdata/src/a/go.mod | 5 ++ pkg/analysis/messagefmt/testdata/src/a/go.sum | 12 +++++ .../messagefmt/testdata/src/a/invalid.go | 18 +++++++ .../messagefmt/testdata/src/a/valid.go | 18 +++++++ pkg/analysis/messagefmt/testdata/src/b/go.mod | 9 ++++ pkg/analysis/messagefmt/testdata/src/b/go.sum | 16 ++++++ .../messagefmt/testdata/src/b/invalid.go | 15 ++++++ .../messagefmt/testdata/src/b/valid.go | 15 ++++++ 11 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 pkg/analysis/messagefmt/analyzer_test.go create mode 100644 pkg/analysis/messagefmt/testdata/src/a/go.mod create mode 100644 pkg/analysis/messagefmt/testdata/src/a/go.sum create mode 100644 pkg/analysis/messagefmt/testdata/src/a/invalid.go create mode 100644 pkg/analysis/messagefmt/testdata/src/a/valid.go create mode 100644 pkg/analysis/messagefmt/testdata/src/b/go.mod create mode 100644 pkg/analysis/messagefmt/testdata/src/b/go.sum create mode 100644 pkg/analysis/messagefmt/testdata/src/b/invalid.go create mode 100644 pkg/analysis/messagefmt/testdata/src/b/valid.go diff --git a/.gitignore b/.gitignore index 66fd13c..839a56d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,4 @@ *.out # Dependency directories (remove the comment below to include it) -# vendor/ +vendor/ diff --git a/pkg/analysis/messagefmt/analyzer.go b/pkg/analysis/messagefmt/analyzer.go index 658af6f..acf85ac 100644 --- a/pkg/analysis/messagefmt/analyzer.go +++ b/pkg/analysis/messagefmt/analyzer.go @@ -36,8 +36,8 @@ var Analyzer = &analysis.Analyzer{ func isException(word string) bool { var exceptions = map[string]struct{}{ - "xDS": struct{}{}, - "gRPC": struct{}{}, + "xDS": {}, + "gRPC": {}, } _, ok := exceptions[word] @@ -66,7 +66,7 @@ func funcForCallExpr(pass *analysis.Pass, call *ast.CallExpr) (*types.Func, bool func isFromPkg(fun *types.Func, pkg string) bool { // Calls to builtin types might not have a package. if fun.Pkg() != nil { - return fun.Pkg().Path() == pkg + return strings.Contains(fun.Pkg().Path(), pkg) } return false @@ -128,7 +128,7 @@ func checkInitialLower(pass *analysis.Pass, lit *ast.BasicLit) { // If the first word is all uppercase, it's an // initialism, so don't flag it. - if first == strings.ToUpper(first) { + if first == strings.ToUpper(first) && len(first) > 1 { return } diff --git a/pkg/analysis/messagefmt/analyzer_test.go b/pkg/analysis/messagefmt/analyzer_test.go new file mode 100644 index 0000000..4b09578 --- /dev/null +++ b/pkg/analysis/messagefmt/analyzer_test.go @@ -0,0 +1,52 @@ +package messagefmt + +import ( + "os" + "os/exec" + "path/filepath" + "testing" + + "golang.org/x/tools/go/analysis/analysistest" +) + +func TestAnalyzer(t *testing.T) { + testdata := analysistest.TestData() + + testCases := []struct { + desc string + pkg string + }{ + { + desc: "logrus", + pkg: "a", + }, + { + desc: "kingpin", + pkg: "b", + }, + } + + for _, test := range testCases { + test := test + t.Run(test.desc, func(t *testing.T) { + t.Parallel() + + dir := filepath.Join(testdata, "src", test.pkg) + + if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { + cmd := exec.Command("go", "mod", "vendor") + cmd.Dir = dir + + t.Cleanup(func() { + _ = os.RemoveAll(filepath.Join(testdata, "src", test.pkg, "vendor")) + }) + + if output, err := cmd.CombinedOutput(); err != nil { + t.Fatal(err, string(output)) + } + } + + analysistest.RunWithSuggestedFixes(t, testdata, Analyzer, test.pkg) + }) + } +} diff --git a/pkg/analysis/messagefmt/testdata/src/a/go.mod b/pkg/analysis/messagefmt/testdata/src/a/go.mod new file mode 100644 index 0000000..93aaa85 --- /dev/null +++ b/pkg/analysis/messagefmt/testdata/src/a/go.mod @@ -0,0 +1,5 @@ +module github.com/projectcontour/lint/pkg/analysis/messagefmt/testdata/src/a + +go 1.16 + +require github.com/sirupsen/logrus v1.8.0 diff --git a/pkg/analysis/messagefmt/testdata/src/a/go.sum b/pkg/analysis/messagefmt/testdata/src/a/go.sum new file mode 100644 index 0000000..a42fced --- /dev/null +++ b/pkg/analysis/messagefmt/testdata/src/a/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g= +github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU= +github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/pkg/analysis/messagefmt/testdata/src/a/invalid.go b/pkg/analysis/messagefmt/testdata/src/a/invalid.go new file mode 100644 index 0000000..d1db232 --- /dev/null +++ b/pkg/analysis/messagefmt/testdata/src/a/invalid.go @@ -0,0 +1,18 @@ +package a + +import ( + "github.com/sirupsen/logrus" +) + +func invalid() { + logrus.Info("A walrus appears") // want `message starts with uppercase: "A walrus appears"` + logrus.Debug("A walrus appears") // want `message starts with uppercase: "A walrus appears"` + logrus.Error("A walrus appears") // want `message starts with uppercase: "A walrus appears"` + logrus.Fatal("A walrus appears") // want `message starts with uppercase: "A walrus appears"` + logrus.Panic("A walrus appears") // want `message starts with uppercase: "A walrus appears"` + logrus.Print("A walrus appears") // want `message starts with uppercase: "A walrus appears"` + logrus.Info("A walrus appears") // want `message starts with uppercase: "A walrus appears"` + logrus.Trace("A walrus appears") // want `message starts with uppercase: "A walrus appears"` + logrus.Warn("A walrus appears") // want `message starts with uppercase: "A walrus appears"` + logrus.Warning("A walrus appears") // want `message starts with uppercase: "A walrus appears"` +} diff --git a/pkg/analysis/messagefmt/testdata/src/a/valid.go b/pkg/analysis/messagefmt/testdata/src/a/valid.go new file mode 100644 index 0000000..6544117 --- /dev/null +++ b/pkg/analysis/messagefmt/testdata/src/a/valid.go @@ -0,0 +1,18 @@ +package a + +import ( + "github.com/sirupsen/logrus" +) + +func valid() { + logrus.Info("a walrus appears") + logrus.Debug("a walrus appears") + logrus.Error("a walrus appears") + logrus.Fatal("a walrus appears") + logrus.Panic("a walrus appears") + logrus.Print("a walrus appears") + logrus.Info("a walrus appears") + logrus.Trace("a walrus appears") + logrus.Warn("a walrus appears") + logrus.Warning("a walrus appears") +} diff --git a/pkg/analysis/messagefmt/testdata/src/b/go.mod b/pkg/analysis/messagefmt/testdata/src/b/go.mod new file mode 100644 index 0000000..eb36177 --- /dev/null +++ b/pkg/analysis/messagefmt/testdata/src/b/go.mod @@ -0,0 +1,9 @@ +module github.com/projectcontour/lint/pkg/analysis/messagefmt/testdata/src/b + +go 1.16 + +require ( + github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect + github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 // indirect + gopkg.in/alecthomas/kingpin.v2 v2.2.6 +) diff --git a/pkg/analysis/messagefmt/testdata/src/b/go.sum b/pkg/analysis/messagefmt/testdata/src/b/go.sum new file mode 100644 index 0000000..ca5983d --- /dev/null +++ b/pkg/analysis/messagefmt/testdata/src/b/go.sum @@ -0,0 +1,16 @@ +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRYS3XieqF+Z9B9gNxo/eANAJCF2eiN4= +github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pkg/analysis/messagefmt/testdata/src/b/invalid.go b/pkg/analysis/messagefmt/testdata/src/b/invalid.go new file mode 100644 index 0000000..fe929ea --- /dev/null +++ b/pkg/analysis/messagefmt/testdata/src/b/invalid.go @@ -0,0 +1,15 @@ +package b + +import ( + "fmt" + + "gopkg.in/alecthomas/kingpin.v2" +) + +func invalid() { + verbose := kingpin.Flag("verbose", "verbose mode.").Short('v').Bool() // want `message starts with lowercase: "verbose mode."` + + deleteCommand := kingpin.Command("delete", "delete an object.") // want `message starts with lowercase: "delete an object."` + + fmt.Println(verbose, deleteCommand) +} diff --git a/pkg/analysis/messagefmt/testdata/src/b/valid.go b/pkg/analysis/messagefmt/testdata/src/b/valid.go new file mode 100644 index 0000000..4356646 --- /dev/null +++ b/pkg/analysis/messagefmt/testdata/src/b/valid.go @@ -0,0 +1,15 @@ +package b + +import ( + "fmt" + + "gopkg.in/alecthomas/kingpin.v2" +) + +func valid() { + verbose := kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool() + + deleteCommand := kingpin.Command("delete", "Delete an object.") + + fmt.Println(verbose, deleteCommand) +}