Skip to main content

temp_project/
temp_project.rs

1//! Build a `TempProject` — a self-cleaning temporary directory pre-populated
2//! with files — and emit a `Report` describing the lifecycle.
3//!
4//! ```text
5//! cargo run --example temp_project
6//! ```
7//!
8//! The temp directory is removed when `project` is dropped. The example
9//! prints the staged paths, asserts they exist on disk, then drops the
10//! handle and confirms the cleanup.
11
12use std::fs;
13
14use dev_fixtures::TempProject;
15
16fn main() {
17    let project = TempProject::new()
18        .with_file(
19            "Cargo.toml",
20            "[package]\nname = \"sample\"\nversion = \"0.1.0\"\n",
21        )
22        .with_file("src/lib.rs", "pub fn answer() -> u32 { 42 }\n")
23        .with_file(
24            "README.md",
25            "# sample\n\nGenerated by the dev-fixtures example.\n",
26        )
27        .with_bytes("assets/payload.bin", vec![0u8, 1, 2, 3, 4, 5])
28        .build()
29        .expect("build temp project");
30
31    println!("temp project root: {}", project.path().display());
32    for (rel, bytes) in project.declared_files() {
33        let absolute = project.path().join(rel);
34        let read_ok = fs::metadata(&absolute).is_ok();
35        println!(
36            "  {:>4} bytes — declared {:?} — exists on disk: {}",
37            bytes.len(),
38            rel,
39            read_ok
40        );
41    }
42
43    let kept_root = project.path().to_path_buf();
44    drop(project);
45    println!(
46        "after drop: {} still on disk? {}",
47        kept_root.display(),
48        kept_root.exists()
49    );
50}