Skip to content

Commit

Permalink
Closes spf13#79
Browse files Browse the repository at this point in the history
Added fix to support "nested" `BasePathFs`. Unit-tests are also included.
  • Loading branch information
francoishill committed Apr 18, 2016
1 parent 2f194a2 commit 9a1fcfb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion basepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
37 changes: 37 additions & 0 deletions basepath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

0 comments on commit 9a1fcfb

Please sign in to comment.