Skip to content

Commit

Permalink
more tests added for bob push
Browse files Browse the repository at this point in the history
  • Loading branch information
06rajesh committed Mar 8, 2022
1 parent 57c79f8 commit 8f334b7
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 81 deletions.
38 changes: 35 additions & 3 deletions bobgit/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,39 @@ var ErrUptodateAllRepo = fmt.Errorf("All repositories up to date.")
const configureInstruction string = "Either specify the URL from the command-line or configure a remote " +
"repository and then push using the remote name."

type PO struct {
testing bool
}

type PushOption func(p *PO)

func EnableTesting() PushOption {
return func(p *PO) {
p.testing = true
}
}

// Push run `git push` commands iteratively
// in all the git repositories under bob workspace.
//
// Run through all the repositories with a confirm dialog in case of
// not configured remote.
func Push() (err error) {
func Push(opts ...PushOption) (err error) {
defer errz.Recover(&err)

o := &PO{
testing: false,
}

// iterate through options for testings and other
// properties
for _, opt := range opts {
if opt == nil {
continue
}
opt(o)
}

bobRoot, err := bobutil.FindBobRoot()
errz.Fatal(err)

Expand All @@ -50,7 +75,7 @@ func Push() (err error) {
// pre-compute maximum repository name length for proper formatting
maxlen := strutil.LongestStrLen(repoNames)

filteredRepo, err := filterReadyToPushRepos(bobRoot, repoNames, maxlen)
filteredRepo, err := filterReadyToPushRepos(bobRoot, repoNames, maxlen, o)
if errors.Is(err, ErrInsufficientConfig) {
return usererror.Wrapm(ErrInsufficientConfig, "Git push failed")
}
Expand Down Expand Up @@ -92,7 +117,7 @@ func Push() (err error) {
//
// ask for confirmation in case of not configured remote. If confirmed with `no` rejects the whole
// command.
func filterReadyToPushRepos(root string, repolist []string, maxlen int) ([]string, error) {
func filterReadyToPushRepos(root string, repolist []string, maxlen int, opt *PO) ([]string, error) {
filtered := []string{}
for _, repo := range repolist {
repoConfig, err := getRepoConfig(root, repo)
Expand All @@ -109,11 +134,18 @@ func filterReadyToPushRepos(root string, repolist []string, maxlen int) ([]strin
} else {
buf := FprintErrorPushDestination(repo, maxlen)
fmt.Println(buf.String())

// in case of testing return the error without confirmation
if opt.testing {
return nil, ErrInsufficientConfig
}

resp := askForConfirmation("Sure want to continue with the rest of the repositories? (yes/no): ")
if !resp {
return nil, ErrInsufficientConfig
}
fmt.Println()

}
}

Expand Down
215 changes: 154 additions & 61 deletions bobgit/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
var sshSeverStoragePath = "/tmp/soft"
var pushTestDataPath = "testdata/push"

// disable running tests for push,
// helpful while writing other tests
var runPushTests = true

var cfg *config.Config
var s *server.Server

Expand Down Expand Up @@ -69,7 +73,7 @@ func TestPush(t *testing.T) {
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("repo/"), 0664))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".bob.workspace"), []byte(""), 0664))
assert.Nil(t, addAllWithCommit(dir, "'Initial Commit'"))
err = cmdutil.RunGitSSHFirstPush(dir, "origin", "master")
err = cmdutil.GitPushFirstTime(dir, "origin", "master", true)
if err != nil {
fmt.Println(err)
}
Expand All @@ -95,7 +99,7 @@ func TestPush(t *testing.T) {
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("repo/"), 0664))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".bob.workspace"), []byte(""), 0664))
assert.Nil(t, addAllWithCommit(dir, "Initial Commit"))
err := cmdutil.RunGitSSHFirstPush(dir, "origin", "master")
err := cmdutil.GitPushFirstTime(dir, "origin", "master", true)
assert.Nil(t, err)
assert.Nil(t, os.WriteFile(filepath.Join(dir, "file2"), []byte("file"), 0664))
assert.Nil(t, addAllWithCommit(dir, "New File Added"))
Expand All @@ -108,7 +112,7 @@ func TestPush(t *testing.T) {
err = createGitDirWithRemote(repo, u.String())
assert.Nil(t, err)
assert.Nil(t, addAllWithCommit(repo, "Initial Commit"))
err = cmdutil.RunGitSSHFirstPush(repo, "origin", "master")
err = cmdutil.GitPushFirstTime(repo, "origin", "master", true)
assert.Nil(t, err)

assert.Nil(t, os.WriteFile(filepath.Join(repo, "file2"), []byte("file"), 0664))
Expand All @@ -133,7 +137,7 @@ func TestPush(t *testing.T) {
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("repo/"), 0664))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".bob.workspace"), []byte(""), 0664))
assert.Nil(t, addAllWithCommit(dir, "Initial Commit"))
err := cmdutil.RunGitSSHFirstPush(dir, "origin", "master")
err := cmdutil.GitPushFirstTime(dir, "origin", "master", true)
assert.Nil(t, err)

