|
1 | 1 | package remote
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "fmt"
|
| 6 | + "github.com/golang/mock/gomock" |
5 | 7 | git2go "github.com/libgit2/git2go/v31"
|
| 8 | + "github.com/neel1996/gitconvex/git/middleware" |
| 9 | + "github.com/neel1996/gitconvex/mocks" |
6 | 10 | "github.com/stretchr/testify/suite"
|
7 | 11 | "os"
|
8 |
| - "path/filepath" |
9 | 12 | "testing"
|
10 | 13 | )
|
11 | 14 |
|
12 | 15 | type GetRemoteNameTestSuite struct {
|
13 | 16 | 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 |
17 | 26 | }
|
18 | 27 |
|
19 | 28 | func (suite *GetRemoteNameTestSuite) SetupTest() {
|
20 | 29 | r, err := git2go.OpenRepository(os.Getenv("GITCONVEX_TEST_REPO"))
|
21 | 30 | if err != nil {
|
22 | 31 | fmt.Println(err)
|
23 | 32 | }
|
24 |
| - noHeadPath := os.Getenv("GITCONVEX_TEST_REPO") + string(filepath.Separator) + "no_head" |
25 |
| - noHeadRepo, _ := git2go.OpenRepository(noHeadPath) |
26 | 33 |
|
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 | + ) |
30 | 47 | }
|
31 | 48 |
|
32 | 49 | func TestGetRemoteNameTestSuite(t *testing.T) {
|
33 | 50 | suite.Run(t, new(GetRemoteNameTestSuite))
|
34 | 51 | }
|
35 | 52 |
|
36 | 53 | 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 | + |
37 | 61 | expectedRemote := "origin"
|
38 | 62 |
|
39 | 63 | actualRemote := suite.getRemoteName.GetRemoteNameWithUrl()
|
40 | 64 |
|
41 | 65 | suite.Equal(expectedRemote, actualRemote)
|
42 | 66 | }
|
43 | 67 |
|
44 |
| -func (suite *GetRemoteNameTestSuite) TestGetRemoteName_ShouldReturnEmptyString_WhenRepoIsNil() { |
| 68 | +func (suite *GetRemoteNameTestSuite) TestGetRemoteName_ShouldReturnEmptyString_WhenValidationFails() { |
45 | 69 | expectedRemote := ""
|
46 |
| - getRemoteName := NewGetRemoteName(nil, "https://github.com/neel1996/gitconvex-test.git") |
47 | 70 |
|
48 |
| - actualRemote := getRemoteName.GetRemoteNameWithUrl() |
| 71 | + suite.mockRemoteValidation.EXPECT().ValidateRemoteFields().Return(errors.New("VALIDATION_ERROR")) |
| 72 | + |
| 73 | + actualRemote := suite.getRemoteName.GetRemoteNameWithUrl() |
49 | 74 |
|
50 | 75 | suite.Equal(expectedRemote, actualRemote)
|
51 | 76 | }
|
52 | 77 |
|
53 | 78 | func (suite *GetRemoteNameTestSuite) TestGetRemoteName_ShouldReturnEmptyString_WhenRepoHasNoRemotes() {
|
54 | 79 | expectedRemote := ""
|
55 |
| - getRemoteName := NewGetRemoteName(suite.noHeadRepo, "https://github.com/neel1996/gitconvex-test.git") |
56 | 80 |
|
57 |
| - actualRemote := getRemoteName.GetRemoteNameWithUrl() |
| 81 | + suite.mockRemoteValidation.EXPECT().ValidateRemoteFields().Return(nil) |
| 82 | + suite.mockRemoteList.EXPECT().GetAllRemotes().Return(nil) |
| 83 | + |
| 84 | + actualRemote := suite.getRemoteName.GetRemoteNameWithUrl() |
58 | 85 |
|
59 | 86 | suite.Equal(expectedRemote, actualRemote)
|
60 | 87 | }
|
0 commit comments