Skip to content

Commit

Permalink
Merge pull request jesseduffield#274 from sascha-andres/master
Browse files Browse the repository at this point in the history
fix: escape quote character on Linux
  • Loading branch information
jesseduffield authored Sep 10, 2018
2 parents 7f4371a + 985196f commit 7d86278
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
17 changes: 11 additions & 6 deletions pkg/commands/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (

// Platform stores the os state
type Platform struct {
os string
shell string
shellArg string
escapedQuote string
openCommand string
os string
shell string
shellArg string
escapedQuote string
openCommand string
fallbackEscapedQuote string
}

// OSCommand holds all the os commands
Expand Down Expand Up @@ -140,7 +141,11 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *ex
// Quote wraps a message in platform-specific quotation marks
func (c *OSCommand) Quote(message string) string {
message = strings.Replace(message, "`", "\\`", -1)
return c.Platform.escapedQuote + message + c.Platform.escapedQuote
escapedQuote := c.Platform.escapedQuote
if strings.Contains(message, c.Platform.escapedQuote) {
escapedQuote = c.Platform.fallbackEscapedQuote
}
return escapedQuote + message + escapedQuote
}

// Unquote removes wrapping quotations marks if they are present
Expand Down
11 changes: 6 additions & 5 deletions pkg/commands/os_default_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (

func getPlatform() *Platform {
return &Platform{
os: runtime.GOOS,
shell: "bash",
shellArg: "-c",
escapedQuote: "\"",
openCommand: "open {{filename}}",
os: runtime.GOOS,
shell: "bash",
shellArg: "-c",
escapedQuote: "'",
openCommand: "open {{filename}}",
fallbackEscapedQuote: "\"",
}
}
26 changes: 26 additions & 0 deletions pkg/commands/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,32 @@ func TestOSCommandQuote(t *testing.T) {
assert.EqualValues(t, expected, actual)
}

// TestOSCommandQuoteSingleQuote tests the quote function with ' quotes explicitly for Linux
func TestOSCommandQuoteSingleQuote(t *testing.T) {
osCommand := newDummyOSCommand()

osCommand.Platform.os = "linux"

actual := osCommand.Quote("hello 'test'")

expected := osCommand.Platform.fallbackEscapedQuote + "hello 'test'" + osCommand.Platform.fallbackEscapedQuote

assert.EqualValues(t, expected, actual)
}

// TestOSCommandQuoteSingleQuote tests the quote function with " quotes explicitly for Linux
func TestOSCommandQuoteDoubleQuote(t *testing.T) {
osCommand := newDummyOSCommand()

osCommand.Platform.os = "linux"

actual := osCommand.Quote(`hello "test"`)

expected := osCommand.Platform.escapedQuote + "hello \"test\"" + osCommand.Platform.escapedQuote

assert.EqualValues(t, expected, actual)
}

func TestOSCommandUnquote(t *testing.T) {
osCommand := newDummyOSCommand()

Expand Down
9 changes: 5 additions & 4 deletions pkg/commands/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package commands

func getPlatform() *Platform {
return &Platform{
os: "windows",
shell: "cmd",
shellArg: "/c",
escapedQuote: `\"`,
os: "windows",
shell: "cmd",
shellArg: "/c",
escapedQuote: `\"`,
fallbackEscapedQuote: "\\'",
}
}

0 comments on commit 7d86278

Please sign in to comment.