Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇
Go aicertificationcloud-engineeringcybercybersecuritycybersecurity-analystcybersecurity-certificationcybersecurity-certificationscybersecurity-educationcybersecurity-jobscybersecurity-portfoliocybersecurity-projectscybersecurity-toolsgrchackinglearningpentestingproject-repositoryprojetcspython
// ©AngelaMos | 2026
// scratch.rs
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::{Mutex, OnceLock};
pub const ENV_CARGO_TARGET_DIR: &str = "CARGO_TARGET_DIR";
pub const SCRATCH_DIR: &str = "lisdex-scratch";
fn claimed() -> &'static Mutex<HashSet<String>> {
static CLAIMED: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
CLAIMED.get_or_init(|| Mutex::new(HashSet::new()))
}
#[must_use]
pub fn root() -> PathBuf {
std::env::var_os(ENV_CARGO_TARGET_DIR)
.map_or_else(std::env::temp_dir, PathBuf::from)
.join(SCRATCH_DIR)
}
#[must_use]
pub fn unclaimed_path(label: &str) -> PathBuf {
let root = root();
let _ = std::fs::create_dir_all(&root);
root.join(format!("{label}-{}", std::process::id()))
}
#[must_use]
pub fn path(label: &str) -> PathBuf {
assert!(
claimed().lock().unwrap().insert(label.to_owned()),
"fixture label {label} was claimed twice in one process: both callers resolve to one \
directory that each of them wipes, so whichever loses the race reads a tree the other \
deleted"
);
unclaimed_path(label)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fixtures_are_rooted_in_the_build_directory_not_the_shared_temp_dir() {
let root = root();
assert!(root.ends_with(SCRATCH_DIR));
if let Some(target) = std::env::var_os(ENV_CARGO_TARGET_DIR) {
assert!(
root.starts_with(PathBuf::from(&target)),
"{} must sit under {target:?}, not the shared temp dir",
root.display()
);
assert_ne!(root.parent(), Some(std::env::temp_dir().as_path()));
}
}
#[test]
fn a_path_carries_the_process_id_so_concurrent_runs_do_not_share_it() {
let path = unclaimed_path("sample");
let name = path.file_name().unwrap().to_string_lossy().into_owned();
assert_eq!(name, format!("sample-{}", std::process::id()));
}
#[test]
fn claiming_one_label_twice_is_refused_rather_than_silently_shared() {
let label = "claimed_twice_control";
let first = path(label);
let second = std::panic::catch_unwind(|| path(label));
assert!(
second.is_err(),
"a second claim of {label} must panic; it resolved to {} again",
first.display()
);
}
}