Skip to content

Commit 9927b78

Browse files
committed
add subshell quoting tests
baseline, single-quote and double quote tests. w/o fix single-quote test breaks, other 2 work.
1 parent 501b5b2 commit 9927b78

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

shellwords_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,53 @@ func TestParseWithEnvs(t *testing.T) {
454454
})
455455
}
456456
}
457+
458+
func TestSubShellEnv(t *testing.T) {
459+
myParser := &Parser{
460+
ParseEnv: true,
461+
}
462+
463+
errTmpl := "bad arg parsing:\nexpected: %#v\nactual : %#v\n"
464+
465+
t.Run("baseline", func(t *testing.T) {
466+
args, err := myParser.Parse(`program -f abc.txt`)
467+
if err != nil {
468+
t.Fatalf("err should be nil: %v", err)
469+
}
470+
expected := []string{"program", "-f", "abc.txt"}
471+
if len(args) != 3 {
472+
t.Fatalf(errTmpl, expected, args)
473+
}
474+
if args[0] != expected[0] || args[1] != expected[1] || args[2] != expected[2] {
475+
t.Fatalf(errTmpl, expected, args)
476+
}
477+
})
478+
479+
t.Run("single-quoted", func(t *testing.T) {
480+
args, err := myParser.Parse(`sh -c 'echo foo'`)
481+
if err != nil {
482+
t.Fatalf("err should be nil: %v", err)
483+
}
484+
expected := []string{"sh", "-c", "echo foo"}
485+
if len(args) != 3 {
486+
t.Fatalf(errTmpl, expected, args)
487+
}
488+
if args[0] != expected[0] || args[1] != expected[1] || args[2] != expected[2] {
489+
t.Fatalf(errTmpl, expected, args)
490+
}
491+
})
492+
493+
t.Run("double-quoted", func(t *testing.T) {
494+
args, err := myParser.Parse(`sh -c "echo foo"`)
495+
if err != nil {
496+
t.Fatalf("err should be nil: %v", err)
497+
}
498+
expected := []string{"sh", "-c", "echo foo"}
499+
if len(args) != 3 {
500+
t.Fatalf(errTmpl, expected, args)
501+
}
502+
if args[0] != expected[0] || args[1] != expected[1] || args[2] != expected[2] {
503+
t.Fatalf(errTmpl, expected, args)
504+
}
505+
})
506+
}

0 commit comments

Comments
 (0)