@@ -12,6 +12,7 @@ import (
12
12
"strings"
13
13
"testing"
14
14
15
+ "github.com/google/go-cmp/cmp"
15
16
"go-hep.org/x/hep/groot/rbase"
16
17
"go-hep.org/x/hep/groot/rhist"
17
18
"go-hep.org/x/hep/groot/root"
@@ -403,6 +404,85 @@ func TestFileOf(t *testing.T) {
403
404
}
404
405
}
405
406
407
+ func TestWalk (t * testing.T ) {
408
+ tmp , err := os .MkdirTemp ("" , "groot-riofs-" )
409
+ if err != nil {
410
+ t .Fatal (err )
411
+ }
412
+ defer os .RemoveAll (tmp )
413
+
414
+ err = os .MkdirAll (stdpath .Join (tmp , "data" ), 0755 )
415
+ if err != nil {
416
+ t .Fatal (err )
417
+ }
418
+
419
+ pwd , err := os .Getwd ()
420
+ if err != nil {
421
+ t .Fatalf ("could not get working directory: %+v" , err )
422
+ }
423
+ defer os .Chdir (pwd )
424
+
425
+ err = os .Chdir (tmp )
426
+ if err != nil {
427
+ t .Fatal (err )
428
+ }
429
+
430
+ fname := "./data/file.root"
431
+
432
+ f , err := Create (fname )
433
+ if err != nil {
434
+ t .Fatalf ("could not create ROOT file: %+v" , err )
435
+ }
436
+ defer f .Close ()
437
+
438
+ err = os .Chdir (pwd )
439
+ if err != nil {
440
+ t .Fatal (err )
441
+ }
442
+
443
+ rd := Dir (f )
444
+
445
+ display := func () string {
446
+ o := new (strings.Builder )
447
+ err := Walk (f , func (path string , obj root.Object , err error ) error {
448
+ fmt .Fprintf (o , "%s (%s)\n " , path , obj .Class ())
449
+ return nil
450
+ })
451
+ if err != nil {
452
+ return fmt .Errorf ("could not display file content: %w" , err ).Error ()
453
+ }
454
+ return o .String ()
455
+ }
456
+
457
+ for _ , name := range []string {
458
+ "dir1/dir11/dir111" ,
459
+ "dir1/dir12/dir121" ,
460
+ "dir2/dir21" ,
461
+ "dir2/dir22" ,
462
+ } {
463
+ _ , err = rd .Mkdir (name )
464
+ if err != nil {
465
+ t .Fatalf ("could not create dir %q: %+v" , name , err )
466
+ }
467
+ }
468
+
469
+ got := display ()
470
+ want := `data/file.root (TFile)
471
+ data/file.root/dir1 (TDirectoryFile)
472
+ data/file.root/dir1/dir11 (TDirectoryFile)
473
+ data/file.root/dir1/dir11/dir111 (TDirectoryFile)
474
+ data/file.root/dir1/dir12 (TDirectoryFile)
475
+ data/file.root/dir1/dir12/dir121 (TDirectoryFile)
476
+ data/file.root/dir2 (TDirectoryFile)
477
+ data/file.root/dir2/dir21 (TDirectoryFile)
478
+ data/file.root/dir2/dir22 (TDirectoryFile)
479
+ `
480
+ if got != want {
481
+ diff := cmp .Diff (want , got )
482
+ t .Fatalf ("invalid Walk display: -- (-ref +got)\n %s" , diff )
483
+ }
484
+ }
485
+
406
486
type unknownDirImpl struct {}
407
487
408
488
func (dir * unknownDirImpl ) Get (namecycle string ) (root.Object , error ) { panic ("not implemented" ) }
0 commit comments