1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2026 Mike Grier
//! Test-only scratch directories.
//!
//! The native layer's subject is real directories, so its tests need real
//! directories: an empty one, one with entries, and a plain file. Each fixture
//! owns a uniquely named directory under the system temporary directory and
//! removes it on drop, so a failing test leaves nothing behind and concurrent
//! tests never collide.
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
/// Distinguishes fixtures created within the same process.
static NEXT: AtomicU64 = AtomicU64::new(0);
/// A temporary directory that deletes itself.
pub(crate) struct Scratch {
path: PathBuf,
}
impl Scratch {
/// Create an empty scratch directory.
pub(crate) fn empty() -> Self {
let unique = format!(
"windows-file-enumeration-sys-{}-{}",
std::process::id(),
NEXT.fetch_add(1, Ordering::Relaxed)
);
let path = std::env::temp_dir().join(unique);
// A previous run that was killed mid-test could have left this behind.
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir(&path).expect("a scratch directory");
Self { path }
}
/// Create a scratch directory holding `names` as empty files.
pub(crate) fn with_files(names: &[&str]) -> Self {
let scratch = Self::empty();
for name in names {
std::fs::write(scratch.path().join(name), b"").expect("a scratch file");
}
scratch
}
/// The directory's path.
pub(crate) fn path(&self) -> &Path {
&self.path
}
/// A path inside the directory, which need not exist.
pub(crate) fn child(&self, name: &str) -> PathBuf {
self.path.join(name)
}
}
impl Drop for Scratch {
fn drop(&mut self) {
// Best effort: a leaked temporary directory is not worth failing a test
// that has already made its point, and panicking here would mask it.
let _ = std::fs::remove_dir_all(&self.path);
}
}