Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce coverage worker #96

Merged
merged 7 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ anyhow = { version = "1.0.83", optional = true }
cargo_metadata = { version = "0.18.1", optional = true }
clap = { version = "4.5.4", features = ["cargo", "derive", "env"], optional = true }
console = { version = "0.15.8", optional = true }
ctrlc = "3.4.4"
env_logger = { version = "0.11.3", optional = true }
fork = { version = "0.1.23", optional = true }
glob = { version = "0.3.1", optional = true }
Expand Down
84 changes: 54 additions & 30 deletions src/bin/cargo-ziggy/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,12 @@ impl Cover {
self.target =
find_target(&self.target).context("⚠️ couldn't find the target to start coverage")?;

// The cargo executable
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));

let mut coverage_rustflags = env::var("COVERAGE_RUSTFLAGS")
.unwrap_or_else(|_| String::from("--cfg=coverage -Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort "));
coverage_rustflags.push_str(&env::var("RUSTFLAGS").unwrap_or_default());

// We build the runner with the appropriate flags for coverage
process::Command::new(cargo)
.args([
"rustc",
"--target-dir=target/coverage",
"--features=ziggy/coverage",
])
.env("RUSTFLAGS", coverage_rustflags)
.env("RUSTDOCFLAGS", "-Cpanic=unwind")
.env("CARGO_INCREMENTAL", "0")
.env("RUSTC_BOOTSTRAP", "1") // Trick to avoid forcing user to use rust nightly
.spawn()
.context("⚠️ couldn't spawn rustc for coverage")?
.wait()
.context("⚠️ couldn't wait for the rustc during coverage")?;
// build the runner
Cover::build_runner()?;

if !self.keep {
// We remove the previous coverage files
if let Ok(gcda_files) = glob("target/coverage/debug/deps/*.gcda") {
for file in gcda_files.flatten() {
let file_string = &file.display();
fs::remove_file(&file)
.context(format!("⚠️ couldn't find {} during coverage", file_string))?;
}
}
Cover::clean_old_cov()?;
}

let mut shared_corpus = PathBuf::new();
Expand Down Expand Up @@ -98,10 +72,50 @@ impl Cover {
};

// We generate the code coverage report
Cover::run_grcov(
&self.target,
output_types,
&coverage_dir,
&source_or_workspace_root,
)
}

/// Build the runner with the appropriate flags for coverage
pub fn build_runner() -> Result<(), anyhow::Error> {
// The cargo executable
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));

let mut coverage_rustflags = env::var("COVERAGE_RUSTFLAGS")
.unwrap_or_else(|_| String::from("--cfg=coverage -Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort"));
coverage_rustflags.push_str(&env::var("RUSTFLAGS").unwrap_or_default());

process::Command::new(cargo)
.args([
"rustc",
"--target-dir=target/coverage",
"--features=ziggy/coverage",
])
.env("RUSTFLAGS", coverage_rustflags)
.env("RUSTDOCFLAGS", "-Cpanic=unwind")
.env("CARGO_INCREMENTAL", "0")
.env("RUSTC_BOOTSTRAP", "1") // Trick to avoid forcing user to use rust nightly
.spawn()
.context("⚠️ couldn't spawn rustc for coverage")?
.wait()
.context("⚠️ couldn't wait for the rustc during coverage")?;
Ok(())
}

pub fn run_grcov(
target: &str,
output_types: &str,
coverage_dir: &str,
source_or_workspace_root: &str,
) -> Result<(), anyhow::Error> {
process::Command::new("grcov")
.args([
".",
&format!("-b=./target/coverage/debug/{}", self.target),
&format!("-b=./target/coverage/debug/{}", target),
&format!("-s={source_or_workspace_root}"),
&format!("-t={}", output_types),
"--llvm",
Expand All @@ -113,7 +127,17 @@ impl Cover {
.context("⚠️ cannot find grcov in your path, please install it")?
.wait()
.context("⚠️ couldn't wait for the grcov process")?;
Ok(())
}

pub fn clean_old_cov() -> Result<(), anyhow::Error> {
if let Ok(gcda_files) = glob("target/coverage/debug/deps/*.gcda") {
for file in gcda_files.flatten() {
let file_string = &file.display();
fs::remove_file(&file)
.context(format!("⚠️ couldn't find {} during coverage", file_string))?;
}
}
Ok(())
}
}
Loading
Loading