Permit accessing basePath root in BasePathFs (with test)
diff --git a/basepath.go b/basepath.go
index b9fa145..3434137 100644
--- a/basepath.go
+++ b/basepath.go
@@ -27,8 +27,7 @@
// on a file outside the base path it returns the given file name and an error,
// else the given file with the base path prepended
func (b *BasePathFs) RealPath(name string) (path string, err error) {
- bpath := filepath.Clean(b.path) + string(os.PathSeparator)
-
+ bpath := filepath.Clean(b.path)
path = filepath.Clean(filepath.Join(bpath, name))
if !strings.HasPrefix(path, bpath) {
return name, os.ErrNotExist
diff --git a/basepath_test.go b/basepath_test.go
index ee9f2d3..d2202f9 100644
--- a/basepath_test.go
+++ b/basepath_test.go
@@ -1,6 +1,7 @@
package afero
import (
+ "os"
"testing"
)
@@ -17,3 +18,20 @@
t.Errorf("succeeded in creating %s ...", fh.Name())
}
}
+
+func TestBasePathRoot(t *testing.T) {
+ baseFs := &MemMapFs{}
+ baseFs.MkdirAll("/base/path/foo/baz", 0777)
+ baseFs.MkdirAll("/base/path/boo/", 0777)
+ bp := NewBasePathFs(baseFs, "/base/path")
+
+ rd, err := ReadDir(bp, string(os.PathSeparator))
+
+ if len(rd) != 2 {
+ t.Errorf("base path doesn't respect root")
+ }
+
+ if err != nil {
+ t.Error(err)
+ }
+}