-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MemFS): make MemFS actually do something
- Loading branch information
Showing
2 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
package qfs | ||
|
||
import "fmt" | ||
// MemFSStore is the minimum interface for creating a MemFS | ||
type MemFSStore interface { | ||
Get(path string) (File, error) | ||
} | ||
|
||
// NewMemFS creates an in-memory filesystem from a set of files | ||
func NewMemFS(files ...File) *MemFS { | ||
return &MemFS{} | ||
func NewMemFS(store MemFSStore) *MemFS { | ||
return &MemFS{ | ||
store: store, | ||
} | ||
} | ||
|
||
// MemFS is an in-memory implementation | ||
// It currently doesn't work, this is just a placeholder for upstream code | ||
// MemFS is an in-memory implementation of the FileSystem interface. it's a | ||
// minimal wrapper around anything that supports getting a file with a | ||
// string key | ||
type MemFS struct { | ||
store MemFSStore | ||
} | ||
|
||
// Get implements PathResolver interface | ||
func (mfs *MemFS) Get(path string) (File, error) { | ||
return nil, fmt.Errorf("memfs is not yet finished") | ||
return mfs.store.Get(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package qfs | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
func TestMemFS(t *testing.T) { | ||
memfs := NewMemFS(testStore(0)) | ||
f, err := memfs.Get("path") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
data, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(data, []byte(`data`)) { | ||
t.Errorf("byte mismatch. expected: %s. got: %s", `data`, string(data)) | ||
} | ||
} | ||
|
||
type testStore int | ||
|
||
func (t testStore) Get(path string) (File, error) { | ||
if path == "path" { | ||
return NewMemfileBytes("path", []byte(`data`)), nil | ||
} | ||
|
||
return nil, ErrNotFound | ||
} |