/
task.rs
77 lines (61 loc) · 1.58 KB
/
task.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
use std::collections::BTreeSet;
use std::process::{Command, Stdio};
pub type Feature = &'static str;
pub struct TestArgs {
pub features: BTreeSet<Feature>,
pub default_features: bool,
pub lib_only: bool,
}
impl Default for TestArgs {
fn default() -> Self {
TestArgs {
features: BTreeSet::new(),
default_features: true,
lib_only: false,
}
}
}
impl TestArgs {
fn features_string(&self) -> Option<String> {
if self.features.is_empty() {
return None;
}
let s = self.features.iter().fold(String::new(), |mut s, f| {
if !s.is_empty() {
s.push(' ');
}
s.push_str(f);
s
});
Some(s)
}
}
pub fn test(args: TestArgs) -> bool {
let features = args.features_string();
let mut command = Command::new("cargo");
command
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.arg("test")
.arg("--verbose");
if !args.default_features {
command.arg("--no-default-features");
}
if args.lib_only {
command.arg("--lib");
}
if let Some(features) = &features {
command.args(["--features", features]);
}
println!("running {:?}", command);
let status = command.status().expect("Failed to execute command");
if !status.success() {
eprintln!(
"test execution failed for features: {}",
features.as_ref().map(AsRef::as_ref).unwrap_or("")
);
false
} else {
true
}
}