Skip to content

Commit

Permalink
add tests for Python shell detection by pyflakes rule
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Apr 9, 2024
1 parent 0dde706 commit f57173a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
23 changes: 13 additions & 10 deletions rule_pyflakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,8 @@ type RulePyflakes struct {
mu sync.Mutex
}

// NewRulePyflakes creates new RulePyflakes instance. Parameter executable can be command name
// or relative/absolute file path. When the given executable is not found in system, it returns
// an error.
func NewRulePyflakes(executable string, proc *concurrentProcess) (*RulePyflakes, error) {
cmd, err := proc.newCommandRunner(executable)
if err != nil {
return nil, err
}
r := &RulePyflakes{
func newRulePyflakes(cmd *externalCommand) *RulePyflakes {
return &RulePyflakes{
RuleBase: RuleBase{
name: "pyflakes",
desc: "Checks for Python script when \"shell: python\" is configured using Pyflakes",
Expand All @@ -52,7 +45,17 @@ func NewRulePyflakes(executable string, proc *concurrentProcess) (*RulePyflakes,
workflowShellIsPython: shellIsPythonKindUnspecified,
jobShellIsPython: shellIsPythonKindUnspecified,
}
return r, nil
}

// NewRulePyflakes creates new RulePyflakes instance. Parameter executable can be command name
// or relative/absolute file path. When the given executable is not found in system, it returns
// an error.
func NewRulePyflakes(executable string, proc *concurrentProcess) (*RulePyflakes, error) {
cmd, err := proc.newCommandRunner(executable)
if err != nil {
return nil, err
}
return newRulePyflakes(cmd), nil
}

// VisitJobPre is callback when visiting Job node before visiting its children.
Expand Down
84 changes: 84 additions & 0 deletions rule_pyflakes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package actionlint

import (
"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)
}
})
}
}

0 comments on commit f57173a

Please sign in to comment.