Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seachicken committed Nov 22, 2022
1 parent 5578566 commit d447271
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 110 deletions.
2 changes: 1 addition & 1 deletion conn/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion conn/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
})
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions conn/fixtures/git/config_remote.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git@github.com:owner/repo.git
1 change: 1 addition & 0 deletions conn/fixtures/git/lsRemoteHead_issue1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0 refs/heads/issue1
1 change: 1 addition & 0 deletions conn/fixtures/git/remoteHead_issue1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a97e9630426df5d34ca9ee77ae1159bdfd5ff8f0
62 changes: 47 additions & 15 deletions conn/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
)
}
Expand Down
4 changes: 2 additions & 2 deletions poi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
Loading

0 comments on commit d447271

Please sign in to comment.