LukasKalbertodt / libtest-mimic

A small test framework to write your own test harness that looks and behaves like the built-in test harness used by `rustc --test`
https://docs.rs/libtest-mimic
Apache License 2.0
95 stars 26 forks source link

Coverage data not generated on Windows when using `libtest_mimic::Conclusion::exit` #39

Closed zaneduffield closed 2 months ago

zaneduffield commented 2 months ago

In https://github.com/nextest-rs/datatest-stable/issues/20 I discovered that coverage data was not being generated for some of my tests. I came to the conclusion (at https://github.com/nextest-rs/datatest-stable/issues/20#issuecomment-2044050867) that the problem was the use of std::process::exit from within libtest_mimic.

It seems that on Windows (but not linux), coverage data isn't written to disk when the process is terminated with std::process::exit. The documentation on which says

Note that because this function never returns, and that it terminates the process, no destructors on the current stack or any other thread's stack will be run.

I presume that the profiling data is written to disk in the destructor for some type, and that isn't run when you terminate abruptly with std::process::terminate. I have no idea why this only affects Windows.

To work around this problem, in datatest-stable the maintainer used libtest_mimic::Conclusion::has_failed to return a std::process::ExitCode from the generated main function (see https://github.com/nextest-rs/datatest-stable/commit/33d4725463e839060c70254b0c879ad5e27a3cfb). When ExitCode is returned from main, threads are cleaned up and coverage data is generated properly.

As discussed in https://github.com/nextest-rs/datatest-stable/issues/20#issuecomment-2044122291 libtest-mimic may want to deprecate libtest_mimic::Conclusion::exit in favour of a method that returns an ExitCode.

LukasKalbertodt commented 2 months ago

Thanks for the report. Fixed in the linked commit and released as 0.7.1.

I added exit_code but decided against a full deprecation of the exit methods. I did add a warning to the docs though and moved them to the very bottom.

zaneduffield commented 2 months ago

That was fast! Thanks 😄