Skip to content

Commit

Permalink
Merge #214
Browse files Browse the repository at this point in the history
214: Add --no-clean flag r=taiki-e a=taiki-e

cargo-llvm-cov cleans some build artifacts by default to avoid false positives/false negatives due to old build artifacts.
This behavior is disabled when `--no-clean`, `--no-report`, or `--no-run` is passed, and old build artifacts are retained.
When using these flags, it is recommended to first run `cargo llvm-cov clean --workspace` to remove artifacts that may affect the coverage results.

```sh
cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
cargo llvm-cov --no-clean
```

Closes #198 (In the long term I want to remove the need for these clean, but it is not easy.)

Co-authored-by: Taiki Endo <te316e89@gmail.com>
  • Loading branch information
bors[bot] and taiki-e authored Sep 6, 2022
2 parents ccac1ed + a10d36d commit aa3556a
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- Add `--no-clean` flag to build without cleaning any old build artifacts. See [#214](https://github.com/taiki-e/cargo-llvm-cov/pull/214) for more.

- cargo-llvm-cov no longer redirects output from stdout to stderr if unnecessary. ([#206](https://github.com/taiki-e/cargo-llvm-cov/pull/206))

## [0.4.14] - 2022-08-06
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ OPTIONS:
--no-report
Run tests, but don't generate coverage report

--no-clean
Build without cleaning any old build artifacts

--fail-under-lines <MIN>
Exit with a status of 1 if the total line coverage is less than MIN percent

Expand Down Expand Up @@ -358,6 +361,15 @@ cargo llvm-cov --html # run tests and generate html report
cargo llvm-cov --no-run --lcov # generate lcov report
```

cargo-llvm-cov cleans some build artifacts by default to avoid false positives/false negatives due to old build artifacts.
This behavior is disabled when `--no-clean`, `--no-report`, or `--no-run` is passed, and old build artifacts are retained.
When using these flags, it is recommended to first run `cargo llvm-cov clean --workspace` to remove artifacts that may affect the coverage results.

```sh
cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
cargo llvm-cov --no-clean
```

### Merge coverages generated under different test conditions

You can merge the coverages generated under different test conditions by using `--no-report` and `--no-run`.
Expand Down
3 changes: 3 additions & 0 deletions docs/cargo-llvm-cov-run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ OPTIONS:
--no-report
Run tests, but don't generate coverage report

--no-clean
Build without cleaning any old build artifacts

--fail-under-lines <MIN>
Exit with a status of 1 if the total line coverage is less than MIN percent

Expand Down
3 changes: 3 additions & 0 deletions docs/cargo-llvm-cov.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ OPTIONS:
--no-report
Run tests, but don't generate coverage report

--no-clean
Build without cleaning any old build artifacts

--fail-under-lines <MIN>
Exit with a status of 1 if the total line coverage is less than MIN percent

Expand Down
5 changes: 3 additions & 2 deletions src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ pub(crate) fn run(options: &mut Args) -> Result<()> {
Ok(())
}

// If --no-run or --no-report is used: do not remove artifacts
// TODO: remove need for this.
// If --no-clean, --no-run, or --no-report is used: do not remove artifacts
// Otherwise, remove the followings to avoid false positives/false negatives:
// - build artifacts of crates to be measured for coverage
// - profdata
// - profraw
// - doctest bins
// - old reports
pub(crate) fn clean_partial(cx: &Context) -> Result<()> {
if cx.no_run || cx.cov.no_report {
if cx.build.no_clean {
return Ok(());
}

Expand Down
19 changes: 17 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl Args {
let mut remap_path_prefix = false;
let mut include_ffi = false;
let mut verbose: usize = 0;
let mut no_clean = false;

// show-env options
let mut export_prefix = false;
Expand Down Expand Up @@ -329,6 +330,7 @@ impl Args {
Long("remap-path-prefix") => parse_flag!(remap_path_prefix),
Long("include-ffi") => parse_flag!(include_ffi),
Short('v') | Long("verbose") => verbose += 1,
Long("no-clean") => parse_flag!(no_clean),

// llvm-cov options
Long("json") => parse_flag!(json),
Expand Down Expand Up @@ -435,8 +437,16 @@ impl Args {
}

// conflicts
if no_run && no_report {
conflicts("--no-run", "--no-report")?;
if no_report && no_run {
conflicts("--no-report", "--no-run")?;
}
if no_report || no_run {
let flag = if no_report { "--no-report" } else { "--no-run" };
if no_clean {
// --no-report/--no-run implicitly enable --no-clean.
conflicts(flag, "--no-clean")?;
}
no_clean = true;
}
if ignore_run_fail && no_fail_fast {
// --ignore-run-fail implicitly enable --no-fail-fast.
Expand Down Expand Up @@ -600,6 +610,7 @@ impl Args {
color,
remap_path_prefix,
include_ffi,
no_clean,
},
manifest: ManifestOptions { manifest_path },
cargo_args,
Expand Down Expand Up @@ -812,6 +823,10 @@ pub(crate) struct BuildOptions {
/// must be set to Clang/LLVM compatible with the LLVM version used in rustc.
// TODO: support specifying languages like: --include-ffi=c, --include-ffi=c,c++
pub(crate) include_ffi: bool,
/// Build without cleaning any old build artifacts.
///
/// Note that this can cause false positives/false negatives due to old build artifacts.
pub(crate) no_clean: bool,
}

#[derive(Debug)]
Expand Down
3 changes: 0 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub(crate) struct Context {
pub(crate) cov: LlvmCovOptions,

pub(crate) doctests: bool,
pub(crate) no_run: bool,

pub(crate) workspace_members: WorkspaceMembers,
pub(crate) build_script_re: Regex,
Expand Down Expand Up @@ -50,7 +49,6 @@ impl Context {
exclude: &[String],
exclude_from_report: &[String],
doctests: bool,
no_run: bool,
show_env: bool,
) -> Result<Self> {
let ws = Workspace::new(manifest, build.target.as_deref(), doctests, show_env)?;
Expand Down Expand Up @@ -137,7 +135,6 @@ impl Context {
build,
cov,
doctests,
no_run,
workspace_members,
build_script_re,
current_dir: env::current_dir().unwrap(),
Expand Down
13 changes: 2 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,8 @@ fn try_main() -> Result<()> {
}

Subcommand::Run => {
let cx = &Context::new(
args.build(),
&args.manifest(),
args.cov(),
&[],
&[],
false,
false,
false,
)?;
let cx =
&Context::new(args.build(), &args.manifest(), args.cov(), &[], &[], false, false)?;

clean::clean_partial(cx)?;
create_dirs(cx)?;
Expand Down Expand Up @@ -167,7 +159,6 @@ fn context_from_args(args: &mut Args, show_env: bool) -> Result<Context> {
&args.exclude,
&args.exclude_from_report,
args.doctests,
args.no_run,
show_env,
)
}
Expand Down

0 comments on commit aa3556a

Please sign in to comment.