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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! A temporary directory that is actually unique, and cleans up after itself.
//!
//! Nine files in this workspace had invented their own, and five different ways
//! of trying to make the name unique:
//!
//! ```text
//! kevy-persist/feed_meta.rs process::id() + Instant::now()
//! kevy-persist/shards_meta.rs process::id() + process::id()
//! kevy-persist/reshard.rs SystemTime
//! kevy-cli/backup.rs nanos
//! kevy-uring/ring_tests.rs process::id()
//! …
//! ```
//!
//! None of them is unique. `process::id()` is the SAME for every test in a
//! binary — cargo runs them as threads, not processes — so two tests in the same
//! file get the same directory and stamp on each other's files. A clock is
//! unique only until two threads read it inside the same tick, which under
//! parallel test execution is exactly what happens. That is a flake: green on a
//! quiet machine, red under load, and it looks like the code is broken rather
//! than the fixture.
//!
//! A process id and a monotonic counter, together, cannot collide: the counter
//! separates threads within a process and the pid separates processes. That is
//! the whole trick, and it is why this is one crate instead of nine copies.
use ;
use ;
static SEQ: AtomicU64 = new;
/// A unique directory as a bare path: created, PRE-CLEARED, and owned by the
/// caller — for call sites that already carry their own cleanup, and for the
/// one production path (kevy-cli's embed scratch) where the directory must
/// outlive the function that made it.
///
/// The pre-clear matters more than it looks. The old pid-only scratch dir was
/// never cleared, so a recycled pid inherited the PREVIOUS run's data files —
/// `create_dir_all` on an existing directory succeeds silently, and the loader
/// then read a mix of stale and fresh dumps as though they were one dataset.
/// # Examples
///
/// Two calls never collide, and the second call for a label does not
/// inherit what the first one left:
///
/// ```
/// let a = kevy_tmpdir::unique_dir("doc");
/// let b = kevy_tmpdir::unique_dir("doc");
/// assert_ne!(a, b);
/// assert!(a.is_dir() && b.is_dir());
///
/// std::fs::write(a.join("stale"), b"old").unwrap();
/// let again = kevy_tmpdir::unique_dir("doc");
/// assert!(!again.join("stale").exists());
///
/// for d in [a, b, again] { std::fs::remove_dir_all(d).unwrap(); }
/// ```
/// A unique directory under the system temp dir, removed when dropped.
///
/// Dropped on unwind too, so a failing test does not leave litter behind for the
/// next one to trip over.
/// # Examples
///
/// ```
/// use std::path::Path;
/// let dir = kevy_tmpdir::TmpDir::new("doctest");
/// let kept: std::path::PathBuf = dir.path().to_path_buf();
/// assert!(kept.is_dir());
/// drop(dir);
/// assert!(!kept.exists(), "the guard deletes the directory it made");
/// ```
;