@@ -12,54 +12,34 @@ import (
12
12
13
13
// This file contains utilities for running tests around file system state.
14
14
15
- // fspath represents a file system path in an OS-agnostic way.
16
- type fsPath []string
17
-
18
- func (f fsPath ) String () string { return filepath .Join (f ... ) }
19
-
20
- func (f fsPath ) prepend (prefix string ) fsPath {
21
- p := fsPath {filepath .FromSlash (prefix )}
22
- return append (p , f ... )
23
- }
24
-
25
15
type fsTestCase struct {
26
16
before , after filesystemState
27
17
}
28
18
29
- // filesystemState represents the state of a file system. It has a setup method
30
- // which inflates its state to the actual host file system, and an assert
31
- // method which checks that the actual file system matches the described state.
32
- type filesystemState struct {
33
- root string
34
- dirs []fsPath
35
- files []fsPath
36
- links []fsLink
37
- }
38
-
39
- // assert makes sure that the fs state matches the state of the actual host
40
- // file system
41
- func (fs filesystemState ) assert (t * testing.T ) {
19
+ // assert makes sure that the tc.after state matches the state of the actual host
20
+ // file system at tc.after.root.
21
+ func (tc fsTestCase ) assert (t * testing.T ) {
42
22
dirMap := make (map [string ]bool )
43
23
fileMap := make (map [string ]bool )
44
24
linkMap := make (map [string ]bool )
45
25
46
- for _ , d := range fs .dirs {
47
- dirMap [d . prepend ( fs . root ). String ( )] = true
26
+ for _ , d := range tc . after .dirs {
27
+ dirMap [filepath . Join ( tc . after . root , d )] = true
48
28
}
49
- for _ , f := range fs .files {
50
- fileMap [f . prepend ( fs . root ). String ( )] = true
29
+ for _ , f := range tc . after .files {
30
+ fileMap [filepath . Join ( tc . after . root , f )] = true
51
31
}
52
- for _ , l := range fs .links {
53
- linkMap [l . path . prepend ( fs . root ). String ( )] = true
32
+ for _ , l := range tc . after .links {
33
+ linkMap [filepath . Join ( tc . after . root , l . path )] = true
54
34
}
55
35
56
- err := filepath .Walk (fs .root , func (path string , info os.FileInfo , err error ) error {
36
+ err := filepath .Walk (tc . after .root , func (path string , info os.FileInfo , err error ) error {
57
37
if err != nil {
58
38
t .Errorf ("filepath.Walk path=%q err=%q" , path , err )
59
39
return err
60
40
}
61
41
62
- if path == fs .root {
42
+ if path == tc . after .root {
63
43
return nil
64
44
}
65
45
@@ -106,32 +86,27 @@ func (fs filesystemState) assert(t *testing.T) {
106
86
}
107
87
}
108
88
109
- // fsLink represents a symbolic link.
110
- type fsLink struct {
111
- path fsPath
112
- to string
113
- }
114
-
115
- // setup inflates fs onto the actual host file system
116
- func (fs filesystemState ) setup (t * testing.T ) {
117
- fs .setupDirs (t )
118
- fs .setupFiles (t )
119
- fs .setupLinks (t )
89
+ // setup inflates fs onto the actual host file system at tc.before.root.
90
+ // It doesn't delete existing files and should be used on empty roots only.
91
+ func (tc fsTestCase ) setup (t * testing.T ) {
92
+ tc .setupDirs (t )
93
+ tc .setupFiles (t )
94
+ tc .setupLinks (t )
120
95
}
121
96
122
- func (fs filesystemState ) setupDirs (t * testing.T ) {
123
- for _ , dir := range fs .dirs {
124
- p := dir . prepend ( fs . root )
125
- if err := os .MkdirAll (p . String () , 0777 ); err != nil {
97
+ func (tc fsTestCase ) setupDirs (t * testing.T ) {
98
+ for _ , dir := range tc . before .dirs {
99
+ p := filepath . Join ( tc . before . root , dir )
100
+ if err := os .MkdirAll (p , 0777 ); err != nil {
126
101
t .Fatalf ("os.MkdirAll(%q, 0777) err=%q" , p , err )
127
102
}
128
103
}
129
104
}
130
105
131
- func (fs filesystemState ) setupFiles (t * testing.T ) {
132
- for _ , file := range fs .files {
133
- p := file . prepend ( fs . root )
134
- f , err := os .Create (p . String () )
106
+ func (tc fsTestCase ) setupFiles (t * testing.T ) {
107
+ for _ , file := range tc . before .files {
108
+ p := filepath . Join ( tc . before . root , file )
109
+ f , err := os .Create (p )
135
110
if err != nil {
136
111
t .Fatalf ("os.Create(%q) err=%q" , p , err )
137
112
}
@@ -141,17 +116,17 @@ func (fs filesystemState) setupFiles(t *testing.T) {
141
116
}
142
117
}
143
118
144
- func (fs filesystemState ) setupLinks (t * testing.T ) {
145
- for _ , link := range fs .links {
146
- p := link . path . prepend ( fs . root )
119
+ func (tc fsTestCase ) setupLinks (t * testing.T ) {
120
+ for _ , link := range tc . before .links {
121
+ p := filepath . Join ( tc . before . root , link . path )
147
122
148
123
// On Windows, relative symlinks confuse filepath.Walk. This is golang/go
149
124
// issue 17540. So, we'll just sigh and do absolute links, assuming they are
150
125
// relative to the directory of link.path.
151
- dir := filepath .Dir (p . String () )
126
+ dir := filepath .Dir (p )
152
127
to := filepath .Join (dir , link .to )
153
128
154
- if err := os .Symlink (to , p . String () ); err != nil {
129
+ if err := os .Symlink (to , p ); err != nil {
155
130
t .Fatalf ("os.Symlink(%q, %q) err=%q" , to , p , err )
156
131
}
157
132
}
0 commit comments