Skip to content

Instantly share code, notes, and snippets.

@lorepozo
Last active April 7, 2018 16:12
Show Gist options
  • Save lorepozo/fed70f63c938fd2d0a24eb6ea8be651e to your computer and use it in GitHub Desktop.
Save lorepozo/fed70f63c938fd2d0a24eb6ea8be651e to your computer and use it in GitHub Desktop.
Get kcov to handle rustdoc tests
[package]
name = "kcov_rustdoc"
version = "0.0.1"
[dependencies]
redhook = "0.1"
libc = "0.2"
[lib]
name = "kcov_rustdoc"
path = "kcov_rustdoc.rs"
crate_type = ["dylib"]
// see https://github.com/xd009642/tarpaulin/issues/13#issuecomment-335564299
// usage: TEST_WRAPPER="kcov --verify target/kcov" LD_PRELOAD=libkcov_rustdoc.so cargo test --doc
#[macro_use]
extern crate redhook;
extern crate libc;
use libc::c_char;
use std::ffi::CString;
use std::slice;
use std::env;
hook! {
unsafe fn execvp(filename: *const c_char, argv: *const *const c_char) => my_execvp {
let file = slice::from_raw_parts(filename as *const u8, libc::strlen(filename));
if let Ok(run) = env::var("TEST_WRAPPER") {
if let Ok(cmd) = run.split_whitespace()
.map(|s| CString::new(s))
.collect::<Result<Vec<_>, _>>() {
if !cmd.is_empty() &&
file.starts_with(b"/tmp/rustdoctest") &&
file.ends_with(b"rust_out")
{
let mut args: Vec<_> = cmd.iter().map(|s| s.as_ptr()).collect();
let mut x = argv;
while !(*x).is_null() {
args.push(*x);
x = x.offset(1);
}
args.push(*x);
real!(execvp)(cmd[0].as_ptr(), args.as_ptr());
return;
}
}
}
real!(execvp)(filename, argv);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment