Skip to content

Commit

Permalink
add subshell quoting tests
Browse files Browse the repository at this point in the history
baseline, single-quote and double quote tests.
w/o fix single-quote test breaks, other 2 work.
  • Loading branch information
eikenb committed Jun 3, 2021
1 parent 501b5b2 commit 9927b78
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions shellwords_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,53 @@ func TestParseWithEnvs(t *testing.T) {
})
}
}

func TestSubShellEnv(t *testing.T) {
myParser := &Parser{
ParseEnv: true,
}

errTmpl := "bad arg parsing:\nexpected: %#v\nactual : %#v\n"

t.Run("baseline", func(t *testing.T) {
args, err := myParser.Parse(`program -f abc.txt`)
if err != nil {
t.Fatalf("err should be nil: %v", err)
}
expected := []string{"program", "-f", "abc.txt"}
if len(args) != 3 {
t.Fatalf(errTmpl, expected, args)
}
if args[0] != expected[0] || args[1] != expected[1] || args[2] != expected[2] {
t.Fatalf(errTmpl, expected, args)
}
})

t.Run("single-quoted", func(t *testing.T) {
args, err := myParser.Parse(`sh -c 'echo foo'`)
if err != nil {
t.Fatalf("err should be nil: %v", err)
}
expected := []string{"sh", "-c", "echo foo"}
if len(args) != 3 {
t.Fatalf(errTmpl, expected, args)
}
if args[0] != expected[0] || args[1] != expected[1] || args[2] != expected[2] {
t.Fatalf(errTmpl, expected, args)
}
})

t.Run("double-quoted", func(t *testing.T) {
args, err := myParser.Parse(`sh -c "echo foo"`)
if err != nil {
t.Fatalf("err should be nil: %v", err)
}
expected := []string{"sh", "-c", "echo foo"}
if len(args) != 3 {
t.Fatalf(errTmpl, expected, args)
}
if args[0] != expected[0] || args[1] != expected[1] || args[2] != expected[2] {
t.Fatalf(errTmpl, expected, args)
}
})
}

0 comments on commit 9927b78

Please sign in to comment.