summaryrefslogtreecommitdiffstats
path: root/pkg/commands/oscommands
diff options
context:
space:
mode:
authorRyooooooga <eial5q265e5@gmail.com>2021-10-09 14:53:43 +0900
committerJesse Duffield <jessedduffield@gmail.com>2021-10-17 11:00:20 +1100
commit1e50764b4db4313f191acc19580fd9920f8b6cdf (patch)
treeaa20e8e31aa3dd1ef68c956ad23527b4d08e4e14 /pkg/commands/oscommands
parent9619d3447f6dba88a0ec0c621270130d0964907c (diff)
Fix tests
Diffstat (limited to 'pkg/commands/oscommands')
-rw-r--r--pkg/commands/oscommands/os.go7
-rw-r--r--pkg/commands/oscommands/os_default_platform.go1
-rw-r--r--pkg/commands/oscommands/os_test.go95
-rw-r--r--pkg/commands/oscommands/os_windows.go7
4 files changed, 96 insertions, 14 deletions
diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go
index 4ab258512..4babbe8a3 100644
--- a/pkg/commands/oscommands/os.go
+++ b/pkg/commands/oscommands/os.go
@@ -26,7 +26,6 @@ type Platform struct {
OS string
Shell string
ShellArg string
- EscapedQuote string
OpenCommand string
OpenLinkCommand string
}
@@ -334,12 +333,15 @@ func (c *OSCommand) PrepareShellSubProcess(command string) *exec.Cmd {
// Quote wraps a message in platform-specific quotation marks
func (c *OSCommand) Quote(message string) string {
+ var quote string
if c.Platform.OS == "windows" {
+ quote = `\"`
message = strings.NewReplacer(
`"`, `"'"'"`,
`\"`, `\\"`,
).Replace(message)
} else {
+ quote = `"`
message = strings.NewReplacer(
`\`, `\\`,
`"`, `\"`,
@@ -347,8 +349,7 @@ func (c *OSCommand) Quote(message string) string {
"`", "\\`",
).Replace(message)
}
- escapedQuote := c.Platform.EscapedQuote
- return escapedQuote + message + escapedQuote
+ return quote + message + quote
}
// AppendLineToFile adds a new line in file
diff --git a/pkg/commands/oscommands/os_default_platform.go b/pkg/commands/oscommands/os_default_platform.go
index 77c488a8d..f5b1f4dbc 100644
--- a/pkg/commands/oscommands/os_default_platform.go
+++ b/pkg/commands/oscommands/os_default_platform.go
@@ -12,7 +12,6 @@ func getPlatform() *Platform {
OS: runtime.GOOS,
Shell: "bash",
ShellArg: "-c",
- EscapedQuote: `"`,
OpenCommand: "open {{filename}}",
OpenLinkCommand: "open {{link}}",
}
diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go
index 3769df071..99ed2257c 100644
--- a/pkg/commands/oscommands/os_test.go
+++ b/pkg/commands/oscommands/os_test.go
@@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"os/exec"
+ "runtime"
"testing"
"github.com/jesseduffield/lazygit/pkg/secureexec"
@@ -111,8 +112,12 @@ func TestOSCommandOpenFile(t *testing.T) {
}
}
-// TestOSCommandOpenFile tests the OpenFile command on Linux
+// TestOSCommandOpenFileLinux tests the OpenFile command on Linux
func TestOSCommandOpenFileLinux(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ return
+ }
+
type scenario struct {
filename string
command func(string, ...string) *exec.Cmd
@@ -185,6 +190,84 @@ func TestOSCommandOpenFileLinux(t *testing.T) {
}
}
+// TestOSCommandOpenFileWindows tests the OpenFile command on Linux
+func TestOSCommandOpenFileWindows(t *testing.T) {
+ if runtime.GOOS != "windows" {
+ return
+ }
+
+ type scenario struct {
+ filename string
+ command func(string, ...string) *exec.Cmd
+ test func(error)
+ }
+
+ scenarios := []scenario{
+ {
+ "test",
+ func(name string, arg ...string) *exec.Cmd {
+ return secureexec.Command("exit", "1")
+ },
+ func(err error) {
+ assert.Error(t, err)
+ },
+ },
+ {
+ "test",
+ func(name string, arg ...string) *exec.Cmd {
+ assert.Equal(t, "cmd", name)
+ assert.Equal(t, []string{"/c", "start", "", "test"}, arg)
+ return secureexec.Command("echo")
+ },
+ func(err error) {
+ assert.NoError(t, err)
+ },
+ },
+ {
+ "filename with spaces",
+ func(name string, arg ...string) *exec.Cmd {
+ assert.Equal(t, "cmd", name)
+ assert.Equal(t, []string{"/c", "start", "", "filename with spaces"}, arg)
+ return secureexec.Command("echo")
+ },
+ func(err error) {
+ assert.NoError(t, err)
+ },
+ },
+ {
+ "let's_test_with_single_quote",
+ func(name string, arg ...string) *exec.Cmd {
+ assert.Equal(t, "cmd", name)
+ assert.Equal(t, []string{"/c", "start", "", "let's_test_with_single_quote"}, arg)
+ return secureexec.Command("echo")
+ },
+ func(err error) {
+ assert.NoError(t, err)
+ },
+ },
+ {
+ "$USER.txt",
+ func(name string, arg ...string) *exec.Cmd {
+ assert.Equal(t, "cmd", name)
+ assert.Equal(t, []string{"/c", "start", "", "$USER.txt"}, arg)
+ return secureexec.Command("echo")
+ },
+ func(err error) {
+ assert.NoError(t, err)
+ },
+ },
+ }
+
+ for _, s := range scenarios {
+ OSCmd := NewDummyOSCommand()
+ OSCmd.Command = s.command
+ OSCmd.Platform.OS = "windows"
+ OSCmd.Config.GetUserConfig().OS.OpenCommand = `cmd /c start "" {{filename}}`
+
+ s.test(OSCmd.OpenFile(s.filename))
+ }
+}
+
// TestOSCommandQuote is a function.
func TestOSCommandQuote(t *testing.T) {
osCommand := NewDummyOSCommand()
@@ -193,7 +276,7 @@ func TestOSCommandQuote(t *testing.T) {
actual := osCommand.Quote("hello `test`")
- expected := osCommand.Platform.EscapedQuote + "hello \\`test\\`" + osCommand.Platform.EscapedQuote
+ expected := "\"hello \\`test\\`\""
assert.EqualValues(t, expected, actual)
}
@@ -206,7 +289,7 @@ func TestOSCommandQuoteSingleQuote(t *testing.T) {
actual := osCommand.Quote("hello 'test'")
- expected := osCommand.Platform.EscapedQuote + "hello 'test'" + osCommand.Platform.EscapedQuote
+ expected := `"hello 'test'"`
assert.EqualValues(t, expected, actual)
}
@@ -219,7 +302,7 @@ func TestOSCommandQuoteDoubleQuote(t *testing.T) {
actual := osCommand.Quote(`hello "test"`)
- expected := osCommand.Platform.EscapedQuote + `hello \"test\"` + osCommand.Platform.EscapedQuote
+ expected := `"hello \"test\""`
assert.EqualValues(t, expected, actual)
}
@@ -230,9 +313,9 @@ func TestOSCommandQuoteWindows(t *testing.T) {
osCommand.Platform.OS = "windows"
- actual := osCommand.Quote(`hello "test"`)
+ actual := osCommand.Quote(`hello "test" 'test2'`)
- expected := osCommand.Platform.EscapedQuote + `hello "'"'"test"'"'"` + osCommand.Platform.EscapedQuote
+ expected := `\"hello "'"'"test"'"'" 'test2'\"`
assert.EqualValues(t, expected, actual)
}
diff --git a/pkg/commands/oscommands/os_windows.go b/pkg/commands/oscommands/os_windows.go
index 611467581..f3b3b4056 100644
--- a/pkg/commands/oscommands/os_windows.go
+++ b/pkg/commands/oscommands/os_windows.go
@@ -2,9 +2,8 @@ package oscommands
func getPlatform() *Platform {
return &Platform{
- OS: "windows",
- Shell: "cmd",
- ShellArg: "/c",
- EscapedQuote: `\"`,
+ OS: "windows",
+ Shell: "cmd",
+ ShellArg: "/c",
}
}