From 8f334b79b108e4d1b66179e4490fdbf968fddac3 Mon Sep 17 00:00:00 2001 From: 06rajesh Date: Tue, 8 Mar 2022 16:33:48 +0100 Subject: [PATCH] more tests added for bob push --- bobgit/push.go | 38 +++- bobgit/push_test.go | 215 +++++++++++++----- ...{multi_repo_push_after => multi_repo_push} | 0 ...h_before => multi_repo_with_one_repo_push} | 0 ...ingle_repo_push_after => single_repo_push} | 0 bobgit/testdata/push/single_repo_push_before | 5 - pkg/cmdutil/run.go | 41 +++- 7 files changed, 218 insertions(+), 81 deletions(-) rename bobgit/testdata/push/{multi_repo_push_after => multi_repo_push} (100%) rename bobgit/testdata/push/{multi_repo_push_before => multi_repo_with_one_repo_push} (100%) rename bobgit/testdata/push/{single_repo_push_after => single_repo_push} (100%) delete mode 100644 bobgit/testdata/push/single_repo_push_before diff --git a/bobgit/push.go b/bobgit/push.go index 44797f7f..73a78187 100644 --- a/bobgit/push.go +++ b/bobgit/push.go @@ -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) @@ -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") } @@ -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) @@ -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() + } } diff --git a/bobgit/push_test.go b/bobgit/push_test.go index ce031657..eb391f2d 100644 --- a/bobgit/push_test.go +++ b/bobgit/push_test.go @@ -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 @@ -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) } @@ -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")) @@ -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)) @@ -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) @@ -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() } @@ -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) } @@ -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 { diff --git a/bobgit/testdata/push/multi_repo_push_after b/bobgit/testdata/push/multi_repo_push similarity index 100% rename from bobgit/testdata/push/multi_repo_push_after rename to bobgit/testdata/push/multi_repo_push diff --git a/bobgit/testdata/push/multi_repo_push_before b/bobgit/testdata/push/multi_repo_with_one_repo_push similarity index 100% rename from bobgit/testdata/push/multi_repo_push_before rename to bobgit/testdata/push/multi_repo_with_one_repo_push diff --git a/bobgit/testdata/push/single_repo_push_after b/bobgit/testdata/push/single_repo_push similarity index 100% rename from bobgit/testdata/push/single_repo_push_after rename to bobgit/testdata/push/single_repo_push diff --git a/bobgit/testdata/push/single_repo_push_before b/bobgit/testdata/push/single_repo_push_before deleted file mode 100644 index 2625c215..00000000 --- a/bobgit/testdata/push/single_repo_push_before +++ /dev/null @@ -1,5 +0,0 @@ -Changes to be committed: - -Changes not staged for commit: - -Untracked files: diff --git a/pkg/cmdutil/run.go b/pkg/cmdutil/run.go index fde9b217..7de67ea7 100644 --- a/pkg/cmdutil/run.go +++ b/pkg/cmdutil/run.go @@ -102,18 +102,6 @@ func RunGit(root string, args ...string) error { return r.Run() } -func RunGitSSHFirstPush(root string, remote string, branch string) error { - - sshcommand := "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" - err := RunGit(root, "config", "core.sshCommand", sshcommand) - if err != nil { - return fmt.Errorf("failed to config git command: %w", err) - } - - args := []string{"push", "-u", remote, branch} - return RunGit(root, args...) -} - func RunGitWithOutput(root string, args ...string) ([]byte, error) { r, err := gitprepare(root, args...) if err != nil { @@ -190,3 +178,32 @@ func GitPush(root string, remote string, ref string) ([]byte, error) { return r.OutputCombined() } + +// GitPushFirstTime, runs git push command for the first time with remote and branch name and +// disable ssh checking if ssh set to true +func GitPushFirstTime(root string, remote string, branch string, ssh bool) error { + if ssh { + err := DisableSSHChecking(root) + if err != nil { + return err + } + } + + r, err := gitprepare(root, "push", "-u", remote, branch) + if err != nil { + return fmt.Errorf("failed to make git command: %w", err) + } + + return r.Run() +} + +func DisableSSHChecking(root string) error { + sshcommand := "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" + + r, err := gitprepare(root, "config", "core.sshCommand", sshcommand) + if err != nil { + return fmt.Errorf("failed to config git command: %w", err) + } + + return r.Run() +}