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
//! A directory a test made, removed when the test is done with it.
//!
//! Tests removed their directory as their last step, so one that failed,
//! returned early, or never had that step left it in the system's temporary
//! directory: a release check left hundreds. Dropped as the test ends, passed
//! or panicked, this removes it. `KEEP=1` leaves it to look at.
use std::ops::Deref;
use std::path::{Path, PathBuf};
pub(crate) struct TestDirectory(PathBuf);
impl TestDirectory {
/// Takes over a directory the test has just created.
pub(crate) fn adopt(path: PathBuf) -> Self {
Self(path)
}
}
impl Deref for TestDirectory {
type Target = Path;
fn deref(&self) -> &Path {
&self.0
}
}
impl AsRef<Path> for TestDirectory {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl Drop for TestDirectory {
fn drop(&mut self) {
if std::env::var_os("KEEP").is_none() {
let _ = std::fs::remove_dir_all(&self.0);
}
}
}