From 9927b78427f5e24e271af708075dc96b8cc9f493 Mon Sep 17 00:00:00 2001 From: John Eikenberry Date: Thu, 3 Jun 2021 15:57:24 -0700 Subject: [PATCH 1/2] add subshell quoting tests baseline, single-quote and double quote tests. w/o fix single-quote test breaks, other 2 work. --- shellwords_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/shellwords_test.go b/shellwords_test.go index 57b8395..da5fc6f 100644 --- a/shellwords_test.go +++ b/shellwords_test.go @@ -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) + } + }) +} From 6bc480843c1c25d23ba9f04f9ffb01e2e4cbede4 Mon Sep 17 00:00:00 2001 From: John Eikenberry Date: Thu, 3 Jun 2021 15:58:18 -0700 Subject: [PATCH 2/2] fix single-quote+ParseEnv bug Fixes issues with parsing shell lines with single quotes used to group things (eg. subshell calls). Fix was to set `got` state to `argQuoted` as the double quote path used (and which worked for it). --- shellwords.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shellwords.go b/shellwords.go index 01afd94..1b42a00 100644 --- a/shellwords.go +++ b/shellwords.go @@ -232,7 +232,7 @@ loop: case '\'': if !doubleQuoted && !dollarQuote { if singleQuoted { - got = argSingle + got = argQuoted } singleQuoted = !singleQuoted continue