Skip to content

Commit

Permalink
Refactor the setup backup directory implementation (#82)
Browse files Browse the repository at this point in the history
Perform some basic refactoring, add a couple more test cases and rearrange the test cases.
  • Loading branch information
amitsaha authored Oct 23, 2022
1 parent 98ef835 commit 1b5f94d
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 65 deletions.
35 changes: 24 additions & 11 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,37 @@ func backUp(backupDir string, repo *Repository, bare bool, wg *sync.WaitGroup) (
return stdoutStderr, err
}

func setupBackupDir(backupDir string, gitHost string) string {
if len(backupDir) == 0 {
func setupBackupDir(backupDir, service, githostURL *string) string {
var gitHost, backupPath string
var err error

if githostURL != nil {
u, err := url.Parse(*githostURL)
if err != nil {
panic(err)
}
gitHost = u.Host
} else {
gitHost = knownServices[*service]
}

if backupDir == nil {
homeDir, err := homedir.Dir()
if err == nil {
backupDir = path.Join(homeDir, ".gitbackup", gitHost)
backupPath = path.Join(homeDir, ".gitbackup", gitHost)
} else {
log.Fatal("Could not determine home directory and backup directory not specified")
}
} else {
backupDir = path.Join(backupDir, gitHost)
backupPath = path.Join(*backupDir, gitHost)
}
_, err := appFS.Stat(backupDir)
err = createBackupRootDirIfRequired(backupPath)
if err != nil {
log.Printf("%s doesn't exist, creating it\n", backupDir)
err := appFS.MkdirAll(backupDir, 0771)
if err != nil {
log.Fatal(err)
}
log.Fatal(err)
}
return backupDir
return backupPath
}

func createBackupRootDirIfRequired(backupPath string) error {
return appFS.MkdirAll(backupPath, 0771)
}
95 changes: 83 additions & 12 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import (
"github.com/spf13/afero"
)

func setupBackupDirTests() {
os.Setenv("HOME", "/home/fakeuser")
os.Setenv("home", "/home/fakeuser")
appFS = afero.NewMemMapFs()
}

func teardownBackupDirTests() {
os.Unsetenv("HOME")
os.Unsetenv("home")
}

func fakePullCommand(command string, args ...string) (cmd *exec.Cmd) {
cs := []string{"-test.run=TestHelperPullProcess", "--", command}
cs = append(cs, args...)
Expand Down Expand Up @@ -138,19 +149,79 @@ func TestHelperRemoteUpdateProcess(t *testing.T) {
}

func TestSetupBackupDir(t *testing.T) {
appFS = afero.NewMemMapFs()
backupdir := setupBackupDir("/tmp", "github.com")
if backupdir != "/tmp/github.com" {
t.Errorf("Expected /tmp/github.com, Got %v", backupdir)
}

backupdir = setupBackupDir("/tmp", "company.github.com")
if backupdir != "/tmp/company.github.com" {
t.Errorf("Expected /tmp/company.github.com, Got %v", backupdir)
setupBackupDirTests()
defer teardownBackupDirTests()

backupRoot := "/my/backup/root"

serviceGithubCustomUrl := "https://company.github.com"
serviceGitlabCustomUrl := "https://company.gitlab.com"

var testConfigs = []struct {
backupRootDir *string
gitService string
gitServiceUrl *string
wantBackupPath string
}{
{
nil,
"github",
nil,
"/home/fakeuser/.gitbackup/github.com",
},
{
&backupRoot,
"github",
nil,
"/my/backup/root/github.com",
},
{
&backupRoot,
"github",
&serviceGithubCustomUrl,
"/my/backup/root/company.github.com",
},
{
nil,
"gitlab",
nil,
"/home/fakeuser/.gitbackup/gitlab.com",
},

{
&backupRoot,
"gitlab",
nil,
"/my/backup/root/gitlab.com",
},
{
&backupRoot,
"gitlab",
&serviceGitlabCustomUrl,
"/my/backup/root/company.gitlab.com",
},
{
&backupRoot,
"bitbucket",
nil,
"/my/backup/root/bitbucket.org",
},
{
nil,
"bitbucket",
nil,
"/home/fakeuser/.gitbackup/bitbucket.org",
},
}

backupdir = setupBackupDir("/tmp", "gitlab.com")
if backupdir != "/tmp/gitlab.com" {
t.Errorf("Expected /tmp/gitlab.com, Got %v", backupdir)
for _, tc := range testConfigs {
backupdir := setupBackupDir(tc.backupRootDir, &tc.gitService, tc.gitServiceUrl)
if backupdir != tc.wantBackupPath {
t.Errorf("Expected %s, Got %s", tc.wantBackupPath, backupdir)
}
_, err := appFS.Stat(backupdir)
if err != nil {
t.Error(err)
}
}
}
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

func TestNewClient(t *testing.T) {
setup()
defer teardown()
setupRepositoryTests()
defer teardownRepositoryTests()

customGitHost, _ := url.Parse("https://git.mycompany.com")
// http://stackoverflow.com/questions/23051339/how-to-avoid-end-of-url-slash-being-removed-when-resolvereference-in-go
Expand Down
31 changes: 8 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"log"
"net/url"
"sync"
"time"

Expand All @@ -23,22 +22,19 @@ var useHTTPSClone *bool
var ignorePrivate *bool
var gitHostUsername string

func main() {
// The services we know of and their default public host names
var knownServices = map[string]string{
"github": "github.com",
"gitlab": "gitlab.com",
"bitbucket": "bitbucket.org",
}

// Git host
var gitHost string
func main() {

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

// The services we know of and their default public host names
knownServices := map[string]string{
"github": "github.com",
"gitlab": "gitlab.com",
"bitbucket": "bitbucket.org",
}

// Generic flags
service := flag.String("service", "", "Git Hosted Service Name (github/gitlab/bitbucket)")
githostURL := flag.String("githost.url", "", "DNS of the custom Git host")
Expand Down Expand Up @@ -71,18 +67,7 @@ func main() {
log.Fatal("Please specify a valid gitlab project membership - all/owner/member")
}

if len(*githostURL) != 0 {
u, err := url.Parse(*githostURL)
if err != nil {
panic(err)
}
gitHost = u.Host
} else {
gitHost = knownServices[*service]
}

*backupDir = setupBackupDir(*backupDir, gitHost)

*backupDir = setupBackupDir(backupDir, service, githostURL)
client := newClient(*service, *githostURL)

if *githubListUserMigrations {
Expand Down
33 changes: 16 additions & 17 deletions repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ var (
server *httptest.Server
)

func setup() {
func setupRepositoryTests() {
os.Setenv("GITHUB_TOKEN", "$$$randome")
os.Setenv("GITLAB_TOKEN", "$$$randome")
os.Setenv("BITBUCKET_USERNAME", "bbuser")
os.Setenv("BITBUCKET_PASSWORD", "$$$randomp")

// test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
Expand All @@ -52,7 +51,7 @@ func setup() {
BitbucketClient.SetApiBaseURL(url.String())
}

func teardown() {
func teardownRepositoryTests() {
os.Unsetenv("GITHUB_TOKEN")
os.Unsetenv("GITLAB_TOKEN")
os.Unsetenv("BITBUCKET_USERNAME")
Expand All @@ -61,8 +60,8 @@ func teardown() {
}

func TestGetPublicGitHubRepositories(t *testing.T) {
setup()
defer teardown()
setupRepositoryTests()
defer teardownRepositoryTests()

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

func TestGetPrivateGitHubRepositories(t *testing.T) {
setup()
defer teardown()
setupRepositoryTests()
defer teardownRepositoryTests()

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

func TestGetStarredGitHubRepositories(t *testing.T) {
setup()
defer teardown()
setupRepositoryTests()
defer teardownRepositoryTests()

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

func TestGetGitLabRepositories(t *testing.T) {
setup()
defer teardown()
setupRepositoryTests()
defer teardownRepositoryTests()

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

func TestGetGitLabPrivateRepositories(t *testing.T) {
setup()
defer teardown()
setupRepositoryTests()
defer teardownRepositoryTests()

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

func TestGetStarredGitLabRepositories(t *testing.T) {
setup()
defer teardown()
setupRepositoryTests()
defer teardownRepositoryTests()

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

func TestGetBitbucketRepositories(t *testing.T) {
setup()
defer teardown()
setupRepositoryTests()
defer teardownRepositoryTests()

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

0 comments on commit 1b5f94d

Please sign in to comment.