diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-03 15:15:26 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-04 09:07:15 +1100 |
commit | 95b2e9540a55973a6ab32dd4522648634ab15b0a (patch) | |
tree | 060e999ab894da8ecc6ead32cf0ccee64b05fd21 /pkg/commands/oscommands | |
parent | 3911575041ab67731a59dd597332bfc4543a9569 (diff) |
update tests
Diffstat (limited to 'pkg/commands/oscommands')
-rw-r--r-- | pkg/commands/oscommands/cmd_obj_builder.go | 19 | ||||
-rw-r--r-- | pkg/commands/oscommands/dummies.go | 22 | ||||
-rw-r--r-- | pkg/commands/oscommands/fake_cmd_obj_runner.go | 19 | ||||
-rw-r--r-- | pkg/commands/oscommands/os.go | 6 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_default_platform.go | 2 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_test_default.go (renamed from pkg/commands/oscommands/os_default_test.go) | 65 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_test_windows.go (renamed from pkg/commands/oscommands/os_windows_test.go) | 6 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_windows.go | 2 |
8 files changed, 54 insertions, 87 deletions
diff --git a/pkg/commands/oscommands/cmd_obj_builder.go b/pkg/commands/oscommands/cmd_obj_builder.go index 85e28f566..633cee322 100644 --- a/pkg/commands/oscommands/cmd_obj_builder.go +++ b/pkg/commands/oscommands/cmd_obj_builder.go @@ -1,6 +1,7 @@ package oscommands import ( + "fmt" "os" "strings" @@ -54,7 +55,23 @@ func (self *CmdObjBuilder) NewFromArgs(args []string) ICmdObj { } func (self *CmdObjBuilder) NewShell(commandStr string) ICmdObj { - return self.NewFromArgs([]string{self.platform.Shell, self.platform.ShellArg, commandStr}) + quotedCommand := "" + // Windows does not seem to like quotes around the command + if self.platform.OS == "windows" { + quotedCommand = strings.NewReplacer( + "^", "^^", + "&", "^&", + "|", "^|", + "<", "^<", + ">", "^>", + "%", "^%", + ).Replace(commandStr) + } else { + quotedCommand = self.Quote(commandStr) + } + + shellCommand := fmt.Sprintf("%s %s %s", self.platform.Shell, self.platform.ShellArg, quotedCommand) + return self.New(shellCommand) } func (self *CmdObjBuilder) CloneWithNewRunner(decorate func(ICmdObjRunner) ICmdObjRunner) *CmdObjBuilder { diff --git a/pkg/commands/oscommands/dummies.go b/pkg/commands/oscommands/dummies.go index d77993e62..f44cc9b72 100644 --- a/pkg/commands/oscommands/dummies.go +++ b/pkg/commands/oscommands/dummies.go @@ -6,25 +6,29 @@ import ( // NewDummyOSCommand creates a new dummy OSCommand for testing func NewDummyOSCommand() *OSCommand { - return NewOSCommand(utils.NewDummyCommon()) + osCmd := NewOSCommand(utils.NewDummyCommon(), dummyPlatform) + + return osCmd } func NewDummyCmdObjBuilder(runner ICmdObjRunner) *CmdObjBuilder { return &CmdObjBuilder{ runner: runner, logCmdObj: func(ICmdObj) {}, - platform: &Platform{ - OS: "darwin", - Shell: "bash", - ShellArg: "-c", - OpenCommand: "open {{filename}}", - OpenLinkCommand: "open {{link}}", - }, + platform: dummyPlatform, } } +var dummyPlatform = &Platform{ + OS: "darwin", + Shell: "bash", + ShellArg: "-c", + OpenCommand: "open {{filename}}", + OpenLinkCommand: "open {{link}}", +} + func NewDummyOSCommandWithRunner(runner *FakeCmdObjRunner) *OSCommand { - osCommand := NewOSCommand(utils.NewDummyCommon()) + osCommand := NewOSCommand(utils.NewDummyCommon(), dummyPlatform) osCommand.Cmd = NewDummyCmdObjBuilder(runner) return osCommand diff --git a/pkg/commands/oscommands/fake_cmd_obj_runner.go b/pkg/commands/oscommands/fake_cmd_obj_runner.go index 9a8a1e798..b542bfee3 100644 --- a/pkg/commands/oscommands/fake_cmd_obj_runner.go +++ b/pkg/commands/oscommands/fake_cmd_obj_runner.go @@ -3,6 +3,7 @@ package oscommands import ( "bufio" "fmt" + "regexp" "strings" "testing" @@ -93,10 +94,24 @@ func (self *FakeCmdObjRunner) ExpectArgs(expectedArgs []string, output string, e return self } +func (self *FakeCmdObjRunner) ExpectGitArgs(expectedArgs []string, output string, err error) *FakeCmdObjRunner { + self.ExpectFunc(func(cmdObj ICmdObj) (string, error) { + // first arg is 'git' on unix and something like '"C:\\Program Files\\Git\\mingw64\\bin\\git.exe" on windows so we'll just ensure it ends in either 'git' or 'git.exe' + re := regexp.MustCompile(`git(\.exe)?$`) + args := cmdObj.GetCmd().Args + if !re.MatchString(args[0]) { + self.t.Errorf("expected first arg to end in .git or .git.exe but was %s", args[0]) + } + assert.EqualValues(self.t, expectedArgs, args[1:], fmt.Sprintf("command %d did not match expectation", self.expectedCmdIndex+1)) + + return output, err + }) + + return self +} + func (self *FakeCmdObjRunner) CheckForMissingCalls() { if self.expectedCmdIndex < len(self.expectedCmds) { self.t.Errorf("expected command %d to be called, but was not", self.expectedCmdIndex+1) } - - return } diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 5a7c5e0bb..c5c6e3649 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -73,9 +73,7 @@ func NewCmdLogEntry(cmdStr string, span string, commandLine bool) CmdLogEntry { } // NewOSCommand os command runner -func NewOSCommand(common *common.Common) *OSCommand { - platform := getPlatform() - +func NewOSCommand(common *common.Common, platform *Platform) *OSCommand { c := &OSCommand{ Common: common, Platform: platform, @@ -138,7 +136,6 @@ func FileType(path string) string { return "file" } -// OpenFile opens a file with the given func (c *OSCommand) OpenFile(filename string) error { commandTemplate := c.UserConfig.OS.OpenCommand templateValues := map[string]string{ @@ -148,7 +145,6 @@ func (c *OSCommand) OpenFile(filename string) error { return c.Cmd.NewShell(command).Run() } -// OpenLink opens a file with the given func (c *OSCommand) OpenLink(link string) error { c.LogCommand(fmt.Sprintf("Opening link '%s'", link), false) commandTemplate := c.UserConfig.OS.OpenLinkCommand diff --git a/pkg/commands/oscommands/os_default_platform.go b/pkg/commands/oscommands/os_default_platform.go index f5b1f4dbc..fd4967d95 100644 --- a/pkg/commands/oscommands/os_default_platform.go +++ b/pkg/commands/oscommands/os_default_platform.go @@ -7,7 +7,7 @@ import ( "runtime" ) -func getPlatform() *Platform { +func GetPlatform() *Platform { return &Platform{ OS: runtime.GOOS, Shell: "bash", diff --git a/pkg/commands/oscommands/os_default_test.go b/pkg/commands/oscommands/os_test_default.go index e53514ddd..f4c1221ed 100644 --- a/pkg/commands/oscommands/os_default_test.go +++ b/pkg/commands/oscommands/os_test_default.go @@ -112,68 +112,3 @@ func TestOSCommandOpenFileLinux(t *testing.T) { s.test(oSCmd.OpenFile(s.filename)) } } - -func TestOSCommandOpenFileWindows(t *testing.T) { - type scenario struct { - filename string - runner *FakeCmdObjRunner - 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)) - } -} diff --git a/pkg/commands/oscommands/os_windows_test.go b/pkg/commands/oscommands/os_test_windows.go index 5f2eefa9f..cf9c1e68a 100644 --- a/pkg/commands/oscommands/os_windows_test.go +++ b/pkg/commands/oscommands/os_test_windows.go @@ -4,18 +4,18 @@ package oscommands import ( - "os/exec" "testing" - "github.com/jesseduffield/lazygit/pkg/secureexec" + "github.com/go-errors/errors" "github.com/stretchr/testify/assert" ) +// handling this in a separate file because str.ToArgv has different behaviour if we're on windows + func TestOSCommandOpenFileWindows(t *testing.T) { type scenario struct { filename string runner *FakeCmdObjRunner - command func(string, ...string) *exec.Cmd test func(error) } diff --git a/pkg/commands/oscommands/os_windows.go b/pkg/commands/oscommands/os_windows.go index f3b3b4056..783f50518 100644 --- a/pkg/commands/oscommands/os_windows.go +++ b/pkg/commands/oscommands/os_windows.go @@ -1,6 +1,6 @@ package oscommands -func getPlatform() *Platform { +func GetPlatform() *Platform { return &Platform{ OS: "windows", Shell: "cmd", |