Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
pkg/analysis/messagefmt: add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fernandez Ludovic <[email protected]>
  • Loading branch information
ldez committed Feb 28, 2021
1 parent 00ba86c commit 31b3059
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/
8 changes: 4 additions & 4 deletions pkg/analysis/messagefmt/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
52 changes: 52 additions & 0 deletions pkg/analysis/messagefmt/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
5 changes: 5 additions & 0 deletions pkg/analysis/messagefmt/testdata/src/a/go.mod
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions pkg/analysis/messagefmt/testdata/src/a/go.sum
Original file line number Diff line number Diff line change
@@ -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=
18 changes: 18 additions & 0 deletions pkg/analysis/messagefmt/testdata/src/a/invalid.go
Original file line number Diff line number Diff line change
@@ -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"`
}
18 changes: 18 additions & 0 deletions pkg/analysis/messagefmt/testdata/src/a/invalid.go.golden
Original file line number Diff line number Diff line change
@@ -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"`
}
18 changes: 18 additions & 0 deletions pkg/analysis/messagefmt/testdata/src/a/valid.go
Original file line number Diff line number Diff line change
@@ -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")
}
9 changes: 9 additions & 0 deletions pkg/analysis/messagefmt/testdata/src/b/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
16 changes: 16 additions & 0 deletions pkg/analysis/messagefmt/testdata/src/b/go.sum
Original file line number Diff line number Diff line change
@@ -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=
15 changes: 15 additions & 0 deletions pkg/analysis/messagefmt/testdata/src/b/invalid.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions pkg/analysis/messagefmt/testdata/src/b/valid.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 31b3059

Please sign in to comment.