mirror of https://github.com/spf13/afero.git
Closes spf13/afero#79
Added fix to support "nested" `BasePathFs`. Unit-tests are also included.
This commit is contained in:
parent
2f194a29d5
commit
9a1fcfb267
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue