1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-21 22:43:27 +02:00

Fix tests

This commit is contained in:
Ryooooooga 2021-10-09 14:53:43 +09:00 committed by Jesse Duffield
parent 9619d3447f
commit 1e50764b4d
6 changed files with 109 additions and 25 deletions

View File

@ -38,6 +38,8 @@ func TestGitCommandResetToCommit(t *testing.T) {
// TestGitCommandCommitStr is a function. // TestGitCommandCommitStr is a function.
func TestGitCommandCommitStr(t *testing.T) { func TestGitCommandCommitStr(t *testing.T) {
gitCmd := NewDummyGitCommand()
type scenario struct { type scenario struct {
testName string testName string
message string message string
@ -50,25 +52,24 @@ func TestGitCommandCommitStr(t *testing.T) {
testName: "Commit", testName: "Commit",
message: "test", message: "test",
flags: "", flags: "",
expected: "git commit -m \"test\"", expected: "git commit -m " + gitCmd.OSCommand.Quote("test"),
}, },
{ {
testName: "Commit with --no-verify flag", testName: "Commit with --no-verify flag",
message: "test", message: "test",
flags: "--no-verify", 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", testName: "Commit with multiline message",
message: "line1\nline2", message: "line1\nline2",
flags: "", 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 { for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
cmdStr := gitCmd.CommitCmdStr(s.message, s.flags) cmdStr := gitCmd.CommitCmdStr(s.message, s.flags)
assert.Equal(t, s.expected, cmdStr) assert.Equal(t, s.expected, cmdStr)
}) })

View File

@ -717,6 +717,8 @@ func TestGitCommandRemoveUntrackedFiles(t *testing.T) {
// TestEditFileCmdStr is a function. // TestEditFileCmdStr is a function.
func TestEditFileCmdStr(t *testing.T) { func TestEditFileCmdStr(t *testing.T) {
gitCmd := NewDummyGitCommand()
type scenario struct { type scenario struct {
filename string filename string
configEditCommand string configEditCommand string
@ -761,7 +763,7 @@ func TestEditFileCmdStr(t *testing.T) {
}, },
func(cmdStr string, err error) { func(cmdStr string, err error) {
assert.NoError(t, err) 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) { func(cmdStr string, err error) {
assert.NoError(t, err) 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) { func(cmdStr string, err error) {
assert.NoError(t, err) 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) { func(cmdStr string, err error) {
assert.NoError(t, err) 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) { func(cmdStr string, err error) {
assert.NoError(t, err) 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) { func(cmdStr string, err error) {
assert.NoError(t, err) 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 { for _, s := range scenarios {
gitCmd := NewDummyGitCommand()
gitCmd.Config.GetUserConfig().OS.EditCommand = s.configEditCommand gitCmd.Config.GetUserConfig().OS.EditCommand = s.configEditCommand
gitCmd.Config.GetUserConfig().OS.EditCommandTemplate = s.configEditCommandTemplate gitCmd.Config.GetUserConfig().OS.EditCommandTemplate = s.configEditCommandTemplate
gitCmd.OSCommand.Command = s.command gitCmd.OSCommand.Command = s.command

View File

@ -26,7 +26,6 @@ type Platform struct {
OS string OS string
Shell string Shell string
ShellArg string ShellArg string
EscapedQuote string
OpenCommand string OpenCommand string
OpenLinkCommand string OpenLinkCommand string
} }
@ -334,12 +333,15 @@ func (c *OSCommand) PrepareShellSubProcess(command string) *exec.Cmd {
// 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 {
var quote string
if c.Platform.OS == "windows" { if c.Platform.OS == "windows" {
quote = `\"`
message = strings.NewReplacer( message = strings.NewReplacer(
`"`, `"'"'"`, `"`, `"'"'"`,
`\"`, `\\"`, `\"`, `\\"`,
).Replace(message) ).Replace(message)
} else { } else {
quote = `"`
message = strings.NewReplacer( message = strings.NewReplacer(
`\`, `\\`, `\`, `\\`,
`"`, `\"`, `"`, `\"`,
@ -347,8 +349,7 @@ func (c *OSCommand) Quote(message string) string {
"`", "\\`", "`", "\\`",
).Replace(message) ).Replace(message)
} }
escapedQuote := c.Platform.EscapedQuote return quote + message + quote
return escapedQuote + message + escapedQuote
} }
// AppendLineToFile adds a new line in file // AppendLineToFile adds a new line in file

View File

@ -12,7 +12,6 @@ func getPlatform() *Platform {
OS: runtime.GOOS, OS: runtime.GOOS,
Shell: "bash", Shell: "bash",
ShellArg: "-c", ShellArg: "-c",
EscapedQuote: `"`,
OpenCommand: "open {{filename}}", OpenCommand: "open {{filename}}",
OpenLinkCommand: "open {{link}}", OpenLinkCommand: "open {{link}}",
} }

View File

@ -4,6 +4,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"runtime"
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/secureexec" "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) { func TestOSCommandOpenFileLinux(t *testing.T) {
if runtime.GOOS == "windows" {
return
}
type scenario struct { type scenario struct {
filename string filename string
command func(string, ...string) *exec.Cmd 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. // TestOSCommandQuote is a function.
func TestOSCommandQuote(t *testing.T) { func TestOSCommandQuote(t *testing.T) {
osCommand := NewDummyOSCommand() osCommand := NewDummyOSCommand()
@ -193,7 +276,7 @@ func TestOSCommandQuote(t *testing.T) {
actual := osCommand.Quote("hello `test`") actual := osCommand.Quote("hello `test`")
expected := osCommand.Platform.EscapedQuote + "hello \\`test\\`" + osCommand.Platform.EscapedQuote expected := "\"hello \\`test\\`\""
assert.EqualValues(t, expected, actual) assert.EqualValues(t, expected, actual)
} }
@ -206,7 +289,7 @@ func TestOSCommandQuoteSingleQuote(t *testing.T) {
actual := osCommand.Quote("hello 'test'") actual := osCommand.Quote("hello 'test'")
expected := osCommand.Platform.EscapedQuote + "hello 'test'" + osCommand.Platform.EscapedQuote expected := `"hello 'test'"`
assert.EqualValues(t, expected, actual) assert.EqualValues(t, expected, actual)
} }
@ -219,7 +302,7 @@ func TestOSCommandQuoteDoubleQuote(t *testing.T) {
actual := osCommand.Quote(`hello "test"`) actual := osCommand.Quote(`hello "test"`)
expected := osCommand.Platform.EscapedQuote + `hello \"test\"` + osCommand.Platform.EscapedQuote expected := `"hello \"test\""`
assert.EqualValues(t, expected, actual) assert.EqualValues(t, expected, actual)
} }
@ -230,9 +313,9 @@ func TestOSCommandQuoteWindows(t *testing.T) {
osCommand.Platform.OS = "windows" 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) assert.EqualValues(t, expected, actual)
} }

View File

@ -5,6 +5,5 @@ func getPlatform() *Platform {
OS: "windows", OS: "windows",
Shell: "cmd", Shell: "cmd",
ShellArg: "/c", ShellArg: "/c",
EscapedQuote: `\"`,
} }
} }