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

Commit 8d9f470

Browse files
committed
pkg/analysis/messagefmt: add tests
Signed-off-by: Fernandez Ludovic <[email protected]>
1 parent 00ba86c commit 8d9f470

File tree

11 files changed

+165
-5
lines changed

11 files changed

+165
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
*.out
1313

1414
# Dependency directories (remove the comment below to include it)
15-
# vendor/
15+
vendor/

pkg/analysis/messagefmt/analyzer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ var Analyzer = &analysis.Analyzer{
3636

3737
func isException(word string) bool {
3838
var exceptions = map[string]struct{}{
39-
"xDS": struct{}{},
40-
"gRPC": struct{}{},
39+
"xDS": {},
40+
"gRPC": {},
4141
}
4242

4343
_, ok := exceptions[word]
@@ -66,7 +66,7 @@ func funcForCallExpr(pass *analysis.Pass, call *ast.CallExpr) (*types.Func, bool
6666
func isFromPkg(fun *types.Func, pkg string) bool {
6767
// Calls to builtin types might not have a package.
6868
if fun.Pkg() != nil {
69-
return fun.Pkg().Path() == pkg
69+
return strings.Contains(fun.Pkg().Path(), pkg)
7070
}
7171

7272
return false
@@ -128,7 +128,7 @@ func checkInitialLower(pass *analysis.Pass, lit *ast.BasicLit) {
128128

129129
// If the first word is all uppercase, it's an
130130
// initialism, so don't flag it.
131-
if first == strings.ToUpper(first) {
131+
if first == strings.ToUpper(first) && len(first) > 1 {
132132
return
133133
}
134134

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package messagefmt
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"path/filepath"
7+
"testing"
8+
9+
"golang.org/x/tools/go/analysis/analysistest"
10+
)
11+
12+
func TestAnalyzer(t *testing.T) {
13+
testdata := analysistest.TestData()
14+
15+
testCases := []struct {
16+
desc string
17+
pkg string
18+
}{
19+
{
20+
desc: "logrus",
21+
pkg: "a",
22+
},
23+
{
24+
desc: "kingpin",
25+
pkg: "b",
26+
},
27+
}
28+
29+
for _, test := range testCases {
30+
test := test
31+
t.Run(test.desc, func(t *testing.T) {
32+
t.Parallel()
33+
34+
dir := filepath.Join(testdata, "src", test.pkg)
35+
36+
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
37+
cmd := exec.Command("go", "mod", "vendor")
38+
cmd.Dir = dir
39+
40+
t.Cleanup(func() {
41+
_ = os.RemoveAll(filepath.Join(testdata, "src", test.pkg, "vendor"))
42+
})
43+
44+
if output, err := cmd.CombinedOutput(); err != nil {
45+
t.Fatal(err, string(output))
46+
}
47+
}
48+
49+
analysistest.RunWithSuggestedFixes(t, testdata, Analyzer, test.pkg)
50+
})
51+
}
52+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/projectcontour/lint/pkg/analysis/messagefmt/testdata/src/a
2+
3+
go 1.16
4+
5+
require github.com/sirupsen/logrus v1.8.0
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
4+
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU=
8+
github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
9+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
10+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
11+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
12+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package a
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
)
6+
7+
func invalid() {
8+
logrus.Info("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
9+
logrus.Debug("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
10+
logrus.Error("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
11+
logrus.Fatal("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
12+
logrus.Panic("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
13+
logrus.Print("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
14+
logrus.Info("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
15+
logrus.Trace("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
16+
logrus.Warn("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
17+
logrus.Warning("A walrus appears") // want `message starts with uppercase: "A walrus appears"`
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package a
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
)
6+
7+
func valid() {
8+
logrus.Info("a walrus appears")
9+
logrus.Debug("a walrus appears")
10+
logrus.Error("a walrus appears")
11+
logrus.Fatal("a walrus appears")
12+
logrus.Panic("a walrus appears")
13+
logrus.Print("a walrus appears")
14+
logrus.Info("a walrus appears")
15+
logrus.Trace("a walrus appears")
16+
logrus.Warn("a walrus appears")
17+
logrus.Warning("a walrus appears")
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/projectcontour/lint/pkg/analysis/messagefmt/testdata/src/b
2+
3+
go 1.16
4+
5+
require (
6+
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
7+
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 // indirect
8+
gopkg.in/alecthomas/kingpin.v2 v2.2.6
9+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
2+
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
3+
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRYS3XieqF+Z9B9gNxo/eANAJCF2eiN4=
4+
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
5+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
6+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
10+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
11+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
12+
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
13+
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
16+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package b
2+
3+
import (
4+
"fmt"
5+
6+
"gopkg.in/alecthomas/kingpin.v2"
7+
)
8+
9+
func invalid() {
10+
verbose := kingpin.Flag("verbose", "verbose mode.").Short('v').Bool() // want `message starts with lowercase: "verbose mode."`
11+
12+
deleteCommand := kingpin.Command("delete", "delete an object.") // want `message starts with lowercase: "delete an object."`
13+
14+
fmt.Println(verbose, deleteCommand)
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package b
2+
3+
import (
4+
"fmt"
5+
6+
"gopkg.in/alecthomas/kingpin.v2"
7+
)
8+
9+
func valid() {
10+
verbose := kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool()
11+
12+
deleteCommand := kingpin.Command("delete", "Delete an object.")
13+
14+
fmt.Println(verbose, deleteCommand)
15+
}

0 commit comments

Comments
 (0)