#file-tree #yaml #assert #folders #testing #file-path

dev tree-fs

Provides a convenient way to create a tree of files

3 unstable releases

new 0.2.1 Nov 10, 2024
0.2.0 Nov 6, 2024
0.1.0 Dec 2, 2023

#308 in Filesystem

Download history 98/week @ 2024-07-27 107/week @ 2024-08-03 75/week @ 2024-08-10 54/week @ 2024-08-17 53/week @ 2024-08-24 168/week @ 2024-08-31 85/week @ 2024-09-07 98/week @ 2024-09-14 114/week @ 2024-09-21 104/week @ 2024-09-28 68/week @ 2024-10-05 148/week @ 2024-10-12 105/week @ 2024-10-19 136/week @ 2024-10-26 212/week @ 2024-11-02 184/week @ 2024-11-09

683 downloads per month
Used in 6 crates

Apache-2.0

12KB
130 lines

crate docs

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 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());

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 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());

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 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())

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 = tree_fs::from_yaml_str(content).expect("create tree fs");
assert!(tree_fs.root.exists())

Dependencies

~0.6–1.5MB
~33K SLoC