diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b09c31b..4de36f03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com ## [Unreleased] +- Fix an issue where some files were incorrectly ignored in reports. ([#191](https://github.com/taiki-e/cargo-llvm-cov/pull/191)) + ## [0.4.8] - 2022-06-16 - Correctly escape regular expressions passed to `-ignore-filename-regex`. ([#188](https://github.com/taiki-e/cargo-llvm-cov/pull/188), thanks @rhysd) diff --git a/src/main.rs b/src/main.rs index 881ca2a2..ad9f3deb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -800,12 +800,6 @@ fn ignore_filename_regex(cx: &Context) -> Option { #[cfg(windows)] const SEPARATOR: &str = "\\\\"; // On windows, we should escape the separator. - fn default_ignore_filename_regex() -> String { - // TODO: Should we use the actual target path instead of using `tests|examples|benches`? - // We may have a directory like tests/support, so maybe we need both? - format!(r"(^|{0})(rustc{0}[0-9a-f]+|tests|examples|benches){0}", SEPARATOR) - } - #[derive(Default)] struct Out(String); @@ -830,20 +824,31 @@ fn ignore_filename_regex(cx: &Context) -> Option { out.push(ignore_filename); } if !cx.cov.disable_default_ignore_filename_regex { - out.push(default_ignore_filename_regex()); - out.push_abs_path(&cx.ws.target_dir); + // TODO: Should we use the actual target path instead of using `tests|examples|benches`? + // We may have a directory like tests/support, so maybe we need both? if cx.build.remap_path_prefix { - for path in [home::home_dir(), home::cargo_home().ok(), home::rustup_home().ok()] - .iter() - .flatten() - { - out.push_abs_path(path); - } + out.push(format!(r"(^|{0})(rustc{0}[0-9a-f]+|tests|examples|benches){0}", SEPARATOR)); } else { - for path in [home::cargo_home().ok(), home::rustup_home().ok()].iter().flatten() { + out.push(format!( + r"{0}rustc{0}[0-9a-f]+{0}|^{1}({0}.*)?{0}(tests|examples|benches){0}", + SEPARATOR, + regex::escape(cx.ws.metadata.workspace_root.as_str()) + )); + } + out.push_abs_path(&cx.ws.target_dir); + if cx.build.remap_path_prefix { + if let Some(path) = home::home_dir() { out.push_abs_path(path); } } + if let Ok(path) = home::cargo_home() { + let path = regex::escape(path.as_os_str().to_string_lossy().as_ref()); + let path = format!("^{1}{0}(registry|git){0}", SEPARATOR, path); + out.push(path); + } + if let Ok(path) = home::rustup_home() { + out.push_abs_path(path.join("toolchains")); + } for path in resolve_excluded_paths(cx) { out.push_abs_path(path); } diff --git a/tests/fixtures/coverage-reports/real1/all.hide-instantiations.txt b/tests/fixtures/coverage-reports/real1/all.hide-instantiations.txt index a82264d9..4b5174b3 100644 --- a/tests/fixtures/coverage-reports/real1/all.hide-instantiations.txt +++ b/tests/fixtures/coverage-reports/real1/all.hide-instantiations.txt @@ -1,38 +1,42 @@ member1/member2/src/lib.rs: - 1| 1|pub fn func(x: u32) {pub fn func(x: u32) { - 2| 1| match x { - 3| 1| 0 => {} + 1| 2|pub fn func(x: u32) {pub fn func(x: u32) { + ^1 + 2| 2| match x { + 3| 2| 0 => {} 4| 0| 1 => {} 5| 0| 2 => {} 6| 0| _ => {} 7| | } - 8| 1|} + 8| 2|} member1/src/lib.rs: - 1| 1|pub fn func(x: u32) {pub fn func(x: u32) { - 2| 1| match x { - 3| 1| 0 => {} - 4| 0| 1 => {} - 5| 0| 2 => {} - 6| 0| _ => {} - 7| | } - 8| 1|} - -src/lib.rs: 1| 2|pub fn func(x: u32) {pub fn func(x: u32) { ^1 2| 2| match x { - 3| 0| 0 => {} - 4| 1| 1 => {} + 3| 2| 0 => {} + 4| 0| 1 => {} 5| 0| 2 => {} - 6| 1| _ => {} + 6| 0| _ => {} 7| | } 8| 2|} - 9| | - 10| 1|#[test] - 11| 1|fn test() { - 12| 1| func(1); - 13| 1| func(3); - 14| 1| member1::func(0); - 15| 1| member2::func(0); - 16| 1|} \ No newline at end of file + +src/lib.rs: + 1| 1|#[cfg(test)]#[cfg(test)] + 2| |mod tests; + 3| | + 4| 4|pub fn func(x: u32) { + 5| 4| match x { + 6| 0| 0 => {} + 7| 2| 1 => {} + 8| 0| 2 => {} + 9| 2| _ => {} + 10| | } + 11| 4|} + 12| | + 13| 1|#[test] + 14| 1|fn test() { + 15| 1| func(1); + 16| 1| func(3); + 17| 1| member1::func(0); + 18| 1| member2::func(0); + 19| 1|} \ No newline at end of file diff --git a/tests/fixtures/coverage-reports/real1/all.txt b/tests/fixtures/coverage-reports/real1/all.txt index 42788ddf..c9cf4c01 100644 --- a/tests/fixtures/coverage-reports/real1/all.txt +++ b/tests/fixtures/coverage-reports/real1/all.txt @@ -1,64 +1,68 @@ member1/member2/src/lib.rs: - 1| 1|pub fn func(x: u32) {pub fn func(x: u32) { - 2| 1| match x { - 3| 1| 0 => {} + 1| 2|pub fn func(x: u32) {pub fn func(x: u32) { + ^1 + 2| 2| match x { + 3| 2| 0 => {} 4| 0| 1 => {} 5| 0| 2 => {} 6| 0| _ => {} 7| | } - 8| 1|} + 8| 2|} ------------------ | member2::func: - | 1| 1|pub fn func(x: u32) { - | 2| 1| match x { - | 3| 1| 0 => {} + | 1| 2|pub fn func(x: u32) { + | 2| 2| match x { + | 3| 2| 0 => {} | 4| 0| 1 => {} | 5| 0| 2 => {} | 6| 0| _ => {} | 7| | } - | 8| 1|} + | 8| 2|} ------------------ | Unexecuted instantiation: member2::func ------------------ member1/src/lib.rs: - 1| 1|pub fn func(x: u32) {pub fn func(x: u32) { - 2| 1| match x { - 3| 1| 0 => {} + 1| 2|pub fn func(x: u32) {pub fn func(x: u32) { + ^1 + 2| 2| match x { + 3| 2| 0 => {} 4| 0| 1 => {} 5| 0| 2 => {} 6| 0| _ => {} 7| | } - 8| 1|} + 8| 2|} ------------------ | member1::func: - | 1| 1|pub fn func(x: u32) { - | 2| 1| match x { - | 3| 1| 0 => {} + | 1| 2|pub fn func(x: u32) { + | 2| 2| match x { + | 3| 2| 0 => {} | 4| 0| 1 => {} | 5| 0| 2 => {} | 6| 0| _ => {} | 7| | } - | 8| 1|} + | 8| 2|} ------------------ | Unexecuted instantiation: member1::func ------------------ src/lib.rs: - 1| 2|pub fn func(x: u32) {pub fn func(x: u32) { - ^1 - 2| 2| match x { - 3| 0| 0 => {} - 4| 1| 1 => {} - 5| 0| 2 => {} - 6| 1| _ => {} - 7| | } - 8| 2|} - 9| | - 10| 1|#[test] - 11| 1|fn test() { - 12| 1| func(1); - 13| 1| func(3); - 14| 1| member1::func(0); - 15| 1| member2::func(0); - 16| 1|} \ No newline at end of file + 1| 1|#[cfg(test)]#[cfg(test)] + 2| |mod tests; + 3| | + 4| 4|pub fn func(x: u32) { + 5| 4| match x { + 6| 0| 0 => {} + 7| 2| 1 => {} + 8| 0| 2 => {} + 9| 2| _ => {} + 10| | } + 11| 4|} + 12| | + 13| 1|#[test] + 14| 1|fn test() { + 15| 1| func(1); + 16| 1| func(3); + 17| 1| member1::func(0); + 18| 1| member2::func(0); + 19| 1|} \ No newline at end of file diff --git a/tests/fixtures/coverage-reports/real1/workspace_root.hide-instantiations.txt b/tests/fixtures/coverage-reports/real1/workspace_root.hide-instantiations.txt index ddfb5919..620f5058 100644 --- a/tests/fixtures/coverage-reports/real1/workspace_root.hide-instantiations.txt +++ b/tests/fixtures/coverage-reports/real1/workspace_root.hide-instantiations.txt @@ -1,38 +1,40 @@ member1/member2/src/lib.rs: - 1| 1|pub fn func(x: u32) { - 2| 1| match x { - 3| 1| 0 => {} + 1| 2|pub fn func(x: u32) { + 2| 2| match x { + 3| 2| 0 => {} 4| 0| 1 => {} 5| 0| 2 => {} 6| 0| _ => {} 7| | } - 8| 1|} + 8| 2|} member1/src/lib.rs: - 1| 1|pub fn func(x: u32) { - 2| 1| match x { - 3| 1| 0 => {} + 1| 2|pub fn func(x: u32) { + 2| 2| match x { + 3| 2| 0 => {} 4| 0| 1 => {} 5| 0| 2 => {} 6| 0| _ => {} 7| | } - 8| 1|} + 8| 2|} src/lib.rs: - 1| 2|pub fn func(x: u32) {pub fn func(x: u32) { - ^1 - 2| 2| match x { - 3| 0| 0 => {} - 4| 1| 1 => {} - 5| 0| 2 => {} - 6| 1| _ => {} - 7| | } - 8| 2|} - 9| | - 10| 1|#[test] - 11| 1|fn test() { - 12| 1| func(1); - 13| 1| func(3); - 14| 1| member1::func(0); - 15| 1| member2::func(0); - 16| 1|} \ No newline at end of file + 1| 1|#[cfg(test)]#[cfg(test)] + 2| |mod tests; + 3| | + 4| 4|pub fn func(x: u32) { + 5| 4| match x { + 6| 0| 0 => {} + 7| 2| 1 => {} + 8| 0| 2 => {} + 9| 2| _ => {} + 10| | } + 11| 4|} + 12| | + 13| 1|#[test] + 14| 1|fn test() { + 15| 1| func(1); + 16| 1| func(3); + 17| 1| member1::func(0); + 18| 1| member2::func(0); + 19| 1|} \ No newline at end of file diff --git a/tests/fixtures/coverage-reports/real1/workspace_root.txt b/tests/fixtures/coverage-reports/real1/workspace_root.txt index ddfb5919..620f5058 100644 --- a/tests/fixtures/coverage-reports/real1/workspace_root.txt +++ b/tests/fixtures/coverage-reports/real1/workspace_root.txt @@ -1,38 +1,40 @@ member1/member2/src/lib.rs: - 1| 1|pub fn func(x: u32) { - 2| 1| match x { - 3| 1| 0 => {} + 1| 2|pub fn func(x: u32) { + 2| 2| match x { + 3| 2| 0 => {} 4| 0| 1 => {} 5| 0| 2 => {} 6| 0| _ => {} 7| | } - 8| 1|} + 8| 2|} member1/src/lib.rs: - 1| 1|pub fn func(x: u32) { - 2| 1| match x { - 3| 1| 0 => {} + 1| 2|pub fn func(x: u32) { + 2| 2| match x { + 3| 2| 0 => {} 4| 0| 1 => {} 5| 0| 2 => {} 6| 0| _ => {} 7| | } - 8| 1|} + 8| 2|} src/lib.rs: - 1| 2|pub fn func(x: u32) {pub fn func(x: u32) { - ^1 - 2| 2| match x { - 3| 0| 0 => {} - 4| 1| 1 => {} - 5| 0| 2 => {} - 6| 1| _ => {} - 7| | } - 8| 2|} - 9| | - 10| 1|#[test] - 11| 1|fn test() { - 12| 1| func(1); - 13| 1| func(3); - 14| 1| member1::func(0); - 15| 1| member2::func(0); - 16| 1|} \ No newline at end of file + 1| 1|#[cfg(test)]#[cfg(test)] + 2| |mod tests; + 3| | + 4| 4|pub fn func(x: u32) { + 5| 4| match x { + 6| 0| 0 => {} + 7| 2| 1 => {} + 8| 0| 2 => {} + 9| 2| _ => {} + 10| | } + 11| 4|} + 12| | + 13| 1|#[test] + 14| 1|fn test() { + 15| 1| func(1); + 16| 1| func(3); + 17| 1| member1::func(0); + 18| 1| member2::func(0); + 19| 1|} \ No newline at end of file diff --git a/tests/fixtures/crates/real1/src/lib.rs b/tests/fixtures/crates/real1/src/lib.rs index cd97d391..325e2de0 100644 --- a/tests/fixtures/crates/real1/src/lib.rs +++ b/tests/fixtures/crates/real1/src/lib.rs @@ -1,3 +1,6 @@ +#[cfg(test)] +mod tests; + pub fn func(x: u32) { match x { 0 => {} diff --git a/tests/fixtures/crates/real1/src/tests/mod.rs b/tests/fixtures/crates/real1/src/tests/mod.rs new file mode 100644 index 00000000..6579a58d --- /dev/null +++ b/tests/fixtures/crates/real1/src/tests/mod.rs @@ -0,0 +1,15 @@ +// src/tests is a well-known pattern: https://grep.app/search?q=%23%5Btest%5D&filter[path][0]=src/tests/&filter[lang][0]=Rust +// +// Note that to check that this pattern is properly supported, +// you need to run cargo-llvm-cov without --remap-path-prefix flag. +// (Our test suite always enables that flag.) + +use super::*; + +#[test] +fn test() { + func(1); + func(3); + member1::func(0); + member2::func(0); +}