fix(mfs): Directory.Path not working, add test
Credit goes to @ridewindx License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Mar 20, 2018 at 13:55 UTC
48f706b0f3c90c776506f6e2e1240f79a051b9da
2 files changed
+14
-2
mfs/dir.go
+9
-2
@@ -404,8 +404,15 @@ func (d *Directory) Path() string {
404
cur := d
405
var out string
406
for cur != nil {
407
- out = path.Join(cur.name, out)
408
- cur = cur.parent.(*Directory)
407
+ switch parent := cur.parent.(type) {
408
+ case *Directory:
409
+ out = path.Join(cur.name, out)
410
+ cur = parent
411
+ case *Root:
412
+ return "/" + out
413
+ default:
414
+ panic("directory parent neither a directory nor a root")
415
+ }
416
}
417
return out
418
}
mfs/mfs_test.go
+5
@@ -324,6 +324,11 @@ func TestDirectoryLoadFromDag(t *testing.T) {
324
325
topd := topi.(*Directory)
326
327
+ path := topd.Path()
328
+ if path != "/foo" {
329
+ t.Fatalf("Expected path '/foo', got '%s'", path)
330
+ }
331
+
332
// mkdir over existing but unloaded child file should fail
333
_, err = topd.Mkdir("a")
334
if err == nil {