syntax: Don't assume std exists for tests

This commit removes the injection of `std::env::args()` from `--test` expanded
code, relying on the test runner itself to call this funciton. This is more
hygienic because we can't assume that `std` exists at the top layer all the
time, and it meaks the injected test module entirely self contained.
This commit is contained in:
Alex Crichton 2015-07-30 08:53:22 -07:00
parent 5cccf3cd25
commit 0d8340327c
7 changed files with 23 additions and 33 deletions

View File

@ -414,11 +414,3 @@ pub mod __rand {
// the rustdoc documentation for primitive types. Using `include!`
// because rustdoc only looks for these modules at the crate level.
include!("primitive_docs.rs");
// The expansion of --test has a few references to `::std::$foo` so this module
// is necessary to get things to compile.
#[cfg(test)]
mod std {
pub use option;
pub use realstd::env;
}

View File

@ -815,7 +815,7 @@ mod tests {
#[cfg(target_os="android")]
#[test]
fn test_inherit_env() {
use std::env;
use env;
let mut result = env_cmd().output().unwrap();
let output = String::from_utf8(result.stdout).unwrap();

View File

@ -166,6 +166,7 @@ impl Drop for Thread {
not(target_os = "netbsd"),
not(target_os = "openbsd")))]
pub mod guard {
#[cfg(stage0)]
use prelude::v1::*;
pub unsafe fn current() -> Option<usize> { None }

View File

@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[cfg(stage0)]
use prelude::v1::*;
use alloc::boxed::FnBox;
@ -87,6 +86,7 @@ impl Thread {
}
pub mod guard {
#[cfg(stage0)]
use prelude::v1::*;
pub unsafe fn current() -> Option<usize> { None }

View File

@ -329,6 +329,7 @@ mod imp {
// Due to rust-lang/rust#18804, make sure this is not generic!
#[cfg(target_os = "linux")]
unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
use prelude::v1::*;
use mem;
use libc;
use sys_common::thread_local as os;

View File

@ -450,18 +450,11 @@ fn mk_main(cx: &mut TestCtxt) -> P<ast::Item> {
// test::test_main_static
let test_main_path = ecx.path(sp, vec![token::str_to_ident("test"),
token::str_to_ident("test_main_static")]);
// ::std::env::args
let os_args_path = ecx.path_global(sp, vec![token::str_to_ident("std"),
token::str_to_ident("env"),
token::str_to_ident("args")]);
// ::std::env::args()
let os_args_path_expr = ecx.expr_path(os_args_path);
let call_os_args = ecx.expr_call(sp, os_args_path_expr, vec![]);
// test::test_main_static(...)
let test_main_path_expr = ecx.expr_path(test_main_path);
let tests_ident_expr = ecx.expr_ident(sp, token::str_to_ident("TESTS"));
let call_test_main = ecx.expr_call(sp, test_main_path_expr,
vec![call_os_args, tests_ident_expr]);
vec![tests_ident_expr]);
let call_test_main = ecx.stmt_expr(call_test_main);
// #![main]
let main_meta = ecx.meta_word(sp, token::intern_and_get_ident("main"));
@ -634,12 +627,14 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
let fail_expr = match test.should_panic {
ShouldPanic::No => ecx.expr_path(should_panic_path("No")),
ShouldPanic::Yes(ref msg) => {
let path = should_panic_path("Yes");
let arg = match *msg {
Some(ref msg) => ecx.expr_some(span, ecx.expr_str(span, msg.clone())),
None => ecx.expr_none(span),
};
ecx.expr_call(span, ecx.expr_path(path), vec![arg])
match *msg {
Some(ref msg) => {
let msg = ecx.expr_str(span, msg.clone());
let path = should_panic_path("YesWithMessage");
ecx.expr_call(span, ecx.expr_path(path), vec![msg])
}
None => ecx.expr_path(should_panic_path("Yes")),
}
}
};

View File

@ -197,7 +197,8 @@ pub struct Bencher {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum ShouldPanic {
No,
Yes(Option<&'static str>)
Yes,
YesWithMessage(&'static str)
}
// The definition of a single test. A test runner will run a list of
@ -262,8 +263,8 @@ pub fn test_main(args: &[String], tests: Vec<TestDescAndFn> ) {
// a Vec<TestDescAndFn> is used in order to effect ownership-transfer
// semantics into parallel test runners, which in turn requires a Vec<>
// rather than a &[].
pub fn test_main_static(args: env::Args, tests: &[TestDescAndFn]) {
let args = args.collect::<Vec<_>>();
pub fn test_main_static(tests: &[TestDescAndFn]) {
let args = env::args().collect::<Vec<_>>();
let owned_tests = tests.iter().map(|t| {
match t.testfn {
StaticTestFn(f) => TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
@ -1027,8 +1028,8 @@ pub fn run_test(opts: &TestOpts,
fn calc_result(desc: &TestDesc, task_result: Result<(), Box<Any+Send>>) -> TestResult {
match (&desc.should_panic, task_result) {
(&ShouldPanic::No, Ok(())) |
(&ShouldPanic::Yes(None), Err(_)) => TrOk,
(&ShouldPanic::Yes(Some(msg)), Err(ref err))
(&ShouldPanic::Yes, Err(_)) => TrOk,
(&ShouldPanic::YesWithMessage(msg), Err(ref err))
if err.downcast_ref::<String>()
.map(|e| &**e)
.or_else(|| err.downcast_ref::<&'static str>().map(|e| *e))
@ -1276,7 +1277,7 @@ mod tests {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic: ShouldPanic::Yes(None)
should_panic: ShouldPanic::Yes,
},
testfn: DynTestFn(Box::new(move|| f())),
};
@ -1293,7 +1294,7 @@ mod tests {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic: ShouldPanic::Yes(Some("error message"))
should_panic: ShouldPanic::YesWithMessage("error message"),
},
testfn: DynTestFn(Box::new(move|| f())),
};
@ -1310,7 +1311,7 @@ mod tests {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic: ShouldPanic::Yes(Some("foobar"))
should_panic: ShouldPanic::YesWithMessage("foobar"),
},
testfn: DynTestFn(Box::new(move|| f())),
};
@ -1327,7 +1328,7 @@ mod tests {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic: ShouldPanic::Yes(None)
should_panic: ShouldPanic::Yes,
},
testfn: DynTestFn(Box::new(move|| f())),
};