From 220647edb92ffafbc2897503a952c800a6bf45d3 Mon Sep 17 00:00:00 2001 From: Steve Francia Date: Tue, 12 Jan 2016 00:34:15 -0500 Subject: [PATCH] Permit accessing basePath root in BasePathFs (with test) --- basepath.go | 3 +-- basepath_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/basepath.go b/basepath.go index b9fa145..3434137 100644 --- a/basepath.go +++ b/basepath.go @@ -27,8 +27,7 @@ func NewBasePathFs(source Fs, path string) Fs { // 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 @@ func TestBasePath(t *testing.T) { 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) + } +}