@@ -25,6 +25,32 @@ func (s *FilesystemSuite) TestCreate(c *C) {
25
25
c .Assert (f .Filename (), Equals , "foo" )
26
26
}
27
27
28
+ func (s * FilesystemSuite ) TestOpen (c * C ) {
29
+ f , err := s .FS .Create ("foo" )
30
+ c .Assert (err , IsNil )
31
+ c .Assert (f .Filename (), Equals , "foo" )
32
+ c .Assert (f .Close (), IsNil )
33
+
34
+ f , err = s .FS .Open ("foo" )
35
+ c .Assert (err , IsNil )
36
+ c .Assert (f .Filename (), Equals , "foo" )
37
+ }
38
+
39
+ func (s * FilesystemSuite ) TestOpenNotExists (c * C ) {
40
+ f , err := s .FS .Open ("not-exists" )
41
+ c .Assert (err , NotNil )
42
+ c .Assert (f , IsNil )
43
+ }
44
+
45
+ func (s * FilesystemSuite ) TestCreateDir (c * C ) {
46
+ err := s .FS .MkdirAll ("foo" , 0644 )
47
+ c .Assert (err , IsNil )
48
+
49
+ f , err := s .FS .Create ("foo" )
50
+ c .Assert (err , NotNil )
51
+ c .Assert (f , IsNil )
52
+ }
53
+
28
54
func (s * FilesystemSuite ) TestCreateDepth (c * C ) {
29
55
f , err := s .FS .Create ("bar/foo" )
30
56
c .Assert (err , IsNil )
@@ -76,6 +102,36 @@ func (s *FilesystemSuite) TestCreateClose(c *C) {
76
102
c .Assert (f .Close (), IsNil )
77
103
}
78
104
105
+ func (s * FilesystemSuite ) TestOpenFile (c * C ) {
106
+ defaultMode := os .FileMode (0666 )
107
+
108
+ f , err := s .FS .OpenFile ("foo1" , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , defaultMode )
109
+ c .Assert (err , IsNil )
110
+ s .testWriteClose (c , f , "foo1" )
111
+
112
+ // Truncate if it exists
113
+ f , err = s .FS .OpenFile ("foo1" , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , defaultMode )
114
+ c .Assert (err , IsNil )
115
+ c .Assert (f .Filename (), Equals , "foo1" )
116
+ s .testWriteClose (c , f , "foo1overwritten" )
117
+
118
+ // Read-only if it exists
119
+ f , err = s .FS .OpenFile ("foo1" , os .O_RDONLY , defaultMode )
120
+ c .Assert (err , IsNil )
121
+ c .Assert (f .Filename (), Equals , "foo1" )
122
+ s .testReadClose (c , f , "foo1overwritten" )
123
+
124
+ // Create when it does exist
125
+ f , err = s .FS .OpenFile ("foo1" , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , defaultMode )
126
+ c .Assert (err , IsNil )
127
+ c .Assert (f .Filename (), Equals , "foo1" )
128
+ s .testWriteClose (c , f , "bar" )
129
+
130
+ f , err = s .FS .OpenFile ("foo1" , os .O_RDONLY , defaultMode )
131
+ c .Assert (err , IsNil )
132
+ s .testReadClose (c , f , "bar" )
133
+ }
134
+
79
135
func (s * FilesystemSuite ) TestOpenFileNoTruncate (c * C ) {
80
136
defaultMode := os .FileMode (0666 )
81
137
@@ -142,36 +198,6 @@ func (s *FilesystemSuite) TestOpenFileReadWrite(c *C) {
142
198
s .testReadClose (c , f , "quxbar" )
143
199
}
144
200
145
- func (s * FilesystemSuite ) TestOpenFile (c * C ) {
146
- defaultMode := os .FileMode (0666 )
147
-
148
- f , err := s .FS .OpenFile ("foo1" , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , defaultMode )
149
- c .Assert (err , IsNil )
150
- s .testWriteClose (c , f , "foo1" )
151
-
152
- // Truncate if it exists
153
- f , err = s .FS .OpenFile ("foo1" , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , defaultMode )
154
- c .Assert (err , IsNil )
155
- c .Assert (f .Filename (), Equals , "foo1" )
156
- s .testWriteClose (c , f , "foo1overwritten" )
157
-
158
- // Read-only if it exists
159
- f , err = s .FS .OpenFile ("foo1" , os .O_RDONLY , defaultMode )
160
- c .Assert (err , IsNil )
161
- c .Assert (f .Filename (), Equals , "foo1" )
162
- s .testReadClose (c , f , "foo1overwritten" )
163
-
164
- // Create when it does exist
165
- f , err = s .FS .OpenFile ("foo1" , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , defaultMode )
166
- c .Assert (err , IsNil )
167
- c .Assert (f .Filename (), Equals , "foo1" )
168
- s .testWriteClose (c , f , "bar" )
169
-
170
- f , err = s .FS .OpenFile ("foo1" , os .O_RDONLY , defaultMode )
171
- c .Assert (err , IsNil )
172
- s .testReadClose (c , f , "bar" )
173
- }
174
-
175
201
func (s * FilesystemSuite ) TestOpenFileWithModes (c * C ) {
176
202
f , err := s .FS .OpenFile ("foo" , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , 0755 )
177
203
c .Assert (err , IsNil )
@@ -196,41 +222,114 @@ func (s *FilesystemSuite) testReadClose(c *C, f File, content string) {
196
222
c .Assert (f .Close (), IsNil )
197
223
}
198
224
199
- func (s * FilesystemSuite ) TestFileCreateReadSeek (c * C ) {
225
+ func (s * FilesystemSuite ) TestFileWrite (c * C ) {
200
226
f , err := s .FS .Create ("foo" )
201
227
c .Assert (err , IsNil )
202
228
203
- n , err := f .Write ([]byte ("0123456789abcdefghijklmnopqrstuvwxyz " ))
229
+ n , err := f .Write ([]byte ("foo " ))
204
230
c .Assert (err , IsNil )
205
- c .Assert (n , Equals , 36 )
231
+ c .Assert (n , Equals , 3 )
206
232
207
- p , err := f .Seek (10 , io .SeekStart )
233
+ f .Seek (0 , io .SeekStart )
234
+ all , err := ioutil .ReadAll (f )
235
+ c .Assert (err , IsNil )
236
+ c .Assert (string (all ), Equals , "foo" )
237
+ c .Assert (f .Close (), IsNil )
238
+ }
239
+
240
+ func (s * FilesystemSuite ) TestFileWriteClose (c * C ) {
241
+ f , err := s .FS .Create ("foo" )
242
+ c .Assert (err , IsNil )
243
+
244
+ c .Assert (f .Close (), IsNil )
245
+
246
+ _ , err = f .Write ([]byte ("foo" ))
247
+ c .Assert (err , NotNil )
248
+ }
249
+
250
+ func (s * FilesystemSuite ) TestFileRead (c * C ) {
251
+ err := WriteFile (s .FS , "foo" , []byte ("foo" ), 0644 )
252
+ c .Assert (err , IsNil )
253
+
254
+ f , err := s .FS .Open ("foo" )
208
255
c .Assert (err , IsNil )
209
- c .Assert (int (p ), Equals , 10 )
210
256
211
257
all , err := ioutil .ReadAll (f )
212
258
c .Assert (err , IsNil )
213
- c .Assert (string (all ), Equals , "abcdefghijklmnopqrstuvwxyz " )
259
+ c .Assert (string (all ), Equals , "foo " )
214
260
c .Assert (f .Close (), IsNil )
215
261
}
216
262
217
- func (s * FilesystemSuite ) TestFileOpenReadSeek (c * C ) {
263
+ func (s * FilesystemSuite ) TestFileClosed (c * C ) {
264
+ err := WriteFile (s .FS , "foo" , []byte ("foo" ), 0644 )
265
+ c .Assert (err , IsNil )
266
+
267
+ f , err := s .FS .Open ("foo" )
268
+ c .Assert (err , IsNil )
269
+ c .Assert (f .Close (), IsNil )
270
+
271
+ _ , err = ioutil .ReadAll (f )
272
+ c .Assert (err , NotNil )
273
+ }
274
+
275
+ func (s * FilesystemSuite ) TestFileNonRead (c * C ) {
276
+ err := WriteFile (s .FS , "foo" , []byte ("foo" ), 0644 )
277
+ c .Assert (err , IsNil )
278
+
279
+ f , err := s .FS .OpenFile ("foo" , os .O_WRONLY , 0 )
280
+ c .Assert (err , IsNil )
281
+
282
+ _ , err = ioutil .ReadAll (f )
283
+ c .Assert (err , NotNil )
284
+ }
285
+
286
+ func (s * FilesystemSuite ) TestFileSeekstart (c * C ) {
287
+ s .testFileSeek (c , 10 , io .SeekStart )
288
+ }
289
+
290
+ func (s * FilesystemSuite ) TestFileSeekCurrent (c * C ) {
291
+ s .testFileSeek (c , 5 , io .SeekCurrent )
292
+ }
293
+
294
+ func (s * FilesystemSuite ) TestFileSeekEnd (c * C ) {
295
+ s .testFileSeek (c , - 26 , io .SeekEnd )
296
+ }
297
+
298
+ func (s * FilesystemSuite ) testFileSeek (c * C , offset int64 , whence int ) {
218
299
err := WriteFile (s .FS , "foo" , []byte ("0123456789abcdefghijklmnopqrstuvwxyz" ), 0644 )
219
300
c .Assert (err , IsNil )
220
301
221
302
f , err := s .FS .Open ("foo" )
222
303
c .Assert (err , IsNil )
223
304
224
- p , err := f .Seek (10 , io .SeekStart )
305
+ some := make ([]byte , 5 )
306
+ _ , err = f .Read (some )
307
+ c .Assert (err , IsNil )
308
+ c .Assert (string (some ), Equals , "01234" )
309
+
310
+ p , err := f .Seek (offset , whence )
225
311
c .Assert (err , IsNil )
226
312
c .Assert (int (p ), Equals , 10 )
227
313
228
314
all , err := ioutil .ReadAll (f )
229
315
c .Assert (err , IsNil )
316
+ c .Assert (all , HasLen , 26 )
230
317
c .Assert (string (all ), Equals , "abcdefghijklmnopqrstuvwxyz" )
231
318
c .Assert (f .Close (), IsNil )
232
319
}
233
320
321
+ func (s * FilesystemSuite ) TestFileSeekClosed (c * C ) {
322
+ err := WriteFile (s .FS , "foo" , []byte ("foo" ), 0644 )
323
+ c .Assert (err , IsNil )
324
+
325
+ f , err := s .FS .Open ("foo" )
326
+ c .Assert (err , IsNil )
327
+ c .Assert (f .Close (), IsNil )
328
+
329
+ _ , err = f .Seek (0 , 0 )
330
+ c .Assert (err , NotNil )
331
+ }
332
+
234
333
func (s * FilesystemSuite ) TestFileCloseTwice (c * C ) {
235
334
f , err := s .FS .Create ("foo" )
236
335
c .Assert (err , IsNil )
@@ -260,6 +359,31 @@ func (s *FilesystemSuite) TestReadDirAndDir(c *C) {
260
359
c .Assert (info , HasLen , 2 )
261
360
}
262
361
362
+ func (s * FilesystemSuite ) TestReadDirAndWithMkDirAll (c * C ) {
363
+ err := s .FS .MkdirAll ("qux" , 0644 )
364
+ c .Assert (err , IsNil )
365
+
366
+ files := []string {"qux/baz" , "qux/qux" }
367
+ for _ , name := range files {
368
+ err := WriteFile (s .FS , name , nil , 0644 )
369
+ c .Assert (err , IsNil )
370
+ }
371
+
372
+ info , err := s .FS .ReadDir ("/" )
373
+ c .Assert (err , IsNil )
374
+ c .Assert (info , HasLen , 1 )
375
+ c .Assert (info [0 ].IsDir (), Equals , true )
376
+
377
+ info , err = s .FS .ReadDir ("/qux" )
378
+ c .Assert (err , IsNil )
379
+ c .Assert (info , HasLen , 2 )
380
+
381
+ qux := s .FS .Dir ("/qux" )
382
+ info , err = qux .ReadDir ("/" )
383
+ c .Assert (err , IsNil )
384
+ c .Assert (info , HasLen , 2 )
385
+ }
386
+
263
387
func (s * FilesystemSuite ) TestReadDirFileInfo (c * C ) {
264
388
err := WriteFile (s .FS , "foo" , []byte {'F' , 'O' , 'O' }, 0644 )
265
389
c .Assert (err , IsNil )
@@ -357,6 +481,33 @@ func (s *FilesystemSuite) TestRename(c *C) {
357
481
c .Assert (err , IsNil )
358
482
}
359
483
484
+ func (s * FilesystemSuite ) TestRenameDir (c * C ) {
485
+ err := s .FS .MkdirAll ("foo" , 0644 )
486
+ c .Assert (err , IsNil )
487
+
488
+ err = WriteFile (s .FS , "foo/bar" , nil , 0644 )
489
+ c .Assert (err , IsNil )
490
+
491
+ err = s .FS .Rename ("foo" , "bar" )
492
+ c .Assert (err , IsNil )
493
+
494
+ dirfoo , err := s .FS .Stat ("foo" )
495
+ c .Assert (dirfoo , IsNil )
496
+ c .Assert (os .IsNotExist (err ), Equals , true )
497
+
498
+ dirbar , err := s .FS .Stat ("bar" )
499
+ c .Assert (err , IsNil )
500
+ c .Assert (dirbar , NotNil )
501
+
502
+ foo , err := s .FS .Stat ("foo/bar" )
503
+ c .Assert (os .IsNotExist (err ), Equals , true )
504
+ c .Assert (foo , IsNil )
505
+
506
+ bar , err := s .FS .Stat ("bar/bar" )
507
+ c .Assert (err , IsNil )
508
+ c .Assert (bar , NotNil )
509
+ }
510
+
360
511
func (s * FilesystemSuite ) TestTempFile (c * C ) {
361
512
f , err := s .FS .TempFile ("" , "bar" )
362
513
c .Assert (err , IsNil )
@@ -585,14 +736,14 @@ func (s *FilesystemSuite) TestMkdirAllNested(c *C) {
585
736
}
586
737
587
738
func (s * FilesystemSuite ) TestMkdirAllIdempotent (c * C ) {
588
- err := s .FS .MkdirAll ("empty" , os . FileMode ( 0755 ) )
739
+ err := s .FS .MkdirAll ("empty" , 0755 )
589
740
c .Assert (err , IsNil )
590
741
fi , err := s .FS .Stat ("empty" )
591
742
c .Assert (err , IsNil )
592
743
c .Assert (fi .IsDir (), Equals , true )
593
744
594
745
// idempotent
595
- err = s .FS .MkdirAll ("empty" , os . FileMode ( 0755 ) )
746
+ err = s .FS .MkdirAll ("empty" , 0755 )
596
747
c .Assert (err , IsNil )
597
748
fi , err = s .FS .Stat ("empty" )
598
749
c .Assert (err , IsNil )
0 commit comments