6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-10 13:44:17 +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

2
go.mod
View File

@@ -10,7 +10,7 @@ require (
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.0.0 github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.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 github.com/xlab/treeprint v1.0.0
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect

View File

@@ -16,8 +16,8 @@ import (
// It's handled by ErrorsCallback to tell the WalkCallback to skip this dir. // It's handled by ErrorsCallback to tell the WalkCallback to skip this dir.
var errSkipNode = errors.New(".git directory found, skipping this node") var errSkipNode = errors.New(".git directory found, skipping this node")
// errDirectoryAccess indicates a directory doesn't exists or can't be accessed var errDirNoAccess = errors.New("directory can't be accessed")
var errDirectoryAccess = errors.New("directory doesn't exist or 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. // 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) { func Exists(path string) (bool, error) {
@@ -29,12 +29,12 @@ func Exists(path string) (bool, error) {
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return false, errDirectoryAccess return false, errors.Wrapf(errDirNotExist, "can't access %s", path)
} }
} }
// Directory exists but can't be accessed // 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. // RepoFinder finds git repositories inside a given path.

View File

@@ -1,8 +1,11 @@
package git package git
import ( import (
"errors"
"git-get/pkg/git/test" "git-get/pkg/git/test"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestFinder(t *testing.T) { func TestFinder(t *testing.T) {
@@ -15,18 +18,15 @@ func TestFinder(t *testing.T) {
name: "no repos", name: "no repos",
reposMaker: makeNoRepos, reposMaker: makeNoRepos,
want: 0, want: 0,
}, }, {
{
name: "single repos", name: "single repos",
reposMaker: makeSingleRepo, reposMaker: makeSingleRepo,
want: 1, want: 1,
}, }, {
{
name: "single nested repo", name: "single nested repo",
reposMaker: makeNestedRepo, reposMaker: makeNestedRepo,
want: 1, want: 1,
}, }, {
{
name: "multiple nested repo", name: "multiple nested repo",
reposMaker: makeMultipleNestedRepos, reposMaker: makeMultipleNestedRepos,
want: 2, want: 2,
@@ -40,9 +40,38 @@ func TestFinder(t *testing.T) {
finder := NewRepoFinder(root) finder := NewRepoFinder(root)
finder.Find() finder.Find()
if len(finder.repos) != test.want { assert.Len(t, finder.repos, test.want)
t.Errorf("expected %d; got %d", test.want, len(finder.repos)) })
} }
}
// 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" "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) { func TestUncommitted(t *testing.T) {
tests := []struct { tests := []struct {
name string name string