-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
99 lines (85 loc) · 2.74 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
package main
import (
"os"
"testing"
pl "github.com/zph/polylint/pkg"
)
const (
forFileIgnore = `# polylint disable-for-file=no-print,no-print-js
print("A")`
nextLineIgnore = `# polylint disable-next-line=no-print,no-print-js
print("A")`
nextLineIgnoreShorthand = `# polylint disable=no-print,no-print-js
print("A")
logging()
`
nextLineIgnoreDoesntApply = `# polylint disable-next-line=no-print,no-print-js
print("A")`
fileWithFaultyIgnoreStatement = `# polylint disable-xyz=no-print,no-print-js
print("A") `
)
func TestProcessFile(t *testing.T) {
simpleConfigFile, err := loadTestingConfigFile(simpleConfigFilePath)
if err != nil {
panic(err)
}
var tests = []struct {
name string
content string
path string
findingsCount int
expectedErr error
}{
{"Basic test without ignores", `print("A")`, "example.py", 5, nil},
{"Basic test for-file ignore", forFileIgnore, "example.py", 3, nil},
{"Basic test next-line ignore", nextLineIgnore, "example.py", 3, nil},
{"Basic test next-line ignore shorthand", nextLineIgnoreShorthand, "example.py", 4, nil},
{"Basic test next-line ignore doesn't apply", nextLineIgnoreDoesntApply, "example.py", 5, nil},
{"Basic test with faulty ignore statement", fileWithFaultyIgnoreStatement, "example.py", 5, nil},
{"Basic test with banned filename", nextLineIgnore, "print.py", 4, nil},
}
for idx, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := pl.LoadConfigFile(simpleConfigFile)
if err != nil {
t.Fatalf("Error loading config file: %v", err)
}
result, err := pl.ProcessFile(tt.content, tt.path, cfg)
if err != tt.expectedErr {
t.Errorf("Test #%d Error was incorrect, got: %v, want: %v.", idx, err, tt.expectedErr)
}
if len(result.Findings) != tt.findingsCount {
t.Errorf("Test #%d Result was incorrect, findings count: got: %d, want: %d.\n", idx, len(result.Findings), tt.findingsCount)
}
})
}
}
var simpleConfigFilePath = "./examples/simple.yaml"
func loadTestingConfigFile(path string) (string, error) {
content, err := os.ReadFile(path)
if err != nil {
return "", err
}
return string(content), nil
}
func TestConfigFileParsing(t *testing.T) {
simpleConfigFile, err := loadTestingConfigFile(simpleConfigFilePath)
if err != nil {
panic(err)
}
var tests = []struct {
name string
content string
expectedRuleCount int
}{
{"basic config file with 1 rule", simpleConfigFile, 7},
}
for idx, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, _ := pl.LoadConfigFile(tt.content)
if len(result.Rules) != tt.expectedRuleCount {
t.Errorf("Test #%d Result was incorrect, findings count: got: %d, want: %d.\n", idx, len(result.Rules), tt.expectedRuleCount)
}
})
}
}