Skip to content

Commit da3c267

Browse files
Rollup merge of rust-lang#140706 - GuillaumeGomez:fix-missing-temp-dir-cleanup, r=notriddle
[rustdoc] Ensure that temporary doctest folder is correctly removed even if doctests failed Fixes rust-lang#139899. The bug was due to the fact that if any doctest fails for any reason, we call `exit` (or it's called inside `libtest` if not edition 2024), meaning that `TempDir`'s destructor isn't called, and therefore the temporary folder isn't cleaned up. Took me a while to figure out how to reproduce but finally I was able to reproduce the bug with: `````rust #![doc(test(attr(deny(warnings))))] //! ``` //! let a = 12; //! ``` ````` And then I ensured that panicking doctests were cleaned up as well: `````rust //! ``` //! panic!(); //! ``` ````` And finally I checked if it was fixed for merged doctests too (`--edition 2024`). To make this work, I needed to add a new public function in `libtest` too which would call a function once all tests have been run. So only issue is: I have absolutely no idea how we can add a regression test for this fix. If anyone has an idea... r? `@notriddle`
2 parents 7a06a5f + dddfddc commit da3c267

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

test/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ const SECONDARY_TEST_BENCH_BENCHMARKS_VAR: &str = "__RUST_TEST_BENCH_BENCHMARKS"
9898
// The default console test runner. It accepts the command line
9999
// arguments and a vector of test_descs.
100100
pub fn test_main(args: &[String], tests: Vec<TestDescAndFn>, options: Option<Options>) {
101+
test_main_with_exit_callback(args, tests, options, || {})
102+
}
103+
104+
pub fn test_main_with_exit_callback<F: FnOnce()>(
105+
args: &[String],
106+
tests: Vec<TestDescAndFn>,
107+
options: Option<Options>,
108+
exit_callback: F,
109+
) {
101110
let mut opts = match cli::parse_opts(args) {
102111
Some(Ok(o)) => o,
103112
Some(Err(msg)) => {
@@ -151,6 +160,7 @@ pub fn test_main(args: &[String], tests: Vec<TestDescAndFn>, options: Option<Opt
151160
let res = console::run_tests_console(&opts, tests);
152161
// Prevent Valgrind from reporting reachable blocks in users' unit tests.
153162
drop(panic::take_hook());
163+
exit_callback();
154164
match res {
155165
Ok(true) => {}
156166
Ok(false) => process::exit(ERROR_EXIT_CODE),

0 commit comments

Comments
 (0)