Adjust MemFs.List() to return the data

This commit is contained in:
Jakub Chábek 2024-07-08 16:59:05 +02:00
parent 5c4385aa20
commit 12da08c58f
2 changed files with 42 additions and 7 deletions

View File

@ -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
}

View File

@ -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)
}
}