diff --git a/go.mod b/go.mod index 36aa2d4..4a88e33 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.0.0 github.com/spf13/viper v1.7.0 - github.com/stretchr/testify v1.4.0 // indirect + github.com/stretchr/testify v1.4.0 github.com/xlab/treeprint v1.0.0 golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect diff --git a/pkg/git/finder.go b/pkg/git/finder.go index d8eb9a0..ac9e90d 100644 --- a/pkg/git/finder.go +++ b/pkg/git/finder.go @@ -16,8 +16,8 @@ import ( // It's handled by ErrorsCallback to tell the WalkCallback to skip this dir. var errSkipNode = errors.New(".git directory found, skipping this node") -// errDirectoryAccess indicates a directory doesn't exists or can't be accessed -var errDirectoryAccess = errors.New("directory doesn't exist or can't be accessed") +var errDirNoAccess = errors.New("directory can't be accessed") +var errDirNotExist = errors.New("directory doesn't exist") // Exists returns true if a directory exists. If it doesn't or the directory can't be accessed it returns an error. func Exists(path string) (bool, error) { @@ -29,12 +29,12 @@ func Exists(path string) (bool, error) { if err != nil { if os.IsNotExist(err) { - return false, errDirectoryAccess + return false, errors.Wrapf(errDirNotExist, "can't access %s", path) } } // Directory exists but can't be accessed - return true, errDirectoryAccess + return true, errors.Wrapf(errDirNoAccess, "can't access %s", path) } // RepoFinder finds git repositories inside a given path. diff --git a/pkg/git/finder_test.go b/pkg/git/finder_test.go index 24205ae..1ab2078 100644 --- a/pkg/git/finder_test.go +++ b/pkg/git/finder_test.go @@ -1,8 +1,11 @@ package git import ( + "errors" "git-get/pkg/git/test" "testing" + + "github.com/stretchr/testify/assert" ) func TestFinder(t *testing.T) { @@ -15,18 +18,15 @@ func TestFinder(t *testing.T) { 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, @@ -40,9 +40,38 @@ func TestFinder(t *testing.T) { finder := NewRepoFinder(root) finder.Find() - if len(finder.repos) != test.want { - t.Errorf("expected %d; got %d", test.want, len(finder.repos)) - } + assert.Len(t, finder.repos, test.want) + }) + } +} + +// TODO: this test will only work on Linux +func TestExists(t *testing.T) { + tests := []struct { + name string + path string + want error + }{ + { + name: "dir does not exist", + path: "/this/directory/does/not/exist", + want: errDirNotExist, + }, { + name: "dir cant be accessed", + path: "/root/some/directory", + want: errDirNoAccess, + }, { + name: "dir exists", + path: "/tmp/", + want: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + _, err := Exists(test.path) + + assert.True(t, errors.Is(err, test.want)) }) } } diff --git a/pkg/git/repo_test.go b/pkg/git/repo_test.go index 38f65c3..24c4e45 100644 --- a/pkg/git/repo_test.go +++ b/pkg/git/repo_test.go @@ -6,14 +6,6 @@ import ( "testing" ) -func TestOpen(t *testing.T) { - _, err := Open("/paththatdoesnotexist/repo") - - if err != errDirectoryAccess { - t.Errorf("Opening a repo in non existing path should throw an error") - } -} - func TestUncommitted(t *testing.T) { tests := []struct { name string