Skip to content

Commit

Permalink
Use --cfg when running doctests
Browse files Browse the repository at this point in the history
Previously passed --cfg was used only when collecting doctests.
  • Loading branch information
sanxiyn committed Dec 15, 2015
1 parent 9e63cec commit eb25721
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
14 changes: 10 additions & 4 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -399,6 +404,7 @@ impl Collector {
testfn: testing::DynTestFn(Box::new(move|| {
runtest(&test,
&cratename,
cfgs,
libs,
externs,
should_panic,
Expand Down
16 changes: 16 additions & 0 deletions src/test/rustdoc/issue-30252.rs
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit eb25721

Please sign in to comment.