forked from VKCOM/noverify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_rules.go
347 lines (276 loc) · 8.27 KB
/
test_rules.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package cmd
import (
"flag"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"runtime"
"strings"
"github.com/VKCOM/noverify/src/ir"
"github.com/VKCOM/noverify/src/linter"
"github.com/VKCOM/noverify/src/rules"
"github.com/VKCOM/noverify/src/utils"
"github.com/VKCOM/noverify/src/workspace"
)
func RegisterTestRulesFlags(ctx *AppContext) (*flag.FlagSet, *FlagsGroups) {
flags := &RulesTestSuite{}
fs := flag.NewFlagSet("test-rules", flag.ContinueOnError)
groups := NewFlagsGroups()
groups.AddGroup("Files")
groups.AddGroup("Other")
fs.StringVar(&flags.Rules, "rules", "./", "Comma separated list of directories or files with rules")
fs.StringVar(&flags.Tests, "tests", "./tests", "Directory or file with tests")
groups.Add("Files", "rules")
groups.Add("Files", "tests")
fs.BoolVar(&flags.KPHP, "kphp", false, "KPHP mode")
groups.Add("Other", "kphp")
ctx.CustomFlags = flags
return fs, groups
}
func TestRules(ctx *AppContext) (status int, err error) {
log.SetFlags(0)
flags := ctx.CustomFlags.(*RulesTestSuite)
suite := NewRulesTestSuite(flags.Rules, flags.Tests, flags.KPHP)
err = suite.Run()
if err != nil {
status = 2
}
if err == nil {
log.Println("[Tests Passed]")
} else {
log.Println("[Tests Failed]")
}
return status, nil
}
type RulesTestSuite struct {
Rules string
Tests string
KPHP bool
}
func NewRulesTestSuite(rulesPaths string, tests string, kphp bool) *RulesTestSuite {
return &RulesTestSuite{Rules: rulesPaths, Tests: tests, KPHP: kphp}
}
func (s *RulesTestSuite) Run() error {
go linter.MemoryLimiterThread(0)
files, err := utils.FindPHPFiles(s.Tests)
if err != nil {
return fmt.Errorf("error find php files %v", err)
}
if len(files) == 0 {
return fmt.Errorf("tests files in '%s' not found", s.Tests)
}
failed := false
for _, file := range files {
errs := s.handleFile(file)
if len(errs) > 0 {
log.Printf("Test '%s' failed\n", file)
for _, err := range errs {
log.Printf(" %v\n", err)
}
failed = true
} else {
log.Printf("Test '%s' passed\n", file)
}
}
if failed {
return fmt.Errorf("test failed")
}
return nil
}
// handleFile processes a file from the list of php files found in the directory.
func (s *RulesTestSuite) handleFile(file string) (errs []error) {
lines, reports, err := s.handleFileContents(file)
if err != nil {
return []error{err}
}
reportsByLine := s.createReportsByLine(reports)
return s.handleLines(lines, reportsByLine)
}
func (s *RulesTestSuite) handleLines(lines []string, reportsByLine map[int][]string) (errs []error) {
for index, line := range lines {
lineIndex := index + 1
reportByLine, hasReports := reportsByLine[lineIndex]
expects, err := s.getExpectationForLine(line, index)
if err != nil {
return []error{err}
}
if expects == nil && hasReports {
for i := range reportByLine {
reportByLine[i] = "'" + reportByLine[i] + "'"
}
return []error{
fmt.Errorf("unhandled errors for line %d, expected: [%s]", lineIndex, strings.Join(reportByLine, ", ")),
}
}
if len(expects) > 0 && !hasReports {
return []error{
fmt.Errorf("no reports matched for line %d", lineIndex),
}
}
unmatched := s.compare(expects, reportByLine)
for _, report := range unmatched {
for i := range reportByLine {
reportByLine[i] = "'" + reportByLine[i] + "'"
}
errs = append(errs, fmt.Errorf("unexpected report: '%s' on line %d, expected: [%s]", report, lineIndex, strings.Join(reportByLine, ", ")))
}
}
return errs
}
func (s *RulesTestSuite) getExpectationForLine(line string, lineIndex int) ([]string, error) {
commIndex := strings.Index(line, "//")
if commIndex == -1 {
return nil, nil
}
comment := line[commIndex+2:]
p := utils.NewCommentParser(comment, lineIndex)
expects, err := p.ParseExpectation()
if err != nil {
return nil, err
}
return expects, nil
}
// compare expected and received reports and returns a list of unmatched errors.
func (s *RulesTestSuite) compare(expects []string, reports []string) (unmatched []string) {
for _, expect := range expects {
var found bool
for _, report := range reports {
if strings.Contains(report, expect) {
found = true
break
}
}
if !found {
unmatched = append(unmatched, expect)
}
}
return unmatched
}
// handleFileContents reads, parses the resulting file, and splits it into lines.
func (s *RulesTestSuite) handleFileContents(file string) (lines []string, reports []*linter.Report, err error) {
rawCheckerName := file
lint := linter.NewLinter(linter.NewConfig("8.1"))
if strings.Contains(file, "_7.4") {
lint = linter.NewLinter(linter.NewConfig("7.4"))
rawCheckerName = strings.ReplaceAll(file, "_7.4", "")
}
runner := NewLinterRunner(lint, linter.NewCheckersFilterWithEnabledAll())
err = InitStubs(lint)
if err != nil {
return nil, nil, fmt.Errorf("load stubs: %v", err)
}
err = s.initEmbeddedRules(lint.Config())
if err != nil {
return nil, nil, err
}
config := lint.Config()
config.KPHP = s.KPHP
ruleSets, err := ParseExternalRules(s.Rules)
if err != nil {
return nil, nil, fmt.Errorf("preload external rules: %v", err)
}
for _, rset := range ruleSets {
config.Checkers.DeclareRules(rset)
}
err = runner.Init(ruleSets, &ParsedFlags{
AllowAll: true,
AllowChecks: AllChecks,
PhpExtensionsArg: "php,inc,php5,phtml",
MaxConcurrency: runtime.NumCPU(),
MaxFileSize: 20 * 1024 * 1024,
UnusedVarPattern: "^_$",
})
if err != nil {
return nil, nil, fmt.Errorf("runner init fail: %v", err)
}
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, nil, fmt.Errorf("error read file '%s': %v", file, err)
}
content := string(data)
// indexing
s.ParseTestFile(lint, file, content)
lint.MetaInfo().SetIndexingComplete(true)
// analyzing
res := s.ParseTestFile(lint, file, content)
checkerNamespace := ""
for _, stmt := range res.RootNode.Stmts {
if namespace, ok := stmt.(*ir.NamespaceStmt); ok {
checkerNamespace = strings.ReplaceAll(namespace.NamespaceName.Value, `\`, `/`) + `/`
break
}
}
lines = strings.Split(content, "\n")
checkerName := filepath.Base(rawCheckerName)
checkerName = checkerName[:len(checkerName)-len(filepath.Ext(file))]
checkerName = checkerNamespace + checkerName
if !strings.HasSuffix(checkerName, "_any") {
if !lint.Config().Checkers.Contains(checkerName) {
return nil, nil, fmt.Errorf("file name with namespace inside must be the name of the checker that is tested. Checker '%s' does not exist", checkerName)
}
return lines, s.filterReports([]string{checkerName}, res.Reports), nil
}
return lines, res.Reports, nil
}
func (s *RulesTestSuite) filterReports(names []string, reports []*linter.Report) []*linter.Report {
set := make(map[string]struct{})
for _, name := range names {
set[name] = struct{}{}
}
var out []*linter.Report
for _, r := range reports {
if _, ok := set[r.CheckName]; ok {
out = append(out, r)
}
}
return out
}
func (s *RulesTestSuite) initEmbeddedRules(config *linter.Config) error {
enableAllRules := func(_ rules.Rule) bool { return true }
ruleSets, err := AddEmbeddedRules(config.Rules, enableAllRules)
if err != nil {
return fmt.Errorf("init embedded rules: %v", err)
}
for _, rset := range ruleSets {
config.Checkers.DeclareRules(rset)
}
return nil
}
// createReportsByLine creates a map with a set of reports for each of the lines
// from the reports. This is necessary because there can be more than one report
// for one line.
func (s *RulesTestSuite) createReportsByLine(reports []*linter.Report) map[int][]string {
reportsByLine := make(map[int][]string)
for _, report := range reports {
line := report.Line
if line < 0 {
continue
}
reportsByLine[line] = append(reportsByLine[line], report.Message)
}
return reportsByLine
}
func (s *RulesTestSuite) ParseTestFile(l *linter.Linter, filename, content string) linter.ParseResult {
var worker *linter.Worker
if l.MetaInfo().IsIndexingComplete() {
worker = l.NewLintingWorker(0)
} else {
worker = l.NewIndexingWorker(0)
}
file := workspace.FileInfo{
Name: filename,
Contents: []byte(content),
}
var err error
var result linter.ParseResult
if worker.MetaInfo().IsIndexingComplete() {
result, err = worker.ParseContents(file)
} else {
err = worker.IndexFile(file)
}
if err != nil {
log.Fatalf("could not parse %s: %v", filename, err.Error())
}
return result
}