-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
111 lines (97 loc) · 2.09 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"flag"
"os/exec"
"strings"
"testing"
)
var fix = flag.Bool("fix", false, "fix (modify) source code")
func TestLint(t *testing.T) {
t.Parallel()
linters := []tool{
{
"vet",
[]string{"go", "vet", "./..."},
nil,
}, {
"staticcheck",
[]string{"go", "tool", "staticcheck", "./..."},
nil,
}, {
"govulncheck",
[]string{"go", "tool", "govulncheck", "./..."},
nil,
}, {
"buf lint",
[]string{"go", "tool", "buf", "lint", "."},
nil,
}, {
"cue vet",
[]string{"go", "tool", "cue", "vet", "-c=false", "./..."},
nil,
},
}
runAll(t, linters)
}
func TestFormat(t *testing.T) {
if !*fix {
t.Parallel()
}
formatters := []tool{
{
"mod tidy",
[]string{"go", "mod", "tidy", "-diff"},
[]string{"go", "mod", "tidy"},
}, {
"gofumpt",
[]string{"go", "tool", "gofumpt", "-d", "."},
[]string{"go", "tool", "gofumpt", "-w", "."},
}, {
"cue fmt",
[]string{"go", "tool", "cue", "fmt", "--check", "--diff", "./..."},
[]string{"go", "tool", "cue", "fmt", "./..."},
}, {
"buf fmt",
[]string{"go", "tool", "buf", "format", "--exit-code", "--diff", "."},
[]string{"go", "tool", "buf", "format", "-w", "."},
},
}
runAll(t, formatters)
}
func TestSpell(t *testing.T) {
ignored := strings.Join([]string{"rebounce"}, ",")
spells := []tool{{
"misspell",
[]string{"go", "tool", "misspell", "-i", ignored, "-error", "."},
[]string{"go", "tool", "misspell", "-i", ignored, "-w", "."},
}}
runAll(t, spells)
}
type tool struct {
name string
args []string
fix []string
}
func runAll(t *testing.T, tos []tool) {
t.Helper()
for _, tc := range tos {
t.Run(tc.name, func(t *testing.T) {
cmd, args := tc.args[0], tc.args[1:]
if !*fix {
t.Parallel()
} else {
if len(tc.fix) == 0 {
t.Skip("no fixer available")
}
cmd, args = tc.fix[0], tc.fix[1:]
}
b, err := exec.CommandContext(t.Context(), cmd, args...).CombinedOutput()
if err != nil {
t.Errorf("%s: %v\n%s", tc.name, err, string(b))
if len(tc.fix) > 0 {
t.Log("fix available, run with: go test -fix")
}
}
})
}
}