Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/caarlos0/testfs
A simple fs.FS implementation to be used inside tests.
https://github.com/caarlos0/testfs
filesystem golang testing
Last synced: 6 days ago
JSON representation
A simple fs.FS implementation to be used inside tests.
- Host: GitHub
- URL: https://github.com/caarlos0/testfs
- Owner: caarlos0
- License: mit
- Created: 2021-02-04T21:21:26.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-06-14T13:38:05.000Z (over 1 year ago)
- Last Synced: 2024-10-03T12:39:38.973Z (about 1 month ago)
- Topics: filesystem, golang, testing
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 33
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# testfs
A simple `fs.FS` which is contained in a test (using `testing.TB`'s `TempDir()`)
and with a few helper methods.PS: This lib only works on Go 1.16+.
## Example
```go
func TestSomething(t *testing.T) {
tmpfs := testfs.New(t)
testfile := "foo/bar/foobar"
_ = tmpfs.MkdirAll(filepath.Dir(testfile), 0o764)
_ = tmpfs.WriteFile(testfile, []byte("example"), 0o644)// you can now use tmpfs as a fs.FS...
// fs.WalkDir(tmpfs, ".", func(path string, d fs.DirEntry, err error) error { return nil })// and read files of course:
bts, _ := fs.ReadFile(tmpfs, testfile)
fmt.Println(string(bts))
}
```## Why
The idea is to able to test code that use a `fs.FS`, without having to,
for example, commit a bunch of files inside `testdata` and without using
in-memory implementation that might not do the same thing as a real FS.This is a real FS, it only limits itself to a temporary directory and
cleans after itself once the test is done. You also get a couple of helper
methods to create the structure you need.## Other options
If you don't mind testing against an in-memory implementation, the native
`fstest.MemFS` is a good option.