Skip to content

Instantly share code, notes, and snippets.

@santosh
Last active November 28, 2022 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santosh/0e7ca6eeef7e0dbd231292fce75e667f to your computer and use it in GitHub Desktop.
Save santosh/0e7ca6eeef7e0dbd231292fce75e667f to your computer and use it in GitHub Desktop.
Mocking a filesystem call
package findgo
import (
"io/fs"
"path/filepath"
)
func Files(folder fs.FS) int {
var count int
fs.WalkDir(folder, ".", func(p string, d fs.DirEntry, err error) error {
if filepath.Ext(p) == ".go" {
count++
}
return nil
})
return count
}
package findgo
import (
"testing"
"testing/fstest"
)
func TestFilesInMemory(t *testing.T) {
t.Parallel()
fsys := fstest.MapFS{
"file.go": {},
"subfolder/subfolder.go": {},
"subfolder/another.go": {},
"subfolder/file.go": {},
}
want := 4
got := Files(fsys)
if want != got {
t.Errorf("want %d, got %d", want, got)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment