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
extern crate cargo;
extern crate fs_extra;
use std::path::{Path, PathBuf};
use std::process::{self, Command};
use std::ffi::{OsString};
use std::fs;
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)))
}
}
fn require_success(status: process::ExitStatus) {
if !status.success() {
process::exit(status.code().unwrap())
}
}
pub fn build_kcov<P: AsRef<Path>>(kcov_dir: P) -> 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");
}
}
}
let kcov_dir: &Path = kcov_dir.as_ref();
let kcov_master_dir = kcov_dir.join("kcov-master");
let kcov_build_dir = kcov_master_dir.join("build");
let kcov_built_path = kcov_build_dir.join("src/kcov");
if kcov_built_path.exists() {
return kcov_built_path;
}
println!("Downloading kcov");
require_success(
Command::new("wget")
.current_dir(kcov_dir)
.arg("https://github.com/SimonKagstrom/kcov/archive/master.zip")
.status()
.unwrap()
);
println!("Extracting kcov");
require_success(
Command::new("unzip")
.current_dir(kcov_dir)
.arg("master.zip")
.status()
.unwrap()
);
fs::create_dir(&kcov_build_dir);
println!("CMaking kcov");
require_success(
Command::new("cmake")
.current_dir(&kcov_build_dir)
.arg("..")
.status()
.unwrap()
);
println!("Making kcov");
require_success(
Command::new("make")
.current_dir(&kcov_build_dir)
.status()
.unwrap()
);
assert!(kcov_build_dir.exists());
kcov_built_path
}
pub fn doc_upload(branch: &str, message: &str, origin: &str, gh_pages: &str) {
let doc_upload = Path::new("target/doc-upload");
if !doc_upload.exists() {
let status = Command::new("git")
.arg("clone")
.arg("--verbose")
.args(&["--branch", gh_pages])
.args(&["--depth", "1"])
.arg(origin)
.arg(doc_upload)
.status()
.unwrap();
if !status.success() {
require_success(
Command::new("git")
.arg("init")
.arg(doc_upload)
.status()
.unwrap()
);
require_success(
Command::new("git")
.current_dir(doc_upload)
.arg("checkout")
.args(&["-b", gh_pages])
.status()
.unwrap()
);
}
}
let doc_upload_branch = doc_upload.join(branch);
fs::create_dir(&doc_upload_branch).ok();
for entry in doc_upload_branch.read_dir().unwrap() {
let dir = entry.unwrap();
if dir.file_name() != OsString::from("index.html") {
let path = dir.path();
println!("rm -r {}", path.to_string_lossy());
fs::remove_dir_all(&path).ok();
fs::remove_file(path).ok();
}
}
let doc = Path::new("target/doc");
println!("cp {} {}", doc.to_string_lossy(), doc_upload_branch.to_string_lossy());
let mut last_progress = 0;
fs_extra::copy_items_with_progress(
&doc.read_dir().unwrap().map(|entry| entry.unwrap().path()).collect(),
doc_upload_branch,
&fs_extra::dir::CopyOptions::new(),
|info| {
if info.copied_bytes >> 20 > last_progress {
last_progress = info.copied_bytes >> 20;
println!("{}/{} MiB", info.copied_bytes >> 20, info.total_bytes >> 20);
}
fs_extra::dir::TransitProcessResult::ContinueOrAbort
}
).unwrap();
require_success(
Command::new("git")
.current_dir(doc_upload)
.arg("add")
.arg("--verbose")
.arg("--all")
.status()
.unwrap()
);
require_success(
Command::new("git")
.current_dir(doc_upload)
.arg("commit")
.arg("--verbose")
.args(&["-m", message])
.status()
.unwrap()
);
let status = Command::new("git")
.current_dir(doc_upload)
.arg("push")
.arg(origin)
.arg(gh_pages)
.status()
.unwrap();
if status.success() {
println!("Successfully updated documentation.");
} else {
println!("Documentation already up-to-date.");
}
}