A golang mock implementation of fs.FS
and friends for testing.
fsmock
implements a mock filesystem satisfying fs.FS
and other interfaces
to enable easier testing of code that uses fs.FS
to access file systems.
fsmock
is provided as a go module and requires go >= 1.16.
go get github.com/halimath/fsmock@main
fsmock
provides two basic types Dir
and File
. These can be used to build
up a filesystem in plain go. Use the provided functions NewDir
and NewFile
to create them conveniently.
Create a new filesystem by invoking fsmock.New
providing a root directory.
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"),
),
))
You can use the methods defined in fs.FS
and other interfaces from the fs
package to access files and directories:
f, err := fsys.Open("cmd/main.go")
if err != nil {
panic(err)
}
fs.WalkDir(fsys, "", func(path string, d fs.DirEntry, err error) error {
// ...
return nil
})
In addition to the read-only functions defined by the fs
interfaces, fsmock
provides some helper functions to modify the filesystem.
To update a modification timestamp of either a file or directory use the
Touch
function. This will also create an empty file if the named file does
not exist (just like the Unix touch
command does):
if err := fsys.Touch("internal/foo/foo.go"); err != nil {
panic(err)
}
To create a directory use the Mkdir
function which works like the mkdir
Unix shell command (without the -p
option):
if err := fsys.Mkdir("internal/foo"); err != nil {
panic(err)
}
See fsmock_test.go
for a full-blown example.
Copyright 2022 Alexander Metzner.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.