mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-11-25 22:32:13 +02:00
Merge pull request #274 from sascha-andres/master
fix: escape quote character on Linux
This commit is contained in:
@@ -22,6 +22,7 @@ type Platform struct {
|
|||||||
shellArg string
|
shellArg string
|
||||||
escapedQuote string
|
escapedQuote string
|
||||||
openCommand string
|
openCommand string
|
||||||
|
fallbackEscapedQuote string
|
||||||
}
|
}
|
||||||
|
|
||||||
// OSCommand holds all the os commands
|
// OSCommand holds all the os commands
|
||||||
@@ -140,7 +141,11 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *ex
|
|||||||
// Quote wraps a message in platform-specific quotation marks
|
// Quote wraps a message in platform-specific quotation marks
|
||||||
func (c *OSCommand) Quote(message string) string {
|
func (c *OSCommand) Quote(message string) string {
|
||||||
message = strings.Replace(message, "`", "\\`", -1)
|
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
|
// Unquote removes wrapping quotations marks if they are present
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ func getPlatform() *Platform {
|
|||||||
os: runtime.GOOS,
|
os: runtime.GOOS,
|
||||||
shell: "bash",
|
shell: "bash",
|
||||||
shellArg: "-c",
|
shellArg: "-c",
|
||||||
escapedQuote: "\"",
|
escapedQuote: "'",
|
||||||
openCommand: "open {{filename}}",
|
openCommand: "open {{filename}}",
|
||||||
|
fallbackEscapedQuote: "\"",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -265,6 +265,32 @@ func TestOSCommandQuote(t *testing.T) {
|
|||||||
assert.EqualValues(t, expected, actual)
|
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) {
|
func TestOSCommandUnquote(t *testing.T) {
|
||||||
osCommand := newDummyOSCommand()
|
osCommand := newDummyOSCommand()
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,6 @@ func getPlatform() *Platform {
|
|||||||
shell: "cmd",
|
shell: "cmd",
|
||||||
shellArg: "/c",
|
shellArg: "/c",
|
||||||
escapedQuote: `\"`,
|
escapedQuote: `\"`,
|
||||||
|
fallbackEscapedQuote: "\\'",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user