Skip to content

Commit

Permalink
feat(MemFS): make MemFS actually do something
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Apr 2, 2019
1 parent 4eae03f commit 840dd74
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
19 changes: 13 additions & 6 deletions mem.go
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)
}
34 changes: 34 additions & 0 deletions mem_test.go
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
}

0 comments on commit 840dd74

Please sign in to comment.