From 65a24d70c332b0ff8f7e10999ab73260a3336fe7 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Wed, 12 Sep 2018 20:43:03 +0200 Subject: commands/git : add tests on SquashPreviousTwoCommits --- pkg/commands/git.go | 5 ++-- pkg/commands/git_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) (limited to 'pkg/commands') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 5744fa6aa..c200a688b 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -327,12 +327,11 @@ func (c *GitCommand) Push(branchName string, force bool) error { // retaining the message of the higher commit func (c *GitCommand) SquashPreviousTwoCommits(message string) error { // TODO: test this - err := c.OSCommand.RunCommand("git reset --soft HEAD^") - if err != nil { + if err := c.OSCommand.RunCommand("git reset --soft HEAD^"); err != nil { return err } // TODO: if password is required, we need to return a subprocess - return c.OSCommand.RunCommand("git commit --amend -m " + c.OSCommand.Quote(message)) + return c.OSCommand.RunCommand(fmt.Sprintf("git commit --amend -m %s", c.OSCommand.Quote(message))) } // SquashFixupCommit squashes a 'FIXUP' commit into the commit beneath it, diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index bda3ea225..0bcbdb7dd 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -953,6 +953,69 @@ func TestGitCommandPush(t *testing.T) { } } +func TestGitCommandSquashPreviousTwoCommits(t *testing.T) { + type scenario struct { + testName string + command func(string, ...string) *exec.Cmd + test func(error) + } + + scenarios := []scenario{ + { + "Git reset triggers an error", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"reset", "--soft", "HEAD^"}, args) + + return exec.Command("exit", "1") + }, + func(err error) { + assert.NotNil(t, err) + }, + }, + { + "Git commit triggers an error", + func(cmd string, args ...string) *exec.Cmd { + if len(args) > 0 && args[0] == "reset" { + return exec.Command("echo") + } + + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"commit", "--amend", "-m", "test"}, args) + + return exec.Command("exit", "1") + }, + func(err error) { + assert.NotNil(t, err) + }, + }, + { + "Stash succeeded", + func(cmd string, args ...string) *exec.Cmd { + if len(args) > 0 && args[0] == "reset" { + return exec.Command("echo") + } + + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"commit", "--amend", "-m", "test"}, args) + + return exec.Command("echo") + }, + func(err error) { + assert.Nil(t, err) + }, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = s.command + s.test(gitCmd.SquashPreviousTwoCommits("test")) + }) + } +} + func TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) -- cgit v1.2.3