Skip to content

Commit 8e67c68

Browse files
committed
Made EquivalentTo aware of absolute paths
1 parent 93ff259 commit 8e67c68

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

paths.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,21 @@ func (p *Path) EqualsTo(other *Path) bool {
478478
}
479479

480480
// EquivalentTo return true if both paths are equivalent (they points to the
481-
// same file even if they are lexicographically different)
481+
// same file even if they are lexicographically different) based on the current
482+
// working directory.
482483
func (p *Path) EquivalentTo(other *Path) bool {
483-
return p.Clean().path == other.Clean().path
484+
if p.Clean().path == other.Clean().path {
485+
return true
486+
}
487+
absP, err := p.Abs()
488+
if err != nil {
489+
return false
490+
}
491+
absOther, err := other.Abs()
492+
if err != nil {
493+
return false
494+
}
495+
return absP.path == absOther.path
484496
}
485497

486498
// Parents returns all the parents directories of the current path. If the path is absolute

paths_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,12 @@ func TestFilterOutDirs(t *testing.T) {
302302
require.Equal(t, "_testdata/anotherFile", list[0].String())
303303
require.Equal(t, "_testdata/file", list[1].String())
304304
}
305+
306+
func TestEquivalentPaths(t *testing.T) {
307+
wd, err := Getwd()
308+
require.NoError(t, err)
309+
require.True(t, New("file1").EquivalentTo(New("file1", "somethingelse", "..")))
310+
require.True(t, New("file1", "abc").EquivalentTo(New("file1", "abc", "def", "..")))
311+
require.True(t, wd.Join("file1").EquivalentTo(New("file1")))
312+
require.True(t, wd.Join("file1").EquivalentTo(New("file1", "abc", "..")))
313+
}

0 commit comments

Comments
 (0)