u, _ = url.Parse(sshpath)
Expand All @@ -144,91 +148,180 @@ func TestPush(t *testing.T) {
err = createGitDirWithRemote(repo, u.String())
assert.Nil(t, err)
assert.Nil(t, addAllWithCommit(repo, "Initial Commit"))
err = cmdutil.RunGitSSHFirstPush(repo, "origin", "master")
err = cmdutil.GitPushFirstTime(repo, "origin", "master", true)
assert.Nil(t, err)
},
},
"",
ErrUptodateAllRepo,
},
}
{
"multi_repo_with_one_repo_push",
input{
func(dir string) {

err := initServer()
assert.Nil(t, err)
testname := filepath.Base(dir)

for _, test := range tests {
dir, err := ioutil.TempDir("", test.name+"-*")
assert.Nil(t, err)
u, _ := url.Parse(sshpath)
u.Path = path.Join(u.Path, testname)

// Don't cleanup in testdir mode
if !createTestDirs {
defer os.RemoveAll(dir)
}
assert.Nil(t, createGitDirWithRemote(dir, u.String()))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("repo/"), 0664))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".bob.workspace"), []byte(""), 0664))
assert.Nil(t, addAllWithCommit(dir, "Initial Commit"))
err := cmdutil.GitPushFirstTime(dir, "origin", "master", true)
assert.Nil(t, err)
assert.Nil(t, os.WriteFile(filepath.Join(dir, "file2"), []byte("file"), 0664))
assert.Nil(t, addAllWithCommit(dir, "New File Added"))

if debug || createTestDirs {
println("Using test dir " + dir)
}
u, _ = url.Parse(sshpath)
u.Path = path.Join(u.Path, testname+"_repo")

test.input.environment(dir)
repo := filepath.Join(dir, "repo")
assert.Nil(t, os.MkdirAll(repo, 0775))
err = createGitDirWithRemote(repo, u.String())
assert.Nil(t, err)
assert.Nil(t, addAllWithCommit(repo, "Initial Commit"))
err = cmdutil.GitPushFirstTime(repo, "origin", "master", true)
assert.Nil(t, err)
},
},
"repo",
nil,
},
{
"single_repo_with_no_configured_remote",
input{
func(dir string) {
assert.Nil(t, os.MkdirAll(dir, 0775))
assert.Nil(t, cmdutil.RunGit(dir, "init"))
assert.Nil(t, os.WriteFile(filepath.Join(dir, "file"), []byte("file"), 0664))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("repo/"), 0664))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".bob.workspace"), []byte(""), 0664))
assert.Nil(t, addAllWithCommit(dir, "Initial Commit"))
},
},
"",
ErrInsufficientConfig,
},
{
"multi_repo_with_no_configured_remote",
input{
func(dir string) {

if createTestDirs {
continue
}
testname := filepath.Base(dir)

execdir := filepath.Join(dir, test.execdir)
statusBeforeFile := test.name + "_before"
statusAfterFile := test.name + "_after"
u, _ := url.Parse(sshpath)
u.Path = path.Join(u.Path, testname)

statusBefore, err := getStatus(execdir)
assert.Nil(t, createGitDirWithRemote(dir, u.String()))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("repo/"), 0664))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".bob.workspace"), []byte(""), 0664))
assert.Nil(t, addAllWithCommit(dir, "Initial Commit"))
err := cmdutil.GitPushFirstTime(dir, "origin", "master", true)
assert.Nil(t, err)
assert.Nil(t, os.WriteFile(filepath.Join(dir, "file2"), []byte("file"), 0664))
assert.Nil(t, addAllWithCommit(dir, "New File Added"))

repo := filepath.Join(dir, "repo")
assert.Nil(t, os.MkdirAll(repo, 0775))
assert.Nil(t, cmdutil.RunGit(repo, "init"))
assert.Nil(t, os.WriteFile(filepath.Join(repo, "file"), []byte("file"), 0664))
assert.Nil(t, addAllWithCommit(repo, "Initial Commit"))
},
},
"",
ErrInsufficientConfig,
},
// normal bob push should fail, as no branch in remote exists in
// configured remote
{
"single_repo_for_first_time_push",
input{
func(dir string) {

testname := filepath.Base(dir)

u, _ := url.Parse(sshpath)
u.Path = path.Join(u.Path, testname)

assert.Nil(t, createGitDirWithRemote(dir, u.String()))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("repo/"), 0664))
assert.Nil(t, os.WriteFile(filepath.Join(dir, ".bob.workspace"), []byte(""), 0664))
assert.Nil(t, addAllWithCommit(dir, "Initial Commit"))
assert.Nil(t, os.WriteFile(filepath.Join(dir, "file2"), []byte("file"), 0664))
assert.Nil(t, addAllWithCommit(dir, "New File Added"))
},
},
"",
ErrInsufficientConfig,
},
}

if runPushTests {
err := initServer()
assert.Nil(t, err)

err = executePush(execdir)
if test.expectederr != nil {
if errors.Is(err, test.expectederr) {
for _, test := range tests {
dir, err := ioutil.TempDir("", test.name+"-*")
assert.Nil(t, err)

// Don't cleanup in testdir mode
if !createTestDirs {
defer os.RemoveAll(dir)
}

if debug || createTestDirs {
println("Using test dir " + dir)
}

test.input.environment(dir)

if createTestDirs {
continue
}
assert.Fail(t, fmt.Sprintf("expected error [%s] got [%s]", test.expectederr.Error(), err.Error()))
}

statusAfter, err := getStatus(execdir)
assert.Nil(t, err)
execdir := filepath.Join(dir, test.execdir)

if update {
// tests expecting a error don't need to compare their before and after outputs
err = executePush(execdir)
if test.expectederr != nil {
continue
if errors.Is(err, test.expectederr) {
continue
}
assert.Fail(t, fmt.Sprintf("expected error [%s] got [%s]", test.expectederr.Error(), err.Error()))
}

err = os.RemoveAll(filepath.Join(pushTestDataPath, statusBeforeFile))
assert.Nil(t, err)
err = os.RemoveAll(filepath.Join(pushTestDataPath, statusAfterFile))
assert.Nil(t, err)
err = os.MkdirAll(pushTestDataPath, 0775)
assert.Nil(t, err)
err = os.WriteFile(filepath.Join(pushTestDataPath, statusBeforeFile), []byte(statusBefore.String()), 0664)
assert.Nil(t, err)
err = os.WriteFile(filepath.Join(pushTestDataPath, statusAfterFile), []byte(statusAfter.String()), 0664)
assert.Nil(t, err)
continue
}

expectBefore, err := os.ReadFile(filepath.Join(pushTestDataPath, statusBeforeFile))
assert.Nil(t, err, test.name)
statusAfter, err := getStatus(execdir)
assert.Nil(t, err)

diff := cmp.Diff(statusBefore.String(), string(expectBefore))
assert.Equal(t, "", diff, statusBeforeFile)
if update {
// tests expecting a error don't need to compare their before and after outputs
if test.expectederr != nil {
continue
}

err = os.RemoveAll(filepath.Join(pushTestDataPath, test.name))
assert.Nil(t, err)
err = os.MkdirAll(pushTestDataPath, 0775)
assert.Nil(t, err)
err = os.WriteFile(filepath.Join(pushTestDataPath, test.name), []byte(statusAfter.String()), 0664)
assert.Nil(t, err)
continue
}

expectAfter, err := os.ReadFile(filepath.Join(pushTestDataPath, statusAfterFile))
assert.Nil(t, err, test.name)
expectAfter, err := os.ReadFile(filepath.Join(pushTestDataPath, test.name))
assert.Nil(t, err, test.name)

diff = cmp.Diff(statusAfter.String(), string(expectAfter))
assert.Equal(t, "", diff, statusAfterFile)
diff := cmp.Diff(statusAfter.String(), string(expectAfter))
assert.Equal(t, "", diff, test.name)
}

assert.Nil(t, stopServer())
assert.Nil(t, cleanups())
}

assert.Nil(t, stopServer())
assert.Nil(t, cleanups())

if createTestDirs || update {
t.FailNow()
}
Expand Down Expand Up @@ -283,12 +376,12 @@ func createCustomConfig(temppath string) *config.Config {
errz.Fatal(err)
}

err = os.MkdirAll(repopath, 0777)
err = os.MkdirAll(repopath, 0775)
if err != nil {
errz.Fatal(err)
}

err = os.MkdirAll(sshpath, 0777)
err = os.MkdirAll(sshpath, 0775)
if err != nil {
errz.Fatal(err)
}
Expand Down Expand Up @@ -317,7 +410,7 @@ func executePush(dir string) (err error) {
}
defer func() { _ = os.Chdir(wd) }()

return Push()
return Push(EnableTesting())
}

func createGitDirWithRemote(dir string, remotepath string) error {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions bobgit/testdata/push/single_repo_push_before

This file was deleted.

Loading

0 comments on commit 8f334b7

Please sign in to comment.