diff --git a/internal/fs/path_prefix.go b/internal/fs/path_prefix.go index d5398db12..e5ab5c0db 100644 --- a/internal/fs/path_prefix.go +++ b/internal/fs/path_prefix.go @@ -5,14 +5,15 @@ import ( ) // HasPathPrefix returns true if p is a subdir of (or a file within) base. It -// assumes a file system which is case sensitive. For relative paths, false is -// returned. +// assumes a file system which is case sensitive. If the paths are not of the +// same type (one is relative, the other is absolute), false is returned. func HasPathPrefix(base, p string) bool { if filepath.VolumeName(base) != filepath.VolumeName(p) { return false } - if !filepath.IsAbs(base) || !filepath.IsAbs(p) { + // handle case when base and p are not of the same type + if filepath.IsAbs(base) != filepath.IsAbs(p) { return false } diff --git a/internal/fs/path_prefix_test.go b/internal/fs/path_prefix_test.go index dfdc69522..aff04bd83 100644 --- a/internal/fs/path_prefix_test.go +++ b/internal/fs/path_prefix_test.go @@ -21,7 +21,10 @@ func TestHasPathPrefix(t *testing.T) { base, p string result bool }{ - {"", "", false}, + {"", "", true}, + {".", ".", true}, + {".", "foo", true}, + {"foo", ".", false}, {"/", "", false}, {"/", "x", false}, {"x", "/", false}, @@ -36,6 +39,10 @@ func TestHasPathPrefix(t *testing.T) { {"/home/user/foo", "/home/user/foobar", false}, {"/home/user/Foo", "/home/user/foo/bar/baz", false}, {"/home/user/foo", "/home/user/Foo/bar/baz", false}, + {"user/foo", "user/foo/bar/baz", true}, + {"user/foo", "./user/foo", true}, + {"user/foo", "./user/foo/", true}, + {"/home/user/foo", "./user/foo/", false}, } for _, test := range tests {