Documentation ¶
Overview ¶
Package fsmock provides a testing facility to generate mock values that satisfy fs.FS and friends. The package supports developers writing readable, easy to maintain tests that work closely with the filesystem without relying to local or temporary filesystem operations.
Example ¶
package main import ( "fmt" "io" "io/fs" "github.com/halimath/fsmock" ) func main() { fsys := fsmock.New(fsmock.NewDir("", fsmock.EmptyFile("go.mod"), fsmock.EmptyFile("go.sum"), fsmock.NewDir("cmd", fsmock.TextFile("main.go", "package main"), ), fsmock.NewDir("internal", fsmock.EmptyFile("tool.go"), fsmock.EmptyFile("tool_test.go"), ), )) f, err := fsys.Open("cmd/main.go") if err != nil { panic(err) } c, err := io.ReadAll(f) if err != nil { panic(err) } fmt.Println(string(c)) fmt.Println("---") fs.WalkDir(fsys, "", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } if !d.IsDir() { fmt.Println(path) } return nil }) fmt.Println("---") cmdTests, err := fs.Glob(fsys, "cmd/*_test.go") if err != nil { panic(err) } fmt.Println(len(cmdTests)) if err := fsys.Touch("cmd/main_test.go"); err != nil { panic(err) } _, err = fsys.ReadFile("cmd/main_test.go") if err != nil { panic(err) } cmdTests, err = fs.Glob(fsys, "cmd/*_test.go") if err != nil { panic(err) } fmt.Println(len(cmdTests)) fmt.Println("---") if err := fsys.Mkdir("internal/foo"); err != nil { panic(err) } if err := fsys.Touch("internal/foo/foo.go"); err != nil { panic(err) } fsub, err := fsys.Sub("internal/foo") if err != nil { panic(err) } if _, err := fsub.Open("foo.go"); err != nil { panic(err) } }
Output: package main --- go.mod go.sum cmd/main.go internal/tool.go internal/tool_test.go --- 0 1 ---
Index ¶
- type Dir
- type Entry
- type FS
- func (f *FS) Mkdir(name string) error
- func (f *FS) Open(name string) (fs.File, error)
- func (f *FS) ReadDir(name string) ([]fs.DirEntry, error)
- func (f *FS) ReadFile(name string) ([]byte, error)
- func (f *FS) Stat(name string) (fs.FileInfo, error)
- func (f *FS) Sub(name string) (fs.FS, error)
- func (f *FS) Touch(name string) error
- type File
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dir ¶
type Dir struct { // Name defines the directory's name. Name string // ModTime defines the directory's modification time. ModTime time.Time // Children contains the directory's direct children. Children []Entry }
Dir implements a directory in the filesystem.
type Entry ¶
type Entry interface {
// contains filtered or unexported methods
}
Entry is an internal interface defining entries in a filesystem's directory. Dir and File both satisfy this internal interface and can be used in client code.
type FS ¶
type FS struct {
// contains filtered or unexported fields
}
FS implements a mocked fs.FS. It also implements several other interfaces from the fs package, namely - fs.ReadDirFS - fs.ReadFileFS - fs.StatFS - fs.SubFS
In addition FS provides some useful methods to create files, directories and modify existing ones which reflect some of the basic POSIX shell commands, such as mkdir, rm, touch, ...
func (*FS) Mkdir ¶
Mkdir works like the POSIX shell command mkdir and creates the directory name. All parent directories must exist for this call to succeed (it does not work like mkdir -p on some systems).
func (*FS) Open ¶
Open opens the named file.
When Open returns an error, it should be of type *PathError with the Op field set to "open", the Path field set to name, and the Err field describing the problem.
Open should reject attempts to open names that do not satisfy ValidPath(name), returning a *PathError with Err set to ErrInvalid or ErrNotExist.
func (*FS) ReadDir ¶
ReadDir reads the named directory and returns a list of directory entries sorted by filename.
func (*FS) ReadFile ¶
ReadFile reads the named file and returns its contents. A successful call returns a nil error, not io.EOF. (Because ReadFile reads the whole file, the expected EOF from the final Read is not treated as an error to be reported.)
The caller is permitted to modify the returned byte slice. This method should return a copy of the underlying data.
func (*FS) Stat ¶
Stat returns a FileInfo describing the file. If there is an error, it should be of type *PathError.