diff options
author | Ryooooooga <eial5q265e5@gmail.com> | 2021-10-16 21:38:26 +0900 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-10-17 11:00:20 +1100 |
commit | 7564e506b525e0831a1fb88dfc70e9d27564d8a1 (patch) | |
tree | 1739d081d43cda4adb3db13fefb536b76b31d966 /pkg/commands/oscommands | |
parent | 1e50764b4db4313f191acc19580fd9920f8b6cdf (diff) |
Enable/disable os specific tests at compile time
Diffstat (limited to 'pkg/commands/oscommands')
-rw-r--r-- | pkg/commands/oscommands/os_default_test.go | 138 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_test.go | 211 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_windows_test.go | 86 |
3 files changed, 224 insertions, 211 deletions
diff --git a/pkg/commands/oscommands/os_default_test.go b/pkg/commands/oscommands/os_default_test.go new file mode 100644 index 000000000..5e15b1498 --- /dev/null +++ b/pkg/commands/oscommands/os_default_test.go @@ -0,0 +1,138 @@ +//go:build !windows +// +build !windows + +package oscommands + +import ( + "os/exec" + "testing" + + "github.com/jesseduffield/lazygit/pkg/secureexec" + "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 + 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, "bash", name) + assert.Equal(t, []string{"-c", `open "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, "bash", name) + assert.Equal(t, []string{"-c", `open "filename with spaces"`}, arg) + return secureexec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + }, + } + + for _, s := range scenarios { + OSCmd := NewDummyOSCommand() + OSCmd.Platform.OS = "darwin" + OSCmd.Command = s.command + OSCmd.Config.GetUserConfig().OS.OpenCommand = "open {{filename}}" + + s.test(OSCmd.OpenFile(s.filename)) + } +} + +// TestOSCommandOpenFileLinux tests the OpenFile command on Linux +func TestOSCommandOpenFileLinux(t *testing.T) { + 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, "bash", name) + assert.Equal(t, []string{"-c", `xdg-open "test" > /dev/null`}, 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, "bash", name) + assert.Equal(t, []string{"-c", `xdg-open "filename with spaces" > /dev/null`}, 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, "bash", name) + assert.Equal(t, []string{"-c", `xdg-open "let's_test_with_single_quote" > /dev/null`}, arg) + return secureexec.Command("echo") + }, + 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") + }, + func(err error) { + assert.NoError(t, err) + }, + }, + } + + for _, s := range scenarios { + OSCmd := NewDummyOSCommand() + OSCmd.Command = s.command + OSCmd.Platform.OS = "linux" + OSCmd.Config.GetUserConfig().OS.OpenCommand = `xdg-open {{filename}} > /dev/null` + + s.test(OSCmd.OpenFile(s.filename)) + } +} diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go index 99ed2257c..4dcca3790 100644 --- a/pkg/commands/oscommands/os_test.go +++ b/pkg/commands/oscommands/os_test.go @@ -3,11 +3,8 @@ package oscommands import ( "io/ioutil" "os" - "os/exec" - "runtime" "testing" - "github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/stretchr/testify/assert" ) @@ -60,214 +57,6 @@ func TestOSCommandRunCommand(t *testing.T) { } } -// TestOSCommandOpenFile is a function. -func TestOSCommandOpenFile(t *testing.T) { - 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, "bash", name) - assert.Equal(t, []string{"-c", `open "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, "bash", name) - assert.Equal(t, []string{"-c", `open "filename with spaces"`}, arg) - return secureexec.Command("echo") - }, - func(err error) { - assert.NoError(t, err) - }, - }, - } - - for _, s := range scenarios { - OSCmd := NewDummyOSCommand() - OSCmd.Platform.OS = "darwin" - OSCmd.Command = s.command - OSCmd.Config.GetUserConfig().OS.OpenCommand = "open {{filename}}" - - s.test(OSCmd.OpenFile(s.filename)) - } -} - -// 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 - 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, "bash", name) - assert.Equal(t, []string{"-c", "xdg-open \"test\" > /dev/null"}, 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, "bash", name) - assert.Equal(t, []string{"-c", "xdg-open \"filename with spaces\" > /dev/null"}, 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, "bash", name) - assert.Equal(t, []string{"-c", "xdg-open \"let's_test_with_single_quote\" > /dev/null"}, arg) - return secureexec.Command("echo") - }, - 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") - }, - func(err error) { - assert.NoError(t, err) - }, - }, - } - - for _, s := range scenarios { - OSCmd := NewDummyOSCommand() - OSCmd.Command = s.command - OSCmd.Platform.OS = "linux" - OSCmd.Config.GetUserConfig().OS.OpenCommand = `xdg-open {{filename}} > /dev/null` - - s.test(OSCmd.OpenFile(s.filename)) - } -} - -// 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() diff --git a/pkg/commands/oscommands/os_windows_test.go b/pkg/commands/oscommands/os_windows_test.go new file mode 100644 index 000000000..720f2705a --- /dev/null +++ b/pkg/commands/oscommands/os_windows_test.go @@ -0,0 +1,86 @@ +//go:build windows +// +build windows + +package oscommands + +import ( + "os/exec" + "testing" + + "github.com/jesseduffield/lazygit/pkg/secureexec" + "github.com/stretchr/testify/assert" +) + +// TestOSCommandOpenFileWindows tests the OpenFile command on Linux +func TestOSCommandOpenFileWindows(t *testing.T) { + 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 = `start "" {{filename}}` + + s.test(OSCmd.OpenFile(s.filename)) + } +} |