diff --git a/memmap.go b/memmap.go index d6c744e..003751f 100644 --- a/memmap.go +++ b/memmap.go @@ -14,13 +14,10 @@ package afero import ( - "fmt" "io" - "log" "os" "path/filepath" - "sort" "strings" "sync" @@ -457,9 +454,11 @@ func (m *MemMapFs) Chtimes(name string, atime time.Time, mtime time.Time) error return nil } -func (m *MemMapFs) List() { - for _, x := range m.data { - y := mem.FileInfo{FileData: x} - fmt.Println(x.Name(), y.Size()) +func (m *MemMapFs) List() map[string]*mem.FileInfo { + files := make(map[string]*mem.FileInfo) + for _, f := range m.data { + files[f.Name()] = mem.GetFileInfo(f) } + + return files } diff --git a/memmap_test.go b/memmap_test.go index c47fadc..cf0bfff 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -6,7 +6,9 @@ import ( "io/fs" "os" "path/filepath" + "reflect" "runtime" + "sort" "strings" "sync" "testing" @@ -918,3 +920,37 @@ func TestMemMapFsRename(t *testing.T) { } } } + +func TestMemFsList(t *testing.T) { + t.Parallel() + + fs := NewMemMapFs() + + if err := WriteFile(fs, "file.txt", []byte("abc"), 0777); err != nil { + t.Fatal(err) + } + + if err := WriteFile(fs, "another/file.txt", []byte("abc"), 0777); err != nil { + t.Fatal(err) + } + + files := fs.(*MemMapFs).List() + if len(files) != 4 { + t.Fatalf("Expected 4 files, got %d", len(files)) + } + + filenames := make([]string, 0, 4) + for file, _ := range files { + filenames = append(filenames, file) + } + + sort.Strings(filenames) + if !reflect.DeepEqual(filenames, []string{ + string(os.PathSeparator), + "another", + "another" + string(os.PathSeparator) + "file.txt", + "file.txt", + }) { + t.Fatalf("Expected different files, got: %s", filenames) + } +}