Skip to content

Commit

Permalink
Adjust MemFs.List() to return the data
Browse files Browse the repository at this point in the history
  • Loading branch information
grongor committed Jul 8, 2024
1 parent 5c4385a commit 12da08c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
13 changes: 6 additions & 7 deletions memmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
package afero

import (
"fmt"
"io"

"log"
"os"
"path/filepath"

"sort"
"strings"
"sync"
Expand Down Expand Up @@ -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
}
36 changes: 36 additions & 0 deletions memmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"io/fs"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 12da08c

Please sign in to comment.