Skip to content

Commit 4a9d14e

Browse files
committed
Updated libraries and added unit tests.
1 parent 9890f03 commit 4a9d14e

File tree

6 files changed

+138
-43
lines changed

6 files changed

+138
-43
lines changed

git/git_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,6 @@ func TestCheckoutTag(t *testing.T) {
3636
expectedError: errors.New("some error (repo.Worktree)"),
3737
hasError: true,
3838
},
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,
49-
},
5039
}
5140

5241
for _, tt := range tests {

git/remote/remote.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/go-git/go-git/v5"
77
"github.com/go-git/go-git/v5/config"
88
"github.com/go-git/go-git/v5/plumbing"
9+
"github.com/go-git/go-git/v5/storage"
910
"github.com/go-git/go-git/v5/storage/memory"
1011
)
1112

@@ -21,18 +22,19 @@ type SRemote struct {
2122
}
2223

2324
var (
24-
gitNewRemote = git.NewRemote
25-
memoryNewStorage = memory.NewStorage
25+
gitNewRemote = func(s storage.Storer, c *config.RemoteConfig) IGoGitRemote {
26+
return git.NewRemote(s, c)
27+
}
2628
)
2729

2830
// Tags returns a list of tags for the repository at the specified URL.
2931
func (s *SRemote) Tags() ([]string, error) {
30-
rem := gitNewRemote(memoryNewStorage(), &config.RemoteConfig{
32+
r := gitNewRemote(memory.NewStorage(), &config.RemoteConfig{
3133
Name: "origin",
3234
URLs: []string{s.url},
3335
})
3436

35-
refs, err := rem.List(&git.ListOptions{
37+
refs, err := r.List(&git.ListOptions{
3638
Auth: s.config.Auth,
3739
InsecureSkipTLS: *s.config.InsecureSkipTLS,
3840
CABundle: s.config.CABundle,
@@ -56,13 +58,13 @@ func (s *SRemote) Tags() ([]string, error) {
5658

5759
// CommitHeadHash retrieves the hash of the most recent commit
5860
func (s *SRemote) CommitHeadHash() (string, error) {
59-
rem := gitNewRemote(memoryNewStorage(), &config.RemoteConfig{
61+
r := gitNewRemote(memory.NewStorage(), &config.RemoteConfig{
6062
Name: "origin",
6163
URLs: []string{s.url},
6264
})
6365

6466
// List all references from the remote repository
65-
refs, err := rem.List(&git.ListOptions{
67+
refs, err := r.List(&git.ListOptions{
6668
Auth: s.config.Auth,
6769
InsecureSkipTLS: *s.config.InsecureSkipTLS,
6870
CABundle: s.config.CABundle,
Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package remote
22

3-
// FIXME: Maybe create separate "static" repos
4-
/*func Test_integration_Tags(t *testing.T) {
5-
gitRemoteService := NewGitRemoteService()
6-
tags, err := gitRemoteService.Tags("https://github.com/AmadlaOrg/QAFixturesEntityMultipleTagVersion")
3+
import (
4+
utilGitConfig "github.com/AmadlaOrg/LibraryUtils/git/config"
5+
"github.com/stretchr/testify/assert"
6+
"reflect"
7+
"sort"
8+
"testing"
9+
)
10+
11+
func Test_integration_Tags(t *testing.T) {
12+
gitRemoteService := NewGitRemoteService(
13+
"https://github.com/AmadlaOrg/QAFixturesEntityMultipleTagVersion",
14+
&utilGitConfig.Config{})
15+
tags, err := gitRemoteService.Tags()
716
if err != nil {
817
t.Errorf("Tags returned an error: %s", err)
918
}
@@ -13,6 +22,8 @@ package remote
1322
"v1.0.0-alpha.2",
1423
"v1.0.0-beta.1",
1524
"v2.0.0",
25+
"v2.0.1",
26+
"v2.1.0",
1627
}
1728

1829
sort.Strings(tags)
@@ -26,15 +37,16 @@ package remote
2637
for _, tag := range tags {
2738
t.Logf("Retrieved tag: %s", tag)
2839
}
29-
}*/
40+
}
3041

31-
// FIXME: Maybe create separate "static" repos
32-
/*func Test_integration_CommitHeadHash(t *testing.T) {
33-
gitRemoteService := NewGitRemoteService()
34-
hash, err := gitRemoteService.CommitHeadHash("https://github.com/AmadlaOrg/QAFixturesEntityMultipleTagVersion")
42+
func Test_integration_CommitHeadHash(t *testing.T) {
43+
gitRemoteService := NewGitRemoteService(
44+
"https://github.com/AmadlaOrg/QAFixturesEntityMultipleTagVersion",
45+
&utilGitConfig.Config{})
46+
hash, err := gitRemoteService.CommitHeadHash()
3547
if err != nil {
3648
t.Errorf("CommitHeadHash returned an error: %s", err)
3749
}
3850

39-
assert.Equal(t, hash, "c351cf75321ae8a7676b8bef6837b67a60cabdbc")
40-
}*/
51+
assert.Equal(t, hash, "8be468562e86eafd0841fe9cfb4a642984c72b87")
52+
}

git/remote/remote_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,105 @@
11
package remote
2+
3+
import (
4+
"errors"
5+
utilGitConfig "github.com/AmadlaOrg/LibraryUtils/git/config"
6+
"github.com/go-git/go-git/v5/config"
7+
"github.com/go-git/go-git/v5/plumbing"
8+
"github.com/go-git/go-git/v5/storage"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/mock"
11+
"testing"
12+
)
13+
14+
func TestTags(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
internalGitNewRemote func(s storage.Storer, c *config.RemoteConfig) IGoGitRemote
18+
expectedError error
19+
hasError bool
20+
}{
21+
{
22+
name: "Success",
23+
internalGitNewRemote: func(s storage.Storer, c *config.RemoteConfig) IGoGitRemote {
24+
mockGoGitRemote := NewMockGoGitRemote(t)
25+
mockGoGitRemote.EXPECT().List(mock.Anything).Return([]*plumbing.Reference{
26+
{},
27+
}, nil)
28+
return mockGoGitRemote
29+
},
30+
hasError: false,
31+
},
32+
//
33+
// Error
34+
//
35+
{
36+
name: "Error: fails at r.List",
37+
internalGitNewRemote: func(s storage.Storer, c *config.RemoteConfig) IGoGitRemote {
38+
mockGoGitRemote := NewMockGoGitRemote(t)
39+
mockGoGitRemote.EXPECT().List(mock.Anything).Return(nil, errors.New("some error (r.List)"))
40+
return mockGoGitRemote
41+
},
42+
expectedError: errors.New("some error (r.List)"),
43+
hasError: true,
44+
},
45+
}
46+
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
originalGitNewRemote := gitNewRemote
50+
defer func() { gitNewRemote = originalGitNewRemote }()
51+
gitNewRemote = tt.internalGitNewRemote
52+
53+
gitRemoteService := NewGitRemoteService("mock_repo_url", &utilGitConfig.Config{})
54+
tags, err := gitRemoteService.Tags()
55+
if tt.hasError {
56+
assert.Error(t, err)
57+
assert.EqualError(t, err, tt.expectedError.Error())
58+
} else {
59+
assert.NoError(t, err)
60+
}
61+
assert.Equal(t, 0, len(tags))
62+
})
63+
}
64+
}
65+
66+
func TestCommitHeadHash(t *testing.T) {
67+
tests := []struct {
68+
name string
69+
internalGitNewRemote func(s storage.Storer, c *config.RemoteConfig) IGoGitRemote
70+
expectedError error
71+
hasError bool
72+
}{
73+
//
74+
// Error
75+
//
76+
{
77+
name: "Error: fails at r.List",
78+
internalGitNewRemote: func(s storage.Storer, c *config.RemoteConfig) IGoGitRemote {
79+
mockGoGitRemote := NewMockGoGitRemote(t)
80+
mockGoGitRemote.EXPECT().List(mock.Anything).Return(nil, errors.New("some error (r.List)"))
81+
return mockGoGitRemote
82+
},
83+
expectedError: errors.New("some error (r.List)"),
84+
hasError: true,
85+
},
86+
}
87+
88+
for _, tt := range tests {
89+
t.Run(tt.name, func(t *testing.T) {
90+
originalGitNewRemote := gitNewRemote
91+
defer func() { gitNewRemote = originalGitNewRemote }()
92+
gitNewRemote = tt.internalGitNewRemote
93+
94+
gitRemoteService := NewGitRemoteService("mock_repo_url", &utilGitConfig.Config{})
95+
tags, err := gitRemoteService.CommitHeadHash()
96+
if tt.hasError {
97+
assert.Error(t, err)
98+
assert.EqualError(t, err, tt.expectedError.Error())
99+
} else {
100+
assert.NoError(t, err)
101+
}
102+
assert.Equal(t, 0, len(tags))
103+
})
104+
}
105+
}

go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@ require (
2020
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
2121
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
2222
github.com/kevinburke/ssh_config v1.2.0 // indirect
23-
github.com/mmcloughlin/avo v0.6.0 // indirect
24-
github.com/pjbgf/sha1cd v0.3.1 // indirect
23+
github.com/pjbgf/sha1cd v0.3.2 // indirect
2524
github.com/pmezard/go-difflib v1.0.0 // indirect
2625
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
2726
github.com/skeema/knownhosts v1.3.0 // indirect
2827
github.com/stretchr/objx v0.5.2 // indirect
2928
github.com/xanzy/ssh-agent v0.3.3 // indirect
3029
golang.org/x/crypto v0.32.0 // indirect
31-
golang.org/x/mod v0.22.0 // indirect
3230
golang.org/x/net v0.34.0 // indirect
33-
golang.org/x/sync v0.10.0 // indirect
3431
golang.org/x/sys v0.29.0 // indirect
35-
golang.org/x/tools v0.29.0 // indirect
3632
gopkg.in/warnings.v0 v0.1.2 // indirect
3733
gopkg.in/yaml.v3 v3.0.1 // indirect
3834
)

go.sum

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4545
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
4646
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4747
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
48-
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
49-
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
5048
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
5149
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
52-
github.com/pjbgf/sha1cd v0.3.1 h1:Dh2GYdpJnO84lIw0LJwTFXjcNbasP/bklicSznyAaPI=
53-
github.com/pjbgf/sha1cd v0.3.1/go.mod h1:Y8t7jSB/dEI/lQE04A1HVKteqjj9bX5O4+Cex0TCu8s=
50+
github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4=
51+
github.com/pjbgf/sha1cd v0.3.2/go.mod h1:zQWigSxVmsHEZow5qaLtPYxpcKMMQpa09ixqBxuCS6A=
5452
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
5553
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
5654
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -76,13 +74,9 @@ golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
7674
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
7775
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
7876
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
79-
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
80-
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
8177
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
8278
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
8379
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
84-
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
85-
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
8680
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8781
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8882
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -98,8 +92,6 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
9892
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
9993
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
10094
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
101-
golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE=
102-
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588=
10395
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10496
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10597
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=

0 commit comments

Comments
 (0)