Skip to content

Commit 9890f03

Browse files
committed
Added more unit tests for git utility and added more mocks.
1 parent e2d2a90 commit 9890f03

File tree

6 files changed

+1105
-120
lines changed

6 files changed

+1105
-120
lines changed

.mockery.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ packages:
2121
dir: "{{.InterfaceDir}}"
2222
mockName: "MockGoGitRepository"
2323
inpackage: True
24+
IGoGitWorktree:
25+
config:
26+
dir: "{{.InterfaceDir}}"
27+
mockName: "MockGoGitWorktree"
28+
inpackage: True
2429
github.com/AmadlaOrg/LibraryUtils/git/remote:
2530
interfaces:
2631
IRemote:

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
- Scripts in `.script/` that are just simple tools to help with development (they do require some level of handholding)
1111

1212
> [!NOTE]
13-
> This repository is here unify common code, and to make it easier to quickly implement mocks for common Golang libraries
14-
> and other external libraries used.
13+
> This repository is here to unify common code use in different Amadla Golang projects
14+
> and to make it easier to quickly implement mocks for common Golang libraries
15+
> and other external libraries.
1516
1617
## 📄 Documentation?
1718
The code is the documentation 🛂. 🌝

git/git.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ type SGit struct {
2121
}
2222

2323
var (
24-
gitPlainOpen = git.PlainOpen
25-
gitPlainClone = git.PlainClone
24+
gitPlainOpen = func(path string) (IGoGitRepository, error) {
25+
return git.PlainOpen(path)
26+
}
27+
gitPlainClone = func(path string, isBare bool, o *git.CloneOptions) (IGoGitRepository, error) {
28+
return git.PlainClone(path, isBare, o)
29+
}
2630
)
2731

2832
// Clone clones the repository from the given URL to the specified destination
@@ -79,7 +83,8 @@ func (s *SGit) CheckoutTag(tagName string) error {
7983

8084
// Attempt to check out the reference as a branch
8185
err = worktree.Checkout(&git.CheckoutOptions{
82-
Branch: plumbing.ReferenceName(fmt.Sprintf("refs/tags/%s", tagName)), //plumbing.NewBranchReferenceName(refName),
86+
// 🤷 -> Branch: plumbing.NewBranchReferenceName(refName),
87+
Branch: plumbing.ReferenceName(fmt.Sprintf("refs/tags/%s", tagName)),
8388
Force: true,
8489
})
8590
if err != nil {

git/git_test.go

Lines changed: 44 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,51 @@
11
package git
22

3-
// FIXME:
3+
import (
4+
"errors"
5+
"github.com/AmadlaOrg/LibraryUtils/git/config"
6+
"github.com/go-git/go-git/v5"
7+
"github.com/stretchr/testify/assert"
8+
"testing"
9+
)
410

5-
// Test FetchRepo
6-
/*func TestFetchRepo(t *testing.T) {
7-
mockGit := new(MockGit)
8-
testURL := "https://github.com/git-fixtures/basic.git"
9-
testDest := "/tmp/repo"
10-
11-
mockGit.On("FetchRepo", testURL, testDest).Return(nil)
12-
13-
sgit := &SGit{}
14-
15-
err := sgit.FetchRepo(testURL, testDest)
16-
17-
// Assert that the method was called with correct params
18-
mockGit.AssertCalled(t, "FetchRepo", testURL, testDest)
19-
require.NoError(t, err)
20-
}*/
21-
22-
// Test FetchRepo with Error
23-
/*func TestFetchRepo_Error(t *testing.T) {
24-
mockGit := new(MockGit)
25-
testURL := "https://github.com/git-fixtures/basic.git"
26-
testDest := "/tmp/repo"
27-
28-
mockGit.On("FetchRepo", testURL, testDest).Return(errors.New("failed to clone repo"))
29-
30-
sgit := &SGit{}
31-
32-
err := sgit.FetchRepo(testURL, testDest)
33-
34-
// Assert that the method was called with correct params
35-
mockGit.AssertCalled(t, "FetchRepo", testURL, testDest)
36-
require.Error(t, err)
37-
require.EqualError(t, err, "failed to clone repo")
38-
}
39-
40-
// Test CommitHeadHash
41-
func TestCommitHeadHash(t *testing.T) {
42-
mockGit := new(MockGit)
43-
testRepoPath := "/tmp/repo"
44-
expectedHash := "abc123"
45-
46-
mockGit.On("CommitHeadHash", testRepoPath).Return(expectedHash, nil)
47-
48-
sgit := &SGit{}
49-
50-
hash, err := sgit.CommitHeadHash(testRepoPath)
51-
52-
// Assert that the method was called with correct params
53-
mockGit.AssertCalled(t, "CommitHeadHash", testRepoPath)
54-
require.NoError(t, err)
55-
require.Equal(t, expectedHash, hash)
56-
}
57-
58-
// Test CommitHeadHash with Error
59-
func TestCommitHeadHash_Error(t *testing.T) {
60-
mockGit := new(MockGit)
61-
testRepoPath := "/tmp/repo"
62-
63-
mockGit.On("CommitHeadHash", testRepoPath).Return("", errors.New("failed to get commit hash"))
64-
65-
sgit := &SGit{}
66-
67-
hash, err := sgit.CommitHeadHash(testRepoPath)
68-
69-
// Assert that the method was called with correct params
70-
mockGit.AssertCalled(t, "CommitHeadHash", testRepoPath)
71-
require.Error(t, err)
72-
require.Equal(t, "", hash)
73-
require.EqualError(t, err, "failed to get commit hash")
74-
}
75-
76-
// Test CheckoutTag
7711
func TestCheckoutTag(t *testing.T) {
78-
mockGit := new(MockGit)
79-
testRepoPath := "/tmp/repo"
80-
testTag := "v1.0.0"
81-
82-
mockGit.On("CheckoutTag", testRepoPath, testTag).Return(nil)
83-
84-
sgit := &SGit{}
85-
86-
err := sgit.CheckoutTag(testRepoPath, testTag)
87-
88-
// Assert that the method was called with correct params
89-
mockGit.AssertCalled(t, "CheckoutTag", testRepoPath, testTag)
90-
require.NoError(t, err)
91-
}
92-
93-
// Test CheckoutTag with Error
94-
func TestCheckoutTag_Error(t *testing.T) {
95-
mockGit := new(MockGit)
96-
testRepoPath := "/tmp/repo"
97-
testTag := "v1.0.0"
98-
99-
mockGit.On("CheckoutTag", testRepoPath, testTag).Return(errors.New("failed to checkout tag"))
100-
101-
sgit := &SGit{}
102-
103-
err := sgit.CheckoutTag(testRepoPath, testTag)
104-
105-
// Assert that the method was called with correct params
106-
mockGit.AssertCalled(t, "CheckoutTag", testRepoPath, testTag)
107-
require.Error(t, err)
108-
require.EqualError(t, err, "failed to checkout tag")
109-
}*/
110-
111-
// TODO:
112-
/*func TestCheckoutTag(t *testing.T) {
11312
tests := []struct {
11413
name string
11514
inputTagName string
116-
internalGitPlainOpen func(path string) (*git.Repository, error)
15+
internalGitPlainOpen func(path string) (IGoGitRepository, error)
11716
expectedError error
11817
hasError bool
11918
}{
12019
{
121-
name: "tag exists",
20+
name: "Error: git.PlainOpen fails",
12221
inputTagName: "v1.0.0",
123-
internalGitPlainOpen: func(path string) (*git.Repository, error) {
124-
125-
return git.PlainOpen(path)
22+
internalGitPlainOpen: func(path string) (IGoGitRepository, error) {
23+
return &git.Repository{}, errors.New("some error (git.PlainOpen)")
24+
},
25+
expectedError: errors.New("some error (git.PlainOpen)"),
26+
hasError: true,
27+
},
28+
{
29+
name: "Error: repo.Worktree fails",
30+
inputTagName: "v1.0.0",
31+
internalGitPlainOpen: func(path string) (IGoGitRepository, error) {
32+
mockGoGitRepository := NewMockGoGitRepository(t)
33+
mockGoGitRepository.EXPECT().Worktree().Return(nil, errors.New("some error (repo.Worktree)"))
34+
return mockGoGitRepository, nil
12635
},
127-
hasError: false,
36+
expectedError: errors.New("some error (repo.Worktree)"),
37+
hasError: true,
38+
},
39+
{
40+
name: "Error: repo.Worktree fails",
41+
inputTagName: "v1.0.0",
42+
internalGitPlainOpen: func(path string) (IGoGitRepository, error) {
43+
mockGoGitRepository := NewMockGoGitRepository(t)
44+
mockGoGitRepository.EXPECT().Worktree().Return(nil, errors.New("some error (repo.Worktree)"))
45+
return mockGoGitRepository, nil
46+
},
47+
expectedError: errors.New("some error (repo.Worktree)"),
48+
hasError: true,
12849
},
12950
}
13051

@@ -134,6 +55,14 @@ func TestCheckoutTag_Error(t *testing.T) {
13455
defer func() { gitPlainOpen = originalGitPlainOpen }()
13556
gitPlainOpen = tt.internalGitPlainOpen
13657

58+
gitService := NewGitService("mock_repo_url", "mock_repo_local_path", &config.Config{})
59+
err := gitService.CheckoutTag(tt.inputTagName)
60+
if tt.hasError {
61+
assert.Error(t, err)
62+
assert.EqualError(t, tt.expectedError, err.Error())
63+
} else {
64+
assert.NoError(t, err)
65+
}
13766
})
13867
}
139-
}*/
68+
}

git/interface.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,25 @@ type IGoGitRepository interface {
5353
RepackObjects(cfg *git.RepackConfig) (err error)
5454
Merge(ref plumbing.Reference, opts git.MergeOptions) error
5555
}
56+
57+
type IGoGitWorktree interface {
58+
Pull(o *git.PullOptions) error
59+
PullContext(ctx context.Context, o *git.PullOptions) error
60+
Checkout(opts *git.CheckoutOptions) error
61+
ResetSparsely(opts *git.ResetOptions, dirs []string) error
62+
Restore(o *git.RestoreOptions) error
63+
Reset(opts *git.ResetOptions) error
64+
Submodule(name string) (*git.Submodule, error)
65+
Submodules() (git.Submodules, error)
66+
Clean(opts *git.CleanOptions) error
67+
Grep(opts *git.GrepOptions) ([]git.GrepResult, error)
68+
Status() (git.Status, error)
69+
StatusWithOptions(o git.StatusOptions) (git.Status, error)
70+
Add(path string) (plumbing.Hash, error)
71+
AddWithOptions(opts *git.AddOptions) error
72+
AddGlob(pattern string) error
73+
Remove(path string) (plumbing.Hash, error)
74+
RemoveGlob(pattern string) error
75+
Move(from, to string) (plumbing.Hash, error)
76+
Commit(msg string, opts *git.CommitOptions) (plumbing.Hash, error)
77+
}

0 commit comments

Comments
 (0)