Skip to content

Commit

Permalink
add tests for parsing command line passed to -shellcheck and `-pyfl…
Browse files Browse the repository at this point in the history
…akes`
  • Loading branch information
rhysd committed Jan 4, 2025
1 parent 895f01a commit e494681
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
18 changes: 9 additions & 9 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (proc *concurrentProcess) wait() {
// is resolved in this function.
func (proc *concurrentProcess) newCommandRunner(exe string, combineOutput bool) (*externalCommand, error) {
var args []string
p, args, err := findExe(exe)
p, args, err := resolveExternalCommand(exe)
if err != nil {
return nil, err
}
Expand All @@ -124,16 +124,16 @@ func (proc *concurrentProcess) newCommandRunner(exe string, combineOutput bool)
return cmd, nil
}

func findExe(exe string) (string, []string, error) {
p, err := execabs.LookPath(exe)
func resolveExternalCommand(exe string) (string, []string, error) {
c, err := execabs.LookPath(exe)
if err == nil {
return p, nil, nil
return c, nil, nil
}
// See if the command string contains args. As it is best effort, we do not
// handle parse errors.
if exeArgs, _ := shellwords.Parse(exe); len(exeArgs) > 0 {
if p, err := execabs.LookPath(exeArgs[0]); err == nil {
return p, exeArgs[1:], nil

// Try to parse the string as a command line instead of a single executable file path.
if a, err := shellwords.Parse(exe); err == nil && len(a) > 0 {
if c, err := execabs.LookPath(a[0]); err == nil {
return c, a[1:], nil
}
}

Expand Down
30 changes: 30 additions & 0 deletions process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,33 @@ func TestProcessCommandExitStatusNonZero(t *testing.T) {
t.Fatalf("Unexpected error happened: %q", msg)
}
}

func TestProcessCommandlineParseError(t *testing.T) {
tests := []struct {
what string
cmd string
}{
{
what: "broken command line",
cmd: "'broken' 'arg",
},
{
what: "executable file not found",
cmd: "this-command-does-not-exist",
},
{
what: "empty",
cmd: "",
},
}

p := newConcurrentProcess(1)
for _, tc := range tests {
t.Run(tc.what, func(t *testing.T) {
_, err := p.newCommandRunner(tc.cmd, true)
if err == nil {
t.Fatalf("Command %q caused no error", tc)
}
})
}
}

0 comments on commit e494681

Please sign in to comment.