Skip to content

Commit

Permalink
add tests for parsing pyflakes output
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Apr 15, 2024
1 parent 59569c8 commit 208b9ac
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rule_pyflakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
113 changes: 113 additions & 0 deletions rule_pyflakes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actionlint

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -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: "<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)
}
}

0 comments on commit 208b9ac

Please sign in to comment.