tree-fs
Oftentimes, testing scenarios involve interactions with the file system. tree-fs
provides a convenient solution for creating file system trees tailored to the needs of your tests. This library offers:
- An easy way to generate a tree with recursive paths.
- Tree creation within a temporary folder.
- The ability to create a tree using either YAML or a builder.
Usage
From builder
With the builder API, you can define file paths and contents in a structured way. Here’s how to create a tree with the builder:
use TreeBuilder;
let tree_fs = default
.add
.add_empty
.create
.expect;
println!;
Drop folder
When the tree_fs
instance is dropped, the temporary folder and its contents are automatically deleted, which is particularly useful for tests that require a clean state.
use TreeBuilder;
let tree_fs = default
.add
.add_empty
.drop
.create
.expect;
println!;
let path = tree_fs.root.clone;
assert!;
drop;
assert!;
Using a YAML File
You can define your file tree structure in a YAML file, which is then loaded and created by tree-fs
. This method is great for more complex, predefined directory structures.
use PathBuf;
let yaml_path = from;
let tree_fs = from_yaml_file.expect;
assert!
Using a YAML String
Alternatively, you can provide the YAML content as a string, which is particularly useful for inline definitions within test code.
let content = r#"
override_file: false
files:
- path: foo.json
content: |
{ "foo;": "bar" }
- path: folder/bar.yaml
content: |
foo: bar
"#;
///
let tree_fs = from_yaml_str.expect;
assert!