-
Notifications
You must be signed in to change notification settings - Fork 65
/
workdir.rs
310 lines (265 loc) · 9 KB
/
workdir.rs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use std::{
env, fmt, fs,
fs::File,
io::{self, Read, Write},
path::{Path, PathBuf},
process,
str::FromStr,
sync::atomic,
time::Duration,
};
use uuid::Uuid;
use crate::Csv;
static QSV_INTEGRATION_TEST_DIR: &str = "xit";
static NEXT_ID: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
pub struct Workdir {
root: PathBuf,
dir: PathBuf,
flexible: bool,
}
impl Drop for Workdir {
fn drop(&mut self) {
if let Err(err) = fs::remove_dir_all(&self.dir) {
panic!("Could not remove '{:?}': {err}", self.dir);
}
}
}
impl Workdir {
pub fn new(name: &str) -> Workdir {
let id = NEXT_ID.fetch_add(1, atomic::Ordering::SeqCst);
let mut root = env::current_exe()
.unwrap()
.parent()
.expect("executable's directory")
.to_path_buf();
if root.ends_with("deps") {
root.pop();
}
let dir = root
.join(QSV_INTEGRATION_TEST_DIR)
.join(name)
.join(format!("test-{id}-{}", Uuid::new_v4()));
if let Err(err) = create_dir_all(&dir) {
panic!("Could not create '{dir:?}': {err}");
}
Workdir {
root,
dir,
flexible: false,
}
}
pub fn flexible(mut self, yes: bool) -> Workdir {
self.flexible = yes;
self
}
pub fn create<T: Csv>(&self, name: &str, rows: T) {
self.create_with_delim(name, rows, b',')
}
pub fn create_with_delim<T: Csv>(&self, name: &str, rows: T, delim: u8) {
let mut wtr = csv::WriterBuilder::new()
.flexible(self.flexible)
.delimiter(delim)
.from_path(self.path(name))
.unwrap();
for row in rows.to_vecs() {
wtr.write_record(row).unwrap();
}
wtr.flush().unwrap();
}
pub fn create_indexed<T: Csv>(&self, name: &str, rows: T) {
self.create(name, rows);
let mut cmd = self.command("index");
cmd.arg(name);
self.run(&mut cmd);
}
pub fn create_from_string(&self, name: &str, data: &str) {
let filename = &self.path(name);
let mut file = File::create(filename).unwrap();
file.write_all(data.as_bytes()).unwrap();
file.flush().unwrap();
}
pub fn read_to_string(&self, filename: &str) -> String {
let mut file = File::open(self.path(filename)).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap_or_default();
contents
}
pub fn read_stdout<T: Csv>(&self, cmd: &mut process::Command) -> T {
let stdout: String = self.stdout(cmd);
let mut rdr = csv::ReaderBuilder::new()
.has_headers(false)
.flexible(true)
.from_reader(io::Cursor::new(stdout));
let records: Vec<Vec<String>> = rdr
.records()
.collect::<Result<Vec<csv::StringRecord>, _>>()
.unwrap()
.into_iter()
.map(|r| r.iter().map(std::string::ToString::to_string).collect())
.collect();
Csv::from_vecs(records)
}
pub fn command(&self, command_str: &str) -> process::Command {
let mut cmd = process::Command::new(self.qsv_bin());
if command_str.is_empty() {
cmd.current_dir(&self.dir);
} else {
cmd.current_dir(&self.dir).arg(command_str);
}
cmd
}
pub fn output(&self, cmd: &mut process::Command) -> process::Output {
cmd.output().unwrap()
}
pub fn run(&self, cmd: &mut process::Command) {
self.output(cmd);
}
pub fn stdout<T: FromStr>(&self, cmd: &mut process::Command) -> T {
let o = self.output(cmd);
let stdout = String::from_utf8_lossy(&o.stdout);
stdout
.trim_matches(&['\r', '\n'][..])
.parse()
.ok()
.unwrap_or_else(|| panic!("Could not convert from string: '{stdout}'"))
}
pub fn output_stderr(&self, cmd: &mut process::Command) -> String {
{
// ensures stderr has been flushed before we run our cmd
let mut _stderr = io::stderr();
_stderr.flush().unwrap();
}
let o = cmd.output().unwrap();
let o_utf8 = String::from_utf8_lossy(&o.stderr).to_string();
if !o.status.success() || !o_utf8.is_empty() {
o_utf8
} else {
"No error".to_string()
}
}
pub fn assert_success(&self, cmd: &mut process::Command) {
let o = cmd.output().unwrap();
assert!(
o.status.success(),
"\n\n===== {:?} =====\ncommand failed but expected success!\n\ncwd: {}\n\nstatus: \
{}\n\nstdout: {}\n\nstderr: {}\n\n=====\n",
cmd,
self.dir.display(),
o.status,
String::from_utf8_lossy(&o.stdout),
String::from_utf8_lossy(&o.stderr)
);
}
pub fn assert_err(&self, cmd: &mut process::Command) {
let o = cmd.output().unwrap();
assert!(
!o.status.success(),
"\n\n===== {:?} =====\ncommand succeeded but expected failure!\n\ncwd: {}\n\nstatus: \
{}\n\nstdout: {}\n\nstderr: {}\n\n=====\n",
cmd,
self.dir.display(),
o.status,
String::from_utf8_lossy(&o.stdout),
String::from_utf8_lossy(&o.stderr)
);
}
/// returns contents of specified file in resources/test directory
pub fn load_test_resource(&self, filename: &str) -> String {
// locate resources/test relative to crate base dir
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("resources/test/");
path.push(filename);
self.from_str::<String>(path.as_path())
}
/// copy the file in resources/test directory to the working directory
/// returns absolute file path of the copied file
pub fn load_test_file(&self, filename: &str) -> String {
// locate resources/test relative to crate base dir
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("resources/test/");
path.push(filename);
let resource_file_path = path.into_os_string().into_string().unwrap();
let mut wrkdir_path = self.dir.clone();
wrkdir_path.push(filename);
fs::copy(resource_file_path, wrkdir_path.clone()).unwrap();
wrkdir_path.into_os_string().into_string().unwrap()
}
#[allow(clippy::wrong_self_convention)]
pub fn from_str<T: FromStr>(&self, name: &Path) -> T {
log::debug!("reading file: {name:?}");
let mut o = String::new();
fs::File::open(name)
.unwrap()
.read_to_string(&mut o)
.unwrap();
o.parse().ok().expect("fromstr")
}
pub fn path(&self, name: &str) -> PathBuf {
self.dir.join(name)
}
#[cfg(feature = "feature_capable")]
pub fn qsv_bin(&self) -> PathBuf {
self.root.join("qsv")
}
#[cfg(feature = "lite")]
pub fn qsv_bin(&self) -> PathBuf {
self.root.join("qsvlite")
}
#[cfg(feature = "datapusher_plus")]
pub fn qsv_bin(&self) -> PathBuf {
self.root.join("qsvdp")
}
// clear all files in directory
pub fn clear_contents(&self) -> io::Result<()> {
for entry in fs::read_dir(&self.dir)? {
fs::remove_file(entry?.path())?;
}
Ok(())
}
// create a subdirectory
pub fn create_subdir(&self, name: &str) -> io::Result<()> {
let mut path = self.dir.clone();
path.push(name);
create_dir_all(path)
}
}
impl fmt::Debug for Workdir {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "path={}", self.dir.display())
}
}
// For whatever reason, `fs::create_dir_all` fails intermittently on CI
// with a weird "file exists" error. Despite my best efforts to get to the
// bottom of it, I've decided a try-wait-and-retry hack is good enough.
fn create_dir_all<P: AsRef<Path>>(p: P) -> io::Result<()> {
let mut last_err = None;
for _ in 0..10 {
if let Err(err) = fs::create_dir_all(&p) {
last_err = Some(err);
::std::thread::sleep(Duration::from_millis(500));
} else {
return Ok(());
}
}
Err(last_err.unwrap())
}
#[cfg(all(feature = "to", feature = "feature_capable"))]
pub fn is_same_file(file1: &Path, file2: &Path) -> Result<bool, std::io::Error> {
use std::io::BufReader;
let f1 = File::open(file1)?;
let f2 = File::open(file2)?;
// Check if file sizes are different
if f1.metadata().unwrap().len() != f2.metadata().unwrap().len() {
return Ok(false);
}
// Use buf readers since they are much faster
let f1 = BufReader::new(f1);
let f2 = BufReader::new(f2);
// Do a byte to byte comparison of the two files
for (b1, b2) in f1.bytes().zip(f2.bytes()) {
if b1.unwrap() != b2.unwrap() {
return Ok(false);
}
}
return Ok(true);
}