summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/commands/git_test.go')
-rw-r--r--pkg/commands/git_test.go182
1 files changed, 179 insertions, 3 deletions
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go
index e5fd539b9..25820558a 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -1496,7 +1496,7 @@ func TestGitCommandGetCommits(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
- test func([]*Commit)
+ test func([]*Commit, error)
}
scenarios := []scenario{
@@ -1512,11 +1512,18 @@ func TestGitCommandGetCommits(t *testing.T) {
case "log":
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
return exec.Command("echo")
+ case "merge-base":
+ assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args)
+ return exec.Command("test")
+ case "symbolic-ref":
+ assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
+ return exec.Command("echo", "master")
}
return nil
},
- func(commits []*Commit) {
+ func(commits []*Commit, err error) {
+ assert.NoError(t, err)
assert.Len(t, commits, 0)
},
},
@@ -1532,28 +1539,65 @@ func TestGitCommandGetCommits(t *testing.T) {
case "log":
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
return exec.Command("echo", "8a2bb0e commit 1\n78976bc commit 2")
+ case "merge-base":
+ assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args)
+ return exec.Command("echo", "78976bc")
+ case "symbolic-ref":
+ assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
+ return exec.Command("echo", "master")
}
return nil
},
- func(commits []*Commit) {
+ func(commits []*Commit, err error) {
+ assert.NoError(t, err)
assert.Len(t, commits, 2)
assert.EqualValues(t, []*Commit{
{
Sha: "8a2bb0e",
Name: "commit 1",
Pushed: true,
+ Merged: false,
DisplayString: "8a2bb0e commit 1",
},
{
Sha: "78976bc",
Name: "commit 2",
Pushed: false,
+ Merged: true,
DisplayString: "78976bc commit 2",
},
}, commits)
},
},
+ {
+ "GetCommits bubbles up an error from setCommitMergedStatuses",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+
+ switch args[0] {
+ case "rev-list":
+ assert.EqualValues(t, []string{"rev-list", "@{u}..head", "--abbrev-commit"}, args)
+ return exec.Command("echo", "8a2bb0e")
+ case "log":
+ assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
+ return exec.Command("echo", "8a2bb0e commit 1\n78976bc commit 2")
+ case "merge-base":
+ assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args)
+ return exec.Command("echo", "78976bc")
+ case "symbolic-ref":
+ assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
+ // here's where we are returning the error
+ return exec.Command("test")
+ }
+
+ return nil
+ },
+ func(commits []*Commit, err error) {
+ assert.Error(t, err)
+ assert.Len(t, commits, 0)
+ },
+ },
}
for _, s := range scenarios {
@@ -1668,3 +1712,135 @@ func TestGitCommandDiff(t *testing.T) {
})
}
}
+
+func TestGitCommandGetMergeBase(t *testing.T) {
+ type scenario struct {
+ testName string
+ command func(string, ...string) *exec.Cmd
+ test func(string, error)
+ }
+
+ scenarios := []scenario{
+ {
+ "swallows an error if the call to merge-base returns an error",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+
+ switch args[0] {
+ case "symbolic-ref":
+ assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
+ return exec.Command("echo", "master")
+ case "merge-base":
+ assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args)
+ return exec.Command("test")
+ }
+ return nil
+ },
+ func(output string, err error) {
+ assert.NoError(t, err)
+ assert.EqualValues(t, "", output)
+ },
+ },
+ {
+ "returns the commit when master",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+
+ switch args[0] {
+ case "symbolic-ref":
+ assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
+ return exec.Command("echo", "master")
+ case "merge-base":
+ assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args)
+ return exec.Command("echo", "blah")
+ }
+ return nil
+ },
+ func(output string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, "blah\n", output)
+ },
+ },
+ {
+ "checks against develop when a feature branch",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+
+ switch args[0] {
+ case "symbolic-ref":
+ assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
+ return exec.Command("echo", "feature/test")
+ case "merge-base":
+ assert.EqualValues(t, []string{"merge-base", "HEAD", "develop"}, args)
+ return exec.Command("echo", "blah")
+ }
+ return nil
+ },
+ func(output string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, "blah\n", output)
+ },
+ },
+ {
+ "bubbles up error if there is one",
+ func(cmd string, args ...string) *exec.Cmd {
+ return exec.Command("test")
+ },
+ func(output string, err error) {
+ assert.Error(t, err)
+ assert.Equal(t, "", output)
+ },
+ },
+ }
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ gitCmd := newDummyGitCommand()
+ gitCmd.OSCommand.command = s.command
+ s.test(gitCmd.getMergeBase())
+ })
+ }
+}
+
+func TestGitCommandCurrentBranchName(t *testing.T) {
+ type scenario struct {
+ testName string
+ command func(string, ...string) *exec.Cmd
+ test func(string, error)
+ }
+
+ scenarios := []scenario{
+ {
+ "says we are on the master branch if we are",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.Equal(t, "git", cmd)
+ assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
+ return exec.Command("echo", "master")
+ },
+ func(output string, err error) {
+ assert.NoError(t, err)
+ assert.EqualValues(t, "master", output)
+ },
+ },
+ {
+ "bubbles up error if there is one",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.Equal(t, "git", cmd)
+ assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
+ return exec.Command("test")
+ },
+ func(output string, err error) {
+ assert.Error(t, err)
+ assert.EqualValues(t, "", output)
+ },
+ },
+ }
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ gitCmd := newDummyGitCommand()
+ gitCmd.OSCommand.command = s.command
+ s.test(gitCmd.CurrentBranchName())
+ })
+ }
+}