Skip to main content

rust_fixture_writable

Function rust_fixture_writable 

Source
pub fn rust_fixture_writable<T, F>(
    name: &str,
    version: u32,
    mode: Creation,
    make_fixture: F,
) -> Result<(TempDir, T)>
where F: FnMut(FixtureState<'_>) -> PostResult<T>,
Expand description

Execute a Rust closure in a directory, returning a writable temporary directory.

The closure is used to create a fixture in the given directory. The resulting directory is writable and will be automatically cleaned up when the returned tempfile::TempDir is dropped. It may be called multiple times, and the returned T will be primed on the final, writable location.

version should be incremented when the closure’s behavior changes to invalidate the cache. name is used to identify this fixture for caching purposes and should be unique within the crate.

§Example

use gix_testtools::{Result, Creation, FixtureState};

#[test]
fn test_with_writable_rust_fixture() -> Result {
    let (dir, ()) = gix_testtools::rust_fixture_writable("my_fixture", 1, Creation::CopyFromReadOnly, |state| {
        if let FixtureState::Uninitialized(path) = state {
            std::fs::write(path.join("file.txt"), "content")?;
        }
        Ok(())
    })?;
    // Can modify files in dir
    std::fs::write(dir.path().join("new_file.txt"), "new content")?;
    Ok(())
}