https://pkg.go.dev/testing/fstest ?

On Sunday, March 28, 2021 at 5:09:12 AM UTC+2 aind...@gmail.com wrote:

> For testing library functions that accept fs.FS, I've been creating mock 
> FS implementations. However, I'm surprised by the amount of code I've had 
> to write for something like an FS with a single file in it. See below:
>
> *type singleFileFS struct {*
> *    f stringFile*
> *}*
>
> *func (r singleFileFS) Open(name string) (fs.File, error)     { return 
> nil, nil }*
> *func (r singleFileFS) Stat(name string) (fs.FileInfo, error) { return r, 
> nil }*
> *func (r singleFileFS) Name() string                          { return "." 
> }*
> *func (r singleFileFS) Size() int64                           { return -1 
> }*
> *func (r singleFileFS) Mode() (m fs.FileMode)                 { return }*
> *func (r singleFileFS) ModTime() (t time.Time)                { return }*
> *func (r singleFileFS) IsDir() bool                           { return 
> true }*
> *func (r singleFileFS) Sys() interface{}                      { return nil 
> }*
> *func (r singleFileFS) ReadDir(string) ([]fs.DirEntry, error) { return 
> []fs.DirEntry{r.f}, nil }*
> *func (r singleFileFS) ReadFile(name string) ([]byte, error)  { return 
> r.f.body, nil }*
>
> *type stringFile struct {*
> *    name string*
> *    body []byte*
> *}*
>
> *func (s stringFile) Stat() (fs.FileInfo, error)   { return s, nil }*
> *func (s stringFile) Name() string                 { return s.name 
> <http://s.name> }*
> *func (s stringFile) Size() int64                  { return 
> int64(len(s.body)) }*
> *func (s stringFile) Mode() (m fs.FileMode)        { return }*
> *func (s stringFile) Type() (m fs.FileMode)        { return }*
> *func (s stringFile) ModTime() (t time.Time)       { return }*
> *func (s stringFile) IsDir() (false bool)          { return }*
> *func (s stringFile) Sys() (i interface{})         { return }*
> *func (s stringFile) Read([]byte) (i int, e error) { return }*
> *func (s stringFile) Close() (err error)           { return }*
> *func (s stringFile) Info() (fs.FileInfo, error)   { return s, nil }*
>
> Does anyone have any tips for creating a smaller version of this kind of 
> adapter? Or are there any FS adapter libraries that can help with this sort 
> of thing?
>
> Thanks,
> Akhil
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ef40b17e-f8b4-4e9d-8060-ac72b9543360n%40googlegroups.com.

Reply via email to