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
pub use per_test_directory_macros::per_test_dir;
use std::env::set_current_dir;
use std::fs::{create_dir_all, remove_dir_all};
use std::path::Path;
pub struct PerTestDirectoryFixture {
path: String,
pub passed: bool,
}
impl PerTestDirectoryFixture {
#[allow(dead_code)]
pub fn new(path: String) -> PerTestDirectoryFixture {
let fp = format!("test_runs/{}", path);
let p = Path::new(&fp);
create_dir_all(p).unwrap();
set_current_dir(p).unwrap();
PerTestDirectoryFixture {
path,
passed: false,
}
}
}
impl Drop for PerTestDirectoryFixture {
fn drop(&mut self) {
if self.passed {
set_current_dir(Path::new("..")).unwrap();
remove_dir_all(&self.path).ok();
set_current_dir(Path::new("..")).unwrap();
} else {
set_current_dir(Path::new("../..")).unwrap();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::panic::catch_unwind;
use std::path::Path;
#[test]
fn test_cleanup() {
{
#[per_test_dir]
fn inner(do_panic: bool) {
if do_panic {
panic!("shu")
}
}
let result = catch_unwind(|| inner(true));
assert!(result.is_err());
assert!(Path::new("test_runs").exists());
assert!(Path::new("test_runs/tests.inner").exists());
let result = catch_unwind(|| inner(false));
assert!(result.is_ok());
assert!(!Path::new("test_runs/tests.inner").exists());
}
}
}