mirror of https://github.com/spf13/afero.git
Adjust MemFs.List() to return the data
This commit is contained in:
parent
5c4385aa20
commit
12da08c58f
13
memmap.go
13
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue