11package 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
7711func 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+ }
0 commit comments