Skip to content

Instantly share code, notes, and snippets.

@Psykar
Last active March 4, 2021 08:46
Show Gist options
  • Save Psykar/e3398f938388420c2338fedcb8d60b42 to your computer and use it in GitHub Desktop.
Save Psykar/e3398f938388420c2338fedcb8d60b42 to your computer and use it in GitHub Desktop.
A quick dirty mock implementation of an fs.FS
package testing
import (
"fmt"
"io/fs"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type Fs struct {
mock.Mock
}
func (f *Fs) Open(s string) (fs.File, error) {
a := f.Called(s)
ret := a.Get(0).(fs.File)
e := a.Error(1)
return ret, e
}
type File struct{ mock.Mock }
var _ fs.ReadDirFile = &File{}
func (f *File) Close() error {
f.Called()
return nil
}
func (f *File) Read(b []byte) (int, error) {
f.Called(b)
for i := range b {
b[i] = 'x'
}
return len(b), nil
}
func (f *File) Stat() (fs.FileInfo, error) {
f.Called()
return f, nil
}
func (f *File) ReadDir(i int) ([]fs.DirEntry, error) {
a := f.Called(i)
ret := a.Get(0).([]fs.DirEntry)
err := a.Error(1)
return ret, err
}
func (f *File) Info() (fs.FileInfo, error) {
f.Called()
return f, nil
}
func (f *File) IsDir() bool {
return f.Called().Bool(0)
}
func (f *File) ModTime() time.Time {
f.Called()
return time.Now()
}
func (f *File) Mode() fs.FileMode {
f.Called()
return 0
}
func (f *File) Name() string {
return f.Called().String(0)
}
func (f *File) Size() int64 {
f.Called()
return 0
}
func (f *File) Sys() interface{} {
f.Called()
return nil
}
func (f *File) Type() fs.FileMode {
f.Called()
return 0
}
func TestWalk(t *testing.T) {
fmt.Println("hello")
stuff := &Fs{}
rootDir := &File{}
file := &File{}
stuff.On("Open", "root").Return(rootDir, nil)
rootDir.On("Close")
rootDir.On("Stat")
rootDir.On("ReadDir", -1).Return([]fs.DirEntry{
file,
}, nil)
rootDir.On("IsDir").Return(true)
rootDir.On("Name").Return("rootName")
file.On("Name").Return("aFileName")
file.On("IsDir").Return(false)
results := map[string]string{}
err := fs.WalkDir(stuff, "root", func(path string, d fs.DirEntry, err error) error {
if err != nil {
panic(err)
}
results[path] = d.Name()
return nil
})
assert.Nil(t, err)
assert.Equal(t, map[string]string{"root": "rootName", "root/aFileName": "aFileName"}, results)
stuff.AssertExpectations(t)
rootDir.AssertExpectations(t)
file.AssertExpectations(t)
}
func TestGlob(t *testing.T) {
fmt.Println("hello")
stuff := &Fs{}
rootDir := &File{}
subDir := &File{}
file := &File{}
stuff.On("Open", ".").Return(rootDir, nil)
rootDir.On("ReadDir", -1).Return([]fs.DirEntry{
subDir,
}, nil)
rootDir.On("Close")
subDir.On("Name").Return("subdirname")
stuff.On("Open", "subdirname").Return(subDir, nil)
subDir.On("ReadDir", -1).Return([]fs.DirEntry{
file,
}, nil)
subDir.On("Close")
file.On("Name").Return("aFileName")
res, err := fs.Glob(stuff, "**/aFileName")
assert.Nil(t, err)
assert.Equal(t, []string{"subdirname/aFileName"}, res)
stuff.AssertExpectations(t)
rootDir.AssertExpectations(t)
file.AssertExpectations(t)
subDir.AssertExpectations(t)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment