Skip to content

Commit

Permalink
fix: initialisation of repos for mirroring and add tests (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
isindir authored Mar 13, 2022
1 parent a66afde commit 8db5b1c
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 6 deletions.
19 changes: 13 additions & 6 deletions gitget/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type RepoI interface {
EnsurePathExists()
GetCurrentBranch() string
GetRepoLocalName() string
GitCheckout(branch string)
GitCheckout(branch string) bool
GitPull()
GitStashPop() bool
GitStashSave() bool
Expand All @@ -127,6 +127,7 @@ type RepoI interface {
IsRefTag() bool
PathExists(path string) (bool, os.FileInfo)
PrepareForGet()
PrepareForMirror(pathPrefix string, mirrorRootURL string)
ProcessRepoBasedOnCleaness()
ProcessRepoBasedOnCurrentBranch()
ProcessSymlinks()
Expand Down Expand Up @@ -232,6 +233,7 @@ func (repo *Repo) PrepareForGet() {

// PrepareForMirror - set repository structure fields for mirror operation
func (repo *Repo) PrepareForMirror(pathPrefix string, mirrorRootURL string) {
repo.SetShellRunner(shellRunner)
repo.SetTempRepoPathForMirror(pathPrefix)
repo.SetDefaultRef()
repo.SetRepoLocalName()
Expand Down Expand Up @@ -358,16 +360,15 @@ func (repo *Repo) RemoveTargetDir(dotGit bool) {
}

func (repo *Repo) IsClean() bool {
res := true
_, err := (*repo.executor).ExecGitCommand([]string{"diff", "--quiet"}, nil, nil, repo.fullPath)
if err != nil {
res = false
return false
}
_, err = (*repo.executor).ExecGitCommand([]string{"diff", "--staged", "--quiet"}, nil, nil, repo.fullPath)
if err != nil {
res = false
return false
}
return res
return true
}

func (repo *Repo) IsCurrentBranchRef() bool {
Expand Down Expand Up @@ -474,17 +475,23 @@ func (repo *Repo) ProcessRepoBasedOnCleaness() {
}
}

func (repo *Repo) GitCheckout(branch string) {
func (repo *Repo) GitCheckout(branch string) bool {
log.Infof("%s: Checkout to '%s' branch in '%s'", repo.sha, colorHighlight.Sprintf(branch), repo.fullPath)
var serr bytes.Buffer
res := true

_, err := (*repo.executor).ExecGitCommand([]string{"checkout", branch}, nil, &serr, repo.fullPath)
if err != nil {
repo.status.Error = true
log.Warnf("%s: %v: %v", repo.sha, err, serr.String())
res = false
}

if repo.Ref != branch {
repo.status.NotOnRefBranch = true
}

return res
}

func (repo *Repo) ProcessRepoBasedOnCurrentBranch() {
Expand Down
221 changes: 221 additions & 0 deletions gitget/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func Test_Repo_IsRefTag(t *testing.T) {
})
}
}

func Test_Repo_IsRefBranch(t *testing.T) {
type testCase struct {
name string
Expand Down Expand Up @@ -252,6 +253,7 @@ func Test_Repo_GitStashPop(t *testing.T) {
})
}
}

func Test_Repo_GitStashSave(t *testing.T) {
type testCase struct {
name string
Expand Down Expand Up @@ -320,6 +322,7 @@ func Test_Repo_CloneMirror(t *testing.T) {
})
}
}

func Test_Repo_GetCurrentBranch(t *testing.T) {
type testCase struct {
name string
Expand Down Expand Up @@ -354,3 +357,221 @@ func Test_Repo_GetCurrentBranch(t *testing.T) {
})
}
}

func Test_Repo_IsClean(t *testing.T) {
type testCase struct {
name string
repo Repo
expectedResult bool
err1 error
err2 error
}

testCases := []testCase{
{name: "test 1: no error", expectedResult: true,
repo: Repo{fullPath: "cde_p"}, err1: nil, err2: nil},
{name: "test 2: err1 raised", expectedResult: false,
repo: Repo{fullPath: "cde_a"}, err1: fmt.Errorf("test error"), err2: nil},
{name: "test 3: err2 raised", expectedResult: false,
repo: Repo{fullPath: "cde_a"}, err1: nil, err2: fmt.Errorf("test error")},
{name: "test 3: err1 and err2 can be raised", expectedResult: false,
repo: Repo{fullPath: "cde_a"}, err1: fmt.Errorf("test error"), err2: fmt.Errorf("test error")},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockGitExec := new(mocks.ShellRunnerI)
exe := &exec.Cmd{}

mockGitExec.On(
"ExecGitCommand",
[]string{"diff", "--quiet"},
(*bytes.Buffer)(nil),
(*bytes.Buffer)(nil),
tc.repo.fullPath).Return(exe, tc.err1)
mockGitExec.On(
"ExecGitCommand",
[]string{"diff", "--staged", "--quiet"},
(*bytes.Buffer)(nil),
(*bytes.Buffer)(nil),
tc.repo.fullPath).Return(exe, tc.err2)

tc.repo.SetShellRunner(mockGitExec)

result := tc.repo.IsClean()
assert.Equal(t, tc.expectedResult, result)
})
}
}

