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
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process;
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
use std::thread;
use std::time;
static TEST_ID: AtomicUsize = ATOMIC_USIZE_INIT;
pub struct TestDir {
bin: PathBuf,
dir: PathBuf,
}
impl TestDir {
pub fn new(bin_name: &str, test_name: &str) -> TestDir {
let mut bin_dir = env::current_exe()
.expect("Could not find executable")
.parent()
.expect("Could not find parent directory for executable")
.to_path_buf();
if bin_dir.ends_with("deps") {
bin_dir.pop();
}
let id = TEST_ID.fetch_add(1, Ordering::SeqCst);
let dir = bin_dir.join("integration-tests")
.join(test_name)
.join(format!("{}", id));
if dir.exists() {
fs::remove_dir_all(&dir)
.expect("Could not remove test output directory");
}
let mut err = None;
for _ in 0..10 {
match fs::create_dir_all(&dir) {
Ok(_) => {
err = None;
break;
}
Err(e) => {
err = Some(e);
}
}
thread::sleep(time::Duration::from_millis(500));
}
if let Some(e) = err {
panic!("Could not create test output directory: {}", e);
}
TestDir {
bin: bin_dir.join(&bin_name),
dir: dir,
}
}
pub fn cmd(&self) -> process::Command {
let mut cmd = process::Command::new(&self.bin);
cmd.current_dir(&self.dir);
cmd
}
pub fn src_path<P: AsRef<Path>>(&self, path: P) -> PathBuf {
let cwd = env::current_dir().expect("Could not get current dir");
fs::canonicalize(cwd.join(path))
.expect("Could not canonicalize path")
}
pub fn expect_path<P: AsRef<Path>>(&self, path: P) {
let path = self.dir.join(path);
assert!(path.exists(), format!("{} exists", path.display()));
}
}