#directory #testing #temp-directory-builder

temp-dir-builder

Provides a convenient way to create a temporary directory containing files

1 unstable release

new 0.1.0 Apr 14, 2025

#826 in Filesystem

Apache-2.0

18KB
303 lines

temp-dir-builder CI

Oftentimes, testing scenarios involve interactions with the file system. temp-dir-builder 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 a builder.

Usage

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: When the temp_dir instance is dropped, the temporary folder and its contents are automatically deleted, which is particularly useful for tests that require a clean state.

use temp_dir_builder::TempDirectoryBuilder;
let temp_dir = TempDirectoryBuilder::default()
    .add_text_file("test/foo.txt", "bar")
    .add_binary_file("test/foo2.txt", &[98u8, 97u8, 114u8])
    .add_empty_file("test/folder-a/folder-b/bar.txt")
    .add_file("test/file.rs", file!())
    .add_directory("test/dir")
    .build()
    .expect("create temp dir");
println!("created successfully in {}", temp_dir.path().display());

Credits

This is a fork of tree-fs I heavily rewritten, original idea by Elad Kaplan.

Dependencies

~0.5–1MB
~21K SLoC