diff --git a/pkg/commands/os.go b/pkg/commands/os.go index fd27d39da..c3574f98e 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -80,8 +80,7 @@ func (c *OSCommand) RunDirectCommand(command string) (string, error) { c.Log.WithField("command", command).Info("RunDirectCommand") return sanitisedCommandOutput( - exec. - Command(c.Platform.shell, c.Platform.shellArg, command). + c.command(c.Platform.shell, c.Platform.shellArg, command). CombinedOutput(), ) } diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go index a7ccef560..21d72dc67 100644 --- a/pkg/commands/os_test.go +++ b/pkg/commands/os_test.go @@ -5,11 +5,28 @@ import ( "os/exec" "testing" + "github.com/jesseduffield/lazygit/pkg/config" + "github.com/spf13/viper" "github.com/stretchr/testify/assert" + yaml "gopkg.in/yaml.v2" ) func newDummyOSCommand() *OSCommand { - return NewOSCommand(newDummyLog()) + return NewOSCommand(newDummyLog(), newDummyAppConfig()) +} + +func newDummyAppConfig() *config.AppConfig { + appConfig := &config.AppConfig{ + Name: "lazygit", + Version: "unversioned", + Commit: "", + BuildDate: "", + Debug: false, + BuildSource: "", + UserConfig: viper.New(), + } + _ = yaml.Unmarshal([]byte{}, appConfig.AppState) + return appConfig } func TestOSCommandRunCommandWithOutput(t *testing.T) { @@ -60,39 +77,43 @@ func TestOSCommandRunCommand(t *testing.T) { } func TestOSCommandGetOpenCommand(t *testing.T) { + // two scenarios to test. One with a config set, the other with the platform default + type scenario struct { - command func(string, ...string) *exec.Cmd - test func(string, string, error) + before func(*OSCommand) + test func(string) } scenarios := []scenario{ { - func(name string, arg ...string) *exec.Cmd { - return exec.Command("exit", "1") - }, - func(name string, trail string, err error) { - assert.EqualError(t, err, "Unsure what command to use to open this file") + func(OSCmd *OSCommand) {}, + func(command string) { + assert.Equal(t, command, "open {{filename}}") }, }, { - func(name string, arg ...string) *exec.Cmd { - assert.Equal(t, "which", name) - assert.Len(t, arg, 1) - assert.Regexp(t, "xdg-open|cygstart|open", arg[0]) - return exec.Command("echo") + func(OSCmd *OSCommand) { + OSCmd.Config.GetUserConfig().Set("os.openCommand", "test {{filename}}") }, - func(name string, trail string, err error) { - assert.NoError(t, err) - assert.Regexp(t, "xdg-open|cygstart|open", name) - assert.Regexp(t, " \\&\\>/dev/null \\&|", trail) + func(command string) { + assert.Equal(t, command, "test {{filename}}") + }, + }, + { + func(OSCmd *OSCommand) { + OSCmd.Platform = &Platform{ + openCommand: "platform specific open {{filename}}", + } + }, + func(command string) { + assert.Equal(t, command, "platform specific open {{filename}}") }, }, } for _, s := range scenarios { OSCmd := newDummyOSCommand() - OSCmd.command = s.command - + s.before(OSCmd) s.test(OSCmd.getOpenCommand()) } } @@ -111,29 +132,14 @@ func TestOSCommandOpenFile(t *testing.T) { return exec.Command("exit", "1") }, func(err error) { - assert.EqualError(t, err, "Unsure what command to use to open this file") + assert.Error(t, err) }, }, { "test", func(name string, arg ...string) *exec.Cmd { - if name == "which" { - return exec.Command("echo") - } - - switch len(arg) { - case 1: - assert.Regexp(t, "open|cygstart", name) - assert.EqualValues(t, "test", arg[0]) - case 3: - assert.Equal(t, "xdg-open", name) - assert.EqualValues(t, "test", arg[0]) - assert.Regexp(t, " \\&\\>/dev/null \\&|", arg[1]) - assert.EqualValues(t, "&", arg[2]) - default: - assert.Fail(t, "Unexisting command given") - } - + assert.Equal(t, name, "bash") + assert.Equal(t, arg, []string{"-c", "open \"test\""}) return exec.Command("echo") }, func(err error) {