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

Fix generic code hit by doctests not correctly remapped #122

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ fn set_env(cx: &Context, target: &mut impl EnvTarget) {
" -Z instrument-coverage -Z unstable-options --persist-doctests {}",
cx.ws.doctests_dir
));
rustdocflags.push_str(&format!(" --remap-path-prefix {}/=", cx.ws.metadata.workspace_root));
if cfg!(windows) {
rustdocflags.push_str(" -C codegen-units=1");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
1| 1|/// ```/// ```
2| 1|/// assert_eq!(crate1::generic_fn("doc", "doctest"), Ok("doctest"));
3| 1|/// ```
4| 2|pub fn generic_fn<T>(s: &str, val: T) -> Result<&str, T> {
5| 2| match s {
6| 2| "unit" => Ok("unit-test"),
^1
7| 1| "doc" => Ok("doctest"),
8| 0| _ => Err(val),
9| | }
10| 2|}
11| |
12| 1|/// ```
13| 1|/// assert_eq!(crate1::non_generic_fn("doc"), "doctest");
14| 1|/// ```
15| 2|pub fn non_generic_fn(s: &str) -> &str {
16| 2| match s {
17| 2| "unit" => "unit-test",
^1
18| 1| "doc" => "doctest",
19| 0| val => val,
20| | }
21| 2|}
22| |
23| 1|#[test]
24| 1|fn unit_test() {
25| 1| assert_eq!(generic_fn("unit", 1), Ok("unit-test"));
26| 1| assert_eq!(non_generic_fn("unit"), "unit-test");
27| 1|}
71 changes: 71 additions & 0 deletions tests/fixtures/coverage-reports/doctest/workspace_root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"data": [
{
"files": [
{
"filename": "src/lib.rs",
"summary": {
"branches": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 8,
"covered": 8,
"percent": 100.0
},
"instantiations": {
"count": 11,
"covered": 10,
"percent": 90.9090909090909
},
"lines": {
"count": 27,
"covered": 25,
"percent": 92.5925925925926
},
"regions": {
"count": 22,
"covered": 18,
"notcovered": 4,
"percent": 81.81818181818183
}
}
}
],
"totals": {
"branches": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0
},
"functions": {
"count": 8,
"covered": 8,
"percent": 100
},
"instantiations": {
"count": 11,
"covered": 10,
"percent": 90.9090909090909
},
"lines": {
"count": 27,
"covered": 25,
"percent": 92.5925925925926
},
"regions": {
"count": 22,
"covered": 18,
"notcovered": 4,
"percent": 81.81818181818183
}
}
}
],
"type": "llvm.coverage.json.export",
"version": "2.0.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SF:src/lib.rs
FNF:8
FNH:8
BRF:0
BRH:0
LF:27
LH:25
end_of_record
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
src/lib.rs 22 4 81.82% 8 0 100.00% 27 2 92.59% 0 0 -
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL 22 4 81.82% 8 0 100.00% 27 2 92.59% 0 0 -
71 changes: 71 additions & 0 deletions tests/fixtures/coverage-reports/doctest/workspace_root.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
1| 1|/// ```/// ```
2| 1|/// assert_eq!(crate1::generic_fn("doc", "doctest"), Ok("doctest"));
3| 1|/// ```
4| 2|pub fn generic_fn<T>(s: &str, val: T) -> Result<&str, T> {
5| 2| match s {
6| 2| "unit" => Ok("unit-test"),
^1
7| 1| "doc" => Ok("doctest"),
8| 0| _ => Err(val),
9| | }
10| 2|}
------------------
| crate1::generic_fn::<i32>:
| 4| 1|pub fn generic_fn<T>(s: &str, val: T) -> Result<&str, T> {
| 5| 1| match s {
| 6| 1| "unit" => Ok("unit-test"),
| 7| 0| "doc" => Ok("doctest"),
| 8| 0| _ => Err(val),
| 9| | }
| 10| 1|}
------------------
| Unexecuted instantiation: crate1::generic_fn::<_>
------------------
| crate1::generic_fn::<&str>:
| 4| 1|pub fn generic_fn<T>(s: &str, val: T) -> Result<&str, T> {
| 5| 1| match s {
| 6| 1| "unit" => Ok("unit-test"),
| ^0
| 7| 1| "doc" => Ok("doctest"),
| 8| 0| _ => Err(val),
| 9| | }
| 10| 1|}
------------------
11| |
12| 1|/// ```
13| 1|/// assert_eq!(crate1::non_generic_fn("doc"), "doctest");
14| 1|/// ```
15| 2|pub fn non_generic_fn(s: &str) -> &str {
16| 2| match s {
17| 2| "unit" => "unit-test",
^1
18| 1| "doc" => "doctest",
19| 0| val => val,
20| | }
21| 2|}
------------------
| crate1::non_generic_fn:
| 15| 1|pub fn non_generic_fn(s: &str) -> &str {
| 16| 1| match s {
| 17| 1| "unit" => "unit-test",
| 18| 0| "doc" => "doctest",
| 19| 0| val => val,
| 20| | }
| 21| 1|}
------------------
| crate1::non_generic_fn:
| 15| 1|pub fn non_generic_fn(s: &str) -> &str {
| 16| 1| match s {
| 17| 1| "unit" => "unit-test",
| ^0
| 18| 1| "doc" => "doctest",
| 19| 0| val => val,
| 20| | }
| 21| 1|}
------------------
22| |
23| 1|#[test]
24| 1|fn unit_test() {
25| 1| assert_eq!(generic_fn("unit", 1), Ok("unit-test"));
26| 1| assert_eq!(non_generic_fn("unit"), "unit-test");
27| 1|}
3 changes: 3 additions & 0 deletions tests/fixtures/crates/doctest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "crate1"
version = "0.0.0"
27 changes: 27 additions & 0 deletions tests/fixtures/crates/doctest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// ```
/// assert_eq!(crate1::generic_fn("doc", "doctest"), Ok("doctest"));
/// ```
pub fn generic_fn<T>(s: &str, val: T) -> Result<&str, T> {
match s {
"unit" => Ok("unit-test"),
"doc" => Ok("doctest"),
_ => Err(val),
}
}

/// ```
/// assert_eq!(crate1::non_generic_fn("doc"), "doctest");
/// ```
pub fn non_generic_fn(s: &str) -> &str {
match s {
"unit" => "unit-test",
"doc" => "doctest",
val => val,
}
}

#[test]
fn unit_test() {
assert_eq!(generic_fn("unit", 1), Ok("unit-test"));
assert_eq!(non_generic_fn("unit"), "unit-test");
}
5 changes: 5 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ fn virtual1() {
run("virtual1", "exclude", &["--workspace", "--exclude", "member2"], &[]);
}

#[test]
fn doctest() {
run("doctest", "workspace_root", &["--doctests"], &[]);
}

#[test]
fn no_test() {
// TODO: we should fix this: https://github.com/taiki-e/cargo-llvm-cov/issues/21
Expand Down