Closes spf13/afero#79 Added fix to support "nested" `BasePathFs`. Unit-tests are also included. - afero - [fork] go afero port for 9front
(HTM) git clone git@git.drkhsh.at/afero.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 9a1fcfb267e0bafd227628a13d6277269fbd0c8f
(DIR) parent 2f194a29d557d6d46aa202870df1e9062dbda4d4
(HTM) Author: Francois Hill (dell laptop) <francoishill11@gmail.com>
Date: Mon, 18 Apr 2016 19:29:19 +0200
Closes spf13/afero#79
Added fix to support "nested" `BasePathFs`. Unit-tests are also included.
Diffstat:
M basepath.go | 6 +++++-
M basepath_test.go | 37 +++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 1 deletion(-)
---
(DIR) diff --git a/basepath.go b/basepath.go
@@ -29,7 +29,6 @@ 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) {
-
if err := validateBasePathName(name); err != nil {
return "", err
}
@@ -39,6 +38,11 @@ func (b *BasePathFs) RealPath(name string) (path string, err error) {
if !strings.HasPrefix(path, bpath) {
return name, os.ErrNotExist
}
+
+ if parentBasePathFs, ok := b.source.(*BasePathFs); ok {
+ return parentBasePathFs.RealPath(path)
+ }
+
return path, nil
}
(DIR) diff --git a/basepath_test.go b/basepath_test.go
@@ -90,3 +90,40 @@ func TestRealPath(t *testing.T) {
}
}
+
+func TestNestedBasePaths(t *testing.T) {
+ type dirSpec struct {
+ Dir1, Dir2, Dir3 string
+ }
+ dirSpecs := []dirSpec{
+ dirSpec{Dir1: "/", Dir2: "/", Dir3: "/"},
+ dirSpec{Dir1: "/", Dir2: "/path2", Dir3: "/"},
+ dirSpec{Dir1: "/path1/dir", Dir2: "/path2/dir/", Dir3: "/path3/dir"},
+ }
+
+ for _, ds := range dirSpecs {
+ memFs := NewMemMapFs()
+ level1Fs := NewBasePathFs(memFs, ds.Dir1)
+ level2Fs := NewBasePathFs(level1Fs, ds.Dir2)
+ level3Fs := NewBasePathFs(level2Fs, ds.Dir3)
+
+ type spec struct {
+ BaseFs Fs
+ FileName string
+ ExpectedPath string
+ }
+ specs := []spec{
+ spec{BaseFs: level3Fs, FileName: "f.txt", ExpectedPath: filepath.Join(ds.Dir1, ds.Dir2, ds.Dir3, "f.txt")},
+ spec{BaseFs: level2Fs, FileName: "f.txt", ExpectedPath: filepath.Join(ds.Dir1, ds.Dir2, "f.txt")},
+ spec{BaseFs: level1Fs, FileName: "f.txt", ExpectedPath: filepath.Join(ds.Dir1, "f.txt")},
+ }
+
+ for _, s := range specs {
+ if actualPath, err := s.BaseFs.(*BasePathFs).RealPath(s.FileName); err != nil {
+ t.Errorf("Got error %s", err.Error())
+ } else if actualPath != s.ExpectedPath {
+ t.Errorf("Expected \n%s got \n%s", s.ExpectedPath, actualPath)
+ }
+ }
+ }
+}