testing/fstest

Guided tour · Testing · pkg.go.dev →

In-memory fs.FS for tests, plus TestFS to validate custom fs.FS implementations.

Fake filesystem

MapFS

fsys := fstest.MapFS{
    "hello.txt": {Data: []byte("hi")},
    "dir/note.md": {Data: []byte("# note")},
}
data, _ := fs.ReadFile(fsys, "hello.txt")

Validate an fs.FS

TestFS

if err := fstest.TestFS(myFS, "a.txt", "dir/b.txt"); err != nil {
    t.Fatal(err)
}