From 13e936c3760b1eef38edbfabfd7e46761c769cf0 Mon Sep 17 00:00:00 2001 From: Grzegorz Dlugoszewski Date: Wed, 8 Jul 2020 14:58:19 +0200 Subject: [PATCH] Add repo finder tests --- pkg/git/finder.go | 3 +- pkg/git/finder_test.go | 90 ++++++++++++++++++++++++++++++++++++++++++ pkg/test/helpers.go | 36 ++++++++--------- pkg/test/testrepos.go | 7 +++- 4 files changed, 116 insertions(+), 20 deletions(-) create mode 100644 pkg/git/finder_test.go diff --git a/pkg/git/finder.go b/pkg/git/finder.go index f65166c..15adabb 100644 --- a/pkg/git/finder.go +++ b/pkg/git/finder.go @@ -81,12 +81,13 @@ func (r *RepoFinder) walkCb(path string, ent *godirwalk.Dirent) error { r.repos = append(r.repos, strings.TrimSuffix(path, ".git")) return errSkipNode } + // Do not traverse directories containing a .git directory if ent.IsDir() { _, err := os.Stat(filepath.Join(path, ".git")) if err == nil { r.repos = append(r.repos, strings.TrimSuffix(path, ".git")) - return ErrSkipNode + return errSkipNode } } return nil diff --git a/pkg/git/finder_test.go b/pkg/git/finder_test.go new file mode 100644 index 0000000..1f5be9b --- /dev/null +++ b/pkg/git/finder_test.go @@ -0,0 +1,90 @@ +package git + +import ( + "git-get/pkg/test" + "testing" +) + +func TestFinder(t *testing.T) { + tests := []struct { + name string + reposMaker func(*testing.T) string + want int + }{ + { + name: "no repos", + reposMaker: makeNoRepos, + want: 0, + }, + { + name: "single repos", + reposMaker: makeSingleRepo, + want: 1, + }, + { + name: "single nested repo", + reposMaker: makeNestedRepo, + want: 1, + }, + { + name: "multiple nested repo", + reposMaker: makeMultipleNestedRepos, + want: 2, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + root := test.reposMaker(t) + + finder := NewRepoFinder(root) + paths, _ := finder.Find() + + if len(paths) != test.want { + t.Errorf("expected %d; got %d", test.want, len(paths)) + } + }) + } +} + +func makeNoRepos(t *testing.T) string { + root := test.TempDir(t, "") + + return root +} + +func makeSingleRepo(t *testing.T) string { + root := test.TempDir(t, "") + + test.RepoEmptyInDir(t, root) + + return root +} + +func makeNestedRepo(t *testing.T) string { + // a repo with single nested repo should still be counted as one beacause finder doesn't traverse inside nested repos + root := test.TempDir(t, "") + + r := test.RepoEmptyInDir(t, root) + test.RepoEmptyInDir(t, r.Path()) + + return root +} + +func makeMultipleNestedRepos(t *testing.T) string { + root := test.TempDir(t, "") + + // create two repos inside root - should be counted as 2 + repo1 := test.RepoEmptyInDir(t, root) + repo2 := test.RepoEmptyInDir(t, root) + + // created repos nested inside two parent roots - shouldn't be counted + test.RepoEmptyInDir(t, repo1.Path()) + test.RepoEmptyInDir(t, repo1.Path()) + test.RepoEmptyInDir(t, repo2.Path()) + + // create a empty dir inside root - shouldn't be counted + test.TempDir(t, root) + + return root +} diff --git a/pkg/test/helpers.go b/pkg/test/helpers.go index 9135a57..fc5a6cb 100644 --- a/pkg/test/helpers.go +++ b/pkg/test/helpers.go @@ -9,6 +9,23 @@ import ( "testing" ) +// TempDir creates a temporary directory inside the parent dir. +// If parent is empty, it will use a system default temp dir (usually /tmp). +func TempDir(t *testing.T, parent string) string { + dir, err := ioutil.TempDir(parent, "git-get-repo-") + checkFatal(t, err) + + // Automatically remove temp dir when the test is over. + t.Cleanup(func() { + err := os.RemoveAll(dir) + if err != nil { + t.Errorf("failed removing test repo %s", dir) + } + }) + + return dir +} + func (r *Repo) init() { err := run.Git("init", "--quiet", r.path).AndShutUp() checkFatal(r.t, err) @@ -51,7 +68,7 @@ func (r *Repo) checkout(name string) { } func (r *Repo) clone() *Repo { - dir := tempDir(r.t, "") + dir := TempDir(r.t, "") url := fmt.Sprintf("file://%s/.git", r.path) err := run.Git("clone", url, dir).AndShutUp() @@ -70,23 +87,6 @@ func (r *Repo) fetch() { checkFatal(r.t, err) } -// tempDir creates a temporary directory inside the parent dir. -// If parent is empty, it will use a system default temp dir (usually /tmp). -func tempDir(t *testing.T, parent string) string { - dir, err := ioutil.TempDir(parent, "git-get-repo-") - checkFatal(t, err) - - // Automatically remove temp dir when the test is over. - t.Cleanup(func() { - err := os.RemoveAll(dir) - if err != nil { - t.Errorf("failed removing test repo %s", dir) - } - }) - - return dir -} - func checkFatal(t *testing.T, err error) { if err != nil { t.Fatalf("failed making test repo: %+v", err) diff --git a/pkg/test/testrepos.go b/pkg/test/testrepos.go index 1f42310..2c65427 100644 --- a/pkg/test/testrepos.go +++ b/pkg/test/testrepos.go @@ -19,8 +19,13 @@ func (r *Repo) Path() string { // RepoEmpty creates an empty git repo. func RepoEmpty(t *testing.T) *Repo { + return RepoEmptyInDir(t, "") +} + +// RepoEmptyInDir creates an empty git repo inside a given parent dir. +func RepoEmptyInDir(t *testing.T, parent string) *Repo { r := &Repo{ - path: tempDir(t, ""), + path: TempDir(t, parent), t: t, }