func Test_Repo_ShallowClone(t *testing.T) {
type testCase struct {
name string
repo Repo
expectedResult bool
returnError error
}

testCases := []testCase{
{name: "test 1", expectedResult: true, repo: Repo{Ref: "abc", URL: "cde", fullPath: "cde_a"}, returnError: nil},
{name: "test 2", expectedResult: false, repo: Repo{Ref: "abc", URL: "cde", fullPath: "cde_a"}, returnError: fmt.Errorf("test error")},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockGitExec := new(mocks.ShellRunnerI)
exe := &exec.Cmd{}
serr := &bytes.Buffer{}

mockGitExec.On(
"ExecGitCommand",
[]string{"clone", "--depth", "1", "--branch", tc.repo.Ref, tc.repo.URL, tc.repo.fullPath},
(*bytes.Buffer)(nil),
serr,
"").Return(exe, tc.returnError)

tc.repo.SetShellRunner(mockGitExec)

result := tc.repo.ShallowClone()
assert.Equal(t, tc.expectedResult, result)
})
}
}

func Test_Repo_Clone(t *testing.T) {
type testCase struct {
name string
repo Repo
expectedResult bool
returnError error
}

testCases := []testCase{
{name: "test 1", expectedResult: true, repo: Repo{Ref: "abc", URL: "cde", fullPath: "cde_a"}, returnError: nil},
{name: "test 2", expectedResult: false, repo: Repo{Ref: "abc", URL: "cde", fullPath: "cde_a"}, returnError: fmt.Errorf("test error")},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockGitExec := new(mocks.ShellRunnerI)
exe := &exec.Cmd{}
serr := &bytes.Buffer{}

mockGitExec.On(
"ExecGitCommand",
[]string{"clone", "--branch", tc.repo.Ref, tc.repo.URL, tc.repo.fullPath},
(*bytes.Buffer)(nil),
serr,
"").Return(exe, tc.returnError)

tc.repo.SetShellRunner(mockGitExec)

result := tc.repo.Clone()
assert.Equal(t, tc.expectedResult, result)
})
}
}

func Test_Repo_PushMirror(t *testing.T) {
type testCase struct {
name string
repo Repo
expectedResult bool
returnError error
}

testCases := []testCase{
{name: "test 1", expectedResult: true, repo: Repo{mirrorURL: "cde"}, returnError: nil},
{name: "test 2", expectedResult: false, repo: Repo{mirrorURL: "cde"}, returnError: fmt.Errorf("test error")},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockGitExec := new(mocks.ShellRunnerI)
exe := &exec.Cmd{}
serr := &bytes.Buffer{}

mockGitExec.On(
"ExecGitCommand",
[]string{"push", "--mirror", tc.repo.mirrorURL},
(*bytes.Buffer)(nil),
serr,
"").Return(exe, tc.returnError)

tc.repo.SetShellRunner(mockGitExec)

result := tc.repo.PushMirror()
assert.Equal(t, tc.expectedResult, result)
})
}
}

func Test_Repo_GitCheckout(t *testing.T) {
type testCase struct {
name string
repo Repo
branch string
expectedResult bool
expectedNotOnRefBranch bool
returnError error
}

testCases := []testCase{
{
name: "test 1",
expectedResult: true,
expectedNotOnRefBranch: false,
branch: "develop",
repo: Repo{sha: "abcabc", Ref: "develop", fullPath: "abc"},
returnError: nil,
},
{
name: "test 2",
expectedResult: false,
expectedNotOnRefBranch: false,
branch: "develop",
repo: Repo{sha: "abcabc", Ref: "develop", fullPath: "abc"},
returnError: fmt.Errorf("test error"),
},
{
name: "test 3",
expectedResult: true,
expectedNotOnRefBranch: true,
branch: "master",
repo: Repo{sha: "abcabc", Ref: "develop", fullPath: "abc"},
returnError: nil,
},
{
name: "test 4",
expectedResult: false,
expectedNotOnRefBranch: true,
branch: "master",
repo: Repo{sha: "abcabc", Ref: "develop", fullPath: "abc"},
returnError: fmt.Errorf("test error"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockGitExec := new(mocks.ShellRunnerI)
exe := &exec.Cmd{}
serr := &bytes.Buffer{}

mockGitExec.On(
"ExecGitCommand",
[]string{"checkout", tc.branch},
(*bytes.Buffer)(nil),
serr,
tc.repo.fullPath).Return(exe, tc.returnError)

tc.repo.SetShellRunner(mockGitExec)
initColors()

result := tc.repo.GitCheckout(tc.branch)

assert.Equal(t, tc.expectedResult, result)
assert.Equal(t, !tc.expectedResult, tc.repo.status.Error)
assert.Equal(t, tc.expectedNotOnRefBranch, tc.repo.status.NotOnRefBranch)
})
}
}

0 comments on commit 8db5b1c

Please sign in to comment.