-
Notifications
You must be signed in to change notification settings - Fork 163
/
rule_pyflakes_test.go
197 lines (187 loc) · 4.88 KB
/
rule_pyflakes_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
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
package actionlint
import (
"strings"
"testing"
)
func TestRulePyflakesDetectPythonShell(t *testing.T) {
tests := []struct {
what string
isPython bool
workflow string // Shell name set at 'defaults' in Workflow node
job string // Shell name set at 'defaults' in Job node
step string // Shell name set at 'shell' in Step node
}{
{
what: "no default shell",
isPython: false,
},
{
what: "workflow default",
isPython: true,
workflow: "python",
},
{
what: "job default",
isPython: true,
job: "python",
},
{
what: "step shell",
isPython: true,
step: "python",
},
{
what: "custom shell",
isPython: true,
workflow: "python {0}",
},
{
what: "other shell",
isPython: false,
workflow: "pwsh",
},
{
what: "other custom shell",
isPython: false,
workflow: "bash -e {0}",
},
}
for _, tc := range tests {
t.Run(tc.what, func(t *testing.T) {
r := newRulePyflakes(&externalCommand{})
w := &Workflow{}
if tc.workflow != "" {
w.Defaults = &Defaults{
Run: &DefaultsRun{
Shell: &String{Value: tc.workflow},
},
}
}
r.VisitWorkflowPre(w)
j := &Job{}
if tc.job != "" {
j.Defaults = &Defaults{
Run: &DefaultsRun{
Shell: &String{Value: tc.job},
},
}
}
r.VisitJobPre(j)
e := &ExecRun{}
if tc.step != "" {
e.Shell = &String{Value: tc.step}
}
if have := r.isPythonShell(e); have != tc.isPython {
t.Fatalf("Actual isPython=%v but wanted isPython=%v", have, tc.isPython)
}
})
}
}
func TestRulePyflakesParsePyflakesOutputOK(t *testing.T) {
tests := []struct {
what string
input string
want []string
}{
{
what: "no error",
input: "",
},
{
what: "ignore unrelated lines",
input: "this line\nshould be\nignored\n",
},
{
what: "single error",
input: "<stdin>:1:7: undefined name 'foo'\n",
want: []string{
":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]",
},
},
{
what: "multiple errors",
input: "<stdin>:1:7: undefined name 'foo'\n" +
"<stdin>:1:7: undefined name 'foo'\n" +
"<stdin>:1:7: undefined name 'foo'\n",
want: []string{
":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]",
":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]",
":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]",
},
},
{
what: "syntax error",
input: "<stdin>:1:7: unexpected EOF while parsing\n" +
"print(\n" +
" ^\n",
want: []string{
":1:2: pyflakes reported issue in this script: 1:7: unexpected EOF while parsing [pyflakes]",
},
},
{
what: "ignore unrelated lines between multiple errors",
input: "this line should be ignored\n" +
"this line should be ignored\n" +
"<stdin>:1:7: undefined name 'foo'\n" +
"this line should be ignored\n" +
"this line should be ignored\n" +
"<stdin>:1:7: undefined name 'foo'\n" +
"this line should be ignored\n" +
"this line should be ignored\n",
want: []string{
":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]",
":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]",
},
},
{
what: "CRLF",
input: "<stdin>:1:7: undefined name 'foo'\r\n" +
"<stdin>:1:7: undefined name 'foo'\r\n",
want: []string{
":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]",
":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]",
},
},
}
for _, tc := range tests {
t.Run(tc.what, func(t *testing.T) {
r := newRulePyflakes(&externalCommand{})
stdout := []byte(tc.input)
pos := &Pos{Line: 1, Col: 2}
for len(stdout) > 0 {
o, err := r.parseNextError(stdout, pos)
if err != nil {
t.Fatalf("Parse error %q while reading input %q", err, stdout)
}
stdout = o
}
have := r.Errs()
if len(have) != len(tc.want) {
msgs := []string{}
for _, e := range have {
msgs = append(msgs, e.Error())
}
t.Fatalf("%d errors were expected but got %d errors. got errors are:\n%#v", len(tc.want), len(have), msgs)
}
for i, want := range tc.want {
have := have[i]
msg := have.Error()
if !strings.Contains(msg, want) {
t.Errorf("Error %q does not contain expected message %q", msg, want)
}
}
})
}
}
func TestRulePyflakesParsePyflakesOutputError(t *testing.T) {
r := newRulePyflakes(&externalCommand{})
_, err := r.parseNextError([]byte("<stdin>:1:7: undefined name 'foo'"), &Pos{})
if err == nil {
t.Fatal("Error did not happen")
}
have := err.Error()
want := `error message from pyflakes does not end with \n nor \r\n`
if !strings.Contains(have, want) {
t.Fatalf("Error %q does not contain expected message %q", have, want)
}
}