Skip to content

Commit f57173a

Browse files
committed
add tests for Python shell detection by pyflakes rule
1 parent 0dde706 commit f57173a

File tree

2 files changed

+97
-10
lines changed

2 files changed

+97
-10
lines changed

rule_pyflakes.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,8 @@ type RulePyflakes struct {
3535
mu sync.Mutex
3636
}
3737

38-
// NewRulePyflakes creates new RulePyflakes instance. Parameter executable can be command name
39-
// or relative/absolute file path. When the given executable is not found in system, it returns
40-
// an error.
41-
func NewRulePyflakes(executable string, proc *concurrentProcess) (*RulePyflakes, error) {
42-
cmd, err := proc.newCommandRunner(executable)
43-
if err != nil {
44-
return nil, err
45-
}
46-
r := &RulePyflakes{
38+
func newRulePyflakes(cmd *externalCommand) *RulePyflakes {
39+
return &RulePyflakes{
4740
RuleBase: RuleBase{
4841
name: "pyflakes",
4942
desc: "Checks for Python script when \"shell: python\" is configured using Pyflakes",
@@ -52,7 +45,17 @@ func NewRulePyflakes(executable string, proc *concurrentProcess) (*RulePyflakes,
5245
workflowShellIsPython: shellIsPythonKindUnspecified,
5346
jobShellIsPython: shellIsPythonKindUnspecified,
5447
}
55-
return r, nil
48+
}
49+
50+
// NewRulePyflakes creates new RulePyflakes instance. Parameter executable can be command name
51+
// or relative/absolute file path. When the given executable is not found in system, it returns
52+
// an error.
53+
func NewRulePyflakes(executable string, proc *concurrentProcess) (*RulePyflakes, error) {
54+
cmd, err := proc.newCommandRunner(executable)
55+
if err != nil {
56+
return nil, err
57+
}
58+
return newRulePyflakes(cmd), nil
5659
}
5760

5861
// VisitJobPre is callback when visiting Job node before visiting its children.

rule_pyflakes_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package actionlint
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestRulePyflakesDetectPythonShell(t *testing.T) {
8+
tests := []struct {
9+
what string
10+
isPython bool
11+
workflow string // Shell name set at 'defaults' in Workflow node
12+
job string // Shell name set at 'defaults' in Job node
13+
step string // Shell name set at 'shell' in Step node
14+
}{
15+
{
16+
what: "no default shell",
17+
isPython: false,
18+
},
19+
{
20+
what: "workflow default",
21+
isPython: true,
22+
workflow: "python",
23+
},
24+
{
25+
what: "job default",
26+
isPython: true,
27+
job: "python",
28+
},
29+
{
30+
what: "step shell",
31+
isPython: true,
32+
step: "python",
33+
},
34+
{
35+
what: "custom shell",
36+
isPython: true,
37+
workflow: "python {0}",
38+
},
39+
{
40+
what: "other shell",
41+
isPython: false,
42+
workflow: "pwsh",
43+
},
44+
{
45+
what: "other custom shell",
46+
isPython: false,
47+
workflow: "bash -e {0}",
48+
},
49+
}
50+
51+
for _, tc := range tests {
52+
t.Run(tc.what, func(t *testing.T) {
53+
r := newRulePyflakes(&externalCommand{})
54+
55+
w := &Workflow{}
56+
if tc.workflow != "" {
57+
w.Defaults = &Defaults{
58+
Run: &DefaultsRun{
59+
Shell: &String{Value: tc.workflow},
60+
},
61+
}
62+
}
63+
r.VisitWorkflowPre(w)
64+
65+
j := &Job{}
66+
if tc.job != "" {
67+
j.Defaults = &Defaults{
68+
Run: &DefaultsRun{
69+
Shell: &String{Value: tc.job},
70+
},
71+
}
72+
}
73+
r.VisitJobPre(j)
74+
75+
e := &ExecRun{}
76+
if tc.step != "" {
77+
e.Shell = &String{Value: tc.step}
78+
}
79+
if have := r.isPythonShell(e); have != tc.isPython {
80+
t.Fatalf("Actual isPython=%v but wanted isPython=%v", have, tc.isPython)
81+
}
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)