Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jackmordaunt/filebuilder
Declaratively build file structures. Useful for testing apps that interact with the filesystem. Built on top of https://github.com/spf13/afero
https://github.com/jackmordaunt/filebuilder
afero filesystem go mocking pluggable testing
Last synced: 4 days ago
JSON representation
Declaratively build file structures. Useful for testing apps that interact with the filesystem. Built on top of https://github.com/spf13/afero
- Host: GitHub
- URL: https://github.com/jackmordaunt/filebuilder
- Owner: JackMordaunt
- License: mit
- Created: 2018-01-24T18:17:35.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-07T04:02:37.000Z (over 6 years ago)
- Last Synced: 2024-06-19T19:39:56.056Z (5 months ago)
- Topics: afero, filesystem, go, mocking, pluggable, testing
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# filebuilder - A quick uitility for creating file structures and comparing them
`go get github.com/jackmordaunt/filebuilder`
## Why?
I had trouble trying to mock out the filesystem for command line apps.
Creating the files and comparing the files procedurally is tedious.
I wanted a declarative way to specify "these are the files I have", "these are the files I want", and "compare this directory with that directory".To that end, I don't know if this is the 'best' way to do it, or even a good way to do it.
Code suggestions and project suggestions very welcome.
```go
func main() {// Declare the files you want, you can nest with relative paths or use
// a flat list, or both.
entries := []filebuilder.Entry{
filebuilder.File{Path: "foo/bar/baz.exe"},
filebuilder.File{Path: "foo/baz.exe"},
filebuilder.File{Path: "baz.exe"},
filebuilder.Dir{Path: "bar", Entries: []filebuilder.Entry{
filebuilder.File{Path: "baz.txt", Content: []byte("foo")},
filebuilder.File{Path: "foo/bar/baz.txt", Content: []byte("bar")},
}},
}
// Grab a filesystem implementation, or use the default by passing in nil.
// The optional root will be the parent of the provided entries.
fs := afero.NewMemMapFs()
cleanup, err := filebuilder.Build(fs, "parent", entries...)
if err != nil {
log.Fatalf("failed creating entries: %v", err)
}// Optional cleanup func which erases all files created.
defer func() {
if err := cleanup(); err != nil {
log.Fatalf("failed cleanup of files: %v", err)
}
}()// fs is stateful, you can build up the file tree over multiple calls.
_, err = filebuilder.Build(fs, "inside/this/folder", entries)
if err != nil {
log.Fatalf("failed creating entries: %v", err)
}// Compare the directories.
diff, ok, err := filebuilder.CompareDirectories(fs, "parent", "inside/this/folder")
if err != nil {
log.Fatalf("error while comparing directories: %v", err)
}
if !ok {
log.Printf("directories are not equivalent, %v\n", diff)
}
}
```