From 8d3cce4a497e781668aad0244e94caaeb97461cf Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 28 Mar 2023 18:44:52 +0200 Subject: Rename test files so that test discovery works again These files were renamed from os_windows_test.go to os_test_windows.go (etc.) in 95b2e9540a5. Since then, the tests have no longer run, since go only looks for tests in files ending with "test.go". It isn't important that the file name ends with "_windows.go", since there are already build constrains in the files themselves. --- pkg/commands/oscommands/os_default_test.go | 142 +++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 pkg/commands/oscommands/os_default_test.go (limited to 'pkg/commands/oscommands/os_default_test.go') diff --git a/pkg/commands/oscommands/os_default_test.go b/pkg/commands/oscommands/os_default_test.go new file mode 100644 index 000000000..39a1226d2 --- /dev/null +++ b/pkg/commands/oscommands/os_default_test.go @@ -0,0 +1,142 @@ +//go:build !windows +// +build !windows + +package oscommands + +import ( + "testing" + + "github.com/go-errors/errors" + "github.com/stretchr/testify/assert" +) + +func TestOSCommandRunWithOutput(t *testing.T) { + type scenario struct { + command string + test func(string, error) + } + + scenarios := []scenario{ + { + "echo -n '123'", + func(output string, err error) { + assert.NoError(t, err) + assert.EqualValues(t, "123", output) + }, + }, + { + "rmdir unexisting-folder", + func(output string, err error) { + assert.Regexp(t, "rmdir.*unexisting-folder.*", err.Error()) + }, + }, + } + + for _, s := range scenarios { + c := NewDummyOSCommand() + s.test(c.Cmd.New(s.command).RunWithOutput()) + } +} + +func TestOSCommandOpenFileDarwin(t *testing.T) { + type scenario struct { + filename string + runner *FakeCmdObjRunner + test func(error) + } + + scenarios := []scenario{ + { + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `open "test"`}, "", errors.New("error")), + test: func(err error) { + assert.Error(t, err) + }, + }, + { + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `open "test"`}, "", nil), + test: func(err error) { + assert.NoError(t, err) + }, + }, + { + 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 := NewDummyOSCommandWithRunner(s.runner) + oSCmd.Platform.OS = "darwin" + oSCmd.UserConfig.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 + runner *FakeCmdObjRunner + test func(error) + } + + scenarios := []scenario{ + { + 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) + }, + }, + { + filename: "test", + runner: NewFakeRunner(t). + ExpectArgs([]string{"bash", "-c", `xdg-open "test" > /dev/null`}, "", nil), + test: func(err error) { + assert.NoError(t, err) + }, + }, + { + 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: "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) + }, + }, + { + 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)) + } +} -- cgit v1.2.3