@@ -13,7 +13,6 @@ import (
13
13
"github.com/stretchr/testify/require"
14
14
)
15
15
16
-
17
16
func TestOpen (t * testing.T ) {
18
17
t .Parallel ()
19
18
@@ -196,6 +195,12 @@ func TestReadDir(t *testing.T) {
196
195
fs : testdata .GetTestData (),
197
196
want : []string {"testdata" },
198
197
},
198
+ {
199
+ name : "nested directory" ,
200
+ path : "testdata/subdir" ,
201
+ fs : testdata .GetTestData (),
202
+ want : []string {"nested.txt" },
203
+ },
199
204
}
200
205
201
206
for _ , tc := range tests {
@@ -212,9 +217,14 @@ func TestReadDir(t *testing.T) {
212
217
assert .Len (t , fis , len (tc .want ))
213
218
matched := 0
214
219
215
- for _ , n := range fis {
216
- for _ , w := range tc .want {
217
- if n .Name () == w {
220
+ for _ , fi := range fis {
221
+ // Verify all entries have proper FileInfo
222
+ assert .NotEmpty (t , fi .Name ())
223
+ assert .NotNil (t , fi .ModTime ())
224
+ assert .Greater (t , fi .Size (), int64 (- 1 )) // Size can be 0 but not negative
225
+
226
+ for _ , expected := range tc .want {
227
+ if fi .Name () == expected {
218
228
matched ++
219
229
}
220
230
}
@@ -260,6 +270,8 @@ func TestFileUnsupported(t *testing.T) {
260
270
}
261
271
262
272
func TestFileSeek (t * testing.T ) {
273
+ t .Parallel ()
274
+
263
275
fs := New (testdata .GetTestData ())
264
276
265
277
f , err := fs .Open ("testdata/file2.txt" )
@@ -271,14 +283,14 @@ func TestFileSeek(t *testing.T) {
271
283
seekWhence int
272
284
want string
273
285
}{
274
- {seekOff : 8 , seekWhence : io .SeekStart , want : "test file" }, // pos now at 17
275
- {seekOff : 8 , seekWhence : io .SeekStart , want : "t" }, // pos now at 9
276
- {seekOff : 9 , seekWhence : io .SeekStart , want : "est" }, // pos now at 12
277
- {seekOff : 1 , seekWhence : io .SeekStart , want : "nother test file" }, // pos now at 17
286
+ {seekOff : 8 , seekWhence : io .SeekStart , want : "test file" }, // pos now at 17
287
+ {seekOff : 8 , seekWhence : io .SeekStart , want : "t" }, // pos now at 9
288
+ {seekOff : 9 , seekWhence : io .SeekStart , want : "est" }, // pos now at 12
289
+ {seekOff : 1 , seekWhence : io .SeekStart , want : "nother test file" }, // pos now at 17
278
290
{seekOff : 0 , seekWhence : io .SeekStart , want : "Another test file" }, // pos now at 17
279
- {seekOff : 0 , seekWhence : io .SeekStart , want : "A" }, // pos now at 1
280
- {seekOff : 0 , seekWhence : io .SeekCurrent , want : "n" }, // pos now at 2
281
- {seekOff : - 4 , seekWhence : io .SeekEnd , want : "file" }, // pos now at 17
291
+ {seekOff : 0 , seekWhence : io .SeekStart , want : "A" }, // pos now at 1
292
+ {seekOff : 0 , seekWhence : io .SeekCurrent , want : "n" }, // pos now at 2
293
+ {seekOff : - 4 , seekWhence : io .SeekEnd , want : "file" }, // pos now at 17
282
294
}
283
295
284
296
for i , tc := range tests {
@@ -333,77 +345,73 @@ func TestJoin(t *testing.T) {
333
345
}
334
346
}
335
347
336
- // Additional comprehensive tests using rich test data
337
-
338
- func TestEmbedfs_ComprehensiveOpen (t * testing.T ) {
348
+ func TestComprehensiveOpen (t * testing.T ) {
339
349
t .Parallel ()
340
-
350
+
341
351
fs := New (testdata .GetTestData ())
342
-
352
+
343
353
// Test opening existing embedded file with content
344
354
f , err := fs .Open ("/testdata/file1.txt" )
345
355
require .NoError (t , err )
346
356
assert .Equal (t , "testdata/file1.txt" , f .Name ())
347
357
require .NoError (t , f .Close ())
348
358
}
349
359
350
- func TestEmbedfs_ComprehensiveRead (t * testing.T ) {
360
+ func TestComprehensiveRead (t * testing.T ) {
351
361
t .Parallel ()
352
-
362
+
353
363
fs := New (testdata .GetTestData ())
354
-
364
+
355
365
f , err := fs .Open ("/testdata/file1.txt" )
356
366
require .NoError (t , err )
357
367
defer f .Close ()
358
-
368
+
359
369
// Read the actual content
360
370
buf := make ([]byte , 100 )
361
371
n , err := f .Read (buf )
362
372
require .NoError (t , err )
363
373
assert .Equal (t , "Hello from embedfs!" , string (buf [:n ]))
364
374
}
365
375
366
- func TestEmbedfs_NestedFileOperations (t * testing.T ) {
376
+ func TestNestedFileOperations (t * testing.T ) {
367
377
t .Parallel ()
368
-
378
+
369
379
fs := New (testdata .GetTestData ())
370
-
380
+
371
381
// Test nested file read
372
382
f , err := fs .Open ("/testdata/subdir/nested.txt" )
373
383
require .NoError (t , err )
374
384
defer f .Close ()
375
-
385
+
376
386
buf := make ([]byte , 100 )
377
387
n , err := f .Read (buf )
378
388
require .NoError (t , err )
379
389
assert .Equal (t , "Nested file content" , string (buf [:n ]))
380
390
}
381
391
382
- func TestEmbedfs_PathNormalization (t * testing.T ) {
392
+ func TestPathNormalization (t * testing.T ) {
383
393
t .Parallel ()
384
-
385
- fs := New ( testdata .GetTestData ())
386
-
387
- // Test that our path normalization works across all methods
394
+
395
+ fs := & Embed { underlying : testdata .GetTestData ()}
396
+
397
+ // Test that our path normalization works correctly
388
398
tests := []struct {
389
- name string
390
- path string
399
+ name string
400
+ input string
401
+ expected string
391
402
}{
392
- {"root" , "/" },
393
- {"top-level" , "/testdata" },
394
- {"nested" , "/testdata/subdir" },
395
- {"deep file" , "/testdata/subdir/nested.txt" },
403
+ {"root" , "/" , "." },
404
+ {"top-level" , "/testdata" , "testdata" },
405
+ {"nested" , "/testdata/subdir" , "testdata/subdir" },
406
+ {"deep file" , "/testdata/subdir/nested.txt" , "testdata/subdir/nested.txt" },
407
+ {"relative path" , "testdata" , "testdata" },
408
+ {"empty path" , "" , "" },
396
409
}
397
-
410
+
398
411
for _ , tt := range tests {
399
412
t .Run (tt .name , func (t * testing.T ) {
400
- // All these should work with our path normalization
401
- _ , err := fs .Stat (tt .path )
402
- if tt .name == "deep file" {
403
- require .NoError (t , err , "file should exist" )
404
- } else {
405
- require .NoError (t , err , "directory should exist" )
406
- }
413
+ result := fs .normalizePath (tt .input )
414
+ assert .Equal (t , tt .expected , result )
407
415
})
408
416
}
409
417
}
@@ -428,15 +436,15 @@ func TestFile_ReadAt(t *testing.T) {
428
436
{"middle" , 6 , 4 , "from" },
429
437
{"end" , 15 , 4 , "dfs!" },
430
438
{"full content" , 0 , 19 , "Hello from embedfs!" },
431
- {"beyond end" , 100 , 10 , "" }, // Should return EOF
439
+ {"beyond end" , 100 , 10 , "" }, // Should return EOF
432
440
}
433
441
434
442
for _ , tt := range tests {
435
443
t .Run (tt .name , func (t * testing.T ) {
436
444
buf := make ([]byte , tt .length )
437
445
n , err := f .ReadAt (buf , tt .offset )
438
-
439
- if tt .offset >= 19 { // Beyond file size
446
+
447
+ if tt .offset >= 19 { // Beyond file size
440
448
require .Error (t , err )
441
449
assert .Equal (t , 0 , n )
442
450
} else {
@@ -510,3 +518,54 @@ func TestFile_LockUnlock(t *testing.T) {
510
518
err = f .Unlock ()
511
519
require .NoError (t , err )
512
520
}
521
+
522
+ func TestReadDirDirectories (t * testing.T ) {
523
+ t .Parallel ()
524
+
525
+ fs := New (testdata .GetTestData ())
526
+
527
+ entries , err := fs .ReadDir ("/testdata" )
528
+ require .NoError (t , err )
529
+
530
+ // Find the subdirectory entry
531
+ var subdirEntry os.FileInfo
532
+ for _ , entry := range entries {
533
+ if entry .Name () == "subdir" {
534
+ subdirEntry = entry
535
+ break
536
+ }
537
+ }
538
+
539
+ require .NotNil (t , subdirEntry , "subdir should be found" )
540
+ assert .True (t , subdirEntry .IsDir (), "subdir should be a directory" )
541
+ assert .Equal (t , "subdir" , subdirEntry .Name ())
542
+ }
543
+
544
+ func TestEmptyFileHandling (t * testing.T ) {
545
+ t .Parallel ()
546
+
547
+ fs := New (testdata .GetTestData ())
548
+
549
+ // Test empty file stat
550
+ fi , err := fs .Stat ("/testdata/empty.txt" )
551
+ require .NoError (t , err )
552
+ assert .Equal (t , "empty.txt" , fi .Name ())
553
+ assert .False (t , fi .IsDir ())
554
+ assert .Equal (t , int64 (0 ), fi .Size ())
555
+
556
+ // Test opening empty file
557
+ f , err := fs .Open ("/testdata/empty.txt" )
558
+ require .NoError (t , err )
559
+ defer f .Close ()
560
+
561
+ // Test reading from empty file
562
+ buf := make ([]byte , 10 )
563
+ n , err := f .Read (buf )
564
+ require .Error (t , err ) // Should be EOF
565
+ assert .Equal (t , 0 , n )
566
+
567
+ // Test ReadAt on empty file
568
+ n , err = f .ReadAt (buf , 0 )
569
+ require .Error (t , err ) // Should be EOF
570
+ assert .Equal (t , 0 , n )
571
+ }
0 commit comments