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
extern crate cargo;
use std::path::{Path, PathBuf};
use std::process::{self, Command};
use std::ffi::{OsString};
use cargo::core::{Workspace};
use cargo::ops::{CompileOptions};
use cargo::util::{CargoError, CargoErrorKind, CargoTestError, Test};
use cargo::util::process;
use cargo::{CargoResult};
pub struct CoverageOptions<'a> {
pub compile_opts: CompileOptions<'a>,
pub merge_dir: &'a Path,
pub no_fail_fast: bool,
pub kcov_path: &'a Path,
pub merge_args: Vec<OsString>,
pub exclude_pattern: Option<String>
}
pub fn run_coverage(ws: &Workspace, options: &CoverageOptions, test_args: &[String]) -> CargoResult<Option<CargoTestError>> {
let mut compilation = try!(cargo::ops::compile(ws, &options.compile_opts));
compilation.tests.sort_by(|a, b| {
(a.0.package_id(), &a.1).cmp(&(b.0.package_id(), &b.1))
});
let config = options.compile_opts.config;
let cwd = options.compile_opts.config.cwd();
let mut errors = vec![];
let v : Vec<std::ffi::OsString> = test_args.iter().cloned().map::<std::ffi::OsString, _>(|val| val.into()).collect();
for &(ref pkg, ref kind, ref test, ref exe) in &compilation.tests {
let to_display = match cargo::util::without_prefix(exe, &cwd) {
Some(path) => path,
None => &**exe
};
let mut cmd = try!(compilation.target_process(options.kcov_path, pkg));
let target = ws.target_dir().join("kcov-".to_string() + to_display.file_name().unwrap().to_str().unwrap()).into_path_unlocked();
let default_include_path = format!("--include-path={}", ws.root().display());
let mut args = vec![
OsString::from("--verify"),
OsString::from(default_include_path),
OsString::from(target)];
if let Some(ref exclude) = options.exclude_pattern {
let exclude_option = OsString::from(format!("--exclude-pattern={}", exclude));
args.push(exclude_option);
}
args.push(OsString::from(exe));
args.extend(v.clone());
cmd.args(&args);
try!(config.shell().concise(|shell| {
shell.status("Running", to_display.display().to_string())
}));
try!(config.shell().verbose(|shell| {
shell.status("Running", cmd.to_string())
}));
let result = cmd.exec();
match result {
Err(CargoError(CargoErrorKind::ProcessErrorKind(e), .. )) => {
errors.push(e);
if !options.no_fail_fast {
return Ok(Some(CargoTestError::new(Test::UnitTest(kind.clone(), test.clone()), errors)))
}
}
Err(e) => {
return Err(e)
}
Ok(()) => {}
}
}
let mut mergeargs : Vec<OsString> = vec!["--merge".to_string().into(), options.merge_dir.as_os_str().to_os_string()];
mergeargs.extend(options.merge_args.iter().cloned());
mergeargs.extend(compilation.tests.iter().map(|&(_, _, _, ref exe)|
ws.target_dir().join("kcov-".to_string() + exe.file_name().unwrap().to_str().unwrap()).into_path_unlocked().into()
));
let mut cmd = process(options.kcov_path.as_os_str().to_os_string());
cmd.args(&mergeargs);
try!(config.shell().concise(|shell| {
shell.status("Merging coverage", options.merge_dir.display().to_string())
}));
try!(config.shell().verbose(|shell| {
shell.status("Merging coverage", cmd.to_string())
}));
try!(cmd.exec());
if errors.is_empty() {
Ok(None)
} else {
Ok(Some(CargoTestError::new(Test::Multiple, errors)))
}
}
pub fn build_kcov() -> PathBuf {
if let Some(paths) = std::env::var_os("PATH") {
for path in std::env::split_paths(&paths) {
if path.join("kcov").exists() {
return path.join("kcov");
}
}
}
if Path::new("kcov/build/src/kcov").exists() {
return std::env::current_dir().unwrap().join("kcov/build/src/kcov");
}
let mut init = String::new();
init.push_str(r"
rm -rf master.zip kcov-master kcov
wget https://github.com/SimonKagstrom/kcov/archive/master.zip
unzip master.zip
mv kcov-master kcov
mkdir -p kcov/build
");
for line in init.split("\n") {
let line = line.trim();
if !line.is_empty() {
println!("Running: {:?}", line);
let tokens: Vec<_> = line.split(" ").collect();
let status = Command::new(tokens[0]).args(&tokens[1..]).status().unwrap();
if !status.success() {
process::exit(status.code().unwrap());
}
}
}
let build = r"
cmake ..
make
";
for line in build.split("\n") {
let line = line.trim();
if !line.is_empty() {
println!("Running: {:?}", line);
let tokens: Vec<_> = line.split(" ").collect();
let status = Command::new(tokens[0]).args(&tokens[1..]).current_dir("kcov/build").status().unwrap();
if !status.success() {
process::exit(status.code().unwrap());
}
}
}
std::env::current_dir().unwrap().join("kcov/build/src/kcov")
}