10 func TestBasePath(t *testing.T) {
12 baseFs.MkdirAll("/base/path/tmp", 0777)
13 bp := NewBasePathFs(baseFs, "/base/path")
15 if _, err := bp.Create("/tmp/foo"); err != nil {
16 t.Errorf("Failed to set real path")
19 if fh, err := bp.Create("../tmp/bar"); err == nil {
20 t.Errorf("succeeded in creating %s ...", fh.Name())
24 func TestBasePathRoot(t *testing.T) {
26 baseFs.MkdirAll("/base/path/foo/baz", 0777)
27 baseFs.MkdirAll("/base/path/boo/", 0777)
28 bp := NewBasePathFs(baseFs, "/base/path")
30 rd, err := ReadDir(bp, string(os.PathSeparator))
33 t.Errorf("base path doesn't respect root")
41 func TestRealPath(t *testing.T) {
43 baseDir, err := TempDir(fs, "", "base")
45 t.Fatal("error creating tempDir", err)
47 defer fs.RemoveAll(baseDir)
48 anotherDir, err := TempDir(fs, "", "another")
50 t.Fatal("error creating tempDir", err)
52 defer fs.RemoveAll(anotherDir)
54 bp := NewBasePathFs(fs, baseDir).(*BasePathFs)
56 subDir := filepath.Join(baseDir, "s1")
58 realPath, err := bp.RealPath("/s1")
61 t.Errorf("Got error %s", err)
64 if realPath != subDir {
65 t.Errorf("Expected \n%s got \n%s", subDir, realPath)
68 if runtime.GOOS == "windows" {
69 _, err = bp.RealPath(anotherDir)
72 t.Errorf("Expected error")
76 // on *nix we have no way of just looking at the path and tell that anotherDir
77 // is not inside the base file system.
78 // The user will receive an os.ErrNotExist later.
79 surrealPath, err := bp.RealPath(anotherDir)
82 t.Errorf("Got error %s", err)
85 excpected := filepath.Join(baseDir, anotherDir)
87 if surrealPath != excpected {
88 t.Errorf("Expected \n%s got \n%s", excpected, surrealPath)
94 func TestNestedBasePaths(t *testing.T) {
96 Dir1, Dir2, Dir3 string
98 dirSpecs := []dirSpec{
99 dirSpec{Dir1: "/", Dir2: "/", Dir3: "/"},
100 dirSpec{Dir1: "/", Dir2: "/path2", Dir3: "/"},
101 dirSpec{Dir1: "/path1/dir", Dir2: "/path2/dir/", Dir3: "/path3/dir"},
102 dirSpec{Dir1: "C:/path1", Dir2: "path2/dir", Dir3: "/path3/dir/"},
105 for _, ds := range dirSpecs {
106 memFs := NewMemMapFs()
107 level1Fs := NewBasePathFs(memFs, ds.Dir1)
108 level2Fs := NewBasePathFs(level1Fs, ds.Dir2)
109 level3Fs := NewBasePathFs(level2Fs, ds.Dir3)
116 spec{BaseFs: level3Fs, FileName: "f.txt"},
117 spec{BaseFs: level2Fs, FileName: "f.txt"},
118 spec{BaseFs: level1Fs, FileName: "f.txt"},
121 for _, s := range specs {
122 if err := s.BaseFs.MkdirAll(s.FileName, 0755); err != nil {
123 t.Errorf("Got error %s", err.Error())
125 if _, err := s.BaseFs.Stat(s.FileName); err != nil {
126 t.Errorf("Got error %s", err.Error())
129 if s.BaseFs == level3Fs {
130 pathToExist := filepath.Join(ds.Dir3, s.FileName)
131 if _, err := level2Fs.Stat(pathToExist); err != nil {
132 t.Errorf("Got error %s (path %s)", err.Error(), pathToExist)
134 } else if s.BaseFs == level2Fs {
135 pathToExist := filepath.Join(ds.Dir2, ds.Dir3, s.FileName)
136 if _, err := level1Fs.Stat(pathToExist); err != nil {
137 t.Errorf("Got error %s (path %s)", err.Error(), pathToExist)