1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-05 15:15:49 +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.
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)
})

View File

@ -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

View File

@ -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

View File

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

View File

@ -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)
}

View File

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