OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / spf13 / afero / basepath_test.go
1 package afero
2
3 import (
4         "os"
5         "path/filepath"
6         "runtime"
7         "testing"
8 )
9
10 func TestBasePath(t *testing.T) {
11         baseFs := &MemMapFs{}
12         baseFs.MkdirAll("/base/path/tmp", 0777)
13         bp := NewBasePathFs(baseFs, "/base/path")
14
15         if _, err := bp.Create("/tmp/foo"); err != nil {
16                 t.Errorf("Failed to set real path")
17         }
18
19         if fh, err := bp.Create("../tmp/bar"); err == nil {
20                 t.Errorf("succeeded in creating %s ...", fh.Name())
21         }
22 }
23
24 func TestBasePathRoot(t *testing.T) {
25         baseFs := &MemMapFs{}
26         baseFs.MkdirAll("/base/path/foo/baz", 0777)
27         baseFs.MkdirAll("/base/path/boo/", 0777)
28         bp := NewBasePathFs(baseFs, "/base/path")
29
30         rd, err := ReadDir(bp, string(os.PathSeparator))
31
32         if len(rd) != 2 {
33                 t.Errorf("base path doesn't respect root")
34         }
35
36         if err != nil {
37                 t.Error(err)
38         }
39 }
40
41 func TestRealPath(t *testing.T) {
42         fs := NewOsFs()
43         baseDir, err := TempDir(fs, "", "base")
44         if err != nil {
45                 t.Fatal("error creating tempDir", err)
46         }
47         defer fs.RemoveAll(baseDir)
48         anotherDir, err := TempDir(fs, "", "another")
49         if err != nil {
50                 t.Fatal("error creating tempDir", err)
51         }
52         defer fs.RemoveAll(anotherDir)
53
54         bp := NewBasePathFs(fs, baseDir).(*BasePathFs)
55
56         subDir := filepath.Join(baseDir, "s1")
57
58         realPath, err := bp.RealPath("/s1")
59
60         if err != nil {
61                 t.Errorf("Got error %s", err)
62         }
63
64         if realPath != subDir {
65                 t.Errorf("Expected \n%s got \n%s", subDir, realPath)
66         }
67
68         if runtime.GOOS == "windows" {
69                 _, err = bp.RealPath(anotherDir)
70
71                 if err == nil {
72                         t.Errorf("Expected error")
73                 }
74
75         } else {
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)
80
81                 if err != nil {
82                         t.Errorf("Got error %s", err)
83                 }
84
85                 excpected := filepath.Join(baseDir, anotherDir)
86
87                 if surrealPath != excpected {
88                         t.Errorf("Expected \n%s got \n%s", excpected, surrealPath)
89                 }
90         }
91
92 }
93
94 func TestNestedBasePaths(t *testing.T) {
95         type dirSpec struct {
96                 Dir1, Dir2, Dir3 string
97         }
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/"},
103         }
104
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)
110
111                 type spec struct {
112                         BaseFs   Fs
113                         FileName string
114                 }
115                 specs := []spec{
116                         spec{BaseFs: level3Fs, FileName: "f.txt"},
117                         spec{BaseFs: level2Fs, FileName: "f.txt"},
118                         spec{BaseFs: level1Fs, FileName: "f.txt"},
119                 }
120
121                 for _, s := range specs {
122                         if err := s.BaseFs.MkdirAll(s.FileName, 0755); err != nil {
123                                 t.Errorf("Got error %s", err.Error())
124                         }
125                         if _, err := s.BaseFs.Stat(s.FileName); err != nil {
126                                 t.Errorf("Got error %s", err.Error())
127                         }
128
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)
133                                 }
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)
138                                 }
139                         }
140                 }
141         }
142 }