Skip to content

Commit

Permalink
Merge pull request #49 from eikenb/issue-48-single-quote-parseenv
Browse files Browse the repository at this point in the history
Fix issue 48: single-quote+ParseEnv bug
  • Loading branch information
mattn committed Jun 4, 2021
2 parents 501b5b2 + 6bc4808 commit 973b9d5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion shellwords.go
Expand Up @@ -232,7 +232,7 @@ loop:
case '\'':
if !doubleQuoted && !dollarQuote {
if singleQuoted {
got = argSingle
got = argQuoted
}
singleQuoted = !singleQuoted
continue
Expand Down
50 changes: 50 additions & 0 deletions shellwords_test.go
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 973b9d5

Please sign in to comment.