-
Notifications
You must be signed in to change notification settings - Fork 5
/
gistfs_test.go
414 lines (335 loc) · 10.5 KB
/
gistfs_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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package gistfs
import (
"context"
"errors"
"io/fs"
"net/http"
"testing"
"time"
"github.com/google/go-github/v33/github"
"github.com/gregjones/httpcache"
)
var referenceGistID = "ded2f6727d98e6b0095e62a7813aa7cf"
var approxModTime, _ = time.Parse("2000-12-31", "2020-01-02") // when the gist was last edited
// Avoid burning rate limit by using a caching http client.
func cachingClient() *github.Client {
c := &http.Client{
Transport: httpcache.NewMemoryCacheTransport(),
}
return github.NewClient(c)
}
var cacheClient = cachingClient()
func TestErrorNotLoaded(t *testing.T) {
if !errors.Is(ErrNotLoaded, fs.ErrInvalid) {
t.Fatal("Err not loaded is not a wrapped ErrInvalid")
}
}
func TestNew(t *testing.T) {
t.Run("New OK", func(t *testing.T) {
gfs := New(referenceGistID)
if got, want := gfs.GetID(), referenceGistID; got != want {
t.Fatalf("New returned a FS with ID=%#v, want %#v", got, want)
}
})
t.Run("NewWithClient OK", func(t *testing.T) {
gfs := NewWithClient(cacheClient, referenceGistID)
if got, want := gfs.GetID(), referenceGistID; got != want {
t.Fatalf("NewWithClient returned a FS with ID=%#v, want %#v", got, want)
}
})
}
func TestOpen(t *testing.T) {
t.Run("Open OK", func(t *testing.T) {
gfs := NewWithClient(cacheClient, referenceGistID)
gfs.Load(context.Background())
tests := []struct {
name string
err error
}{
{"test1.txt", nil},
{"test2.txt", nil},
{"non-existing-file.txt", fs.ErrNotExist},
}
for _, test := range tests {
f, err := gfs.Open(test.name)
if test.err != nil && !errors.Is(err, test.err) {
t.Fatalf("Opened %#v, got error %#v, want %#v", test.name, err, test.err)
}
if test.err == nil && f == nil {
t.Fatalf("Opened %#v, got nil", test.name)
}
}
})
t.Run("Open NOK not loaded", func(t *testing.T) {
gfs := NewWithClient(cacheClient, referenceGistID)
_, err := gfs.Open("test1.txt")
if err == nil {
t.Fatalf("Opened without loading, got no error, want %#v", ErrNotLoaded)
} else if err != ErrNotLoaded {
t.Fatalf("Opened without loading, got an error %#v, want %#v", err, ErrNotLoaded)
}
})
}
func TestReadFile(t *testing.T) {
t.Run("ReadFile OK", func(t *testing.T) {
gfs := NewWithClient(cacheClient, referenceGistID)
gfs.Load(context.Background())
tests := []struct {
name string
content string
err error
}{
{"test1.txt", "foobar\nbarfoo", nil},
{"test2.txt", "olala\n12345\nabcde", nil},
{"non-existing-file.txt", "", fs.ErrNotExist},
}
for _, test := range tests {
b, err := gfs.ReadFile(test.name)
if test.err != nil && !errors.Is(err, test.err) {
t.Fatalf("Read file %#v, got error %#v, want %#v", test.name, err, test.err)
}
if test.err == nil && b == nil {
t.Fatalf("Read file %#v, got nil", test.name)
}
if test.content != string(b) {
t.Fatalf("Read file %#v, expected %#v but got %#v", test.name, test.content, string(b))
}
}
})
}
func TestRead(t *testing.T) {
gfs := NewWithClient(cacheClient, referenceGistID)
gfs.Load(context.Background())
t.Run("Read OK", func(t *testing.T) {
f, err := gfs.Open("test1.txt")
if err != nil {
t.Fatalf("Opened file and got an error %#v, want no error", err)
}
b := make([]byte, len("foobar\n"))
n, err := f.Read(b)
if err != nil {
t.Fatalf("Read and got an error %#v, want no error", err)
}
if got, want := n, len("foobar\n"); got != want {
t.Fatalf("Read %d bytes in b, want %d bytes", got, want)
}
if got, want := string(b), "foobar\n"; got != want {
t.Fatalf("Read %d bytes in b (%#v), want %#v", n, got, want)
}
})
t.Run("Read NOK closed file", func(t *testing.T) {
f, err := gfs.Open("test1.txt")
if err != nil {
t.Fatalf("Opened file and got an error %#v, want no error", err)
}
b := make([]byte, len("foobar\n"))
_ = f.Close()
_, err = f.Read(b)
if got, want := err, fs.ErrClosed; got != want {
t.Fatalf("Read on a closed file and got %#v, want %#v", got, want)
}
})
}
func TestStat(t *testing.T) {
gfs := NewWithClient(cacheClient, referenceGistID)
gfs.Load(context.Background())
t.Run("Stat OK", func(t *testing.T) {
tests := []struct {
filename string
err error
size int64
mode fs.FileMode
approxModTime time.Time
isDir bool
}{
{
filename: "test1.txt",
err: nil,
size: int64(len("foobar\nbarfoo")),
mode: fs.FileMode(0444),
approxModTime: approxModTime,
isDir: false,
},
{
filename: "test2.txt",
err: nil,
size: int64(len("olala\n12345\nabcde")),
mode: fs.FileMode(0444),
approxModTime: approxModTime,
isDir: false,
},
}
for _, test := range tests {
f, err := gfs.Open(test.filename)
if err != nil {
t.Fatalf("Opened file and got an error %#v, want no error", err)
}
stat, err := f.Stat()
if err != test.err {
t.Fatalf("Stat and got an error %#v, want %#v", err, test.err)
}
if test.err == nil {
if got, want := stat.Name(), test.filename; got != want {
t.Fatalf("got filename %#v, want %#v", got, want)
}
if got, want := stat.Size(), test.size; got != want {
t.Fatalf("got size %#v, want %#v", got, want)
}
if got, want := stat.Mode(), test.mode; got != want {
t.Fatalf("got mode %#v, want %#v", got, want)
}
if got, want := stat.ModTime(), test.approxModTime; got.After(approxModTime) &&
got.Before(test.approxModTime.Add(24*time.Hour)) {
t.Fatalf("got modTime %#v, want approx %#v", got, want)
}
if got, want := stat.IsDir(), test.isDir; got != want {
t.Fatalf("got isDir %#v, want %#v", got, want)
}
_, ok := stat.Sys().(*github.GistFile)
if got, want := ok, true; got != want {
t.Fatal("got Sys with type different from *github.GistFile, want it to be the case")
}
}
}
})
t.Run("Stat NOK closed file", func(t *testing.T) {
f, err := gfs.Open("test1.txt")
if err != nil {
t.Fatalf("Opened file and got an error %#v, want no error", err)
}
_ = f.Close()
_, err = f.Stat()
if got, want := err, fs.ErrClosed; got != want {
t.Fatalf("Read on a closed file and got %#v, want %#v", got, want)
}
})
}
func TestReadDir(t *testing.T) {
gfs := NewWithClient(cacheClient, referenceGistID)
gfs.Load(context.Background())
t.Run("OK", func(t *testing.T) {
file, err := gfs.Open(".")
if err != nil {
t.Fatalf("Opening root directory, expected no error but got %#v", err)
}
dir, ok := file.(fs.ReadDirFile)
if !ok {
t.Fatal("Reading root directory, expected a ReadDirFile but got something else")
}
files, err := dir.ReadDir(-1)
if err != nil {
t.Fatalf("Reading root directory, expected no error but got %#v", err)
}
if got, want := len(files), 2; got != want {
t.Fatalf("Reading root directory, got %#v files, want %#v", got, want)
}
})
t.Run("NOK non existing dir", func(t *testing.T) {
tests := []string{"/", "non-existing/", "..", "../"}
for _, test := range tests {
_, err := gfs.Open(test)
var pathError *fs.PathError
if !errors.As(err, &pathError) {
t.Fatalf("Reading a non existing directory %#v, got %#v error, want %T", test, err, &fs.PathError{})
}
}
})
t.Run("OK subsequent reads", func(t *testing.T) {
file, err := gfs.Open(".")
if err != nil {
t.Fatalf("Opening root directory, expected no error but got %#v", err)
}
dir, ok := file.(fs.ReadDirFile)
if !ok {
t.Fatal("Reading root directory, expected a ReadDirFile but got something else")
}
// first read
files, err := dir.ReadDir(1)
if err != nil {
t.Fatalf("Reading root directory, expected no error but got %#v", err)
}
if got, want := len(files), 1; got != want {
t.Fatalf("Reading root directory, got %#v files, want %#v", got, want)
}
// second read
files, err = dir.ReadDir(1)
if err != nil {
t.Fatalf("Reading root directory, expected no error but got %#v", err)
}
if got, want := len(files), 1; got != want {
t.Fatalf("Reading root directory, got %#v files, want %#v", got, want)
}
// last read (no entries left)
files, err = dir.ReadDir(1)
if err != nil {
t.Fatalf("Reading root directory, expected no error but got %#v", err)
}
if got, want := len(files), 0; got != want {
t.Fatalf("Reading root directory, got %#v files, want %#v", got, want)
}
})
t.Run("OK ReadDir", func(t *testing.T) {
files, err := gfs.ReadDir(".")
if err != nil {
t.Fatalf("Reading root directory, expected no error but got %#v", err)
}
if got, want := len(files), 2; got != want {
t.Fatalf("Reading root directory, got %#v files, want %#v", got, want)
}
})
t.Run("NOK ReadDir on a file", func(t *testing.T) {
file, err := gfs.Open("test1.txt")
if err != nil {
t.Fatalf("Opening root directory, expected no error but got %#v", err)
}
dir, ok := file.(fs.ReadDirFile)
if !ok {
t.Fatal("Reading root directory, expected a ReadDirFile but got something else")
}
_, err = dir.ReadDir(-1)
var pathError *fs.PathError
if !errors.As(err, &pathError) {
t.Fatalf("Reading directory on a file, got %#v error, want a %T", err, &fs.PathError{})
}
})
t.Run("NOK Read on a directory", func(t *testing.T) {
file, err := gfs.Open(".")
if err != nil {
t.Fatalf("Opening root directory, expected no error but got %#v", err)
}
dir, ok := file.(fs.ReadDirFile)
if !ok {
t.Fatal("Reading root directory, expected a ReadDirFile but got something else")
}
b := make([]byte, 1)
_, err = dir.Read(b)
var pathError *fs.PathError
if !errors.As(err, &pathError) {
t.Fatalf("Reading bytes on a directory, got %#v error, want %T", err, &fs.PathError{})
}
})
t.Run("OK Stat", func(t *testing.T) {
file, err := gfs.Open(".")
if err != nil {
t.Fatalf("Opening root directory, expected no error but got %#v", err)
}
dir, ok := file.(fs.ReadDirFile)
if !ok {
t.Fatal("Reading root directory, expected a ReadDirFile but got something else")
}
stat, err := dir.Stat()
if err != nil {
t.Fatalf("Getting stat of root directory, expected no error but got %#v", err)
}
if got, want := stat.Name(), "./"; got != want {
t.Fatalf("Reading name of root directory, got %#v files, want %#v", got, want)
}
if got, want := stat.ModTime(), approxModTime; got.After(approxModTime) &&
got.Before(approxModTime.Add(24*time.Hour)) {
t.Fatalf("got modTime %#v, want approx %#v", got, want)
}
if got, want := stat.IsDir(), true; got != want {
t.Fatalf("got isDir %#v, want %#v", got, want)
}
})
}