diff --git a/rule_pyflakes.go b/rule_pyflakes.go index 58ac4afcf..cfbc29a42 100644 --- a/rule_pyflakes.go +++ b/rule_pyflakes.go @@ -163,7 +163,7 @@ func (rule *RulePyflakes) parseNextError(stdout []byte, pos *Pos) ([]byte, error msg = b[:idx] b = b[idx+1:] } else { - return nil, fmt.Errorf("error message from pyflakes does not end with \\n nor \\r\\n while checking script at %s. output: %q", pos, stdout) + return nil, fmt.Errorf(`error message from pyflakes does not end with \n nor \r\n while checking script at %s. output: %q`, pos, stdout) } // This method needs to be thread-safe since concurrentProcess.run calls its callback in a different goroutine. diff --git a/rule_pyflakes_test.go b/rule_pyflakes_test.go index 4021d49a7..e9ccc810b 100644 --- a/rule_pyflakes_test.go +++ b/rule_pyflakes_test.go @@ -1,6 +1,7 @@ package actionlint import ( + "strings" "testing" ) @@ -82,3 +83,115 @@ func TestRulePyflakesDetectPythonShell(t *testing.T) { }) } } + +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: ":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: ":1:7: undefined name 'foo'\n" + + ":1:7: undefined name 'foo'\n" + + ":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: ":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" + + ":1:7: undefined name 'foo'\n" + + "this line should be ignored\n" + + "this line should be ignored\n" + + ":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: ":1:7: undefined name 'foo'\r\n" + + ":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(":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) + } +}