From b641d6bd965fb24b18ebf8475aa4f72c39988dd7 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Sun, 16 Sep 2018 22:08:23 +0200 Subject: [PATCH] commands/git : add test to Checkout, refactor --- pkg/commands/git.go | 2 +- pkg/commands/git_test.go | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 7143309e4..402bee7ac 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -428,7 +428,7 @@ func (c *GitCommand) Checkout(branch string, force bool) error { if force { forceArg = "--force " } - return c.OSCommand.RunCommand("git checkout " + forceArg + branch) + return c.OSCommand.RunCommand(fmt.Sprintf("git checkout %s %s", forceArg, branch)) } // AddPatch prepares a subprocess for adding a patch by patch diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 0ee0749a7..87b0e4cee 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1421,6 +1421,52 @@ func TestGitCommandRemoveFile(t *testing.T) { } } +func TestGitCommandCheckout(t *testing.T) { + type scenario struct { + testName string + command func(string, ...string) *exec.Cmd + test func(error) + force bool + } + + scenarios := []scenario{ + { + "Checkout", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"checkout", "test"}, args) + + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + false, + }, + { + "Checkout forced", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"checkout", "--force", "test"}, args) + + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + true, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = s.command + s.test(gitCmd.Checkout("test", s.force)) + }) + } +} + func TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))