-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
61 lines (53 loc) · 1.61 KB
/
lib.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
#![feature(custom_test_frameworks)]
#![test_runner(crate::runner)]
use std::env;
#[cfg(test)] use std::io::{self, Write};
use std::process::{Command, exit};
// A test that "fails" if it returns false
type SillyTest = Fn() -> bool;
// Looks at argv to see if we're the master runner or the subordinate runner
// If no args are supplied we're the master
// Otherwise the parameter provided is the index of the test to run
pub fn runner(tests: &[&SillyTest]) {
let args: Vec<_> = env::args().collect();
if args.len() == 1 {
master_main(tests);
} else {
sub_main(
tests,
args[1].parse().unwrap());
}
}
// Runs each test in a separate process
fn master_main(tests: &[&SillyTest]) {
let curr_exe = env::current_exe().expect("Couldn't get current exe... sorry?");
for i in 0..tests.len() {
let output = Command::new(&curr_exe)
.arg(&i.to_string())
.output().expect("failed to execute child process");
if output.status.success() {
print!(".");
} else {
print!("f");
println!("\n{}", ::std::str::from_utf8(&output.stdout).unwrap());
println!("\n{}", ::std::str::from_utf8(&output.stderr).unwrap());
}
}
}
// Runs a specific test
fn sub_main(tests: &[&SillyTest], idx: usize) {
if !tests[idx]() {
exit(1);
}
exit(0);
}
#[test_case]
fn foo() -> bool {
io::stdout().lock().write(b"Should be swallowed!\n").unwrap();
true
}
#[test_case]
fn foo() -> bool {
io::stdout().lock().write(b"Is properly reported\n").unwrap();
false
}