From ad4fd67db331327483e59c18f45ecda6cd93d2cc Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Fri, 31 Dec 2021 10:44:47 +1100 Subject: WIP --- pkg/commands/oscommands/dummies.go | 7 ++ pkg/commands/oscommands/os_default_test.go | 166 ++++++++++++++++++----------- pkg/commands/oscommands/os_test.go | 4 - pkg/commands/oscommands/os_windows_test.go | 70 ++++++------ 4 files changed, 143 insertions(+), 104 deletions(-) (limited to 'pkg') diff --git a/pkg/commands/oscommands/dummies.go b/pkg/commands/oscommands/dummies.go index ba60374f4..34d23a399 100644 --- a/pkg/commands/oscommands/dummies.go +++ b/pkg/commands/oscommands/dummies.go @@ -24,3 +24,10 @@ func NewDummyCmdObjBuilder(runner ICmdObjRunner) *CmdObjBuilder { }, } } + +func NewDummyOSCommandWithRunner(runner *FakeCmdObjRunner) *OSCommand { + osCommand := NewOSCommand(utils.NewDummyCommon()) + osCommand.Cmd = NewDummyCmdObjBuilder(runner) + + return osCommand +} diff --git a/pkg/commands/oscommands/os_default_test.go b/pkg/commands/oscommands/os_default_test.go index 60b45f0dd..425b3c2f4 100644 --- a/pkg/commands/oscommands/os_default_test.go +++ b/pkg/commands/oscommands/os_default_test.go @@ -7,59 +7,50 @@ import ( "os/exec" "testing" - "github.com/jesseduffield/lazygit/pkg/secureexec" + "github.com/go-errors/errors" "github.com/stretchr/testify/assert" ) -// TestOSCommandOpenFileDarwin is a function. func TestOSCommandOpenFileDarwin(t *testing.T) { type scenario struct { filename string - command func(string, ...string) *exec.Cmd + runner *FakeCmdObjRunner test func(error) } scenarios := []scenario{ { - "test", - func(name string, arg ...string) *exec.Cmd { - return secureexec.Command("exit", "1") - }, - func(err error) { + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `open "test"`}, "", errors.New("error")), + test: func(err error) { assert.Error(t, err) }, }, { - "test", - func(name string, arg ...string) *exec.Cmd { - assert.Equal(t, "bash", name) - assert.Equal(t, []string{"-c", `open "test"`}, arg) - return secureexec.Command("echo") - }, - func(err error) { + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `open "test"`}, "", nil), + test: func(err error) { assert.NoError(t, err) }, }, { - "filename with spaces", - func(name string, arg ...string) *exec.Cmd { - assert.Equal(t, "bash", name) - assert.Equal(t, []string{"-c", `open "filename with spaces"`}, arg) - return secureexec.Command("echo") - }, - func(err error) { + filename: "filename with spaces", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `open "filename with spaces"`}, "", nil), + test: func(err error) { assert.NoError(t, err) }, }, } for _, s := range scenarios { - OSCmd := NewDummyOSCommand() - OSCmd.Platform.OS = "darwin" - OSCmd.Command = s.command - OSCmd.UserConfig.OS.OpenCommand = "open {{filename}}" + oSCmd := NewDummyOSCommandWithRunner(s.runner) + oSCmd.Platform.OS = "darwin" + 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) { type scenario struct { filename string + runner *FakeCmdObjRunner 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) { + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `xdg-open "test" > /dev/null`}, "", errors.New("error")), + test: func(err error) { assert.Error(t, err) }, }, { - "test", - func(name string, arg ...string) *exec.Cmd { - assert.Equal(t, "bash", name) - assert.Equal(t, []string{"-c", `xdg-open "test" > /dev/null`}, arg) - return secureexec.Command("echo") + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `xdg-open "test" > /dev/null`}, "", nil), + test: func(err error) { + assert.NoError(t, err) }, - func(err error) { + }, + { + filename: "filename with spaces", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `xdg-open "filename with spaces" > /dev/null`}, "", nil), + test: func(err error) { assert.NoError(t, err) }, }, { - "filename with spaces", - func(name string, arg ...string) *exec.Cmd { - assert.Equal(t, "bash", name) - assert.Equal(t, []string{"-c", `xdg-open "filename with spaces" > /dev/null`}, arg) - return secureexec.Command("echo") + filename: "let's_test_with_single_quote", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `xdg-open "let's_test_with_single_quote" > /dev/null`}, "", nil), + test: func(err error) { + assert.NoError(t, err) }, - func(err error) { + }, + { + filename: "$USER.txt", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `xdg-open "\$USER.txt" > /dev/null`}, "", nil), + test: func(err error) { assert.NoError(t, err) }, }, + } + + for _, s := range scenarios { + oSCmd := NewDummyOSCommandWithRunner(s.runner) + oSCmd.Platform.OS = "linux" + oSCmd.UserConfig.OS.OpenCommand = `xdg-open {{filename}} > /dev/null` + + 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{ { - "let's_test_with_single_quote", - func(name string, arg ...string) *exec.Cmd { - assert.Equal(t, "bash", name) - assert.Equal(t, []string{"-c", `xdg-open "let's_test_with_single_quote" > /dev/null`}, arg) - return secureexec.Command("echo") + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"cmd", "/c", "start", "", "test"}, "", errors.New("error")), + test: func(err error) { + assert.Error(t, err) }, - func(err error) { + }, + { + 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) }, }, { - "$USER.txt", - func(name string, arg ...string) *exec.Cmd { - assert.Equal(t, "bash", name) - assert.Equal(t, []string{"-c", `xdg-open "\$USER.txt" > /dev/null`}, arg) - return secureexec.Command("echo") + 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) }, - func(err error) { + }, + { + 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 := NewDummyOSCommand() - OSCmd.Command = s.command - OSCmd.Platform.OS = "linux" - OSCmd.UserConfig.OS.OpenCommand = `xdg-open {{filename}} > /dev/null` + 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)) + s.test(oSCmd.OpenFile(s.filename)) } } diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go index 04e940f03..b5d8879b9 100644 --- a/pkg/commands/oscommands/os_test.go +++ b/pkg/commands/oscommands/os_test.go @@ -8,7 +8,6 @@ import ( "github.com/stretchr/testify/assert" ) -// TestOSCommandRunWithOutput is a function. func TestOSCommandRunWithOutput(t *testing.T) { type scenario struct { command string @@ -37,7 +36,6 @@ func TestOSCommandRunWithOutput(t *testing.T) { } } -// TestOSCommandRun is a function. func TestOSCommandRun(t *testing.T) { type scenario struct { command string @@ -59,7 +57,6 @@ func TestOSCommandRun(t *testing.T) { } } -// TestOSCommandQuote is a function. func TestOSCommandQuote(t *testing.T) { osCommand := NewDummyOSCommand() @@ -111,7 +108,6 @@ func TestOSCommandQuoteWindows(t *testing.T) { assert.EqualValues(t, expected, actual) } -// TestOSCommandFileType is a function. func TestOSCommandFileType(t *testing.T) { type scenario struct { path string diff --git a/pkg/commands/oscommands/os_windows_test.go b/pkg/commands/oscommands/os_windows_test.go index 60853b841..5f2eefa9f 100644 --- a/pkg/commands/oscommands/os_windows_test.go +++ b/pkg/commands/oscommands/os_windows_test.go @@ -11,76 +11,68 @@ import ( "github.com/stretchr/testify/assert" ) -// TestOSCommandOpenFileWindows tests the OpenFile command on Linux func TestOSCommandOpenFileWindows(t *testing.T) { type scenario struct { filename string + runner *FakeCmdObjRunner 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) { + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"cmd", "/c", "start", "", "test"}, "", errors.New("error")), + test: 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) { + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"cmd", "/c", "start", "", "test"}, "", nil), + test: 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) { + filename: "filename with spaces", + runner: NewFakeRunner(t). + ExpectArgs([]string{"cmd", "/c", "start", "", "filename with spaces"}, "", nil), + test: 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) { + 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) }, }, { - "$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) { + 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 := NewDummyOSCommand() - OSCmd.Command = s.command - OSCmd.Platform.OS = "windows" - OSCmd.UserConfig.OS.OpenCommand = `start "" {{filename}}` + 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)) + s.test(oSCmd.OpenFile(s.filename)) } } -- cgit v1.2.3