Skip to content

Commit

Permalink
Merge f077c0c into 2b0d8a7
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Feb 25, 2019
2 parents 2b0d8a7 + f077c0c commit 95c3f8e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions shellwords.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"os"
"regexp"
"strings"
)

var (
Expand Down Expand Up @@ -120,7 +121,11 @@ loop:
if err != nil {
return nil, err
}
buf = out
if r == ')' {
buf = buf[:len(buf)-len(backtick)-2] + out
} else {
buf = buf[:len(buf)-len(backtick)-1] + out
}
}
backtick = ""
dollarQuote = !dollarQuote
Expand All @@ -131,7 +136,7 @@ loop:
}
case '(':
if !singleQuoted && !doubleQuoted && !backQuote {
if !dollarQuote && len(buf) > 0 && buf == "$" {
if !dollarQuote && strings.HasSuffix(buf, "$") {
dollarQuote = true
buf += "("
continue
Expand Down
13 changes: 13 additions & 0 deletions shellwords_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ func TestBacktick(t *testing.T) {
t.Fatalf("Expected %#v, but %#v:", expected, args)
}

args, err = parser.Parse(`echo bar=$(echo 200)cm`)
if err != nil {
t.Fatal(err)
}
expected = []string{"echo", "bar=200cm"}
if !reflect.DeepEqual(args, expected) {
t.Fatalf("Expected %#v, but %#v:", expected, args)
}

parser.ParseBacktick = false
args, err = parser.Parse(`echo $(echo "foo")`)
expected = []string{"echo", `$(echo "foo")`}
Expand Down Expand Up @@ -124,6 +133,10 @@ func TestBacktickError(t *testing.T) {
if err == nil {
t.Fatal("Should be an error")
}
_, err = parser.Parse(`echo FOO=$(echo1)`)
if err == nil {
t.Fatal("Should be an error")
}
_, err = parser.Parse(`echo $(echo1`)
if err == nil {
t.Fatal("Should be an error")
Expand Down

0 comments on commit 95c3f8e

Please sign in to comment.