diff --git a/conn/command.go b/conn/command.go index 9a6568f..ee8402b 100644 --- a/conn/command.go +++ b/conn/command.go @@ -81,7 +81,7 @@ func (conn *Connection) GetMergedBranchNames(ctx context.Context, remoteName str func (conn *Connection) GetRemoteHeadOid(ctx context.Context, remoteName string, branchName string) (string, error) { args := []string{ - "rev-parse", fmt.Sprintf("%s/%s", remoteName, branchName), "--", + "rev-parse", fmt.Sprintf("%s/%s", remoteName, branchName), } return conn.run(ctx, "git", args, None) } diff --git a/conn/command_test.go b/conn/command_test.go index c0b20e5..f6af4af 100644 --- a/conn/command_test.go +++ b/conn/command_test.go @@ -79,7 +79,7 @@ func Test_RepoBasic(t *testing.T) { t.Run("GetConfig", func(t *testing.T) { actual, _ := conn.GetConfig(context.Background(), "branch.main.merge") assert.Equal(t, - stub.readFile("git", "configMerge", "main"), + stub.readFile("git", "config", "mergeMain"), actual, ) }) diff --git a/conn/fixtures/git/configMerge_forkMain.txt b/conn/fixtures/git/config_mergeForkMain.txt similarity index 100% rename from conn/fixtures/git/configMerge_forkMain.txt rename to conn/fixtures/git/config_mergeForkMain.txt diff --git a/conn/fixtures/git/configMerge_issue1.txt b/conn/fixtures/git/config_mergeIssue1.txt similarity index 100% rename from conn/fixtures/git/configMerge_issue1.txt rename to conn/fixtures/git/config_mergeIssue1.txt diff --git a/conn/fixtures/git/configMerge_main.txt b/conn/fixtures/git/config_mergeMain.txt similarity index 100% rename from conn/fixtures/git/configMerge_main.txt rename to conn/fixtures/git/config_mergeMain.txt diff --git a/conn/fixtures/git/config_remote.txt b/conn/fixtures/git/config_remote.txt new file mode 100644 index 0000000..5e4e788 --- /dev/null +++ b/conn/fixtures/git/config_remote.txt @@ -0,0 +1 @@ +git@github.com:owner/repo.git diff --git a/conn/fixtures/git/lsRemoteHead_issue1.txt b/conn/fixtures/git/lsRemoteHead_issue1.txt new file mode 100644 index 0000000..a3633d9 --- /dev/null +++ b/conn/fixtures/git/lsRemoteHead_issue1.txt @@ -0,0 +1 @@ +a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0 refs/heads/issue1 diff --git a/conn/fixtures/git/remoteHead_issue1.txt b/conn/fixtures/git/remoteHead_issue1.txt new file mode 100644 index 0000000..4915d74 --- /dev/null +++ b/conn/fixtures/git/remoteHead_issue1.txt @@ -0,0 +1 @@ +a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0 diff --git a/conn/stub.go b/conn/stub.go index d4264a7..2acd4f5 100644 --- a/conn/stub.go +++ b/conn/stub.go @@ -23,6 +23,16 @@ type ( Times *Times } + RemoteHeadStub struct { + BranchName string + Filename string + } + + LsRemoteHeadStub struct { + BranchName string + Filename string + } + AssociatedBranchNamesStub struct { Oid string Filename string @@ -124,25 +134,47 @@ func (s *Stub) GetMergedBranchNames(filename string, err error, conf *Conf) *Stu return s } -func (s *Stub) GetRemoteHeadOid(err error, conf *Conf) *Stub { +func (s *Stub) GetRemoteHeadOid(stubs []RemoteHeadStub, err error, conf *Conf) *Stub { s.t.Helper() - configure( - s.Conn.EXPECT(). - GetRemoteHeadOid(gomock.Any(), gomock.Any(), gomock.Any()). - Return("", err), - conf, - ) + if stubs == nil { + configure( + s.Conn.EXPECT(). + GetRemoteHeadOid(gomock.Any(), gomock.Any(), gomock.Any()). + Return("", err), + conf, + ) + } else { + for _, stub := range stubs { + configure( + s.Conn.EXPECT(). + GetRemoteHeadOid(gomock.Any(), gomock.Any(), stub.BranchName). + Return(s.readFile("git", "remoteHead", stub.Filename), err), + conf, + ) + } + } return s } -func (s *Stub) GetLsRemoteHeadOid(err error, conf *Conf) *Stub { +func (s *Stub) GetLsRemoteHeadOid(stubs []LsRemoteHeadStub, err error, conf *Conf) *Stub { s.t.Helper() - configure( - s.Conn.EXPECT(). - GetLsRemoteHeadOid(gomock.Any(), gomock.Any(), gomock.Any()). - Return("", err), - conf, - ) + if stubs == nil { + configure( + s.Conn.EXPECT(). + GetLsRemoteHeadOid(gomock.Any(), gomock.Any(), gomock.Any()). + Return("", err), + conf, + ) + } else { + for _, stub := range stubs { + configure( + s.Conn.EXPECT(). + GetLsRemoteHeadOid(gomock.Any(), gomock.Any(), stub.BranchName). + Return(s.readFile("git", "lsRemoteHead", stub.Filename), err), + conf, + ) + } + } return s } @@ -203,7 +235,7 @@ func (s *Stub) GetConfig(stubs []ConfigStub, err error, conf *Conf) *Stub { s.Conn. EXPECT(). GetConfig(gomock.Any(), stub.BranchName). - Return(s.readFile("git", "configMerge", stub.Filename), err), + Return(s.readFile("git", "config", stub.Filename), err), conf, ) } diff --git a/poi.go b/poi.go index 0a50dd9..b67ffe6 100644 --- a/poi.go +++ b/poi.go @@ -299,14 +299,14 @@ func applyCommits(ctx context.Context, remote Remote, branches []Branch, default } if remoteHeadOid, err := connection.GetRemoteHeadOid(ctx, remote.Name, branch.Name); err == nil { - branch.RemoteHeadOid = remoteHeadOid + branch.RemoteHeadOid = splitLines(remoteHeadOid)[0] } else { result, _ := connection.GetConfig(ctx, fmt.Sprintf("branch.%s.remote", branch.Name)) splitResults := splitLines(result) if len(splitResults) > 0 { remoteUrl := splitResults[0] if result, err := connection.GetLsRemoteHeadOid(ctx, remoteUrl, branch.Name); err == nil { - splitResults := strings.Split(result, " ") + splitResults := strings.Fields(result) if len(splitResults) > 0 { branch.RemoteHeadOid = splitResults[0] } diff --git a/poi_test.go b/poi_test.go index 80c4afc..1e65186 100644 --- a/poi_test.go +++ b/poi_test.go @@ -10,6 +10,120 @@ import ( "github.com/stretchr/testify/assert" ) +var ErrCommand = errors.New("failed to run external command") + +func Test_ShouldBeDeletableWhenRemoteBranchesAssociatedWithMergedPR(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + s := conn.Setup(ctrl). + CheckRepos(nil, nil). + GetRemoteNames("origin", nil, nil). + GetSshConfig("github.com", nil, nil). + GetRepoNames("origin", nil, nil). + GetBranchNames("@main_issue1", nil, nil). + GetMergedBranchNames("@main_issue1", nil, nil). + GetRemoteHeadOid([]conn.RemoteHeadStub{ + {BranchName: "issue1", Filename: "issue1"}, + }, nil, nil). + GetLog([]conn.LogStub{ + {BranchName: "main", Filename: "main_issue1Merged"}, {BranchName: "issue1", Filename: "issue1Merged"}, + }, nil, nil). + GetPullRequests("issue1Merged", nil, nil). + GetUncommittedChanges("", nil, nil). + GetConfig([]conn.ConfigStub{ + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + }, nil, nil) + remote, _ := GetRemote(context.Background(), s.Conn) + + actual, _ := GetBranches(context.Background(), remote, s.Conn, false) + + assert.Equal(t, []Branch{ + { + false, "issue1", true, + "a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0", + []string{ + "a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0", + }, + []PullRequest{ + { + "issue1", Merged, false, 1, + []string{ + "a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0", + }, + "https://github.com/owner/repo/pull/1", "owner", + }, + }, + Deletable, + }, + { + true, "main", true, + "", + []string{}, + []PullRequest{}, + NotDeletable, + }, + }, actual) +} + +func Test_ShouldBeDeletableWhenLsRemoteBranchesAssociatedWithMergedPR(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + s := conn.Setup(ctrl). + CheckRepos(nil, nil). + GetRemoteNames("origin", nil, nil). + GetSshConfig("github.com", nil, nil). + GetRepoNames("origin", nil, nil). + GetBranchNames("@main_issue1", nil, nil). + GetMergedBranchNames("@main_issue1", nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid([]conn.LsRemoteHeadStub{ + {BranchName: "issue1", Filename: "issue1"}, + }, nil, nil). + GetLog([]conn.LogStub{ + {BranchName: "main", Filename: "main_issue1Merged"}, {BranchName: "issue1", Filename: "issue1Merged"}, + }, nil, nil). + GetPullRequests("issue1Merged", nil, nil). + GetUncommittedChanges("", nil, nil). + GetConfig([]conn.ConfigStub{ + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, + }, nil, nil) + remote, _ := GetRemote(context.Background(), s.Conn) + + actual, _ := GetBranches(context.Background(), remote, s.Conn, false) + + assert.Equal(t, []Branch{ + { + false, "issue1", true, + "a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0", + []string{ + "a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0", + }, + []PullRequest{ + { + "issue1", Merged, false, 1, + []string{ + "a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0", + }, + "https://github.com/owner/repo/pull/1", "owner", + }, + }, + Deletable, + }, + { + true, "main", true, + "", + []string{}, + []PullRequest{}, + NotDeletable, + }, + }, actual) +} + func Test_ShouldBeDeletableWhenBranchesAssociatedWithMergedPR(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -21,8 +135,8 @@ func Test_ShouldBeDeletableWhenBranchesAssociatedWithMergedPR(t *testing.T) { GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main_issue1", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main_issue1Merged"}, {BranchName: "issue1", Filename: "issue1Merged"}, }, nil, nil). @@ -34,8 +148,9 @@ func Test_ShouldBeDeletableWhenBranchesAssociatedWithMergedPR(t *testing.T) { GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -80,8 +195,8 @@ func Test_ShouldBeDeletableWhenBranchesAssociatedWithSquashAndMergedPR(t *testin GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -92,8 +207,9 @@ func Test_ShouldBeDeletableWhenBranchesAssociatedWithSquashAndMergedPR(t *testin GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -138,8 +254,8 @@ func Test_ShouldBeDeletableWhenBranchesAssociatedWithUpstreamSquashAndMergedPR(t GetRepoNames("origin_upstream", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -150,8 +266,9 @@ func Test_ShouldBeDeletableWhenBranchesAssociatedWithUpstreamSquashAndMergedPR(t GetPullRequests("issue1UpMerged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -196,8 +313,8 @@ func Test_ShouldBeDeletableWhenPRCheckoutBranchesAssociatedWithUpstreamSquashAnd GetRepoNames("origin_upstream", nil, nil). GetBranchNames("@main_forkMain", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "fork/main", Filename: "issue1"}, }, nil, nil). @@ -208,8 +325,9 @@ func Test_ShouldBeDeletableWhenPRCheckoutBranchesAssociatedWithUpstreamSquashAnd GetPullRequests("forkMainUpMerged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.fork/main.merge", Filename: "forkMain"}, - {BranchName: "branch.main.merge", Filename: "main"}, + {BranchName: "branch.fork/main.merge", Filename: "mergeForkMain"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.fork/main.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -254,8 +372,8 @@ func Test_ShouldBeDeletableWhenBranchIsCheckedOutWithTheCheckIsFalse(t *testing. GetRepoNames("origin", nil, nil). GetBranchNames("main_@issue1", nil, nil). GetMergedBranchNames("main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -266,8 +384,9 @@ func Test_ShouldBeDeletableWhenBranchIsCheckedOutWithTheCheckIsFalse(t *testing. GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil). CheckoutBranch(nil, conn.NewConf(&conn.Times{N: 1})) remote, _ := GetRemote(context.Background(), s.Conn) @@ -313,8 +432,8 @@ func Test_ShouldBeDeletableWhenBranchIsCheckedOutWithTheCheckIsTrue(t *testing.T GetRepoNames("origin", nil, nil). GetBranchNames("main_@issue1", nil, nil). GetMergedBranchNames("main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -325,8 +444,9 @@ func Test_ShouldBeDeletableWhenBranchIsCheckedOutWithTheCheckIsTrue(t *testing.T GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil). CheckoutBranch(nil, conn.NewConf(&conn.Times{N: 0})) remote, _ := GetRemote(context.Background(), s.Conn) @@ -372,8 +492,8 @@ func Test_ShouldBeDeletableWhenBranchIsCheckedOutWithoutADefaultBranch(t *testin GetRepoNames("origin", nil, nil). GetBranchNames("@issue1", nil, nil). GetMergedBranchNames("empty", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -384,7 +504,8 @@ func Test_ShouldBeDeletableWhenBranchIsCheckedOutWithoutADefaultBranch(t *testin GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil). CheckoutBranch(nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -430,8 +551,8 @@ func Test_ShouldNotDeletableWhenBranchHasModifiedUncommittedChanges(t *testing.T GetRepoNames("origin", nil, nil). GetBranchNames("main_@issue1", nil, nil). GetMergedBranchNames("main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -442,8 +563,9 @@ func Test_ShouldNotDeletableWhenBranchHasModifiedUncommittedChanges(t *testing.T GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges(" M README.md", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil). CheckoutBranch(nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -489,8 +611,8 @@ func Test_ShouldBeDeletableWhenBranchHasUntrackedUncommittedChanges(t *testing.T GetRepoNames("origin", nil, nil). GetBranchNames("main_@issue1", nil, nil). GetMergedBranchNames("main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -501,8 +623,9 @@ func Test_ShouldBeDeletableWhenBranchHasUntrackedUncommittedChanges(t *testing.T GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges("?? new.txt", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil). CheckoutBranch(nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -548,8 +671,8 @@ func Test_ShouldNotDeletableWhenBranchesAssociatedWithClosedPR(t *testing.T) { GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -560,8 +683,9 @@ func Test_ShouldNotDeletableWhenBranchesAssociatedWithClosedPR(t *testing.T) { GetPullRequests("issue1Closed", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -606,8 +730,8 @@ func Test_ShouldBeDeletableWhenBranchesAssociatedWithSquashAndMergedAndClosedPRs GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -618,8 +742,9 @@ func Test_ShouldBeDeletableWhenBranchesAssociatedWithSquashAndMergedAndClosedPRs GetPullRequests("issue1Merged_issue1Closed", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -671,8 +796,8 @@ func Test_ShouldNotDeletableWhenBranchesAssociatedWithNotFullyMergedPR(t *testin GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main_issue1SquashAndMerged"}, {BranchName: "issue1", Filename: "issue1CommitAfterMerge"}, }, nil, nil). @@ -685,8 +810,9 @@ func Test_ShouldNotDeletableWhenBranchesAssociatedWithNotFullyMergedPR(t *testin GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -732,8 +858,8 @@ func Test_ShouldNotDeletableWhenDefaultBranchAssociatedWithMergedPR(t *testing.T GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -744,8 +870,9 @@ func Test_ShouldNotDeletableWhenDefaultBranchAssociatedWithMergedPR(t *testing.T GetPullRequests("mainMerged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -790,8 +917,8 @@ func Test_BranchesAndPRsAreNotAssociatedWhenManyLocalCommitsAreAhead(t *testing. GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1ManyCommits"}, // return with '--max-count=3' @@ -804,8 +931,9 @@ func Test_BranchesAndPRsAreNotAssociatedWhenManyLocalCommitsAreAhead(t *testing. GetPullRequests("notFound", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -844,8 +972,8 @@ func Test_ShouldBeNoCommitHistoryWhenTheFirstCommitOfATopicBranchIsAssociatedWit GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "main"}, }, nil, nil). @@ -855,8 +983,9 @@ func Test_ShouldBeNoCommitHistoryWhenTheFirstCommitOfATopicBranchIsAssociatedWit GetPullRequests("notFound", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -891,8 +1020,8 @@ func Test_ShouldBeNoCommitHistoryWhenDetachedBranch(t *testing.T) { GetRepoNames("origin", nil, nil). GetBranchNames("main_@detached", nil, nil). GetMergedBranchNames("main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, }, nil, nil). @@ -902,7 +1031,7 @@ func Test_ShouldBeNoCommitHistoryWhenDetachedBranch(t *testing.T) { GetPullRequests("notFound", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -931,7 +1060,7 @@ func Test_ReturnsAnErrorWhenGetRemoteNamesFails(t *testing.T) { defer ctrl.Finish() s := conn.Setup(ctrl). - GetRemoteNames("origin", errors.New("failed to run external command: git"), nil) + GetRemoteNames("origin", ErrCommand, nil) _, err := GetRemote(context.Background(), s.Conn) @@ -945,12 +1074,12 @@ func Test_DoesNotReturnsAnErrorWhenGetSshConfigFails(t *testing.T) { s := conn.Setup(ctrl). CheckRepos(nil, nil). GetRemoteNames("origin", nil, nil). - GetSshConfig("github.com", errors.New("failed to run external command: ssh"), nil). + GetSshConfig("github.com", ErrCommand, nil). GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -961,8 +1090,9 @@ func Test_DoesNotReturnsAnErrorWhenGetSshConfigFails(t *testing.T) { GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges("", nil, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -978,7 +1108,7 @@ func Test_ReturnsAnErrorWhenGetRepoNamesFails(t *testing.T) { s := conn.Setup(ctrl). GetRemoteNames("origin", nil, nil). GetSshConfig("github.com", nil, nil). - GetRepoNames("origin", errors.New("failed to run external command: gh"), nil) + GetRepoNames("origin", ErrCommand, nil) remote, _ := GetRemote(context.Background(), s.Conn) _, err := GetBranches(context.Background(), remote, s.Conn, false) @@ -991,7 +1121,7 @@ func Test_ReturnsAnErrorWhenCheckReposFails(t *testing.T) { defer ctrl.Finish() s := conn.Setup(ctrl). - CheckRepos(errors.New("failed to run external command: gh"), nil). + CheckRepos(ErrCommand, nil). GetRemoteNames("origin", nil, nil). GetSshConfig("github.com", nil, nil). GetRepoNames("origin", nil, nil) @@ -1011,7 +1141,7 @@ func Test_ReturnsAnErrorWhenGetBranchNamesFails(t *testing.T) { GetRemoteNames("origin", nil, nil). GetSshConfig("github.com", nil, nil). GetRepoNames("origin", nil, nil). - GetBranchNames("@main_issue1", errors.New("failed to run external command: git"), nil) + GetBranchNames("@main_issue1", ErrCommand, nil) remote, _ := GetRemote(context.Background(), s.Conn) _, err := GetBranches(context.Background(), remote, s.Conn, false) @@ -1029,7 +1159,7 @@ func Test_ReturnsAnErrorWhenGetMergedBranchNames(t *testing.T) { GetSshConfig("github.com", nil, nil). GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). - GetMergedBranchNames("@main", errors.New("failed to run external command: git"), nil) + GetMergedBranchNames("@main", ErrCommand, nil) remote, _ := GetRemote(context.Background(), s.Conn) _, err := GetBranches(context.Background(), remote, s.Conn, false) @@ -1048,11 +1178,14 @@ func Test_ReturnsAnErrorWhenGetLogFails(t *testing.T) { GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, - }, errors.New("failed to run external command: git"), nil) + }, ErrCommand, nil). + GetConfig([]conn.ConfigStub{ + {BranchName: "branch.issue1.remote", Filename: "remote"}, + }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) _, err := GetBranches(context.Background(), remote, s.Conn, false) @@ -1071,15 +1204,18 @@ func Test_ReturnsAnErrorWhenGetAssociatedRefNamesFails(t *testing.T) { GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). GetAssociatedRefNames([]conn.AssociatedBranchNamesStub{ {Oid: "a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0", Filename: "issue1"}, {Oid: "6ebe3d30d23531af56bd23b5a098d3ccae2a534a", Filename: "main_issue1"}, - }, errors.New("failed to run external command: git"), nil) + }, ErrCommand, nil). + GetConfig([]conn.ConfigStub{ + {BranchName: "branch.issue1.remote", Filename: "remote"}, + }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) _, err := GetBranches(context.Background(), remote, s.Conn, false) @@ -1098,8 +1234,8 @@ func Test_ReturnsAnErrorWhenGetPullRequestsFails(t *testing.T) { GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -1107,7 +1243,10 @@ func Test_ReturnsAnErrorWhenGetPullRequestsFails(t *testing.T) { {Oid: "a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0", Filename: "issue1"}, {Oid: "6ebe3d30d23531af56bd23b5a098d3ccae2a534a", Filename: "main_issue1"}, }, nil, nil). - GetPullRequests("issue1Merged", errors.New("failed to run external command: gh"), nil) + GetPullRequests("issue1Merged", ErrCommand, nil). + GetConfig([]conn.ConfigStub{ + {BranchName: "branch.issue1.remote", Filename: "remote"}, + }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) _, err := GetBranches(context.Background(), remote, s.Conn, false) @@ -1126,8 +1265,8 @@ func Test_ReturnsAnErrorWhenGetUncommittedChangesFails(t *testing.T) { GetRepoNames("origin", nil, nil). GetBranchNames("@main_issue1", nil, nil). GetMergedBranchNames("@main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -1136,10 +1275,11 @@ func Test_ReturnsAnErrorWhenGetUncommittedChangesFails(t *testing.T) { {Oid: "6ebe3d30d23531af56bd23b5a098d3ccae2a534a", Filename: "main_issue1"}, }, nil, nil). GetPullRequests("issue1Merged", nil, nil). - GetUncommittedChanges("", errors.New("failed to run external command: git"), nil). + GetUncommittedChanges("", ErrCommand, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn) @@ -1159,8 +1299,8 @@ func Test_ReturnsAnErrorWhenCheckoutBranchFails(t *testing.T) { GetRepoNames("origin", nil, nil). GetBranchNames("main_@issue1", nil, nil). GetMergedBranchNames("main", nil, nil). - GetRemoteHeadOid(nil, nil). - GetLsRemoteHeadOid(nil, nil). + GetRemoteHeadOid(nil, ErrCommand, nil). + GetLsRemoteHeadOid(nil, nil, nil). GetLog([]conn.LogStub{ {BranchName: "main", Filename: "main"}, {BranchName: "issue1", Filename: "issue1"}, }, nil, nil). @@ -1170,10 +1310,11 @@ func Test_ReturnsAnErrorWhenCheckoutBranchFails(t *testing.T) { }, nil, nil). GetPullRequests("issue1Merged", nil, nil). GetUncommittedChanges("", nil, nil). - CheckoutBranch(errors.New("failed to run external command: git"), nil). + CheckoutBranch(ErrCommand, nil). GetConfig([]conn.ConfigStub{ - {BranchName: "branch.main.merge", Filename: "main"}, - {BranchName: "branch.issue1.merge", Filename: "issue1"}, + {BranchName: "branch.main.merge", Filename: "mergeMain"}, + {BranchName: "branch.issue1.merge", Filename: "mergeIssue1"}, + {BranchName: "branch.issue1.remote", Filename: "remote"}, }, nil, nil) remote, _ := GetRemote(context.Background(), s.Conn)