From 06c5f7d7506bd70f58cf781949947ad4c03f1927 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 18 Sep 2026 17:39:44 +0200 Subject: [PATCH 1/2] add custom test runner test --- tests/pretty/custom-test-runner.pp | 33 ++++++++++++++++++++++++++++++ tests/pretty/custom-test-runner.rs | 25 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/pretty/custom-test-runner.pp create mode 100644 tests/pretty/custom-test-runner.rs diff --git a/tests/pretty/custom-test-runner.pp b/tests/pretty/custom-test-runner.pp new file mode 100644 index 0000000000000..dc777e37e3041 --- /dev/null +++ b/tests/pretty/custom-test-runner.pp @@ -0,0 +1,33 @@ +#![feature(prelude_import)] +#![no_std] +//@ compile-flags: --crate-type=lib --test --remap-path-prefix={{src-base}}/=/the/src/ --remap-path-prefix={{src-base}}\=/the/src/ +//@ pretty-compare-only +//@ pretty-mode:expanded +//@ pp-exact:custom-test-runner.pp + +// Example taken from the unstable book. + +#![feature(custom_test_frameworks)] +#![test_runner(my_runner)] +extern crate std; +#[prelude_import] +use ::std::prelude::rust_2015::*; + +fn my_runner(tests: &[&i32]) { + for t in tests { + if **t == 0 { + + + { ::std::io::_print(format_args!("PASSED\n")); }; + } else { { ::std::io::_print(format_args!("FAILED\n")); }; } + } +} +#[rustc_test_marker = "WILL_PASS"] +pub const WILL_PASS: i32 = 0; +#[rustc_test_marker = "WILL_FAIL"] +pub const WILL_FAIL: i32 = 4; +extern crate test; +#[rustc_main] +#[coverage(off)] +#[doc(hidden)] +pub fn main() -> () { my_runner(&[&WILL_FAIL, &WILL_PASS]) } diff --git a/tests/pretty/custom-test-runner.rs b/tests/pretty/custom-test-runner.rs new file mode 100644 index 0000000000000..b0d7360bcd879 --- /dev/null +++ b/tests/pretty/custom-test-runner.rs @@ -0,0 +1,25 @@ +//@ compile-flags: --crate-type=lib --test --remap-path-prefix={{src-base}}/=/the/src/ --remap-path-prefix={{src-base}}\=/the/src/ +//@ pretty-compare-only +//@ pretty-mode:expanded +//@ pp-exact:custom-test-runner.pp + +// Example taken from the unstable book. + +#![feature(custom_test_frameworks)] +#![test_runner(my_runner)] + +fn my_runner(tests: &[&i32]) { + for t in tests { + if **t == 0 { + println!("PASSED"); + } else { + println!("FAILED"); + } + } +} + +#[test_case] +const WILL_PASS: i32 = 0; + +#[test_case] +const WILL_FAIL: i32 = 4; From 95296e6c9628b9d1a5ca6bc107ea4f8e55380c24 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 18 Sep 2026 17:24:19 +0200 Subject: [PATCH 2/2] libtest harness: avoid 'extern crate test' with custom runner --- compiler/rustc_builtin_macros/src/test_harness.rs | 7 ++++++- tests/pretty/custom-test-runner.pp | 1 - 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index 677db66530ecb..bd2ea5a3e6696 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -364,7 +364,12 @@ fn add_main(cx: &mut TestCtxt<'_>, c: &mut ast::Crate) { }); // Integrate the new item into existing module structures. - let items = AstFragment::Items(smallvec![test_extern_stmt, main]); + // `extern crate test;` is only needed with the default runner. + let items = AstFragment::Items(if cx.test_runner.is_none() { + smallvec![test_extern_stmt, main] + } else { + smallvec![main] + }); c.items.extend(cx.ext_cx.monotonic_expander().fully_expand_fragment(items).make_items()); } diff --git a/tests/pretty/custom-test-runner.pp b/tests/pretty/custom-test-runner.pp index dc777e37e3041..5965d76bbc8a5 100644 --- a/tests/pretty/custom-test-runner.pp +++ b/tests/pretty/custom-test-runner.pp @@ -26,7 +26,6 @@ pub const WILL_PASS: i32 = 0; #[rustc_test_marker = "WILL_FAIL"] pub const WILL_FAIL: i32 = 4; -extern crate test; #[rustc_main] #[coverage(off)] #[doc(hidden)]