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

Do not inject test harness for --cfg test #33821

Merged
merged 2 commits into from
May 28, 2016
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
5 changes: 4 additions & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,10 @@ pub fn phase_2_configure_and_expand(sess: &Session,
})?;

krate = time(time_passes, "maybe building test harness", || {
syntax::test::modify_for_testing(&sess.parse_sess, &sess.opts.cfg, krate, sess.diagnostic())
syntax::test::modify_for_testing(&sess.parse_sess,
sess.opts.test,
krate,
sess.diagnostic())
});

krate = time(time_passes,
Expand Down
14 changes: 3 additions & 11 deletions src/libsyntax/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ struct TestCtxt<'a> {
testfns: Vec<Test>,
reexport_test_harness_main: Option<InternedString>,
is_test_crate: bool,
config: ast::CrateConfig,

// top-level re-export submodule, filled out after folding is finished
toplevel_reexport: Option<ast::Ident>,
Expand All @@ -68,14 +67,9 @@ struct TestCtxt<'a> {
// Traverse the crate, collecting all the test functions, eliding any
// existing main functions, and synthesizing a main test harness
pub fn modify_for_testing(sess: &ParseSess,
cfg: &ast::CrateConfig,
should_test: bool,
krate: ast::Crate,
span_diagnostic: &errors::Handler) -> ast::Crate {
// We generate the test harness when building in the 'test'
// configuration, either with the '--test' or '--cfg test'
// command line options.
let should_test = attr::contains_name(&krate.config, "test");

// Check for #[reexport_test_harness_main = "some_name"] which
// creates a `use some_name = __test::main;`. This needs to be
// unconditional, so that the attribute is still marked as used in
Expand All @@ -85,7 +79,7 @@ pub fn modify_for_testing(sess: &ParseSess,
"reexport_test_harness_main");

if should_test {
generate_test_harness(sess, reexport_test_harness_main, krate, cfg, span_diagnostic)
generate_test_harness(sess, reexport_test_harness_main, krate, span_diagnostic)
} else {
strip_test_functions(span_diagnostic, krate)
}
Expand Down Expand Up @@ -271,7 +265,6 @@ fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
fn generate_test_harness(sess: &ParseSess,
reexport_test_harness_main: Option<InternedString>,
krate: ast::Crate,
cfg: &ast::CrateConfig,
sd: &errors::Handler) -> ast::Crate {
// Remove the entry points
let mut cleaner = EntryPointCleaner { depth: 0 };
Expand All @@ -281,14 +274,13 @@ fn generate_test_harness(sess: &ParseSess,
let mut cx: TestCtxt = TestCtxt {
sess: sess,
span_diagnostic: sd,
ext_cx: ExtCtxt::new(sess, cfg.clone(),
ext_cx: ExtCtxt::new(sess, vec![],
ExpansionConfig::default("test".to_string()),
&mut feature_gated_cfgs),
path: Vec::new(),
testfns: Vec::new(),
reexport_test_harness_main: reexport_test_harness_main,
is_test_crate: is_test_crate(&krate),
config: krate.config.clone(),
toplevel_reexport: None,
};
cx.ext_cx.crate_root = Some("std");
Expand Down
18 changes: 18 additions & 0 deletions src/test/run-pass/test-vs-cfg-test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: --cfg test

// Make sure `--cfg test` does not inject test harness

#[test]
fn test() { panic!(); }

fn main() {}