diff options
author | Anthony HAMON <hamon.anth@gmail.com> | 2018-09-10 22:38:08 +0200 |
---|---|---|
committer | Anthony HAMON <hamon.anth@gmail.com> | 2018-09-11 21:56:17 +0200 |
commit | ccbc5e569c27b4b7a2160f07a63b6ba0cc3ae17a (patch) | |
tree | 0cc472aa60d7dc644aef9b7440668a3b2a0fee91 /pkg | |
parent | 415aad600c9ec5db052e4e99c556a9a4f38df8cf (diff) |
commands/git : add test to Push func, refactor
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/git.go | 6 | ||||
-rw-r--r-- | pkg/commands/git_test.go | 59 |
2 files changed, 63 insertions, 2 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index edaf65ced..a770c03ee 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -304,13 +304,15 @@ func (c *GitCommand) Pull() error { return c.OSCommand.RunCommand("git pull --no-edit") } -// Push push to a branch +// Push pushes to a branch func (c *GitCommand) Push(branchName string, force bool) error { forceFlag := "" + if force { forceFlag = "--force-with-lease " } - return c.OSCommand.RunCommand("git push " + forceFlag + "-u origin " + branchName) + + return c.OSCommand.RunCommand(fmt.Sprintf("git push %s -u origin %s", forceFlag, branchName)) } // SquashPreviousTwoCommits squashes a commit down to the one below it diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index fbc953ec2..c90ce324c 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -894,6 +894,65 @@ func TestGitCommandCommit(t *testing.T) { } } +func TestGitCommandPush(t *testing.T) { + type scenario struct { + testName string + command func(string, ...string) *exec.Cmd + forcePush bool + test func(error) + } + + scenarios := []scenario{ + { + "Push with force disabled", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"push", "-u", "origin", "test"}, args) + + return exec.Command("echo") + }, + false, + func(err error) { + assert.Nil(t, err) + }, + }, + { + "Push with force enable", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"push", "--force-with-lease", "-u", "origin", "test"}, args) + + return exec.Command("echo") + }, + true, + func(err error) { + assert.Nil(t, err) + }, + }, + { + "Push with an error occurring", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"push", "-u", "origin", "test"}, args) + + return exec.Command("exit", "1") + }, + false, + func(err error) { + assert.Error(t, err) + }, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = s.command + s.test(gitCmd.Push("test", s.forcePush)) + }) + } +} + func TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) |