1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00
This commit is contained in:
Jesse Duffield
2021-12-31 10:44:47 +11:00
parent ad9b2df104
commit 25195eacee
4 changed files with 147 additions and 108 deletions

View File

@ -24,3 +24,10 @@ func NewDummyCmdObjBuilder(runner ICmdObjRunner) *CmdObjBuilder {
}, },
} }
} }
func NewDummyOSCommandWithRunner(runner *FakeCmdObjRunner) *OSCommand {
osCommand := NewOSCommand(utils.NewDummyCommon())
osCommand.Cmd = NewDummyCmdObjBuilder(runner)
return osCommand
}

View File

@ -7,59 +7,50 @@ import (
"os/exec" "os/exec"
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/go-errors/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// TestOSCommandOpenFileDarwin is a function.
func TestOSCommandOpenFileDarwin(t *testing.T) { func TestOSCommandOpenFileDarwin(t *testing.T) {
type scenario struct { type scenario struct {
filename string filename string
command func(string, ...string) *exec.Cmd runner *FakeCmdObjRunner
test func(error) test func(error)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"test", filename: "test",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
return secureexec.Command("exit", "1") ExpectArgs([]string{"bash", "-c", `open "test"`}, "", errors.New("error")),
}, test: func(err error) {
func(err error) {
assert.Error(t, err) assert.Error(t, err)
}, },
}, },
{ {
"test", filename: "test",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "bash", name) ExpectArgs([]string{"bash", "-c", `open "test"`}, "", nil),
assert.Equal(t, []string{"-c", `open "test"`}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"filename with spaces", filename: "filename with spaces",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "bash", name) ExpectArgs([]string{"bash", "-c", `open "filename with spaces"`}, "", nil),
assert.Equal(t, []string{"-c", `open "filename with spaces"`}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
} }
for _, s := range scenarios { for _, s := range scenarios {
OSCmd := NewDummyOSCommand() oSCmd := NewDummyOSCommandWithRunner(s.runner)
OSCmd.Platform.OS = "darwin" oSCmd.Platform.OS = "darwin"
OSCmd.Command = s.command oSCmd.UserConfig.OS.OpenCommand = "open {{filename}}"
OSCmd.UserConfig.OS.OpenCommand = "open {{filename}}"
s.test(OSCmd.OpenFile(s.filename)) s.test(oSCmd.OpenFile(s.filename))
} }
} }
@ -67,72 +58,125 @@ func TestOSCommandOpenFileDarwin(t *testing.T) {
func TestOSCommandOpenFileLinux(t *testing.T) { func TestOSCommandOpenFileLinux(t *testing.T) {
type scenario struct { type scenario struct {
filename string filename string
runner *FakeCmdObjRunner
command func(string, ...string) *exec.Cmd command func(string, ...string) *exec.Cmd
test func(error) test func(error)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"test", filename: "test",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
return secureexec.Command("exit", "1") ExpectArgs([]string{"bash", "-c", `xdg-open "test" > /dev/null`}, "", errors.New("error")),
}, test: func(err error) {
func(err error) {
assert.Error(t, err) assert.Error(t, err)
}, },
}, },
{ {
"test", filename: "test",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "bash", name) ExpectArgs([]string{"bash", "-c", `xdg-open "test" > /dev/null`}, "", nil),
assert.Equal(t, []string{"-c", `xdg-open "test" > /dev/null`}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"filename with spaces", filename: "filename with spaces",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "bash", name) ExpectArgs([]string{"bash", "-c", `xdg-open "filename with spaces" > /dev/null`}, "", nil),
assert.Equal(t, []string{"-c", `xdg-open "filename with spaces" > /dev/null`}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"let's_test_with_single_quote", filename: "let's_test_with_single_quote",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "bash", name) ExpectArgs([]string{"bash", "-c", `xdg-open "let's_test_with_single_quote" > /dev/null`}, "", nil),
assert.Equal(t, []string{"-c", `xdg-open "let's_test_with_single_quote" > /dev/null`}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"$USER.txt", filename: "$USER.txt",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "bash", name) ExpectArgs([]string{"bash", "-c", `xdg-open "\$USER.txt" > /dev/null`}, "", nil),
assert.Equal(t, []string{"-c", `xdg-open "\$USER.txt" > /dev/null`}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
} }
for _, s := range scenarios { for _, s := range scenarios {
OSCmd := NewDummyOSCommand() oSCmd := NewDummyOSCommandWithRunner(s.runner)
OSCmd.Command = s.command oSCmd.Platform.OS = "linux"
OSCmd.Platform.OS = "linux" oSCmd.UserConfig.OS.OpenCommand = `xdg-open {{filename}} > /dev/null`
OSCmd.UserConfig.OS.OpenCommand = `xdg-open {{filename}} > /dev/null`
s.test(OSCmd.OpenFile(s.filename)) s.test(oSCmd.OpenFile(s.filename))
}
}
func TestOSCommandOpenFileWindows(t *testing.T) {
type scenario struct {
filename string
runner *FakeCmdObjRunner
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
filename: "test",
runner: NewFakeRunner(t).
ExpectArgs([]string{"cmd", "/c", "start", "", "test"}, "", errors.New("error")),
test: func(err error) {
assert.Error(t, err)
},
},
{
filename: "test",
runner: NewFakeRunner(t).
ExpectArgs([]string{"cmd", "/c", "start", "", "test"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
{
filename: "filename with spaces",
runner: NewFakeRunner(t).
ExpectArgs([]string{"cmd", "/c", "start", "", "filename with spaces"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
{
filename: "let's_test_with_single_quote",
runner: NewFakeRunner(t).
ExpectArgs([]string{"cmd", "/c", "start", "", "let's_test_with_single_quote"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
{
filename: "$USER.txt",
runner: NewFakeRunner(t).
ExpectArgs([]string{"cmd", "/c", "start", "", "$USER.txt"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
}
for _, s := range scenarios {
oSCmd := NewDummyOSCommandWithRunner(s.runner)
platform := &Platform{
OS: "windows",
Shell: "cmd",
ShellArg: "/c",
}
oSCmd.Platform = platform
oSCmd.Cmd.platform = platform
oSCmd.UserConfig.OS.OpenCommand = `start "" {{filename}}`
s.test(oSCmd.OpenFile(s.filename))
} }
} }

View File

@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// TestOSCommandRunWithOutput is a function.
func TestOSCommandRunWithOutput(t *testing.T) { func TestOSCommandRunWithOutput(t *testing.T) {
type scenario struct { type scenario struct {
command string command string
@ -37,7 +36,6 @@ func TestOSCommandRunWithOutput(t *testing.T) {
} }
} }
// TestOSCommandRun is a function.
func TestOSCommandRun(t *testing.T) { func TestOSCommandRun(t *testing.T) {
type scenario struct { type scenario struct {
command string command string
@ -59,7 +57,6 @@ func TestOSCommandRun(t *testing.T) {
} }
} }
// TestOSCommandQuote is a function.
func TestOSCommandQuote(t *testing.T) { func TestOSCommandQuote(t *testing.T) {
osCommand := NewDummyOSCommand() osCommand := NewDummyOSCommand()
@ -111,7 +108,6 @@ func TestOSCommandQuoteWindows(t *testing.T) {
assert.EqualValues(t, expected, actual) assert.EqualValues(t, expected, actual)
} }
// TestOSCommandFileType is a function.
func TestOSCommandFileType(t *testing.T) { func TestOSCommandFileType(t *testing.T) {
type scenario struct { type scenario struct {
path string path string

View File

@ -11,76 +11,68 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// TestOSCommandOpenFileWindows tests the OpenFile command on Linux
func TestOSCommandOpenFileWindows(t *testing.T) { func TestOSCommandOpenFileWindows(t *testing.T) {
type scenario struct { type scenario struct {
filename string filename string
runner *FakeCmdObjRunner
command func(string, ...string) *exec.Cmd command func(string, ...string) *exec.Cmd
test func(error) test func(error)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"test", filename: "test",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
return secureexec.Command("exit", "1") ExpectArgs([]string{"cmd", "/c", "start", "", "test"}, "", errors.New("error")),
}, test: func(err error) {
func(err error) {
assert.Error(t, err) assert.Error(t, err)
}, },
}, },
{ {
"test", filename: "test",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "cmd", name) ExpectArgs([]string{"cmd", "/c", "start", "", "test"}, "", nil),
assert.Equal(t, []string{"/c", "start", "", "test"}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"filename with spaces", filename: "filename with spaces",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "cmd", name) ExpectArgs([]string{"cmd", "/c", "start", "", "filename with spaces"}, "", nil),
assert.Equal(t, []string{"/c", "start", "", "filename with spaces"}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"let's_test_with_single_quote", filename: "let's_test_with_single_quote",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "cmd", name) ExpectArgs([]string{"cmd", "/c", "start", "", "let's_test_with_single_quote"}, "", nil),
assert.Equal(t, []string{"/c", "start", "", "let's_test_with_single_quote"}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"$USER.txt", filename: "$USER.txt",
func(name string, arg ...string) *exec.Cmd { runner: NewFakeRunner(t).
assert.Equal(t, "cmd", name) ExpectArgs([]string{"cmd", "/c", "start", "", "$USER.txt"}, "", nil),
assert.Equal(t, []string{"/c", "start", "", "$USER.txt"}, arg) test: func(err error) {
return secureexec.Command("echo")
},
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
} }
for _, s := range scenarios { for _, s := range scenarios {
OSCmd := NewDummyOSCommand() oSCmd := NewDummyOSCommandWithRunner(s.runner)
OSCmd.Command = s.command platform := &Platform{
OSCmd.Platform.OS = "windows" OS: "windows",
OSCmd.UserConfig.OS.OpenCommand = `start "" {{filename}}` Shell: "cmd",
ShellArg: "/c",
}
oSCmd.Platform = platform
oSCmd.Cmd.platform = platform
oSCmd.UserConfig.OS.OpenCommand = `start "" {{filename}}`
s.test(OSCmd.OpenFile(s.filename)) s.test(oSCmd.OpenFile(s.filename))
} }
} }