Last active
November 28, 2022 07:03
-
-
Save santosh/0e7ca6eeef7e0dbd231292fce75e667f to your computer and use it in GitHub Desktop.
Mocking a filesystem call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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