Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
internal/fs: add EqualPaths()
Browse files Browse the repository at this point in the history
Signed-off-by: Ibrahim AshShohail <[email protected]>
  • Loading branch information
ibrasho committed Aug 4, 2017
1 parent 44a454b commit 0ec076d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
4 changes: 2 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (c *Ctx) DetectProjectGOPATH(p *Project) (string, error) {
pGOPATH, perr := c.detectGOPATH(p.AbsRoot)

// If p.AbsRoot is a not symlink, attempt to detect GOPATH for p.AbsRoot only.
if p.AbsRoot == p.ResolvedAbsRoot {
if equal, _ := fs.EqualPaths(p.AbsRoot, p.ResolvedAbsRoot); equal {
return pGOPATH, perr
}

Expand All @@ -191,7 +191,7 @@ func (c *Ctx) DetectProjectGOPATH(p *Project) (string, error) {
}

// If pGOPATH equals rGOPATH, then both are within the same GOPATH.
if pGOPATH == rGOPATH {
if equal, _ := fs.EqualPaths(pGOPATH, rGOPATH); equal {
return "", errors.Errorf("both %s and %s are in the same GOPATH %s", p.AbsRoot, p.ResolvedAbsRoot, pGOPATH)
}

Expand Down
31 changes: 30 additions & 1 deletion internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,36 @@ func HasFilepathPrefix(path, prefix string) bool {
return true
}

// EqualPaths compares the paths passed to check if the are equivalent.
// It respects the case-sensitivity of the underlaying filesysyems.
func EqualPaths(p1, p2 string) (bool, error) {
p1 = filepath.Clean(p1)
p2 = filepath.Clean(p2)

if isDir, err := IsDir(p2); err != nil && !strings.HasSuffix(err.Error(), "is not a directory") {
return false, err
} else if !isDir {
// If p2 is not a directory, compare the bases of the paths to ensure
// they are equivalent.
var p1Base, p2Base string

p1, p1Base = filepath.Split(p1)
p2, p2Base = filepath.Split(p2)

if isCaseSensitiveFilesystem(p1) {
if p1Base != p2Base {
return false, nil
}
} else {
if strings.ToLower(p1Base) != strings.ToLower(p2Base) {
return false, nil
}
}
}

return len(p1) == len(p2) && HasFilepathPrefix(p1, p2), nil
}

// RenameWithFallback attempts to rename a file or directory, but falls back to
// copying in the event of a cross-device link error. If the fallback copy
// succeeds, src is still removed, emulating normal rename behavior.
Expand Down Expand Up @@ -336,7 +366,6 @@ func cloneSymlink(sl, dst string) error {

// IsDir determines is the path given is a directory or not.
func IsDir(name string) (bool, error) {
// TODO: lstat?
fi, err := os.Stat(name)
if err != nil {
return false, err
Expand Down
40 changes: 40 additions & 0 deletions internal/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,46 @@ func TestHasFilepathPrefix_Files(t *testing.T) {
}
}

func TestEqualPaths(t *testing.T) {
h := test.NewHelper(t)
h.TempDir("dir")
h.TempDir("dir2")

h.TempFile("file", "")
h.TempFile("file2", "")

h.TempDir("DIR")
h.TempFile("FILE", "")

// caseInsensitive flag ensures that these cases are only equivalent on a
// case-insensitive filesystem.
caseInsensitive := isCaseSensitiveFilesystem(h.Path("dir"))

testcases := []struct {
p1, p2 string
want bool
err bool
}{
{h.Path("dir"), h.Path("dir"), true, false},
{h.Path("file"), h.Path("file"), true, false},
{h.Path("dir"), h.Path("dir2"), false, false},
{h.Path("file"), h.Path("file2"), false, false},
{h.Path("dir"), h.Path("file"), false, false},
{h.Path("dir"), h.Path("DIR"), caseInsensitive, false},
{strings.ToLower(h.Path("dir")), strings.ToUpper(h.Path("dir")), caseInsensitive, true},
}

for _, tc := range testcases {
got, err := EqualPaths(tc.p1, tc.p2)
if err != nil && !tc.err {
t.Error("unexpected error:", err)
}
if got != tc.want {
t.Errorf("expected EqualPaths(%q, %q) to be %t, got %t", tc.p1, tc.p2, tc.want, got)
}
}
}

func TestRenameWithFallback(t *testing.T) {
dir, err := ioutil.TempDir("", "dep")
if err != nil {
Expand Down

0 comments on commit 0ec076d

Please sign in to comment.