1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-13 01:30:53 +02:00

update tests

This commit is contained in:
Jesse Duffield
2018-09-01 12:13:41 +10:00
parent ae0d88f855
commit 3f14b764d5
2 changed files with 44 additions and 39 deletions

View File

@ -80,8 +80,7 @@ func (c *OSCommand) RunDirectCommand(command string) (string, error) {
c.Log.WithField("command", command).Info("RunDirectCommand") c.Log.WithField("command", command).Info("RunDirectCommand")
return sanitisedCommandOutput( return sanitisedCommandOutput(
exec. c.command(c.Platform.shell, c.Platform.shellArg, command).
Command(c.Platform.shell, c.Platform.shellArg, command).
CombinedOutput(), CombinedOutput(),
) )
} }

View File

@ -5,11 +5,28 @@ import (
"os/exec" "os/exec"
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
yaml "gopkg.in/yaml.v2"
) )
func newDummyOSCommand() *OSCommand { 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) { func TestOSCommandRunCommandWithOutput(t *testing.T) {
@ -60,39 +77,43 @@ func TestOSCommandRunCommand(t *testing.T) {
} }
func TestOSCommandGetOpenCommand(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 { type scenario struct {
command func(string, ...string) *exec.Cmd before func(*OSCommand)
test func(string, string, error) test func(string)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
func(name string, arg ...string) *exec.Cmd { func(OSCmd *OSCommand) {},
return exec.Command("exit", "1") func(command string) {
}, assert.Equal(t, command, "open {{filename}}")
func(name string, trail string, err error) {
assert.EqualError(t, err, "Unsure what command to use to open this file")
}, },
}, },
{ {
func(name string, arg ...string) *exec.Cmd { func(OSCmd *OSCommand) {
assert.Equal(t, "which", name) OSCmd.Config.GetUserConfig().Set("os.openCommand", "test {{filename}}")
assert.Len(t, arg, 1)
assert.Regexp(t, "xdg-open|cygstart|open", arg[0])
return exec.Command("echo")
}, },
func(name string, trail string, err error) { func(command string) {
assert.NoError(t, err) assert.Equal(t, command, "test {{filename}}")
assert.Regexp(t, "xdg-open|cygstart|open", name) },
assert.Regexp(t, " \\&\\>/dev/null \\&|", trail) },
{
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 { for _, s := range scenarios {
OSCmd := newDummyOSCommand() OSCmd := newDummyOSCommand()
OSCmd.command = s.command s.before(OSCmd)
s.test(OSCmd.getOpenCommand()) s.test(OSCmd.getOpenCommand())
} }
} }
@ -111,29 +132,14 @@ func TestOSCommandOpenFile(t *testing.T) {
return exec.Command("exit", "1") return exec.Command("exit", "1")
}, },
func(err error) { func(err error) {
assert.EqualError(t, err, "Unsure what command to use to open this file") assert.Error(t, err)
}, },
}, },
{ {
"test", "test",
func(name string, arg ...string) *exec.Cmd { func(name string, arg ...string) *exec.Cmd {
if name == "which" { assert.Equal(t, name, "bash")
return exec.Command("echo") assert.Equal(t, arg, []string{"-c", "open \"test\""})
}
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")
}
return exec.Command("echo") return exec.Command("echo")
}, },
func(err error) { func(err error) {