6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-05 00:54:41 +00:00

Add a path to error messages when directory can't be accessed

This commit is contained in:
Grzegorz Dlugoszewski
2020-07-27 13:23:49 +02:00
parent 8904cd1bb3
commit d660a73c7f
4 changed files with 43 additions and 22 deletions

View File

@@ -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.

View File

@@ -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))
})
}
}

View File

@@ -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