Skip to content

Commit

Permalink
groot/riofs: provide a stable top-dir to Walk
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastien Binet <binet@cern.ch>
  • Loading branch information
sbinet committed Sep 16, 2022
1 parent 853be00 commit 18cd86e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
5 changes: 4 additions & 1 deletion groot/rcmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ func (cmd *mergeCmd) mergeTasksFrom(o *riofs.File, fname string) ([]task, error)
}
defer f.Close()

// handle relative/absolute path
top := stdpath.Join(f.Name(), ".")

var tsks []task
err = riofs.Walk(f, func(path string, obj root.Object, err error) error {
if err != nil {
return err
}
name := path[len(f.Name()):]
name := path[len(top):]
if name == "" {
return nil
}
Expand Down
15 changes: 14 additions & 1 deletion groot/riofs/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@ var SkipDir = errors.New("riofs: skip this directory") //lint:ignore ST1012 EOF-
//
// If an object exists with multiple cycle values, only the latest one is considered.
func Walk(dir Directory, walkFn WalkFunc) error {
err := walk(dir.(root.Named).Name(), dir.(root.Object), walkFn)
// prepare a "stable" top directory.
// depending on whether the dir is rooted in a file that was created
// with an absolute path, the call to Name() may return a path like:
// ./data/file.root
// the first call to walkFn will be given "./data/file.root" as a 'path'
// argument.
// but the subsequent calls (walking through directories' hierarchy) will
// be given "data/file.root/dir11", instead of the probably expected
// "./data/file.root/dir11".
//
// side-step this by providing directly the "stable" top directory in a
// more regularized form.
top := stdpath.Join(dir.(root.Named).Name(), ".")
err := walk(top, dir.(root.Object), walkFn)
if err == SkipDir {
return nil
}
Expand Down
80 changes: 80 additions & 0 deletions groot/riofs/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"go-hep.org/x/hep/groot/rbase"
"go-hep.org/x/hep/groot/rhist"
"go-hep.org/x/hep/groot/root"
Expand Down Expand Up @@ -403,6 +404,85 @@ func TestFileOf(t *testing.T) {
}
}

func TestWalk(t *testing.T) {
tmp, err := os.MkdirTemp("", "groot-riofs-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

err = os.MkdirAll(stdpath.Join(tmp, "data"), 0755)
if err != nil {
t.Fatal(err)
}

pwd, err := os.Getwd()
if err != nil {
t.Fatalf("could not get working directory: %+v", err)
}
defer os.Chdir(pwd)

err = os.Chdir(tmp)
if err != nil {
t.Fatal(err)
}

fname := "./data/file.root"

f, err := Create(fname)
if err != nil {
t.Fatalf("could not create ROOT file: %+v", err)
}
defer f.Close()

err = os.Chdir(pwd)
if err != nil {
t.Fatal(err)
}

rd := Dir(f)

display := func() string {
o := new(strings.Builder)
err := Walk(f, func(path string, obj root.Object, err error) error {
fmt.Fprintf(o, "%s (%s)\n", path, obj.Class())
return nil
})
if err != nil {
return fmt.Errorf("could not display file content: %w", err).Error()
}
return o.String()
}

for _, name := range []string{
"dir1/dir11/dir111",
"dir1/dir12/dir121",
"dir2/dir21",
"dir2/dir22",
} {
_, err = rd.Mkdir(name)
if err != nil {
t.Fatalf("could not create dir %q: %+v", name, err)
}
}

got := display()
want := `data/file.root (TFile)
data/file.root/dir1 (TDirectoryFile)
data/file.root/dir1/dir11 (TDirectoryFile)
data/file.root/dir1/dir11/dir111 (TDirectoryFile)
data/file.root/dir1/dir12 (TDirectoryFile)
data/file.root/dir1/dir12/dir121 (TDirectoryFile)
data/file.root/dir2 (TDirectoryFile)
data/file.root/dir2/dir21 (TDirectoryFile)
data/file.root/dir2/dir22 (TDirectoryFile)
`
if got != want {
diff := cmp.Diff(want, got)
t.Fatalf("invalid Walk display: -- (-ref +got)\n%s", diff)
}
}

type unknownDirImpl struct{}

func (dir *unknownDirImpl) Get(namecycle string) (root.Object, error) { panic("not implemented") }
Expand Down

0 comments on commit 18cd86e

Please sign in to comment.