/afero:aferofrom Testable Filesystems: afero
Expert skill for writing testable filesystem code in Go using spf13/afero. Covers injecting afero.Fs, in-memory testing with MemMapFs, layering with ReadOnlyFs/CopyOnWriteFs/BasePathFs, bridging to io/fs, and understanding MemMapFs differences from real disk. Activates when Go code reads/writes files and needs unit tests without touching disk.
Ships in Testable Filesystems: afero. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Business logic that calls os directly can only be tested against a real disk. Inject the filesystem instead, so tests run in memory and production runs on the OS. github.com/spf13/afero is the interface for code that writes; io/fs is the interface for code that only reads.
Every behavior below was verified against afero v1.15.0 on Go 1.27.
afero.Fs, or someone asks how to fake the filesystem.--dry-run that shows what would change without writing.| The code… | Accept | Test with | Production |
|---|---|---|---|
| only reads | fs.FS | fstest.MapFS | os.DirFS(dir) or root.FS() |
| reads and writes | afero.Fs | afero.NewMemMapFs() | afero.NewOsFs() |
| must stay inside a directory, whatever the names | *os.Root | os.OpenRoot(t.TempDir()) | os.OpenRoot(dir) |
Read-only code needs no dependency: io/fs and testing/fstest are the standard library. Reach for afero when the code writes.
maintype Store struct {
fs afero.Fs
dir string
}
func NewStore(fs afero.Fs, dir string) *Store {
return &Store{fs: fs, dir: dir}
}
func (s *Store) Save(name string, data []byte) error {
if err := s.fs.MkdirAll(s.dir, 0o755); err != nil {
return err
}
return afero.WriteFile(s.fs, filepath.Join(s.dir, name), data, 0o644)
}
func TestSave(t *testing.T) {
fs := afero.NewMemMapFs()
s := NewStore(fs, "/data")
if err := s.Save("a.txt", []byte("hi")); err != nil {
t.Fatal(err)
}
got, err := afero.ReadFile(fs, "/data/a.txt")
if err != nil || string(got) != "hi" {
t.Fatalf("ReadFile = %q, %v", got, err)
}
}
main passes afero.NewOsFs(). Nothing below main imports os for file access; one stray os.WriteFile makes the in-memory test lie.
A test that passes on MemMapFs proves the logic, not the filesystem contract. Four differences decide whether a green test means anything:
afero.WriteFile(mem, "/no/such/dir/f.txt", …) succeeds and the directories appear; on OsFs the same call fails. A forgotten MkdirAll passes every in-memory test. To prove the code creates its directories, run that test on disk: afero.NewBasePathFs(afero.NewOsFs(), t.TempDir()).0o444 opens for writing and accepts the write. Test permission handling on disk, or with ReadOnlyFs.data/x.txt and /data/x.txt are separate entries. Paths are cleaned (/data//x.txt, /data/./x.txt and /data/../data/x.txt all find /data/x.txt), but a leading / is never added or removed. Build every path from one absolute root with filepath.Join.fs.FS. afero.NewIOFS(mem) opens only relative-keyed files (fs.FS names never start with /), yet ReadDir(".") still lists the top directory of a rooted key, which then fails to open. Its files also fail fstest.TestFS: a ReadAt that runs past end of file returns the bytes it read with a nil error where io.EOF is required. Test fs.FS consumers with fstest.MapFS, not a bridged MemMapFs.It is safe for concurrent use: parallel goroutines writing different files pass the race detector, so t.Parallel subtests may share one MemMapFs.
| Constructor | Behavior |
|---|---|
afero.NewReadOnlyFs(fs) | Every write fails with EPERM; errors.Is(err, os.ErrPermission) is true. |
afero.NewCopyOnWriteFs(base, layer) | Reads fall through to base; writes land in layer; base is never modified. Removing or renaming a file that base holds does not work (see below). |
afero.NewBasePathFs(fs, dir) | Maps every path under dir; /in.txt is dir/in.txt, and ../ above dir does not resolve. |
afero.FromIOFS{FS: fsys} | Adapts a read-only fs.FS (embed.FS, fstest.MapFS) to afero.Fs; writes fail. |
afero.NewIOFS(fs) | Adapts an afero.Fs to fs.FS (see the MemMapFs caveat above). |
CopyOnWriteFs is a dry run for free when the command only creates or overwrites files. It runs its real code against an overlay, then reports what changed, and the disk is untouched:
func run(fs afero.Fs, dryRun bool) (afero.Fs, error) {
if dryRun {
fs = afero.NewCopyOnWriteFs(fs, afero.NewMemMapFs())
}
return fs, apply(fs) // apply writes through fs; on a dry run only the overlay changes
}
It cannot delete or move a file that the base holds. For a file only in the base, Remove fails with "file does not exist", Rename fails with EPERM, and RemoveAll returns nil while the file stays visible; for a file the command already overwrote, Remove returns nil and the base copy shows through again. Both are a dry run that reports success for a delete it never staged. A command that deletes or moves files needs its own plan-then-apply step for --dry-run.
BasePathFs Is Not a Security BoundaryBasePathFs confines lexical paths only. It follows symlinks: a link inside the base that points outside it reads the outside file. For names from users, archives, or the network, open the directory with os.Root (Go 1.24), which rejects both ../ and symlinks that escape with path escapes from parent:
root, err := os.OpenRoot(dir)
if err != nil {
return err
}
defer root.Close()
data, err := root.ReadFile(name) // Root.ReadFile and Root.WriteFile: Go 1.25
Below Go 1.25, use root.Open and root.Create. To make an untrusted name a safe file name before it reaches the filesystem, see the fileflow-pathologize skill.
The package-level helpers take an Fs first: afero.ReadFile, afero.WriteFile, afero.ReadDir, afero.Exists, afero.DirExists, afero.Walk, afero.TempDir, afero.Glob. afero.Afero{Fs: fs} wraps the same helpers as methods (a.Exists(path)), which reads well when a type uses many of them.
os and the injected Fs. One os.Stat or os.ReadFile in the logic means the test checks a disk the code never wrote to.afero.NewOsFs() in a unit test. It touches the disk, so the test is slow, order-dependent, and leaves files. Use NewMemMapFs, or NewBasePathFs(NewOsFs(), t.TempDir()) when disk behavior is the point.MemMapFs to catch a missing MkdirAll or a permission bug. It cannot; see the four differences.data/x and /data/x keys. The test writes one file and the code reads another.BasePathFs as a sandbox. Symlinks escape it. Use os.Root.afero.Fs in read-only code. Accept fs.FS; the caller can pass os.DirFS, embed.FS, fstest.MapFS, or afero.NewIOFS.go get github.com/spf13/afero@latest
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
go-filesystem, testable-code, in-memory-fs, afero-fs, unit-testing
npx claudepluginhub spf13/go-skills --plugin aferoOther plugins' skills for the same job