forked from mirror/afero
Permit accessing basePath root in BasePathFs (with test)
This commit is contained in:
parent
52ef806ebb
commit
220647edb9
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue