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

Line coverage false negative issue with async functions called inside a std::panic::catch_unwind #1079

Open
Fran314 opened this issue Aug 21, 2022 · 0 comments
Assignees
Labels

Comments

@Fran314
Copy link

Fran314 commented Aug 21, 2022

Describe the bug
Tarpaulin doesn't recognise a line as covered even though it is. In particular, in the example tarpaulin says that the line 5 is not covered.

This does not happen if the call to the async function that I want to test happens outside a catch_unwind block. I can't just put it outside a catch_unwind block because my test requires some setup and teardown, and I have to make sure that even if the test fails, I do the teardown, and I'm using the catch_unwind block as suggested here.

This does not happen if, even if inside a catch_unwind block, the function is sync, as shown in the example

To Reproduce
[./Cargo.toml]

[package]
name = "tarpaulin-bug-async"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1", features = ["full"] }

[./src/lib.rs]

pub fn some_function() -> u8 {
    127
}

pub async fn some_async_function() -> u8 {
    127
}

#[cfg(test)]
mod tests {
    use super::{some_async_function, some_function};

    #[tokio::test]
    async fn async_test() {
        // This test should cover line 5, but tarpaulin doesn't recognise that.
        // It does recognise it if I just do `assert_eq!(some_async_function().await, 127);`
        //	without the `catch_unwind` but I do need the catch_unwind because
        //	the actual async_test for my library involves a bit of setup and
        //	teardown (see https://medium.com/@ericdreichert/test-setup-and-teardown-in-rust-without-a-framework-ba32d97aa5ab )
        let result = std::panic::catch_unwind(|| async {
            assert_eq!(some_async_function().await, 127);
        });
        assert!(result.is_ok())
    }

    #[test]
    fn test() {
        // Even though here there's a catch_unwind just like the async_test, tarpaulin
        //	does recognise that this test tests line 1, unlike what happens with
        //	the async_test
        let result = std::panic::catch_unwind(|| {
            assert_eq!(some_function(), 127);
        });
        assert!(result.is_ok())
    }
}

On calling cargo tarpaulin is the following

Aug 21 14:56:10.009  INFO cargo_tarpaulin::config: Creating config
Aug 21 14:56:10.042  INFO cargo_tarpaulin: Running Tarpaulin
Aug 21 14:56:10.042  INFO cargo_tarpaulin: Building project
Aug 21 14:56:10.042  INFO cargo_tarpaulin::cargo: Cleaning project
   Compiling libc v0.2.132
   Compiling autocfg v1.1.0
   Compiling proc-macro2 v1.0.43
   Compiling quote v1.0.21
   Compiling unicode-ident v1.0.3
   Compiling cfg-if v1.0.0
   Compiling parking_lot_core v0.9.3
   Compiling log v0.4.17
   Compiling syn v1.0.99
   Compiling smallvec v1.9.0
   Compiling scopeguard v1.1.0
   Compiling memchr v2.5.0
   Compiling pin-project-lite v0.2.9
   Compiling bytes v1.2.1
   Compiling once_cell v1.13.1
   Compiling lock_api v0.4.7
   Compiling tokio v1.20.1
   Compiling signal-hook-registry v1.4.0
   Compiling socket2 v0.4.4
   Compiling num_cpus v1.13.1
   Compiling mio v0.8.4
   Compiling parking_lot v0.12.1
   Compiling tokio-macros v1.8.0
   Compiling tarpaulin-bug-async v0.1.0 (/home/[redacted, path to working directory]/tarpaulin-bug-async)
    Finished test [unoptimized + debuginfo] target(s) in 11.91s
  Executable unittests src/lib.rs (target/debug/deps/tarpaulin_bug_async-54856c8466d7b03f)
Aug 21 14:56:22.084  INFO cargo_tarpaulin::process_handling::linux: Launching test
Aug 21 14:56:22.085  INFO cargo_tarpaulin::process_handling: running /home/[redacted, path to working directory]/tarpaulin-bug-async/target/debug/deps/tarpaulin_bug_async-54856c8466d7b03f

running 2 tests
test tests::test ... ok
test tests::async_test ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s

Aug 21 14:56:22.723  INFO cargo_tarpaulin::report: Coverage Results:
|| Uncovered Lines:
|| src/lib.rs: 5
|| Tested/Total Lines:
|| src/lib.rs: 8/9
|| 
88.89% coverage, 8/9 lines covered

I'm reproducing this on Linux Mint 20.3, and this is the output of uname -a

Linux lenovo-ideapad5 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

The version of rustc used is rustc 1.61.0 (fe5b13d68 2022-05-18)

Expected behavior
The line 5 should be counted as covered, so the Total Lines for src/lib.rs should be 8/8, and the coverage should be 100% instead of 88.89%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants