Use --cfg when running doctests

Previously passed --cfg was used only when collecting doctests.
This commit is contained in:
Seo Sanghyeon 2015-12-15 18:03:55 +09:00
parent 9e63cecb10
commit eb25721063
4 changed files with 29 additions and 7 deletions

View File

@ -261,7 +261,7 @@ pub fn main_args(args: &[String]) -> isize {
match (should_test, markdown_input) {
(true, true) => {
return markdown::test(input, libs, externs, test_args)
return markdown::test(input, cfgs, libs, externs, test_args)
}
(true, false) => {
return test::run(input, cfgs, libs, externs, test_args, crate_name)

View File

@ -142,13 +142,13 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
}
/// Run any tests/code examples in the markdown file `input`.
pub fn test(input: &str, libs: SearchPaths, externs: core::Externs,
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: core::Externs,
mut test_args: Vec<String>) -> isize {
let input_str = load_or_return!(input, 1, 2);
let mut opts = TestOptions::default();
opts.no_crate_inject = true;
let mut collector = Collector::new(input.to_string(), libs, externs,
let mut collector = Collector::new(input.to_string(), cfgs, libs, externs,
true, opts);
find_testable_code(&input_str, &mut collector);
test_args.insert(0, "rustdoctest".to_string());

View File

@ -85,7 +85,7 @@ pub fn run(input: &str,
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let mut cfg = config::build_configuration(&sess);
cfg.extend(config::parse_cfgspecs(cfgs));
cfg.extend(config::parse_cfgspecs(cfgs.clone()));
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate,
"rustdoc-test", None)
@ -122,6 +122,7 @@ pub fn run(input: &str,
let (krate, _) = passes::unindent_comments(krate);
let mut collector = Collector::new(krate.name.to_string(),
cfgs,
libs,
externs,
false,
@ -168,7 +169,7 @@ fn scrape_test_config(krate: &::rustc_front::hir::Crate) -> TestOptions {
return opts;
}
fn runtest(test: &str, cratename: &str, libs: SearchPaths,
fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
externs: core::Externs,
should_panic: bool, no_run: bool, as_test_harness: bool,
opts: &TestOptions) {
@ -239,7 +240,8 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
let outdir = TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir");
let out = Some(outdir.path().to_path_buf());
let cfg = config::build_configuration(&sess);
let mut cfg = config::build_configuration(&sess);
cfg.extend(config::parse_cfgspecs(cfgs));
let libdir = sess.target_filesearch(PathKind::All).get_lib_path();
let mut control = driver::CompileController::basic();
if no_run {
@ -349,6 +351,7 @@ fn partition_source(s: &str) -> (String, String) {
pub struct Collector {
pub tests: Vec<testing::TestDescAndFn>,
names: Vec<String>,
cfgs: Vec<String>,
libs: SearchPaths,
externs: core::Externs,
cnt: usize,
@ -359,11 +362,12 @@ pub struct Collector {
}
impl Collector {
pub fn new(cratename: String, libs: SearchPaths, externs: core::Externs,
pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: core::Externs,
use_headers: bool, opts: TestOptions) -> Collector {
Collector {
tests: Vec::new(),
names: Vec::new(),
cfgs: cfgs,
libs: libs,
externs: externs,
cnt: 0,
@ -384,6 +388,7 @@ impl Collector {
format!("{}_{}", self.names.join("::"), self.cnt)
};
self.cnt += 1;
let cfgs = self.cfgs.clone();
let libs = self.libs.clone();
let externs = self.externs.clone();
let cratename = self.cratename.to_string();
@ -399,6 +404,7 @@ impl Collector {
testfn: testing::DynTestFn(Box::new(move|| {
runtest(&test,
&cratename,
cfgs,
libs,
externs,
should_panic,

View File

@ -0,0 +1,16 @@
// Copyright 2015 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:--test --cfg feature="bar"
/// ```rust
/// assert_eq!(cfg!(feature = "bar"), true);
/// ```
pub fn foo() {}