@@ -13,7 +13,6 @@ import (
1313 "github.com/stretchr/testify/require"
1414)
1515
16-
1716func TestOpen (t * testing.T ) {
1817 t .Parallel ()
1918
@@ -196,6 +195,12 @@ func TestReadDir(t *testing.T) {
196195 fs : testdata .GetTestData (),
197196 want : []string {"testdata" },
198197 },
198+ {
199+ name : "nested directory" ,
200+ path : "testdata/subdir" ,
201+ fs : testdata .GetTestData (),
202+ want : []string {"nested.txt" },
203+ },
199204 }
200205
201206 for _ , tc := range tests {
@@ -212,9 +217,14 @@ func TestReadDir(t *testing.T) {
212217 assert .Len (t , fis , len (tc .want ))
213218 matched := 0
214219
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 {
218228 matched ++
219229 }
220230 }
@@ -260,6 +270,8 @@ func TestFileUnsupported(t *testing.T) {
260270}
261271
262272func TestFileSeek (t * testing.T ) {
273+ t .Parallel ()
274+
263275 fs := New (testdata .GetTestData ())
264276
265277 f , err := fs .Open ("testdata/file2.txt" )
@@ -271,14 +283,14 @@ func TestFileSeek(t *testing.T) {
271283 seekWhence int
272284 want string
273285 }{
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
278290 {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
282294 }
283295
284296 for i , tc := range tests {
@@ -333,77 +345,73 @@ func TestJoin(t *testing.T) {
333345 }
334346}
335347
336- // Additional comprehensive tests using rich test data
337-
338- func TestEmbedfs_ComprehensiveOpen (t * testing.T ) {
348+ func TestComprehensiveOpen (t * testing.T ) {
339349 t .Parallel ()
340-
350+
341351 fs := New (testdata .GetTestData ())
342-
352+
343353 // Test opening existing embedded file with content
344354 f , err := fs .Open ("/testdata/file1.txt" )
345355 require .NoError (t , err )
346356 assert .Equal (t , "testdata/file1.txt" , f .Name ())
347357 require .NoError (t , f .Close ())
348358}
349359
350- func TestEmbedfs_ComprehensiveRead (t * testing.T ) {
360+ func TestComprehensiveRead (t * testing.T ) {
351361 t .Parallel ()
352-
362+
353363 fs := New (testdata .GetTestData ())
354-
364+
355365 f , err := fs .Open ("/testdata/file1.txt" )
356366 require .NoError (t , err )
357367 defer f .Close ()
358-
368+
359369 // Read the actual content
360370 buf := make ([]byte , 100 )
361371 n , err := f .Read (buf )
362372 require .NoError (t , err )
363373 assert .Equal (t , "Hello from embedfs!" , string (buf [:n ]))
364374}
365375
366- func TestEmbedfs_NestedFileOperations (t * testing.T ) {
376+ func TestNestedFileOperations (t * testing.T ) {
367377 t .Parallel ()
368-
378+
369379 fs := New (testdata .GetTestData ())
370-
380+
371381 // Test nested file read
372382 f , err := fs .Open ("/testdata/subdir/nested.txt" )
373383 require .NoError (t , err )
374384 defer f .Close ()
375-
385+
376386 buf := make ([]byte , 100 )
377387 n , err := f .Read (buf )
378388 require .NoError (t , err )
379389 assert .Equal (t , "Nested file content" , string (buf [:n ]))
380390}
381391
382- func TestEmbedfs_PathNormalization (t * testing.T ) {
392+ func TestPathNormalization (t * testing.T ) {
383393 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
388398 tests := []struct {
389- name string
390- path string
399+ name string
400+ input string
401+ expected string
391402 }{
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" , "" , "" },
396409 }
397-
410+
398411 for _ , tt := range tests {
399412 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 )
407415 })
408416 }
409417}
@@ -428,15 +436,15 @@ func TestFile_ReadAt(t *testing.T) {
428436 {"middle" , 6 , 4 , "from" },
429437 {"end" , 15 , 4 , "dfs!" },
430438 {"full content" , 0 , 19 , "Hello from embedfs!" },
431- {"beyond end" , 100 , 10 , "" }, // Should return EOF
439+ {"beyond end" , 100 , 10 , "" }, // Should return EOF
432440 }
433441
434442 for _ , tt := range tests {
435443 t .Run (tt .name , func (t * testing.T ) {
436444 buf := make ([]byte , tt .length )
437445 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
440448 require .Error (t , err )
441449 assert .Equal (t , 0 , n )
442450 } else {
@@ -510,3 +518,54 @@ func TestFile_LockUnlock(t *testing.T) {
510518 err = f .Unlock ()
511519 require .NoError (t , err )
512520}
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