diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-03-02 13:08:09 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-03-02 13:39:09 +1100 |
commit | 4de31da4bee9c622d4c6b7152e4d918b79cb4a94 (patch) | |
tree | bd64b31772230132dcbeac5be7787c612ee0e53c /pkg/test | |
parent | 23c51ba70841ef779e7fcef9fd2a5c717835e168 (diff) |
fix up tests
This fixes up some git and oscommand tests, and pulls some tests into commit_list_builder_test.go
I've also made the NewDummyBlah functions public so that I didn't need to duplicate them across packages
I've also given OSCommand a SetCommand() method for setting the command on the struct
I've also created a file utils.go in the test package for creating convient 'CommandSwapper's, which
basically enable you to assert a sequence of commands on the command line, and swap each one out for
a different one to actually be executed
Diffstat (limited to 'pkg/test')
-rw-r--r-- | pkg/test/utils.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/pkg/test/utils.go b/pkg/test/utils.go new file mode 100644 index 000000000..5603fd0ae --- /dev/null +++ b/pkg/test/utils.go @@ -0,0 +1,45 @@ +package test + +import ( + "fmt" + "os/exec" + "strings" + "testing" + + "github.com/mgutz/str" + "github.com/stretchr/testify/assert" +) + +// CommandSwapper takes a command, verifies that it is what it's expected to be +// and then returns a replacement command that will actually be called by the os +type CommandSwapper struct { + Expect string + Replace string +} + +// SwapCommand verifies the command is what we expected, and swaps it out for a different command +func (i *CommandSwapper) SwapCommand(t *testing.T, cmd string, args []string) *exec.Cmd { + splitCmd := str.ToArgv(i.Expect) + assert.EqualValues(t, splitCmd[0], cmd, fmt.Sprintf("received command: %s %s", cmd, strings.Join(args, " "))) + if len(splitCmd) > 1 { + assert.EqualValues(t, splitCmd[1:], args, fmt.Sprintf("received command: %s %s", cmd, strings.Join(args, " "))) + } + + splitCmd = str.ToArgv(i.Replace) + return exec.Command(splitCmd[0], splitCmd[1:]...) +} + +// CreateMockCommand creates a command function that will verify its receiving the right sequence of commands from lazygit +func CreateMockCommand(t *testing.T, swappers []*CommandSwapper) func(cmd string, args ...string) *exec.Cmd { + commandIndex := 0 + + return func(cmd string, args ...string) *exec.Cmd { + var command *exec.Cmd + if commandIndex > len(swappers)-1 { + assert.Fail(t, fmt.Sprintf("too many commands run. This command was (%s %s)", cmd, strings.Join(args, " "))) + } + command = swappers[commandIndex].SwapCommand(t, cmd, args) + commandIndex++ + return command + } +} |