From 94b7a85ab56f7ad7d02c74b14b01f55c6b7a4dbb Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 3 Mar 2020 15:34:20 +0000 Subject: [PATCH] test: add Directory.ListNames test --- go.mod | 2 ++ mfs_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/go.mod b/go.mod index 6e1d925..ebfacab 100644 --- a/go.mod +++ b/go.mod @@ -15,3 +15,5 @@ require ( github.com/ipfs/go-unixfs v0.1.0 github.com/libp2p/go-libp2p-testing v0.0.4 ) + +go 1.13 diff --git a/mfs_test.go b/mfs_test.go index 81c63f9..3c08fd9 100644 --- a/mfs_test.go +++ b/mfs_test.go @@ -620,6 +620,48 @@ func TestMfsFile(t *testing.T) { } } +func TestMfsDirListNames(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + ds, rt := setupRoot(ctx, t) + + rootdir := rt.GetDirectory() + + rand.Seed(time.Now().UTC().UnixNano()) + + total := rand.Intn(10) + 1 + fNames := make([]string, 0, total) + + for i := 0; i < total; i++ { + fn := randomName() + fNames = append(fNames, fn) + nd := getRandFile(t, ds, rand.Int63n(1000)+1) + err := rootdir.AddChild(fn, nd) + if err != nil { + t.Fatal(err) + } + } + + list, err := rootdir.ListNames(ctx) + + if err != nil { + t.Fatal(err) + } + + for _, lName := range list { + found := false + for _, fName := range fNames { + if lName == fName { + found = true + break + } + } + if !found { + t.Fatal(lName + " not found in directory listing") + } + } +} + func randomWalk(d *Directory, n int) (*Directory, error) { for i := 0; i < n; i++ { dirents, err := d.List(context.Background())