Skip to content

Commit e494681

Browse files
committed
add tests for parsing command line passed to -shellcheck and -pyflakes
1 parent 895f01a commit e494681

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

process.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (proc *concurrentProcess) wait() {
111111
// is resolved in this function.
112112
func (proc *concurrentProcess) newCommandRunner(exe string, combineOutput bool) (*externalCommand, error) {
113113
var args []string
114-
p, args, err := findExe(exe)
114+
p, args, err := resolveExternalCommand(exe)
115115
if err != nil {
116116
return nil, err
117117
}
@@ -124,16 +124,16 @@ func (proc *concurrentProcess) newCommandRunner(exe string, combineOutput bool)
124124
return cmd, nil
125125
}
126126

127-
func findExe(exe string) (string, []string, error) {
128-
p, err := execabs.LookPath(exe)
127+
func resolveExternalCommand(exe string) (string, []string, error) {
128+
c, err := execabs.LookPath(exe)
129129
if err == nil {
130-
return p, nil, nil
130+
return c, nil, nil
131131
}
132-
// See if the command string contains args. As it is best effort, we do not
133-
// handle parse errors.
134-
if exeArgs, _ := shellwords.Parse(exe); len(exeArgs) > 0 {
135-
if p, err := execabs.LookPath(exeArgs[0]); err == nil {
136-
return p, exeArgs[1:], nil
132+
133+
// Try to parse the string as a command line instead of a single executable file path.
134+
if a, err := shellwords.Parse(exe); err == nil && len(a) > 0 {
135+
if c, err := execabs.LookPath(a[0]); err == nil {
136+
return c, a[1:], nil
137137
}
138138
}
139139

process_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,33 @@ func TestProcessCommandExitStatusNonZero(t *testing.T) {
343343
t.Fatalf("Unexpected error happened: %q", msg)
344344
}
345345
}
346+
347+
func TestProcessCommandlineParseError(t *testing.T) {
348+
tests := []struct {
349+
what string
350+
cmd string
351+
}{
352+
{
353+
what: "broken command line",
354+
cmd: "'broken' 'arg",
355+
},
356+
{
357+
what: "executable file not found",
358+
cmd: "this-command-does-not-exist",
359+
},
360+
{
361+
what: "empty",
362+
cmd: "",
363+
},
364+
}
365+
366+
p := newConcurrentProcess(1)
367+
for _, tc := range tests {
368+
t.Run(tc.what, func(t *testing.T) {
369+
_, err := p.newCommandRunner(tc.cmd, true)
370+
if err == nil {
371+
t.Fatalf("Command %q caused no error", tc)
372+
}
373+
})
374+
}
375+
}

0 commit comments

Comments
 (0)