Skip to content

Commit 8780dcc

Browse files
committed
Fixed IsInsideDir for paths on different filesystems
1 parent ce00145 commit 8780dcc

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

paths.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ func (p *Path) Clean() *Path {
191191
func (p *Path) IsInsideDir(dir *Path) (bool, error) {
192192
rel, err := filepath.Rel(dir.path, p.path)
193193
if err != nil {
194-
return false, err
194+
// If the dir cannot be made relative to this path it means
195+
// that it belong to a different filesystems, so it cannot be
196+
// inside this path.
197+
return false, nil
195198
}
196199
return !strings.Contains(rel, ".."+string(os.PathSeparator)) && rel != ".." && rel != ".", nil
197200
}

paths_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,17 @@ func TestResetStatCacheWhenFollowingSymlink(t *testing.T) {
154154
}
155155

156156
func TestIsInsideDir(t *testing.T) {
157-
inside := func(a, b *Path) {
157+
notInside := func(a, b *Path) {
158158
in, err := a.IsInsideDir(b)
159159
require.NoError(t, err)
160-
require.True(t, in, "%s is inside %s", a, b)
160+
require.False(t, in, "%s is inside %s", a, b)
161161
}
162162

163-
notInside := func(a, b *Path) {
163+
inside := func(a, b *Path) {
164164
in, err := a.IsInsideDir(b)
165165
require.NoError(t, err)
166-
require.False(t, in, "%s is inside %s", a, b)
166+
require.True(t, in, "%s is inside %s", a, b)
167+
notInside(b, a)
167168
}
168169

169170
f1 := New("/a/b/c")
@@ -196,6 +197,14 @@ func TestIsInsideDir(t *testing.T) {
196197
f5 := New("/home/megabug/a15/packages")
197198
notInside(f5, f4)
198199
notInside(f4, f5)
200+
201+
if runtime.GOOS == "windows" {
202+
f6 := New("C:\\", "A")
203+
f7 := New("C:\\", "A", "B", "C")
204+
f8 := New("E:\\", "A", "B", "C")
205+
inside(f7, f6)
206+
notInside(f8, f6)
207+
}
199208
}
200209

201210
func TestReadFileAsLines(t *testing.T) {

0 commit comments

Comments
 (0)