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

panic=abort support in libtest #64158

Merged
merged 4 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"show extended diagnostic help"),
terminal_width: Option<usize> = (None, parse_opt_uint, [UNTRACKED],
"set the current terminal width"),
panic_abort_tests: bool = (false, parse_bool, [TRACKED],
"support compiling tests with panic=abort"),
continue_parse_after_error: bool = (false, parse_bool, [TRACKED],
"attempt to recover from parse errors (experimental)"),
dep_tasks: bool = (false, parse_bool, [UNTRACKED],
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,9 @@ fn configure_and_expand_inner<'a>(
&mut krate,
sess.diagnostic(),
&sess.features_untracked(),
sess.panic_strategy(),
sess.target.target.options.panic_strategy,
sess.opts.debugging_opts.panic_abort_tests,
)
});

Expand Down
30 changes: 28 additions & 2 deletions src/libsyntax_ext/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use log::debug;
use smallvec::{smallvec, SmallVec};
use rustc_target::spec::PanicStrategy;
use syntax::ast::{self, Ident};
use syntax::attr;
use syntax::entry::{self, EntryPointType};
Expand All @@ -25,6 +26,7 @@ struct Test {

struct TestCtxt<'a> {
ext_cx: ExtCtxt<'a>,
panic_strategy: PanicStrategy,
def_site: Span,
test_cases: Vec<Test>,
reexport_test_harness_main: Option<Symbol>,
Expand All @@ -40,6 +42,9 @@ pub fn inject(
krate: &mut ast::Crate,
span_diagnostic: &errors::Handler,
features: &Features,
panic_strategy: PanicStrategy,
platform_panic_strategy: PanicStrategy,
enable_panic_abort_tests: bool,
) {
// Check for #![reexport_test_harness_main = "some_name"] which gives the
// main test function the name `some_name` without hygiene. This needs to be
Expand All @@ -53,8 +58,22 @@ pub fn inject(
let test_runner = get_test_runner(span_diagnostic, &krate);

if should_test {
let panic_strategy = match (panic_strategy, enable_panic_abort_tests) {
(PanicStrategy::Abort, true) =>
PanicStrategy::Abort,
(PanicStrategy::Abort, false) if panic_strategy == platform_panic_strategy => {
// Silently allow compiling with panic=abort on these platforms,
// but with old behavior (abort if a test fails).
PanicStrategy::Unwind
}
(PanicStrategy::Abort, false) => {
span_diagnostic.err("building tests with panic=abort is not yet supported");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a hint referring to -Zenable-panic-abort-tests.

PanicStrategy::Unwind
}
(PanicStrategy::Unwind, _) => PanicStrategy::Unwind,
};
generate_test_harness(sess, resolver, reexport_test_harness_main,
krate, features, test_runner)
krate, features, panic_strategy, test_runner)
}
}

Expand Down Expand Up @@ -183,6 +202,7 @@ fn generate_test_harness(sess: &ParseSess,
reexport_test_harness_main: Option<Symbol>,
krate: &mut ast::Crate,
features: &Features,
panic_strategy: PanicStrategy,
test_runner: Option<ast::Path>) {
let mut econfig = ExpansionConfig::default("test".to_string());
econfig.features = Some(features);
Expand All @@ -203,6 +223,7 @@ fn generate_test_harness(sess: &ParseSess,

let cx = TestCtxt {
ext_cx,
panic_strategy,
def_site,
test_cases: Vec::new(),
reexport_test_harness_main,
Expand Down Expand Up @@ -248,9 +269,14 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
let ecx = &cx.ext_cx;
let test_id = Ident::new(sym::test, sp);

let runner_name = match cx.panic_strategy {
PanicStrategy::Unwind => "test_main_static",
PanicStrategy::Abort => "test_main_static_abort",
};

// test::test_main_static(...)
let mut test_runner = cx.test_runner.clone().unwrap_or(
ecx.path(sp, vec![test_id, ecx.ident_of("test_main_static", sp)]));
ecx.path(sp, vec![test_id, ecx.ident_of(runner_name, sp)]));

test_runner.span = sp;

Expand Down
9 changes: 9 additions & 0 deletions src/libtest/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ pub(crate) trait OutputFormatter {
) -> io::Result<()>;
fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool>;
}

pub(crate) fn write_stderr_delimiter(test_output: &mut Vec<u8>, test_name: &TestName) {
match test_output.last() {
Some(b'\n') => (),
Some(_) => test_output.push(b'\n'),
None => (),
}
write!(test_output, "---- {} stderr ----\n", test_name).unwrap();
}
Loading