-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
362 lines (284 loc) · 8.55 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package backupfs
import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"sort"
"testing"
"github.com/stretchr/testify/require"
)
func fileMustContainText(t *testing.T, fsys FS, path, content string) {
path = filepath.Clean(path)
require := require.New(t)
f, err := fsys.Open(path)
require.NoError(err)
defer f.Close()
b, err := io.ReadAll(f)
require.NoError(err)
require.Equal(string(b), content)
}
func symlinkMustExist(t *testing.T, fsys FS, symlinkPath string) (path string) {
symlinkPath = filepath.Clean(symlinkPath)
require := require.New(t)
fi, err := fsys.Lstat(symlinkPath)
require.NotErrorIs(fs.ErrNotExist, err, "target symlink does not exist but is expected to exist: ", symlinkPath)
require.NoError(err)
hasSymlinkFlag := fi.Mode()&os.ModeType&os.ModeSymlink != 0
require.Truef(hasSymlinkFlag, "target symlink does not have the symlink flag: %s", symlinkPath)
actualPointsTo, err := fsys.Readlink(symlinkPath)
require.NoError(err)
require.True(actualPointsTo != "", "symlink target path is empty")
return actualPointsTo
}
func symlinkMustExistWithTragetPath(t *testing.T, fsys FS, symlinkPath, expectedPointsTo string) {
expectedPointsTo = filepath.Clean(expectedPointsTo)
actualPointsTo := symlinkMustExist(t, fsys, symlinkPath)
require.Equalf(t, expectedPointsTo, actualPointsTo, "symlink located at %s does not point to the expected path", symlinkPath)
}
func mustNotExist(t *testing.T, fsys FS, path string) {
path = filepath.Clean(path)
require := require.New(t)
_, found, err := lexists(fsys, path)
require.NoError(err)
require.False(found, "found file path but should not exist: "+path)
}
func mustNotLExist(t *testing.T, fsys FS, path string) {
path = filepath.Clean(path)
require := require.New(t)
_, found, err := lexists(fsys, path)
require.NoError(err)
require.Falsef(found, "path found but should not exist: %s", path)
}
func mustExist(t *testing.T, fsys FS, path string) {
path = filepath.Clean(path)
require := require.New(t)
_, found, err := lexists(fsys, path)
require.NoError(err)
require.True(found, "file path not found but should exist: "+path)
}
func mustLExist(t *testing.T, fsys FS, path string) {
path = filepath.Clean(path)
require := require.New(t)
_, found, err := lexists(fsys, path)
require.NoError(err)
require.Truef(found, "path not found but should exist: %s", path)
}
func removeFile(t *testing.T, fsys FS, path string) {
path = filepath.Clean(path)
require := require.New(t)
err := fsys.Remove(path)
require.NoError(err)
mustNotLExist(t, fsys, path)
}
func removeAll(t *testing.T, fsys FS, path string) {
path = filepath.Clean(path)
require := require.New(t)
err := fsys.RemoveAll(path)
require.NoError(err)
mustNotLExist(t, fsys, path)
}
func createSymlink(t *testing.T, fsys FS, oldpath, newpath string) {
require := require.New(t)
oldpath = filepath.Clean(oldpath)
newpath = filepath.Clean(newpath)
oldpath = toAbsSymlink(oldpath, newpath)
_, _, err := lexists(fsys, oldpath)
require.NoError(err)
dirPath := filepath.Dir(oldpath)
_, found, err := lexists(fsys, dirPath)
require.NoError(err)
if !found {
mkdirAll(t, fsys, dirPath, 0755)
}
dirPath = filepath.Dir(newpath)
_, found, err = lexists(fsys, dirPath)
require.NoError(err)
if !found {
mkdirAll(t, fsys, dirPath, 0755)
}
// check newpath after creating the symlink
err = fsys.Symlink(oldpath, newpath)
require.NoError(err)
fi, err := fsys.Lstat(newpath)
require.NoError(err)
hasSymlinkFlag := fi.Mode()&os.ModeType&os.ModeSymlink != 0
require.True(hasSymlinkFlag, "the target(newpath) symlink does not have the symlink flag set: ", newpath)
// check oldpath after creating the symlink
fi, err = fsys.Lstat(oldpath)
switch {
case err == nil:
hasSymlinkFlag = fi.Mode()&os.ModeType&os.ModeSymlink != 0
require.Falsef(hasSymlinkFlag, "the source (oldpath) symlink does have the symlink flag set but is expected not to have it set: %s", oldpath)
case errors.Is(err, fs.ErrNotExist):
// broken symlink that points to an invalid location may be created
return
default:
require.NoError(err)
}
}
func createFile(t *testing.T, fsys FS, path, content string) {
path = filepath.Clean(path)
require := require.New(t)
dirPath := filepath.Dir(path)
_, found, err := lexists(fsys, dirPath)
require.NoError(err)
if !found {
err = fsys.MkdirAll(dirPath, 0755)
require.NoError(err)
}
f, err := fsys.Create(path)
require.NoError(err)
defer func(file File) {
err := f.Close()
require.NoError(err)
}(f)
ret, err := f.WriteString(content)
require.NoError(err)
require.Equal(ret, len(content))
}
func openFile(t *testing.T, fsys FS, path, content string, perm fs.FileMode) {
path = filepath.Clean(path)
require := require.New(t)
dirPath := filepath.Dir(path)
_, found, err := lexists(fsys, dirPath)
require.NoError(err)
if !found {
err = fsys.MkdirAll(dirPath, 0755)
require.NoError(err)
}
f, err := fsys.OpenFile(path, os.O_RDWR|os.O_TRUNC|os.O_CREATE, perm)
require.NoError(err)
defer func(file File) {
err := f.Close()
require.NoError(err)
}(f)
ret, err := f.WriteString(content)
require.NoError(err)
require.Equal(ret, len(content))
}
func mkdirAll(t *testing.T, fsys FS, path string, perm fs.FileMode) {
path = filepath.Clean(path)
require := require.New(t)
err := fsys.MkdirAll(path, perm)
require.NoError(err)
_, err = IterateDirTree(path, func(s string) (bool, error) {
_, exists, err := lexists(fsys, s)
if err != nil {
return false, err
}
require.True(exists, "path not found but is expected to exist: ", s)
return true, nil
})
require.NoError(err)
}
func mkdir(t *testing.T, fsys FS, path string, perm fs.FileMode) error {
path = filepath.Clean(path)
require := require.New(t)
err := fsys.Mkdir(path, perm)
if err != nil {
// assert that it is indeed a path error
_, ok := err.(*os.PathError)
require.True(ok)
return err
}
fi, b, err := lexists(fsys, path)
require.NoError(err)
require.True(b, "directory: ", path, "must exist after it has been created but does not.")
require.True(fi.IsDir(), "path: ", path, "is not a directory after it has been created.")
return nil
}
func modeMustBeEqual(t *testing.T, a, b fs.FileMode) {
require := require.New(t)
a &= chmodBits
b &= chmodBits
require.Equalf(a, b, "expected: %0o got: %0o", a, b)
}
func chmod(t *testing.T, fsys FS, path string, perm fs.FileMode) {
path = filepath.Clean(path)
require := require.New(t)
_, found, err := lexists(fsys, path)
require.NoError(err)
if !found {
err = fsys.Chmod(path, perm)
require.Error(err)
return
}
// lexists
err = fsys.Chmod(path, perm)
require.NoError(err)
fiAfter, err := fsys.Lstat(path)
require.NoError(err)
permAfter := fiAfter.Mode()
modeMustBeEqual(t, perm, permAfter)
}
func countFiles(t *testing.T, fsys FS, path string, expectedFilesAndDirs int) {
require := require.New(t)
path = filepath.Clean(path)
files, err := allFiles(fsys, path)
require.NoError(err)
sort.Strings(files)
require.Equalf(expectedFilesAndDirs, len(files), "files: %v", files)
}
func createFSState(t *testing.T, fsys FS, entrypoint string) []pathState {
state, err := newFSState(fsys, entrypoint)
require.NoError(t, err)
return state
}
func mustEqualFSState(t *testing.T, before []pathState, fsys FS, entrypoint string) {
after := createFSState(t, fsys, entrypoint)
require.Equal(t, before, after)
}
func newFSState(fsys FS, entrypoint string) ([]pathState, error) {
var paths []pathState
err := Walk(fsys, entrypoint, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
content := ""
if info.Mode().IsRegular() {
f, err := fsys.Open(path)
if err != nil {
return err
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return err
}
content = string(b)
} else if info.Mode()&os.ModeSymlink != 0 {
content, err = fsys.Readlink(path)
if err != nil {
return err
}
}
paths = append(paths, pathState{
Path: path,
Name: info.Name(),
Size: info.Size(),
Mode: info.Mode(),
Content: content,
})
return nil
})
if err != nil {
return nil, err
}
sort.Sort(byPathStateLeastFilePathSeparators(paths))
return paths, nil
}
type pathState struct {
Path string
Name string
Size int64
Mode fs.FileMode
Content string
}
type byPathStateLeastFilePathSeparators []pathState
func (a byPathStateLeastFilePathSeparators) Len() int { return len(a) }
func (a byPathStateLeastFilePathSeparators) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byPathStateLeastFilePathSeparators) Less(i, j int) bool {
return LessFilePathSeparators(a[i].Path, a[j].Path)
}