Crates.io | tree-fs |
lib.rs | tree-fs |
version | 0.2.1 |
source | src |
created_at | 2023-12-02 07:41:08.608535 |
updated_at | 2024-11-10 09:11:19.560163 |
description | Provides a convenient way to create a tree of files |
homepage | |
repository | https://github.com/kaplanelad/tree-fs |
max_upload_size | |
id | 1055971 |
size | 24,700 |
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:
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 tree_fs::TreeBuilder;
let tree_fs = TreeBuilder::default()
.add("test/foo.txt", "bar")
.add_empty("test/folder-a/folder-b/bar.txt")
.create()
.expect("create tree fs");
println!("created successfully in {}", tree_fs.root.display());
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 tree_fs::TreeBuilder;
let tree_fs = TreeBuilder::default()
.add("test/foo.txt", "bar")
.add_empty("test/folder-a/folder-b/bar.txt")
.drop(true)
.create()
.expect("create tree fs");
println!("created successfully in {}", tree_fs.root.display());
let path = tree_fs.root.clone();
assert!(path.exists());
drop(tree_fs);
assert!(!path.exists());
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 std::path::PathBuf;
let yaml_path = PathBuf::from("tests/fixtures/tree.yaml");
let tree_fs = tree_fs::from_yaml_file(&yaml_path).expect("create tree fs");
assert!(tree_fs.root.exists())
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 = tree_fs::from_yaml_str(content).expect("create tree fs");
assert!(tree_fs.root.exists())