|
1 | 1 | package actionlint
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "strings" |
4 | 5 | "testing"
|
5 | 6 | )
|
6 | 7 |
|
@@ -82,3 +83,115 @@ func TestRulePyflakesDetectPythonShell(t *testing.T) {
|
82 | 83 | })
|
83 | 84 | }
|
84 | 85 | }
|
| 86 | + |
| 87 | +func TestRulePyflakesParsePyflakesOutputOK(t *testing.T) { |
| 88 | + tests := []struct { |
| 89 | + what string |
| 90 | + input string |
| 91 | + want []string |
| 92 | + }{ |
| 93 | + { |
| 94 | + what: "no error", |
| 95 | + input: "", |
| 96 | + }, |
| 97 | + { |
| 98 | + what: "ignore unrelated lines", |
| 99 | + input: "this line\nshould be\nignored\n", |
| 100 | + }, |
| 101 | + { |
| 102 | + what: "single error", |
| 103 | + input: "<stdin>:1:7: undefined name 'foo'\n", |
| 104 | + want: []string{ |
| 105 | + ":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]", |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + what: "multiple errors", |
| 110 | + input: "<stdin>:1:7: undefined name 'foo'\n" + |
| 111 | + "<stdin>:1:7: undefined name 'foo'\n" + |
| 112 | + "<stdin>:1:7: undefined name 'foo'\n", |
| 113 | + want: []string{ |
| 114 | + ":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]", |
| 115 | + ":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]", |
| 116 | + ":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]", |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + what: "syntax error", |
| 121 | + input: "<stdin>:1:7: unexpected EOF while parsing\n" + |
| 122 | + "print(\n" + |
| 123 | + " ^\n", |
| 124 | + want: []string{ |
| 125 | + ":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]", |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + what: "ignore unrelated lines between multiple errors", |
| 130 | + input: "this line should be ignored\n" + |
| 131 | + "this line should be ignored\n" + |
| 132 | + "<stdin>:1:7: undefined name 'foo'\n" + |
| 133 | + "this line should be ignored\n" + |
| 134 | + "this line should be ignored\n" + |
| 135 | + "<stdin>:1:7: undefined name 'foo'\n" + |
| 136 | + "this line should be ignored\n" + |
| 137 | + "this line should be ignored\n", |
| 138 | + want: []string{ |
| 139 | + ":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]", |
| 140 | + ":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]", |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + what: "CRLF", |
| 145 | + input: "<stdin>:1:7: undefined name 'foo'\r\n" + |
| 146 | + "<stdin>:1:7: undefined name 'foo'\r\n", |
| 147 | + want: []string{ |
| 148 | + ":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]", |
| 149 | + ":1:2: pyflakes reported issue in this script: 1:7: undefined name 'foo' [pyflakes]", |
| 150 | + }, |
| 151 | + }, |
| 152 | + } |
| 153 | + |
| 154 | + for _, tc := range tests { |
| 155 | + t.Run(tc.what, func(t *testing.T) { |
| 156 | + r := newRulePyflakes(&externalCommand{}) |
| 157 | + stdout := []byte(tc.input) |
| 158 | + pos := &Pos{Line: 1, Col: 2} |
| 159 | + for len(stdout) > 0 { |
| 160 | + o, err := r.parseNextError(stdout, pos) |
| 161 | + if err != nil { |
| 162 | + t.Fatalf("Parse error %q while reading input %q", err, stdout) |
| 163 | + } |
| 164 | + stdout = o |
| 165 | + } |
| 166 | + have := r.Errs() |
| 167 | + if len(have) != len(tc.want) { |
| 168 | + msgs := []string{} |
| 169 | + for _, e := range have { |
| 170 | + msgs = append(msgs, e.Error()) |
| 171 | + } |
| 172 | + t.Fatalf("%d errors were expected but got %d errors. got errors are:\n%#v", len(tc.want), len(have), msgs) |
| 173 | + } |
| 174 | + |
| 175 | + for i, want := range tc.want { |
| 176 | + have := have[i] |
| 177 | + msg := have.Error() |
| 178 | + if !strings.Contains(msg, want) { |
| 179 | + t.Errorf("Error %q does not contain expected message %q", msg, want) |
| 180 | + } |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestRulePyflakesParsePyflakesOutputError(t *testing.T) { |
| 187 | + r := newRulePyflakes(&externalCommand{}) |
| 188 | + _, err := r.parseNextError([]byte("<stdin>:1:7: undefined name 'foo'"), &Pos{}) |
| 189 | + if err == nil { |
| 190 | + t.Fatal("Error did not happen") |
| 191 | + } |
| 192 | + have := err.Error() |
| 193 | + want := `error message from pyflakes does not end with \n nor \r\n` |
| 194 | + if !strings.Contains(have, want) { |
| 195 | + t.Fatalf("Error %q does not contain expected message %q", have, want) |
| 196 | + } |
| 197 | +} |
0 commit comments