Skip to content

Commit b68ef31

Browse files
Copilotamitsaha
andcommitted
Add tests for helper functions and improve error handling
- Add comprehensive tests for extractBitbucketCloneURLs function - Add tests for buildRepoPaths function - Replace log.Fatal with error return in handleGitRepositoryClone - Set global variables from config to avoid nil pointer dereferences - Add function documentation for handleGitRepositoryClone Co-authored-by: amitsaha <512598+amitsaha@users.noreply.github.com>
1 parent 965a138 commit b68ef31

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

bitbucket_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestExtractBitbucketCloneURLs(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
links map[string]interface{}
12+
wantHTTPSURL string
13+
wantSSHURL string
14+
}{
15+
{
16+
name: "valid links with both HTTPS and SSH",
17+
links: map[string]interface{}{
18+
"clone": []interface{}{
19+
map[string]interface{}{
20+
"name": "https",
21+
"href": "https://bitbucket.org/user/repo.git",
22+
},
23+
map[string]interface{}{
24+
"name": "ssh",
25+
"href": "git@bitbucket.org:user/repo.git",
26+
},
27+
},
28+
},
29+
wantHTTPSURL: "https://bitbucket.org/user/repo.git",
30+
wantSSHURL: "git@bitbucket.org:user/repo.git",
31+
},
32+
{
33+
name: "empty links",
34+
links: map[string]interface{}{
35+
"clone": []interface{}{},
36+
},
37+
wantHTTPSURL: "",
38+
wantSSHURL: "",
39+
},
40+
{
41+
name: "missing clone key",
42+
links: map[string]interface{}{},
43+
wantHTTPSURL: "",
44+
wantSSHURL: "",
45+
},
46+
{
47+
name: "only HTTPS URL",
48+
links: map[string]interface{}{
49+
"clone": []interface{}{
50+
map[string]interface{}{
51+
"name": "https",
52+
"href": "https://bitbucket.org/user/repo.git",
53+
},
54+
},
55+
},
56+
wantHTTPSURL: "https://bitbucket.org/user/repo.git",
57+
wantSSHURL: "",
58+
},
59+
}
60+
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
gotHTTPSURL, gotSSHURL := extractBitbucketCloneURLs(tt.links)
64+
if gotHTTPSURL != tt.wantHTTPSURL {
65+
t.Errorf("extractBitbucketCloneURLs() httpsURL = %v, want %v", gotHTTPSURL, tt.wantHTTPSURL)
66+
}
67+
if gotSSHURL != tt.wantSSHURL {
68+
t.Errorf("extractBitbucketCloneURLs() sshURL = %v, want %v", gotSSHURL, tt.wantSSHURL)
69+
}
70+
})
71+
}
72+
}
73+
74+
func TestBuildRepoPaths(t *testing.T) {
75+
repos := []*Repository{
76+
{Name: "repo1", Namespace: "user1"},
77+
{Name: "repo2", Namespace: "org1"},
78+
{Name: "repo3", Namespace: "user1"},
79+
}
80+
81+
expected := []string{"user1/repo1", "org1/repo2", "user1/repo3"}
82+
result := buildRepoPaths(repos)
83+
84+
if !reflect.DeepEqual(result, expected) {
85+
t.Errorf("buildRepoPaths() = %v, want %v", result, expected)
86+
}
87+
}
88+
89+
func TestBuildRepoPathsEmpty(t *testing.T) {
90+
repos := []*Repository{}
91+
result := buildRepoPaths(repos)
92+
93+
if len(result) != 0 {
94+
t.Errorf("buildRepoPaths() for empty input = %v, want empty slice", result)
95+
}
96+
}

git_repository_clone.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ import (
66
"sync"
77
)
88

9+
// handleGitRepositoryClone clones or updates all repositories for the configured service
910
func handleGitRepositoryClone(client interface{}, c *appConfig) error {
1011

1112
// Used for waiting for all the goroutines to finish before exiting
1213
var wg sync.WaitGroup
1314
defer wg.Wait()
1415

16+
// Set global variables used by helper functions
17+
useHTTPSClone = &c.useHTTPSClone
18+
ignorePrivate = &c.ignorePrivate
19+
1520
tokens := make(chan bool, MaxConcurrentClones)
1621
gitHostUsername = getUsername(client, c.service)
1722

18-
if len(gitHostUsername) == 0 && !*ignorePrivate && *useHTTPSClone {
19-
log.Fatal("Your Git host's username is needed for backing up private repositories via HTTPS")
23+
if len(gitHostUsername) == 0 && c.ignorePrivate && c.useHTTPSClone {
24+
return fmt.Errorf("your Git host's username is needed for backing up private repositories via HTTPS")
2025
}
2126

2227
repositories, err := getRepositories(

0 commit comments

Comments
 (0)