-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
140 lines (122 loc) · 3.87 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bytes"
"fmt"
"go/format"
"log"
"os"
"path/filepath"
"strings"
"github.com/Antonboom/testifylint/internal/checkers"
)
var analyserTestdataPath = filepath.Join("analyzer", "testdata", "src")
var freeTestsGenerators = map[string]TestsGenerator{ // by subdirectory.
"base-test": BaseTestsGenerator{},
"error-as-target": ErrorAsTargetTestsGenerator{},
"expected-var-custom-pattern": ExpectedVarCustomPatternTestsGenerator{},
"formatter-not-defaults": FormatterNotDefaultsTestsGenerator{},
"require-error-fn-pattern": RequireErrorFnPatternTestsGenerator{},
"suite-require-extra-assert-call": SuiteRequireExtraAssertCallTestsGenerator{},
}
var checkerTestsGenerators = []CheckerTestsGenerator{
BlankImportTestsGenerator{},
BoolCompareTestsGenerator{},
ComparesTestsGenerator{},
ContainsTestsGenerator{},
EmptyTestsGenerator{},
EncodedCompareTestsGenerator{},
ErrorNilTestsGenerator{},
ErrorIsAsTestsGenerator{},
ExpectedActualTestsGenerator{},
FloatCompareTestsGenerator{},
FormatterTestsGenerator{},
GoRequireTestsGenerator{},
LenTestsGenerator{},
NegativePositiveTestsGenerator{},
NilCompareTestsGenerator{},
RegexpTestsGenerator{},
RequireErrorTestsGenerator{},
SuiteBrokenParallelTestsGenerator{},
SuiteDontUsePkgTestsGenerator{},
SuiteExtraAssertCallTestsGenerator{},
SuiteSubtestRunTestsGenerator{},
SuiteTHelperTestsGenerator{},
UselessAssertTestsGenerator{},
}
func init() {
genForChecker := make(map[string]struct{}, len(checkerTestsGenerators))
for _, g := range checkerTestsGenerators {
name := g.Checker().Name()
if _, ok := genForChecker[name]; ok {
panic(fmt.Sprintf("multiple test generators for checker %q", name))
}
genForChecker[name] = struct{}{}
}
for _, ch := range checkers.All() {
if _, ok := genForChecker[ch]; !ok {
log.Printf("[WARN] No generated tests for %q checker\n", ch)
}
}
}
func main() {
if err := generateFreeTests(); err != nil {
log.Panic(err)
}
if err := generateCheckersDefaultTests(); err != nil {
log.Panic(err)
}
}
func generateFreeTests() error {
for dir, g := range freeTestsGenerators {
testFile := strings.ReplaceAll(dir, "-", "_") + "_test.go"
output := filepath.Join(analyserTestdataPath, dir, testFile)
if err := genTestFilesPair(g, output); err != nil {
return fmt.Errorf("%s: %v", dir, err)
}
}
return nil
}
func generateCheckersDefaultTests() error {
for _, g := range checkerTestsGenerators {
checker := g.Checker().Name()
testFile := strings.ReplaceAll(checker, "-", "_") + "_test.go"
output := filepath.Join(analyserTestdataPath, "checkers-default", checker, testFile)
if err := genTestFilesPair(g, output); err != nil {
return fmt.Errorf("%s: %v", checker, err)
}
}
return nil
}
func genTestFilesPair(g TestsGenerator, path string) error {
dir := filepath.Dir(path)
if err := os.RemoveAll(dir); err != nil {
return fmt.Errorf("rm tests dir: %v", err)
}
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("mkdir tests dir: %v", err)
}
tmplData := g.TemplateData()
if err := genGoFileFromTmpl(path, g.ErroredTemplate(), tmplData); err != nil {
return fmt.Errorf("generate test file: %v", err)
}
if goldenTmpl := g.GoldenTemplate(); goldenTmpl != nil {
if err := genGoFileFromTmpl(path+".golden", goldenTmpl, tmplData); err != nil {
return fmt.Errorf("generate golden file: %v", err)
}
} else {
log.Printf("[WARN] No golden file in %q\n", dir)
}
return nil
}
func genGoFileFromTmpl(output string, exec Executor, data any) error {
b := bytes.NewBuffer(nil)
if err := exec.Execute(b, data); err != nil {
return fmt.Errorf("execute cases tmpl: %v", err)
}
formatted, err := format.Source(b.Bytes())
if err != nil {
_ = os.WriteFile(output, b.Bytes(), 0o644) // For debug.
return fmt.Errorf("format %s: %v", output, err)
}
return os.WriteFile(output, formatted, 0o644)
}