-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio-test.go
395 lines (340 loc) · 7.37 KB
/
fileio-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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package testutils
import (
"errors"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
)
type TestFileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
isDir bool
}
func (tfi *TestFileInfo) Name() string { return tfi.name }
func (tfi *TestFileInfo) Size() int64 { return tfi.size }
func (tfi *TestFileInfo) Mode() os.FileMode { return tfi.mode }
func (tfi *TestFileInfo) ModTime() time.Time { return tfi.modTime }
func (tfi *TestFileInfo) IsDir() bool { return tfi.isDir }
func (tfi *TestFileInfo) Sys() interface{} { return nil }
type TestFile struct {
data []byte
perm os.FileMode
fi os.FileInfo
}
type TestFileHook func(args ...string) error
type TestFileIo struct {
files map[string]*TestFile
ForceError map[string]error
Hooks map[string]TestFileHook
}
func NewTestFileIo() *TestFileIo {
return &TestFileIo{
files: make(map[string]*TestFile),
ForceError: make(map[string]error),
Hooks: make(map[string]TestFileHook),
}
}
func (tfi *TestFileIo) getError(api string, path ...string) error {
hook, exists := tfi.Hooks[api]
if exists {
err := hook(path...)
if err != nil {
return err
}
}
err, exists := tfi.ForceError[api]
if !exists {
for _, p := range path {
err, exists = tfi.ForceError[p]
if exists {
break
}
}
}
if exists {
delete(tfi.ForceError, api)
return err
}
return nil
}
func (tfi *TestFileIo) ReadFile(filePath string) ([]byte, error) {
err := tfi.getError("ReadFile", filePath)
if err != nil {
return nil, err
}
tf, exist := tfi.files[filePath]
if !exist || tf.fi.IsDir() {
return nil, os.ErrNotExist
}
return tf.data, nil
}
func (tfi *TestFileIo) WriteFile(filePath string, data []byte, perm fs.FileMode) error {
err := tfi.getError("WriteFile", filePath)
if err != nil {
return err
}
parent := path.Dir(filePath)
if parent == "." {
panic("relative path is not mocked")
}
parentTfi, exists := tfi.files[parent]
if !exists || !parentTfi.fi.IsDir() {
return os.ErrNotExist
}
existingTfi, exists := tfi.files[filePath]
if exists {
if existingTfi.fi.IsDir() {
return os.ErrExist
}
}
dirPart, fileName := path.Split(filePath)
dirPart = strings.TrimSuffix(dirPart, "/")
dfi, exist := tfi.files[dirPart]
if !exist || !dfi.fi.IsDir() {
return os.ErrNotExist
}
fi := &TestFileInfo{
name: fileName,
size: int64(len(data)),
mode: 0,
modTime: time.Now(),
isDir: false,
}
tf := &TestFile{
data: data,
perm: perm,
fi: fi,
}
tfi.files[filePath] = tf
return nil
}
func (tfi *TestFileIo) Stat(name string) (os.FileInfo, error) {
err := tfi.getError("Stat", name)
if err != nil {
return nil, err
}
tf, exist := tfi.files[name]
if !exist {
return nil, os.ErrNotExist
}
return tf.fi, nil
}
func (tfi *TestFileIo) MkdirAll(name string, perm fs.FileMode) error {
err := tfi.getError("MkdirAll", name)
if err != nil {
return err
}
existingTfi, exists := tfi.files[name]
if exists {
if !existingTfi.fi.IsDir() {
return os.ErrExist
}
return nil
}
parent := path.Dir(name)
if parent != "." && parent != "/" {
err := tfi.MkdirAll(parent, perm)
if err != nil {
return err
}
}
_, fileName := path.Split(name)
fi := &TestFileInfo{
name: fileName,
size: 0,
mode: 0,
modTime: time.Now(),
isDir: true,
}
tf := &TestFile{
data: nil,
perm: perm,
fi: fi,
}
tfi.files[name] = tf
return nil
}
func (tfi *TestFileIo) IsEmptyDir(name string) bool {
dirTfi, exists := tfi.files[name]
if !exists || !dirTfi.fi.IsDir() {
return false
}
dirSlash := name + "/"
for file := range tfi.files {
if strings.HasPrefix(file, dirSlash) {
return false
}
}
return true
}
func (tfi *TestFileIo) RemoveAll(name string) error {
err := tfi.getError("RemoveAll", name)
if err != nil {
return err
}
nameSlash := name + "/"
removeList := []string{}
for file := range tfi.files {
if file == name || strings.HasPrefix(file, nameSlash) {
removeList = append(removeList, file)
}
}
for _, file := range removeList {
delete(tfi.files, file)
}
return nil
}
func (tfi *TestFileIo) IsDirectory(name string) (bool, error) {
err := tfi.getError("IsDirectory", name)
if err != nil {
return false, err
}
fi, err := tfi.Stat(name)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
return fi.IsDir(), nil
}
func (tfi *TestFileIo) CopyFile(src, dest string) (int64, error) {
err := tfi.getError("CopyFile", src, dest)
if err != nil {
return 0, err
}
existingTfi, exists := tfi.files[src]
if !exists || existingTfi.fi.IsDir() {
return 0, os.ErrNotExist
}
parent := path.Dir(dest)
parentTfi, exists := tfi.files[parent]
if !exists || !parentTfi.fi.IsDir() {
return 0, os.ErrNotExist
}
_, fileName := path.Split(dest)
fi := &TestFileInfo{
name: fileName,
size: existingTfi.fi.Size(),
mode: existingTfi.fi.Mode(),
modTime: existingTfi.fi.ModTime(),
isDir: false,
}
tf := &TestFile{
data: make([]byte, len(existingTfi.data)),
perm: existingTfi.perm,
fi: fi,
}
copy(tf.data, existingTfi.data)
tfi.files[dest] = tf
return fi.size, nil
}
func (tfi *TestFileIo) ReadDir(src string) (files []fs.DirEntry, err error) {
err = tfi.getError("ReadDir", src)
if err != nil {
return
}
dirSlash := path.Clean(src) + "/"
for file, tf := range tfi.files {
if strings.HasPrefix(file, dirSlash) {
files = append(files, fs.FileInfoToDirEntry(tf.fi))
}
}
return
}
func (tfi *TestFileIo) FileExists(name string) (bool, error) {
err := tfi.getError("FileExists", name)
if err != nil {
return false, err
}
tf, exists := tfi.files[name]
if !exists || tf.fi.IsDir() {
return false, nil
}
return true, nil
}
func (tfi *TestFileIo) Walk(root string, fn filepath.WalkFunc) (err error) {
err = tfi.getError("Walk", root)
if err != nil {
return
}
cleanRoot := path.Clean(root)
tf, exists := tfi.files[cleanRoot]
if exists {
err = fn(cleanRoot, tf.fi, nil)
if err != nil {
if errors.Is(err, filepath.SkipDir) {
err = nil
}
return
}
dirSlash := cleanRoot + "/"
for file, tf := range tfi.files {
if strings.HasPrefix(file, dirSlash) {
if !strings.Contains(file[len(dirSlash):], "/") {
// found a file or subdirectory within root (but not a subpath)
err = tfi.getError("Walk", file)
if err != nil {
return
}
if tf.fi.IsDir() {
// subdirectory - recurse
if err = tfi.Walk(file, fn); err != nil {
return
}
} else {
// file
err = fn(file, tf.fi, nil)
if err != nil {
if errors.Is(err, filepath.SkipDir) {
err = nil
}
return
}
}
}
}
}
}
return
}
func (tfi *TestFileIo) Chtimes(name string, atime, mtime time.Time) error {
err := tfi.getError("Chtimes", name)
if err != nil {
return err
}
tf, exist := tfi.files[name]
if !exist {
return os.ErrNotExist
}
tf.fi = &TestFileInfo{
name: tf.fi.Name(),
size: tf.fi.Size(),
mode: tf.fi.Mode(),
modTime: mtime,
isDir: tf.fi.IsDir(),
}
return nil
}
func (tfi *TestFileIo) Dump() {
keys := make([]string, 0, len(tfi.files))
for k := range tfi.files {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := tfi.files[k]
if !v.fi.IsDir() {
fmt.Printf("%s - %d bytes\n", k, len(v.data))
} else {
fmt.Printf("%s (dir)\n", k)
}
}
}