Skip to content

Commit

Permalink
Improve errors and apply go imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mojatter committed Nov 9, 2021
1 parent 11b0da0 commit a0185dd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 46 deletions.
2 changes: 1 addition & 1 deletion memfs/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package memfs_test

import (
"fmt"
"log"
"io/fs"
"log"

"github.com/jarxorg/io2"
"github.com/jarxorg/io2/memfs"
Expand Down
14 changes: 12 additions & 2 deletions memfs/memfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"sync"
"syscall"

"github.com/jarxorg/io2"
)
Expand Down Expand Up @@ -108,6 +109,7 @@ func (fsys *MemFS) Open(name string) (fs.File, error) {
f := &MemFile{
fsys: fsys,
name: name,
mode: v.mode,
}
if !v.isDir {
f.buf = bytes.NewBuffer(v.data)
Expand Down Expand Up @@ -147,9 +149,14 @@ func (fsys *MemFS) ReadDir(dir string) ([]fs.DirEntry, error) {
fsys.mutex.Lock()
defer fsys.mutex.Unlock()

if !fs.ValidPath(dir) {
return nil, &fs.PathError{Op: "ReadDir", Path: dir, Err: fs.ErrInvalid}
v, err := fsys.open(dir)
if err != nil {
return nil, err
}
if !v.isDir {
return nil, &fs.PathError{Op: "ReadDir", Path: dir, Err: syscall.ENOTDIR}
}

prefix := fsys.key(dir)
keys := fsys.store.prefixKeys(prefix)
var dirEntries []fs.DirEntry
Expand Down Expand Up @@ -289,6 +296,9 @@ var (

// Read reads bytes from this file.
func (f *MemFile) Read(p []byte) (int, error) {
if f.buf == nil {
return 0, &fs.PathError{Op: "Read", Path: f.name, Err: syscall.EISDIR}
}
return f.buf.Read(p)
}

Expand Down
34 changes: 29 additions & 5 deletions memfs/memfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,14 @@ func TestReadDir(t *testing.T) {
},
dir: "dir0",
}, {
dir: "not-found",
dir: "not-found",
errStr: "Open not-found: file does not exist",
}, {
dir: "dir0/file01.txt",
errStr: "ReadDir dir0/file01.txt: not a directory",
}, {
dir: "../invalid",
errStr: "ReadDir ../invalid: invalid argument",
errStr: "Open ../invalid: invalid argument",
},
}

Expand Down Expand Up @@ -328,7 +332,7 @@ func TestWriteFile(t *testing.T) {
{
name: "new.txt",
}, {
name: "dir0/file01.txt",
name: "dir0/file01.txt",
}, {
name: "dir0",
errStr: "Create dir0: invalid argument",
Expand Down Expand Up @@ -394,7 +398,7 @@ func TestRemoveAll(t *testing.T) {

var want []string
for _, k := range fsys.store.keys {
if !strings.HasPrefix(k, "/" + dir) {
if !strings.HasPrefix(k, "/"+dir) {
want = append(want, k)
}
}
Expand Down Expand Up @@ -422,6 +426,26 @@ func TestRemoveAll_Errors(t *testing.T) {
}
}

func TestMemFile_Read_Errors(t *testing.T) {
fsys := newMemFSTest(t)
name := "dir0"

f, err := fsys.Open(name)
if err != nil {
t.Fatal(err)
}

memf, ok := f.(*MemFile)
if !ok {
t.Fatalf(`Fatal not MemFile: %#v`, f)
}

_, err = memf.Read([]byte{})
if err == nil {
t.Fatalf(`Fatal Read(1) returns no error`)
}
}

func TestMemFile_ReadDir(t *testing.T) {
fsys := newMemFSTest(t)
dir := "dir0"
Expand All @@ -438,7 +462,7 @@ func TestMemFile_ReadDir(t *testing.T) {

testCases := []struct {
name string
err error
err error
}{
{
name: "file01.txt",
Expand Down
66 changes: 33 additions & 33 deletions memfs/store.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package memfs

import (
"io/fs"
"io/fs"
"path"
"path/filepath"
"path/filepath"
"sort"
"strings"
"time"
"time"
)

// Value works as fs.DirEntry or fs.FileInfo.
Expand Down Expand Up @@ -87,7 +87,7 @@ func (s *store) put(k string, v *value) *value {
}

func (s *store) remove(key string) *value {
i := s.keyIndex(key)
i := s.keyIndex(key)
if i == -1 {
return nil
}
Expand All @@ -98,40 +98,40 @@ func (s *store) remove(key string) *value {
}

func (s *store) removeAll(prefix string) {
from := s.keyIndex(prefix)
if from == -1 {
return
}

max := len(s.keys)
to := -1
for i := from; i < max; i++ {
key := s.keys[i]
if !strings.HasPrefix(key, prefix) {
break
}
delete(s.values, key)
to = i
}
s.keys = append(s.keys[0:from], s.keys[to+1:]...)
from := s.keyIndex(prefix)
if from == -1 {
return
}

max := len(s.keys)
to := -1
for i := from; i < max; i++ {
key := s.keys[i]
if !strings.HasPrefix(key, prefix) {
break
}
delete(s.values, key)
to = i
}
s.keys = append(s.keys[0:from], s.keys[to+1:]...)
}

func (s *store) keyIndex(key string) int {
i := sort.SearchStrings(s.keys, key)
if i < len(s.keys) && s.keys[i] == key {
return i
}
return -1
i := sort.SearchStrings(s.keys, key)
if i < len(s.keys) && s.keys[i] == key {
return i
}
return -1
}

func (s *store) prefixKeys(prefix string) []string {
i := s.keyIndex(prefix)
i := s.keyIndex(prefix)
if i == -1 {
return nil
}
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
}
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
}

var keys []string
max := len(s.keys)
Expand All @@ -149,13 +149,13 @@ func (s *store) prefixKeys(prefix string) []string {
}

func (s *store) prefixGlobKeys(prefix, pattern string) ([]string, error) {
i := s.keyIndex(prefix)
i := s.keyIndex(prefix)
if i == -1 {
return nil, nil
}
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
}
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
}

var keys []string
max := len(s.keys)
Expand Down
10 changes: 5 additions & 5 deletions memfs/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,18 @@ func TestStore_prefixGlobKeys(t *testing.T) {
s := newStoreTest()
for _, tc := range testCases {
got, err := s.prefixGlobKeys(tc.prefix, tc.pattern)
errStr := ""
errStr := ""
if err != nil {
errStr = err.Error()
}
errStr = err.Error()
}
if errStr != tc.errStr {
t.Errorf(`Error prefixGlobKeys("%s", "%s") error got "%s"; want "%s"`,
tc.prefix, tc.pattern, errStr, tc.errStr)
tc.prefix, tc.pattern, errStr, tc.errStr)
continue
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf(`Error prefixGlobKeys("%s", "%s") got %v; want %v`,
tc.prefix, tc.pattern, got, tc.want)
tc.prefix, tc.pattern, got, tc.want)
}
}
}

0 comments on commit a0185dd

Please sign in to comment.