1 // Code that generates a test runner to run all the tests in a crate
4 use rustc_expand::base::{ExtCtxt, Resolver};
5 use rustc_expand::expand::{AstFragment, ExpansionConfig};
6 use rustc_feature::Features;
7 use rustc_session::parse::ParseSess;
8 use rustc_span::hygiene::{AstPass, SyntaxContext, Transparency};
9 use rustc_span::source_map::respan;
10 use rustc_span::symbol::{sym, Symbol};
11 use rustc_span::{Span, DUMMY_SP};
12 use rustc_target::spec::PanicStrategy;
13 use smallvec::{smallvec, SmallVec};
14 use syntax::ast::{self, Ident};
16 use syntax::entry::{self, EntryPointType};
17 use syntax::mut_visit::{ExpectOne, *};
29 panic_strategy: PanicStrategy,
31 test_cases: Vec<Test>,
32 reexport_test_harness_main: Option<Symbol>,
33 test_runner: Option<ast::Path>,
36 // Traverse the crate, collecting all the test functions, eliding any
37 // existing main functions, and synthesizing a main test harness
40 resolver: &mut dyn Resolver,
42 krate: &mut ast::Crate,
43 span_diagnostic: &rustc_errors::Handler,
45 panic_strategy: PanicStrategy,
46 platform_panic_strategy: PanicStrategy,
47 enable_panic_abort_tests: bool,
49 // Check for #![reexport_test_harness_main = "some_name"] which gives the
50 // main test function the name `some_name` without hygiene. This needs to be
51 // unconditional, so that the attribute is still marked as used in
53 let reexport_test_harness_main =
54 attr::first_attr_value_str_by_name(&krate.attrs, sym::reexport_test_harness_main);
56 // Do this here so that the test_runner crate attribute gets marked as used
57 // even in non-test builds
58 let test_runner = get_test_runner(span_diagnostic, &krate);
61 let panic_strategy = match (panic_strategy, enable_panic_abort_tests) {
62 (PanicStrategy::Abort, true) => PanicStrategy::Abort,
63 (PanicStrategy::Abort, false) if panic_strategy == platform_panic_strategy => {
64 // Silently allow compiling with panic=abort on these platforms,
65 // but with old behavior (abort if a test fails).
68 (PanicStrategy::Abort, false) => {
70 "building tests with panic=abort is not supported \
71 without `-Zpanic_abort_tests`",
75 (PanicStrategy::Unwind, _) => PanicStrategy::Unwind,
77 generate_test_harness(
80 reexport_test_harness_main,
89 struct TestHarnessGenerator<'a> {
94 impl<'a> MutVisitor for TestHarnessGenerator<'a> {
95 fn visit_crate(&mut self, c: &mut ast::Crate) {
96 noop_visit_crate(c, self);
98 // Create a main function to run our tests
99 c.module.items.push(mk_main(&mut self.cx));
102 fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
103 let mut item = i.into_inner();
104 if is_test_case(&item) {
105 debug!("this is a test item");
107 let test = Test { span: item.span, ident: item.ident };
108 self.tests.push(test);
111 // We don't want to recurse into anything other than mods, since
112 // mods or tests inside of functions will break things
113 if let ast::ItemKind::Mod(mut module) = item.kind {
114 let tests = mem::take(&mut self.tests);
115 noop_visit_mod(&mut module, self);
116 let mut tests = mem::replace(&mut self.tests, tests);
118 if !tests.is_empty() {
120 if item.id == ast::DUMMY_NODE_ID { ast::CRATE_NODE_ID } else { item.id };
121 // Create an identifier that will hygienically resolve the test
122 // case name, even in another module.
123 let expn_id = self.cx.ext_cx.resolver.expansion_for_ast_pass(
125 AstPass::TestHarness,
129 for test in &mut tests {
130 // See the comment on `mk_main` for why we're using
131 // `apply_mark` directly.
132 test.ident.span = test.ident.span.apply_mark(expn_id, Transparency::Opaque);
134 self.cx.test_cases.extend(tests);
136 item.kind = ast::ItemKind::Mod(module);
141 fn visit_mac(&mut self, _mac: &mut ast::Mac) {
146 /// A folder used to remove any entry points (like fn main) because the harness
147 /// generator will provide its own
148 struct EntryPointCleaner {
149 // Current depth in the ast
154 impl MutVisitor for EntryPointCleaner {
155 fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
157 let item = noop_flat_map_item(i, self).expect_one("noop did something");
160 // Remove any #[main] or #[start] from the AST so it doesn't
161 // clash with the one we're going to add, but mark it as
162 // #[allow(dead_code)] to avoid printing warnings.
163 let item = match entry::entry_point_type(&item, self.depth) {
164 EntryPointType::MainNamed | EntryPointType::MainAttr | EntryPointType::Start => item
165 .map(|ast::Item { id, ident, attrs, kind, vis, span, tokens }| {
166 let allow_ident = Ident::new(sym::allow, self.def_site);
167 let dc_nested = attr::mk_nested_word_item(Ident::from_str_and_span(
171 let allow_dead_code_item = attr::mk_list_item(allow_ident, vec![dc_nested]);
172 let allow_dead_code = attr::mk_attr_outer(allow_dead_code_item);
180 !attr.check_name(sym::main) && !attr.check_name(sym::start)
182 .chain(iter::once(allow_dead_code))
190 EntryPointType::None | EntryPointType::OtherMain => item,
196 fn visit_mac(&mut self, _mac: &mut ast::Mac) {
201 /// Crawl over the crate, inserting test reexports and the test main function
202 fn generate_test_harness(
204 resolver: &mut dyn Resolver,
205 reexport_test_harness_main: Option<Symbol>,
206 krate: &mut ast::Crate,
208 panic_strategy: PanicStrategy,
209 test_runner: Option<ast::Path>,
211 let mut econfig = ExpansionConfig::default("test".to_string());
212 econfig.features = Some(features);
214 let ext_cx = ExtCtxt::new(sess, econfig, resolver);
216 let expn_id = ext_cx.resolver.expansion_for_ast_pass(
218 AstPass::TestHarness,
219 &[sym::main, sym::test, sym::rustc_attrs],
222 let def_site = DUMMY_SP.with_def_site_ctxt(expn_id);
224 // Remove the entry points
225 let mut cleaner = EntryPointCleaner { depth: 0, def_site };
226 cleaner.visit_crate(krate);
232 test_cases: Vec::new(),
233 reexport_test_harness_main,
237 TestHarnessGenerator { cx, tests: Vec::new() }.visit_crate(krate);
240 /// Creates a function item for use as the main function of a test build.
241 /// This function will call the `test_runner` as specified by the crate attribute
243 /// By default this expands to
247 /// extern crate test;
248 /// test::test_main_static(&[
255 /// Most of the Ident have the usual def-site hygiene for the AST pass. The
256 /// exception is the `test_const`s. These have a syntax context that has two
257 /// opaque marks: one from the expansion of `test` or `test_case`, and one
258 /// generated in `TestHarnessGenerator::flat_map_item`. When resolving this
259 /// identifier after failing to find a matching identifier in the root module
260 /// we remove the outer mark, and try resolving at its def-site, which will
261 /// then resolve to `test_const`.
263 /// The expansion here can be controlled by two attributes:
265 /// `reexport_test_harness_main` provides a different name for the `main`
266 /// function and `test_runner` provides a path that replaces
267 /// `test::test_main_static`.
268 fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
269 let sp = cx.def_site;
270 let ecx = &cx.ext_cx;
271 let test_id = Ident::new(sym::test, sp);
273 let runner_name = match cx.panic_strategy {
274 PanicStrategy::Unwind => "test_main_static",
275 PanicStrategy::Abort => "test_main_static_abort",
278 // test::test_main_static(...)
279 let mut test_runner = cx
282 .unwrap_or(ecx.path(sp, vec![test_id, ecx.ident_of(runner_name, sp)]));
284 test_runner.span = sp;
286 let test_main_path_expr = ecx.expr_path(test_runner);
287 let call_test_main = ecx.expr_call(sp, test_main_path_expr, vec![mk_tests_slice(cx, sp)]);
288 let call_test_main = ecx.stmt_expr(call_test_main);
291 let test_extern_stmt =
292 ecx.stmt_item(sp, ecx.item(sp, test_id, vec![], ast::ItemKind::ExternCrate(None)));
295 let main_meta = ecx.meta_word(sp, sym::main);
296 let main_attr = ecx.attribute(main_meta);
298 // pub fn main() { ... }
299 let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(vec![]));
301 // If no test runner is provided we need to import the test crate
302 let main_body = if cx.test_runner.is_none() {
303 ecx.block(sp, vec![test_extern_stmt, call_test_main])
305 ecx.block(sp, vec![call_test_main])
308 let decl = ecx.fn_decl(vec![], ast::FunctionRetTy::Ty(main_ret_ty));
309 let sig = ast::FnSig { decl, header: ast::FnHeader::default() };
310 let main = ast::ItemKind::Fn(sig, ast::Generics::default(), Some(main_body));
312 // Honor the reexport_test_harness_main attribute
313 let main_id = match cx.reexport_test_harness_main {
314 Some(sym) => Ident::new(sym, sp.with_ctxt(SyntaxContext::root())),
315 None => Ident::new(sym::main, sp),
318 let main = P(ast::Item {
320 attrs: vec![main_attr],
321 id: ast::DUMMY_NODE_ID,
323 vis: respan(sp, ast::VisibilityKind::Public),
328 // Integrate the new item into existing module structures.
329 let main = AstFragment::Items(smallvec![main]);
330 cx.ext_cx.monotonic_expander().fully_expand_fragment(main).make_items().pop().unwrap()
333 /// Creates a slice containing every test like so:
334 /// &[&test1, &test2]
335 fn mk_tests_slice(cx: &TestCtxt<'_>, sp: Span) -> P<ast::Expr> {
336 debug!("building test vector from {} tests", cx.test_cases.len());
337 let ref ecx = cx.ext_cx;
344 ecx.expr_addr_of(test.span, ecx.expr_path(ecx.path(test.span, vec![test.ident])))
350 fn is_test_case(i: &ast::Item) -> bool {
351 attr::contains_name(&i.attrs, sym::rustc_test_marker)
354 fn get_test_runner(sd: &rustc_errors::Handler, krate: &ast::Crate) -> Option<ast::Path> {
355 let test_attr = attr::find_by_name(&krate.attrs, sym::test_runner)?;
356 test_attr.meta_item_list().map(|meta_list| {
357 if meta_list.len() != 1 {
358 sd.span_fatal(test_attr.span, "`#![test_runner(..)]` accepts exactly 1 argument")
361 match meta_list[0].meta_item() {
362 Some(meta_item) if meta_item.is_word() => meta_item.path.clone(),
363 _ => sd.span_fatal(test_attr.span, "`test_runner` argument must be a path").raise(),