Skip to content

Commit 1b5f94d

Browse files
authored
Refactor the setup backup directory implementation (#82)
Perform some basic refactoring, add a couple more test cases and rearrange the test cases.
1 parent 98ef835 commit 1b5f94d

File tree

5 files changed

+133
-65
lines changed

5 files changed

+133
-65
lines changed

backup.go

+24-11
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,37 @@ func backUp(backupDir string, repo *Repository, bare bool, wg *sync.WaitGroup) (
7171
return stdoutStderr, err
7272
}
7373

74-
func setupBackupDir(backupDir string, gitHost string) string {
75-
if len(backupDir) == 0 {
74+
func setupBackupDir(backupDir, service, githostURL *string) string {
75+
var gitHost, backupPath string
76+
var err error
77+
78+
if githostURL != nil {
79+
u, err := url.Parse(*githostURL)
80+
if err != nil {
81+
panic(err)
82+
}
83+
gitHost = u.Host
84+
} else {
85+
gitHost = knownServices[*service]
86+
}
87+
88+
if backupDir == nil {
7689
homeDir, err := homedir.Dir()
7790
if err == nil {
78-
backupDir = path.Join(homeDir, ".gitbackup", gitHost)
91+
backupPath = path.Join(homeDir, ".gitbackup", gitHost)
7992
} else {
8093
log.Fatal("Could not determine home directory and backup directory not specified")
8194
}
8295
} else {
83-
backupDir = path.Join(backupDir, gitHost)
96+
backupPath = path.Join(*backupDir, gitHost)
8497
}
85-
_, err := appFS.Stat(backupDir)
98+
err = createBackupRootDirIfRequired(backupPath)
8699
if err != nil {
87-
log.Printf("%s doesn't exist, creating it\n", backupDir)
88-
err := appFS.MkdirAll(backupDir, 0771)
89-
if err != nil {
90-
log.Fatal(err)
91-
}
100+
log.Fatal(err)
92101
}
93-
return backupDir
102+
return backupPath
103+
}
104+
105+
func createBackupRootDirIfRequired(backupPath string) error {
106+
return appFS.MkdirAll(backupPath, 0771)
94107
}

backup_test.go

+83-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ import (
1111
"github.com/spf13/afero"
1212
)
1313

14+
func setupBackupDirTests() {
15+
os.Setenv("HOME", "/home/fakeuser")
16+
os.Setenv("home", "/home/fakeuser")
17+
appFS = afero.NewMemMapFs()
18+
}
19+
20+
func teardownBackupDirTests() {
21+
os.Unsetenv("HOME")
22+
os.Unsetenv("home")
23+
}
24+
1425
func fakePullCommand(command string, args ...string) (cmd *exec.Cmd) {
1526
cs := []string{"-test.run=TestHelperPullProcess", "--", command}
1627
cs = append(cs, args...)
@@ -138,19 +149,79 @@ func TestHelperRemoteUpdateProcess(t *testing.T) {
138149
}
139150

140151
func TestSetupBackupDir(t *testing.T) {
141-
appFS = afero.NewMemMapFs()
142-
backupdir := setupBackupDir("/tmp", "github.com")
143-
if backupdir != "/tmp/github.com" {
144-
t.Errorf("Expected /tmp/github.com, Got %v", backupdir)
145-
}
146-
147-
backupdir = setupBackupDir("/tmp", "company.github.com")
148-
if backupdir != "/tmp/company.github.com" {
149-
t.Errorf("Expected /tmp/company.github.com, Got %v", backupdir)
152+
setupBackupDirTests()
153+
defer teardownBackupDirTests()
154+
155+
backupRoot := "/my/backup/root"
156+
157+
serviceGithubCustomUrl := "https://company.github.com"
158+
serviceGitlabCustomUrl := "https://company.gitlab.com"
159+
160+
var testConfigs = []struct {
161+
backupRootDir *string
162+
gitService string
163+
gitServiceUrl *string
164+
wantBackupPath string
165+
}{
166+
{
167+
nil,
168+
"github",
169+
nil,
170+
"/home/fakeuser/.gitbackup/github.com",
171+
},
172+
{
173+
&backupRoot,
174+
"github",
175+
nil,
176+
"/my/backup/root/github.com",
177+
},
178+
{
179+
&backupRoot,
180+
"github",
181+
&serviceGithubCustomUrl,
182+
"/my/backup/root/company.github.com",
183+
},
184+
{
185+
nil,
186+
"gitlab",
187+
nil,
188+
"/home/fakeuser/.gitbackup/gitlab.com",
189+
},
190+
191+
{
192+
&backupRoot,
193+
"gitlab",
194+
nil,
195+
"/my/backup/root/gitlab.com",
196+
},
197+
{
198+
&backupRoot,
199+
"gitlab",
200+
&serviceGitlabCustomUrl,
201+
"/my/backup/root/company.gitlab.com",
202+
},
203+
{
204+
&backupRoot,
205+
"bitbucket",
206+
nil,
207+
"/my/backup/root/bitbucket.org",
208+
},
209+
{
210+
nil,
211+
"bitbucket",
212+
nil,
213+
"/home/fakeuser/.gitbackup/bitbucket.org",
214+
},
150215
}
151216

152-
backupdir = setupBackupDir("/tmp", "gitlab.com")
153-
if backupdir != "/tmp/gitlab.com" {
154-
t.Errorf("Expected /tmp/gitlab.com, Got %v", backupdir)
217+
for _, tc := range testConfigs {
218+
backupdir := setupBackupDir(tc.backupRootDir, &tc.gitService, tc.gitServiceUrl)
219+
if backupdir != tc.wantBackupPath {
220+
t.Errorf("Expected %s, Got %s", tc.wantBackupPath, backupdir)
221+
}
222+
_, err := appFS.Stat(backupdir)
223+
if err != nil {
224+
t.Error(err)
225+
}
155226
}
156227
}

client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
)
1111

1212
func TestNewClient(t *testing.T) {
13-
setup()
14-
defer teardown()
13+
setupRepositoryTests()
14+
defer teardownRepositoryTests()
1515

1616
customGitHost, _ := url.Parse("https://git.mycompany.com")
1717
// http://stackoverflow.com/questions/23051339/how-to-avoid-end-of-url-slash-being-removed-when-resolvereference-in-go

main.go

+8-23
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"flag"
66
"fmt"
77
"log"
8-
"net/url"
98
"sync"
109
"time"
1110

@@ -23,22 +22,19 @@ var useHTTPSClone *bool
2322
var ignorePrivate *bool
2423
var gitHostUsername string
2524

26-
func main() {
25+
// The services we know of and their default public host names
26+
var knownServices = map[string]string{
27+
"github": "github.com",
28+
"gitlab": "gitlab.com",
29+
"bitbucket": "bitbucket.org",
30+
}
2731

28-
// Git host
29-
var gitHost string
32+
func main() {
3033

3134
// Used for waiting for all the goroutines to finish before exiting
3235
var wg sync.WaitGroup
3336
defer wg.Wait()
3437

35-
// The services we know of and their default public host names
36-
knownServices := map[string]string{
37-
"github": "github.com",
38-
"gitlab": "gitlab.com",
39-
"bitbucket": "bitbucket.org",
40-
}
41-
4238
// Generic flags
4339
service := flag.String("service", "", "Git Hosted Service Name (github/gitlab/bitbucket)")
4440
githostURL := flag.String("githost.url", "", "DNS of the custom Git host")
@@ -71,18 +67,7 @@ func main() {
7167
log.Fatal("Please specify a valid gitlab project membership - all/owner/member")
7268
}
7369

74-
if len(*githostURL) != 0 {
75-
u, err := url.Parse(*githostURL)
76-
if err != nil {
77-
panic(err)
78-
}
79-
gitHost = u.Host
80-
} else {
81-
gitHost = knownServices[*service]
82-
}
83-
84-
*backupDir = setupBackupDir(*backupDir, gitHost)
85-
70+
*backupDir = setupBackupDir(backupDir, service, githostURL)
8671
client := newClient(*service, *githostURL)
8772

8873
if *githubListUserMigrations {

repositories_test.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ var (
2323
server *httptest.Server
2424
)
2525

26-
func setup() {
26+
func setupRepositoryTests() {
2727
os.Setenv("GITHUB_TOKEN", "$$$randome")
2828
os.Setenv("GITLAB_TOKEN", "$$$randome")
2929
os.Setenv("BITBUCKET_USERNAME", "bbuser")
3030
os.Setenv("BITBUCKET_PASSWORD", "$$$randomp")
31-
3231
// test server
3332
mux = http.NewServeMux()
3433
server = httptest.NewServer(mux)
@@ -52,7 +51,7 @@ func setup() {
5251
BitbucketClient.SetApiBaseURL(url.String())
5352
}
5453

55-
func teardown() {
54+
func teardownRepositoryTests() {
5655
os.Unsetenv("GITHUB_TOKEN")
5756
os.Unsetenv("GITLAB_TOKEN")
5857
os.Unsetenv("BITBUCKET_USERNAME")
@@ -61,8 +60,8 @@ func teardown() {
6160
}
6261

6362
func TestGetPublicGitHubRepositories(t *testing.T) {
64-
setup()
65-
defer teardown()
63+
setupRepositoryTests()
64+
defer teardownRepositoryTests()
6665

6766
mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) {
6867
fmt.Fprint(w, `[{"full_name": "test/r1", "id":1, "ssh_url": "https://github.com/u/r1", "name": "r1", "private": false, "fork": false}]`)
@@ -80,8 +79,8 @@ func TestGetPublicGitHubRepositories(t *testing.T) {
8079
}
8180

8281
func TestGetPrivateGitHubRepositories(t *testing.T) {
83-
setup()
84-
defer teardown()
82+
setupRepositoryTests()
83+
defer teardownRepositoryTests()
8584

8685
mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) {
8786
fmt.Fprint(w, `[{"full_name": "test/r1", "id":1, "ssh_url": "https://github.com/u/r1", "name": "r1", "private": true, "fork": false}]`)
@@ -99,8 +98,8 @@ func TestGetPrivateGitHubRepositories(t *testing.T) {
9998
}
10099

101100
func TestGetStarredGitHubRepositories(t *testing.T) {
102-
setup()
103-
defer teardown()
101+
setupRepositoryTests()
102+
defer teardownRepositoryTests()
104103

105104
mux.HandleFunc("/user/starred", func(w http.ResponseWriter, r *http.Request) {
106105
fmt.Fprint(w, `[{"repo":{"full_name": "test/r1", "id":1, "ssh_url": "https://github.com/u/r1", "name": "r1", "private": true, "fork": false}}]`)
@@ -118,8 +117,8 @@ func TestGetStarredGitHubRepositories(t *testing.T) {
118117
}
119118

120119
func TestGetGitLabRepositories(t *testing.T) {
121-
setup()
122-
defer teardown()
120+
setupRepositoryTests()
121+
defer teardownRepositoryTests()
123122

124123
mux.HandleFunc("/api/v4/projects", func(w http.ResponseWriter, r *http.Request) {
125124
fmt.Fprint(w, `[{"path_with_namespace": "test/r1", "id":1, "ssh_url_to_repo": "https://gitlab.com/u/r1", "name": "r1"}]`)
@@ -139,8 +138,8 @@ func TestGetGitLabRepositories(t *testing.T) {
139138
}
140139

141140
func TestGetGitLabPrivateRepositories(t *testing.T) {
142-
setup()
143-
defer teardown()
141+
setupRepositoryTests()
142+
defer teardownRepositoryTests()
144143

145144
mux.HandleFunc("/api/v4/projects", func(w http.ResponseWriter,
146145
r *http.Request) {
@@ -165,8 +164,8 @@ func TestGetGitLabPrivateRepositories(t *testing.T) {
165164
}
166165

167166
func TestGetStarredGitLabRepositories(t *testing.T) {
168-
setup()
169-
defer teardown()
167+
setupRepositoryTests()
168+
defer teardownRepositoryTests()
170169

171170
mux.HandleFunc("/api/v4/projects", func(w http.ResponseWriter, r *http.Request) {
172171
log.Printf("%#v\n", r.URL.Query())
@@ -195,8 +194,8 @@ func TestGetStarredGitLabRepositories(t *testing.T) {
195194
}
196195

197196
func TestGetBitbucketRepositories(t *testing.T) {
198-
setup()
199-
defer teardown()
197+
setupRepositoryTests()
198+
defer teardownRepositoryTests()
200199

201200
mux.HandleFunc("/workspaces", func(w http.ResponseWriter, r *http.Request) {
202201
fmt.Fprint(w, `{"pagelen": 10, "page": 1, "size": 1, "values": [{"slug": "abc"}]}`)

0 commit comments

Comments
 (0)