Skip to content

Commit aa8e85a

Browse files
committed
revise tests for get remote name api
1 parent a8ad434 commit aa8e85a

5 files changed

+152
-24
lines changed

generate_all_mocks.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ mockgen -source=git/middleware/reference.go -destination=mocks/mock_reference.go
66
mockgen -source=git/middleware/index.go -destination=mocks/mock_index.go -package=mocks
77
mockgen -source=git/commit/git_list_all_commit_logs.go -destination=mocks/mock_git_list_all_commit_logs.go -package=mocks
88
mockgen -source=git/middleware/commit.go -destination=mocks/mock_commit.go -package=mocks
9-
mockgen -source=git/commit/git_commit_file_history.go -destination=mocks/mock_git_commit_file_history.go -package=mocks
9+
mockgen -source=git/commit/git_commit_file_history.go -destination=mocks/mock_git_commit_file_history.go -package=mocks
10+
mockgen -source=git/remote/remote_validation.go -destination=mocks/mock_remote_validation.go -package=mocks
11+
mockgen -source=git/remote/git_remote_list.go -destination=mock_git_remote_list.go -package=mocks

git/remote/git_remote_remote_name.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ type Name interface {
1010
}
1111

1212
type remoteName struct {
13-
repo middleware.Repository
14-
remoteUrl string
13+
repo middleware.Repository
14+
remoteUrl string
15+
remoteValidation Validation
16+
remoteList List
1517
}
1618

1719
func (r remoteName) GetRemoteNameWithUrl() string {
18-
repo := r.repo
19-
20-
if validationErr := NewRemoteValidation(repo).ValidateRemoteFields(); validationErr != nil {
20+
if validationErr := r.remoteValidation.ValidateRemoteFields(); validationErr != nil {
2121
logger.Log(validationErr.Error(), global.StatusError)
2222
return ""
2323
}
2424

25-
remoteList := NewRemoteList(r.repo).GetAllRemotes()
25+
remoteList := r.remoteList.GetAllRemotes()
2626
if remoteList == nil {
2727
logger.Log("repo has no remotes", global.StatusError)
2828
return ""
@@ -38,9 +38,11 @@ func (r remoteName) GetRemoteNameWithUrl() string {
3838
return ""
3939
}
4040

41-
func NewGetRemoteName(repo middleware.Repository, remoteUrl string) Name {
41+
func NewGetRemoteName(repo middleware.Repository, remoteUrl string, remoteValidation Validation, remoteList List) Name {
4242
return remoteName{
43-
repo: repo,
44-
remoteUrl: remoteUrl,
43+
repo: repo,
44+
remoteUrl: remoteUrl,
45+
remoteValidation: remoteValidation,
46+
remoteList: remoteList,
4547
}
4648
}
Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,87 @@
11
package remote
22

33
import (
4+
"errors"
45
"fmt"
6+
"github.com/golang/mock/gomock"
57
git2go "github.com/libgit2/git2go/v31"
8+
"github.com/neel1996/gitconvex/git/middleware"
9+
"github.com/neel1996/gitconvex/mocks"
610
"github.com/stretchr/testify/suite"
711
"os"
8-
"path/filepath"
912
"testing"
1013
)
1114

1215
type GetRemoteNameTestSuite struct {
1316
suite.Suite
14-
getRemoteName Name
15-
repo *git2go.Repository
16-
noHeadRepo *git2go.Repository
17+
getRemoteName Name
18+
repo middleware.Repository
19+
remoteValidation Validation
20+
remoteList List
21+
mockController *gomock.Controller
22+
mockRepo *mocks.MockRepository
23+
mockRemoteValidation *mocks.MockValidation
24+
mockRemoteList *mocks.MockList
25+
noHeadRepo *git2go.Repository
1726
}
1827

1928
func (suite *GetRemoteNameTestSuite) SetupTest() {
2029
r, err := git2go.OpenRepository(os.Getenv("GITCONVEX_TEST_REPO"))
2130
if err != nil {
2231
fmt.Println(err)
2332
}
24-
noHeadPath := os.Getenv("GITCONVEX_TEST_REPO") + string(filepath.Separator) + "no_head"
25-
noHeadRepo, _ := git2go.OpenRepository(noHeadPath)
2633

27-
suite.repo = r
28-
suite.noHeadRepo = noHeadRepo
29-
suite.getRemoteName = NewGetRemoteName(suite.repo, "https://github.com/neel1996/gitconvex-test.git")
34+
suite.repo = middleware.NewRepository(r)
35+
suite.remoteValidation = NewRemoteValidation(suite.repo)
36+
suite.remoteList = NewRemoteList(suite.repo)
37+
suite.mockController = gomock.NewController(suite.T())
38+
suite.mockRepo = mocks.NewMockRepository(suite.mockController)
39+
suite.mockRemoteValidation = mocks.NewMockValidation(suite.mockController)
40+
suite.mockRemoteList = mocks.NewMockList(suite.mockController)
41+
suite.getRemoteName = NewGetRemoteName(
42+
suite.mockRepo,
43+
"https://github.com/neel1996/gitconvex-test.git",
44+
suite.mockRemoteValidation,
45+
suite.mockRemoteList,
46+
)
3047
}
3148

3249
func TestGetRemoteNameTestSuite(t *testing.T) {
3350
suite.Run(t, new(GetRemoteNameTestSuite))
3451
}
3552

3653
func (suite *GetRemoteNameTestSuite) TestGetRemoteName_ShouldReturnRemoteName_WhenRemoteUrlIsValid() {
54+
suite.getRemoteName = NewGetRemoteName(
55+
suite.repo,
56+
"https://github.com/neel1996/gitconvex-test.git",
57+
suite.remoteValidation,
58+
suite.remoteList,
59+
)
60+
3761
expectedRemote := "origin"
3862

3963
actualRemote := suite.getRemoteName.GetRemoteNameWithUrl()
4064

4165
suite.Equal(expectedRemote, actualRemote)
4266
}
4367

44-
func (suite *GetRemoteNameTestSuite) TestGetRemoteName_ShouldReturnEmptyString_WhenRepoIsNil() {
68+
func (suite *GetRemoteNameTestSuite) TestGetRemoteName_ShouldReturnEmptyString_WhenValidationFails() {
4569
expectedRemote := ""
46-
getRemoteName := NewGetRemoteName(nil, "https://github.com/neel1996/gitconvex-test.git")
4770

48-
actualRemote := getRemoteName.GetRemoteNameWithUrl()
71+
suite.mockRemoteValidation.EXPECT().ValidateRemoteFields().Return(errors.New("VALIDATION_ERROR"))
72+
73+
actualRemote := suite.getRemoteName.GetRemoteNameWithUrl()
4974

5075
suite.Equal(expectedRemote, actualRemote)
5176
}
5277

5378
func (suite *GetRemoteNameTestSuite) TestGetRemoteName_ShouldReturnEmptyString_WhenRepoHasNoRemotes() {
5479
expectedRemote := ""
55-
getRemoteName := NewGetRemoteName(suite.noHeadRepo, "https://github.com/neel1996/gitconvex-test.git")
5680

57-
actualRemote := getRemoteName.GetRemoteNameWithUrl()
81+
suite.mockRemoteValidation.EXPECT().ValidateRemoteFields().Return(nil)
82+
suite.mockRemoteList.EXPECT().GetAllRemotes().Return(nil)
83+
84+
actualRemote := suite.getRemoteName.GetRemoteNameWithUrl()
5885

5986
suite.Equal(expectedRemote, actualRemote)
6087
}

mocks/mock_git_remote_list.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/mock_remote_validation.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)