diff --git a/src/main.rs b/src/main.rs index 2817da71..e7d7bec6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"); } diff --git a/tests/fixtures/coverage-reports/doctest/workspace_root.hide-instantiations.txt b/tests/fixtures/coverage-reports/doctest/workspace_root.hide-instantiations.txt new file mode 100644 index 00000000..117f4f49 --- /dev/null +++ b/tests/fixtures/coverage-reports/doctest/workspace_root.hide-instantiations.txt @@ -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(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|} \ No newline at end of file diff --git a/tests/fixtures/coverage-reports/doctest/workspace_root.json b/tests/fixtures/coverage-reports/doctest/workspace_root.json new file mode 100644 index 00000000..f4fcacfc --- /dev/null +++ b/tests/fixtures/coverage-reports/doctest/workspace_root.json @@ -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" +} \ No newline at end of file diff --git a/tests/fixtures/coverage-reports/doctest/workspace_root.lcov.info b/tests/fixtures/coverage-reports/doctest/workspace_root.lcov.info new file mode 100644 index 00000000..4dec27c2 --- /dev/null +++ b/tests/fixtures/coverage-reports/doctest/workspace_root.lcov.info @@ -0,0 +1,8 @@ +SF:src/lib.rs +FNF:8 +FNH:8 +BRF:0 +BRH:0 +LF:27 +LH:25 +end_of_record \ No newline at end of file diff --git a/tests/fixtures/coverage-reports/doctest/workspace_root.summary.txt b/tests/fixtures/coverage-reports/doctest/workspace_root.summary.txt new file mode 100644 index 00000000..f2294bb7 --- /dev/null +++ b/tests/fixtures/coverage-reports/doctest/workspace_root.summary.txt @@ -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 - \ No newline at end of file diff --git a/tests/fixtures/coverage-reports/doctest/workspace_root.txt b/tests/fixtures/coverage-reports/doctest/workspace_root.txt new file mode 100644 index 00000000..d263c514 --- /dev/null +++ b/tests/fixtures/coverage-reports/doctest/workspace_root.txt @@ -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(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::: + | 4| 1|pub fn generic_fn(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(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|} \ No newline at end of file diff --git a/tests/fixtures/crates/doctest/Cargo.toml b/tests/fixtures/crates/doctest/Cargo.toml new file mode 100644 index 00000000..6ad48092 --- /dev/null +++ b/tests/fixtures/crates/doctest/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "crate1" +version = "0.0.0" diff --git a/tests/fixtures/crates/doctest/src/lib.rs b/tests/fixtures/crates/doctest/src/lib.rs new file mode 100644 index 00000000..75473862 --- /dev/null +++ b/tests/fixtures/crates/doctest/src/lib.rs @@ -0,0 +1,27 @@ +/// ``` +/// assert_eq!(crate1::generic_fn("doc", "doctest"), Ok("doctest")); +/// ``` +pub fn generic_fn(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"); +} diff --git a/tests/test.rs b/tests/test.rs index 074c7f2a..51e39c7d 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -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