From 1e50764b4db4313f191acc19580fd9920f8b6cdf Mon Sep 17 00:00:00 2001 From: Ryooooooga Date: Sat, 9 Oct 2021 14:53:43 +0900 Subject: Fix tests --- pkg/commands/commits_test.go | 9 +-- pkg/commands/files_test.go | 15 ++-- pkg/commands/oscommands/os.go | 7 +- pkg/commands/oscommands/os_default_platform.go | 1 - pkg/commands/oscommands/os_test.go | 95 ++++++++++++++++++++++++-- pkg/commands/oscommands/os_windows.go | 7 +- 6 files changed, 109 insertions(+), 25 deletions(-) (limited to 'pkg/commands') diff --git a/pkg/commands/commits_test.go b/pkg/commands/commits_test.go index f979e9507..eed6f2207 100644 --- a/pkg/commands/commits_test.go +++ b/pkg/commands/commits_test.go @@ -38,6 +38,8 @@ func TestGitCommandResetToCommit(t *testing.T) { // TestGitCommandCommitStr is a function. func TestGitCommandCommitStr(t *testing.T) { + gitCmd := NewDummyGitCommand() + type scenario struct { testName string message string @@ -50,25 +52,24 @@ func TestGitCommandCommitStr(t *testing.T) { testName: "Commit", message: "test", flags: "", - expected: "git commit -m \"test\"", + expected: "git commit -m " + gitCmd.OSCommand.Quote("test"), }, { testName: "Commit with --no-verify flag", message: "test", flags: "--no-verify", - expected: "git commit --no-verify -m \"test\"", + expected: "git commit --no-verify -m " + gitCmd.OSCommand.Quote("test"), }, { testName: "Commit with multiline message", message: "line1\nline2", flags: "", - expected: "git commit -m \"line1\" -m \"line2\"", + expected: "git commit -m " + gitCmd.OSCommand.Quote("line1") + " -m " + gitCmd.OSCommand.Quote("line2"), }, } for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { - gitCmd := NewDummyGitCommand() cmdStr := gitCmd.CommitCmdStr(s.message, s.flags) assert.Equal(t, s.expected, cmdStr) }) diff --git a/pkg/commands/files_test.go b/pkg/commands/files_test.go index 19a014eaa..0d718a7bf 100644 --- a/pkg/commands/files_test.go +++ b/pkg/commands/files_test.go @@ -717,6 +717,8 @@ func TestGitCommandRemoveUntrackedFiles(t *testing.T) { // TestEditFileCmdStr is a function. func TestEditFileCmdStr(t *testing.T) { + gitCmd := NewDummyGitCommand() + type scenario struct { filename string configEditCommand string @@ -761,7 +763,7 @@ func TestEditFileCmdStr(t *testing.T) { }, func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, "nano \"test\"", cmdStr) + assert.Equal(t, "nano "+gitCmd.OSCommand.Quote("test"), cmdStr) }, }, { @@ -780,7 +782,7 @@ func TestEditFileCmdStr(t *testing.T) { }, func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, "nano \"test\"", cmdStr) + assert.Equal(t, "nano "+gitCmd.OSCommand.Quote("test"), cmdStr) }, }, { @@ -825,7 +827,7 @@ func TestEditFileCmdStr(t *testing.T) { }, func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, "emacs \"test\"", cmdStr) + assert.Equal(t, "emacs "+gitCmd.OSCommand.Quote("test"), cmdStr) }, }, { @@ -844,7 +846,7 @@ func TestEditFileCmdStr(t *testing.T) { }, func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, "vi \"test\"", cmdStr) + assert.Equal(t, "vi "+gitCmd.OSCommand.Quote("test"), cmdStr) }, }, { @@ -863,7 +865,7 @@ func TestEditFileCmdStr(t *testing.T) { }, func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, "vi \"file/with space\"", cmdStr) + assert.Equal(t, "vi "+gitCmd.OSCommand.Quote("file/with space"), cmdStr) }, }, { @@ -882,13 +884,12 @@ func TestEditFileCmdStr(t *testing.T) { }, func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, "vim +1 \"open file/at line\"", cmdStr) + assert.Equal(t, "vim +1 "+gitCmd.OSCommand.Quote("open file/at line"), cmdStr) }, }, } for _, s := range scenarios { - gitCmd := NewDummyGitCommand() gitCmd.Config.GetUserConfig().OS.EditCommand = s.configEditCommand gitCmd.Config.GetUserConfig().OS.EditCommandTemplate = s.configEditCommandTemplate gitCmd.OSCommand.Command = s.command 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", } } -- cgit v1.2.3