From b46e4b497610aaa771d709f3cab14652e48b8512 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Thu, 6 Sep 2018 23:38:49 +0200 Subject: [PATCH] commands/git : add several tests, do some cleanup --- pkg/commands/git.go | 18 ++++---- pkg/commands/git_test.go | 97 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 9 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 514967ebd..fec0bc6ec 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -230,7 +230,7 @@ func (c *GitCommand) GetCommitsToPush() []string { // RenameCommit renames the topmost commit with the given name func (c *GitCommand) RenameCommit(name string) error { - return c.OSCommand.RunCommand("git commit --allow-empty --amend -m " + c.OSCommand.Quote(name)) + return c.OSCommand.RunCommand(fmt.Sprintf("git commit --allow-empty --amend -m %s", c.OSCommand.Quote(name))) } // Fetch fetch git repo @@ -240,23 +240,23 @@ func (c *GitCommand) Fetch() error { // ResetToCommit reset to commit func (c *GitCommand) ResetToCommit(sha string) error { - return c.OSCommand.RunCommand("git reset " + sha) + return c.OSCommand.RunCommand(fmt.Sprintf("git reset %s", sha)) } // NewBranch create new branch func (c *GitCommand) NewBranch(name string) error { - return c.OSCommand.RunCommand("git checkout -b " + name) + return c.OSCommand.RunCommand(fmt.Sprintf("git checkout -b %s", name)) } // DeleteBranch delete branch func (c *GitCommand) DeleteBranch(branch string, force bool) error { - var command string + command := "git branch -d" + if force { - command = "git branch -D " - } else { - command = "git branch -d " + command = "git branch -D" } - return c.OSCommand.RunCommand(command + branch) + + return c.OSCommand.RunCommand(fmt.Sprintf("%s %s", command, branch)) } // ListStash list stash @@ -266,7 +266,7 @@ func (c *GitCommand) ListStash() (string, error) { // Merge merge func (c *GitCommand) Merge(branchName string) error { - return c.OSCommand.RunCommand("git merge --no-edit " + branchName) + return c.OSCommand.RunCommand(fmt.Sprintf("git merge --no-edit %s", branchName)) } // AbortMerge abort merge diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index ac1fe8094..7af75286d 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -633,6 +633,103 @@ func TestGitCommandGetCommitsToPush(t *testing.T) { } } +func TestGitCommandRenameCommit(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"commit", "--allow-empty", "--amend", "-m", "test"}, args) + + return exec.Command("echo") + } + + assert.NoError(t, gitCmd.RenameCommit("test")) +} + +func TestGitCommandResetToCommit(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"reset", "78976bc"}, args) + + return exec.Command("echo") + } + + assert.NoError(t, gitCmd.ResetToCommit("78976bc")) +} + +func TestGitCommandNewBranch(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"checkout", "-b", "test"}, args) + + return exec.Command("echo") + } + + assert.NoError(t, gitCmd.NewBranch("test")) +} + +func TestGitCommandDeleteBranch(t *testing.T) { + type scenario struct { + testName string + branch string + force bool + command func(string, ...string) *exec.Cmd + test func(error) + } + + scenarios := []scenario{ + { + "Delete a branch", + "test", + false, + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"branch", "-d", "test"}, args) + + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + }, + { + "Force delete a branch", + "test", + true, + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"branch", "-D", "test"}, args) + + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = s.command + s.test(gitCmd.DeleteBranch(s.branch, s.force)) + }) + } +} + +func TestGitCommandMerge(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"merge", "--no-edit", "test"}, args) + + return exec.Command("echo") + } + + assert.NoError(t, gitCmd.Merge("test")) +} + func TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))