From 48f706b0f3c90c776506f6e2e1240f79a051b9da Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 20 Mar 2018 13:55:52 +0100 Subject: [PATCH] fix(mfs): Directory.Path not working, add test Credit goes to @ridewindx License: MIT Signed-off-by: Jakub Sztandera --- mfs/dir.go | 11 +++++++++-- mfs/mfs_test.go | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mfs/dir.go b/mfs/dir.go index 08b703fb2..0cc6e6568 100644 --- a/mfs/dir.go +++ b/mfs/dir.go @@ -404,8 +404,15 @@ func (d *Directory) Path() string { cur := d var out string for cur != nil { - out = path.Join(cur.name, out) - cur = cur.parent.(*Directory) + switch parent := cur.parent.(type) { + case *Directory: + out = path.Join(cur.name, out) + cur = parent + case *Root: + return "/" + out + default: + panic("directory parent neither a directory nor a root") + } } return out } diff --git a/mfs/mfs_test.go b/mfs/mfs_test.go index d7124bdf1..25e61854b 100644 --- a/mfs/mfs_test.go +++ b/mfs/mfs_test.go @@ -324,6 +324,11 @@ func TestDirectoryLoadFromDag(t *testing.T) { topd := topi.(*Directory) + path := topd.Path() + if path != "/foo" { + t.Fatalf("Expected path '/foo', got '%s'", path) + } + // mkdir over existing but unloaded child file should fail _, err = topd.Mkdir("a") if err == nil {