test.rs 11.6 KB
Newer Older
1
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9 10
// 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.

11
use std::cell::RefCell;
12
use std::dynamic_lib::DynamicLibrary;
13
use std::io::{Command, TempDir};
14
use std::io;
15 16
use std::os;
use std::str;
17
use std::string::String;
18

19
use std::collections::{HashSet, HashMap};
L
Liigo Zhuang 已提交
20
use testing;
21
use rustc::session::{mod, config};
22
use rustc_driver::driver;
23 24
use syntax::ast;
use syntax::codemap::{CodeMap, dummy_spanned};
25
use syntax::diagnostic;
26
use syntax::parse::token;
27
use syntax::ptr::P;
28 29 30 31 32 33 34 35 36

use core;
use clean;
use clean::Clean;
use fold::DocFolder;
use html::markdown;
use passes;
use visit_ast::RustdocVisitor;

37
pub fn run(input: &str,
38
           cfgs: Vec<String>,
39
           libs: Vec<Path>,
40
           externs: core::Externs,
41 42
           mut test_args: Vec<String>,
           crate_name: Option<String>)
43
           -> int {
44
    let input_path = Path::new(input);
45
    let input = config::Input::File(input_path.clone());
46

N
Nick Cameron 已提交
47
    let sessopts = config::Options {
E
Eduard Burtescu 已提交
48 49
        maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
        addl_lib_search_paths: RefCell::new(libs.clone()),
N
Nick Cameron 已提交
50
        crate_types: vec!(config::CrateTypeDylib),
51
        externs: externs.clone(),
N
Nick Cameron 已提交
52
        ..config::basic_options().clone()
53 54
    };

E
Eduard Burtescu 已提交
55
    let codemap = CodeMap::new();
56
    let diagnostic_handler = diagnostic::default_handler(diagnostic::Auto, None);
57
    let span_diagnostic_handler =
E
Eduard Burtescu 已提交
58
    diagnostic::mk_span_handler(diagnostic_handler, codemap);
59

N
Nick Cameron 已提交
60
    let sess = session::build_session_(sessopts,
61
                                      Some(input_path.clone()),
62 63
                                      span_diagnostic_handler);

N
Nick Cameron 已提交
64
    let mut cfg = config::build_configuration(&sess);
A
Aaron Turon 已提交
65
    cfg.extend(cfgs.into_iter().map(|cfg_| {
66
        let cfg_ = token::intern_and_get_ident(cfg_.as_slice());
67
        P(dummy_spanned(ast::MetaWord(cfg_)))
68
    }));
E
Eduard Burtescu 已提交
69
    let krate = driver::phase_1_parse_input(&sess, cfg, &input);
70 71
    let krate = driver::phase_2_configure_and_expand(&sess, krate,
                                                     "rustdoc-test", None)
72
        .expect("phase_2_configure_and_expand aborted in rustdoc!");
73

74
    let ctx = core::DocContext {
75
        krate: &krate,
E
Eduard Burtescu 已提交
76
        maybe_typed: core::NotTyped(sess),
77
        src: input_path,
78
        external_paths: RefCell::new(Some(HashMap::new())),
79 80
        external_traits: RefCell::new(None),
        external_typarams: RefCell::new(None),
81
        inlined: RefCell::new(None),
82
        populated_crate_impls: RefCell::new(HashSet::new()),
83 84
    };

85
    let mut v = RustdocVisitor::new(&ctx, None);
86
    v.visit(ctx.krate);
87
    let mut krate = v.clean(&ctx);
88 89 90 91
    match crate_name {
        Some(name) => krate.name = name,
        None => {}
    }
92
    let (krate, _) = passes::collapse_docs(krate);
93
    let (krate, _) = passes::unindent_comments(krate);
94

95
    let mut collector = Collector::new(krate.name.to_string(),
96
                                       libs,
97
                                       externs,
98
                                       false);
99
    collector.fold_crate(krate);
100

101
    test_args.insert(0, "rustdoctest".to_string());
102

103
    testing::test_main(test_args.as_slice(),
A
Aaron Turon 已提交
104
                       collector.tests.into_iter().collect());
105 106 107
    0
}

108
fn runtest(test: &str, cratename: &str, libs: Vec<Path>, externs: core::Externs,
109
           should_fail: bool, no_run: bool, as_test_harness: bool) {
110 111 112
    // the test harness wants its own `main` & top level functions, so
    // never wrap the test in `fn main() { ... }`
    let test = maketest(test, Some(cratename), true, as_test_harness);
113
    let input = config::Input::Str(test.to_string());
114

N
Nick Cameron 已提交
115
    let sessopts = config::Options {
E
Eduard Burtescu 已提交
116 117
        maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
        addl_lib_search_paths: RefCell::new(libs),
N
Nick Cameron 已提交
118
        crate_types: vec!(config::CrateTypeExecutable),
119
        output_types: vec!(config::OutputTypeExe),
120
        no_trans: no_run,
121
        externs: externs,
N
Nick Cameron 已提交
122
        cg: config::CodegenOptions {
123
            prefer_dynamic: true,
N
Nick Cameron 已提交
124
            .. config::basic_codegen_options()
125
        },
126
        test: as_test_harness,
N
Nick Cameron 已提交
127
        ..config::basic_options().clone()
128 129
    };

130 131 132 133 134 135 136 137 138 139 140
    // Shuffle around a few input and output handles here. We're going to pass
    // an explicit handle into rustc to collect output messages, but we also
    // want to catch the error message that rustc prints when it fails.
    //
    // We take our task-local stderr (likely set by the test runner), and move
    // it into another task. This helper task then acts as a sink for both the
    // stderr of this task and stderr of rustc itself, copying all the info onto
    // the stderr channel we originally started with.
    //
    // The basic idea is to not use a default_handler() for rustc, and then also
    // not print things by default to the actual stderr.
141 142
    let (tx, rx) = channel();
    let w1 = io::ChanWriter::new(tx);
143
    let w2 = w1.clone();
144
    let old = io::stdio::set_stderr(box w1);
145
    spawn(proc() {
146
        let mut p = io::ChanReader::new(rx);
147 148 149 150 151 152 153 154
        let mut err = match old {
            Some(old) => {
                // Chop off the `Send` bound.
                let old: Box<Writer> = old;
                old
            }
            None => box io::stderr() as Box<Writer>,
        };
155 156
        io::util::copy(&mut p, &mut err).unwrap();
    });
157
    let emitter = diagnostic::EmitterWriter::new(box w2, None);
158 159

    // Compile the code
E
Eduard Burtescu 已提交
160
    let codemap = CodeMap::new();
161
    let diagnostic_handler = diagnostic::mk_handler(box emitter);
162
    let span_diagnostic_handler =
E
Eduard Burtescu 已提交
163
        diagnostic::mk_span_handler(diagnostic_handler, codemap);
164

N
Nick Cameron 已提交
165
    let sess = session::build_session_(sessopts,
166
                                      None,
167 168
                                      span_diagnostic_handler);

169
    let outdir = TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir");
170
    let out = Some(outdir.path().clone());
N
Nick Cameron 已提交
171
    let cfg = config::build_configuration(&sess);
F
Felix S. Klock II 已提交
172
    let libdir = sess.target_filesearch().get_lib_path();
173
    driver::compile_input(sess, cfg, &input, &out, &None, None);
174

175 176
    if no_run { return }

177
    // Run the code!
F
Felix S. Klock II 已提交
178 179 180 181 182
    //
    // We're careful to prepend the *target* dylib search path to the child's
    // environment to ensure that the target loads the right libraries at
    // runtime. It would be a sad day if the *host* libraries were loaded as a
    // mistake.
183
    let mut cmd = Command::new(outdir.path().join("rust-out"));
184
    let newpath = {
F
Felix S. Klock II 已提交
185 186
        let mut path = DynamicLibrary::search_path();
        path.insert(0, libdir.clone());
187
        DynamicLibrary::create_path(path.as_slice())
F
Felix S. Klock II 已提交
188
    };
189 190 191
    cmd.env(DynamicLibrary::envvar(), newpath.as_slice());

    match cmd.output() {
S
Steve Klabnik 已提交
192
        Err(e) => panic!("couldn't run the test: {}{}", e,
193 194 195
                        if e.kind == io::PermissionDenied {
                            " - maybe your tempdir is mounted with noexec?"
                        } else { "" }),
A
Alex Crichton 已提交
196
        Ok(out) => {
197
            if should_fail && out.status.success() {
S
Steve Klabnik 已提交
198
                panic!("test executable succeeded when it should have failed");
199
            } else if !should_fail && !out.status.success() {
S
Steve Klabnik 已提交
200
                panic!("test executable failed:\n{}",
S
Steven Fackler 已提交
201
                      str::from_utf8(out.error.as_slice()));
202 203 204 205 206
            }
        }
    }
}

207
pub fn maketest(s: &str, cratename: Option<&str>, lints: bool, dont_insert_main: bool) -> String {
208 209 210
    let mut prog = String::new();
    if lints {
        prog.push_str(r"
211
#![deny(warnings)]
A
Aaron Turon 已提交
212
#![allow(unused_variables, unused_assignments, unused_mut, unused_attributes, dead_code)]
213
");
214
    }
215

216 217 218
    // Don't inject `extern crate std` because it's already injected by the
    // compiler.
    if !s.contains("extern crate") && cratename != Some("std") {
219 220 221 222 223 224 225 226
        match cratename {
            Some(cratename) => {
                if s.contains(cratename) {
                    prog.push_str(format!("extern crate {};\n",
                                          cratename).as_slice());
                }
            }
            None => {}
227
        }
228
    }
229
    if dont_insert_main || s.contains("fn main") {
230 231
        prog.push_str(s);
    } else {
232 233
        prog.push_str("fn main() {\n    ");
        prog.push_str(s.replace("\n", "\n    ").as_slice());
234 235 236
        prog.push_str("\n}");
    }

237
    return prog
238 239 240
}

pub struct Collector {
241
    pub tests: Vec<testing::TestDescAndFn>,
242
    names: Vec<String>,
243
    libs: Vec<Path>,
244
    externs: core::Externs,
245 246
    cnt: uint,
    use_headers: bool,
247 248
    current_header: Option<String>,
    cratename: String,
249 250 251
}

impl Collector {
252
    pub fn new(cratename: String, libs: Vec<Path>, externs: core::Externs,
253
               use_headers: bool) -> Collector {
254
        Collector {
255 256
            tests: Vec::new(),
            names: Vec::new(),
257
            libs: libs,
258
            externs: externs,
259 260 261
            cnt: 0,
            use_headers: use_headers,
            current_header: None,
262
            cratename: cratename,
263 264 265
        }
    }

266 267
    pub fn add_test(&mut self, test: String,
                    should_fail: bool, no_run: bool, should_ignore: bool, as_test_harness: bool) {
268 269
        let name = if self.use_headers {
            let s = self.current_header.as_ref().map(|s| s.as_slice()).unwrap_or("");
A
Alex Crichton 已提交
270
            format!("{}_{}", s, self.cnt)
271
        } else {
A
Alex Crichton 已提交
272
            format!("{}_{}", self.names.connect("::"), self.cnt)
273
        };
274
        self.cnt += 1;
E
Eduard Burtescu 已提交
275
        let libs = self.libs.clone();
276
        let externs = self.externs.clone();
R
Richo Healey 已提交
277
        let cratename = self.cratename.to_string();
278
        debug!("Creating test {}: {}", name, test);
L
Liigo Zhuang 已提交
279 280 281
        self.tests.push(testing::TestDescAndFn {
            desc: testing::TestDesc {
                name: testing::DynTestName(name),
282
                ignore: should_ignore,
283
                should_fail: false, // compiler failures are test failures
284
            },
L
Liigo Zhuang 已提交
285
            testfn: testing::DynTestFn(proc() {
286
                runtest(test.as_slice(),
287
                        cratename.as_slice(),
288
                        libs,
289
                        externs,
290
                        should_fail,
291 292
                        no_run,
                        as_test_harness);
293 294 295
            }),
        });
    }
296 297 298 299 300 301

    pub fn register_header(&mut self, name: &str, level: u32) {
        if self.use_headers && level == 1 {
            // we use these headings as test names, so it's good if
            // they're valid identifiers.
            let name = name.chars().enumerate().map(|(i, c)| {
302 303
                    if (i == 0 && c.is_xid_start()) ||
                        (i != 0 && c.is_xid_continue()) {
304 305 306 307
                        c
                    } else {
                        '_'
                    }
308
                }).collect::<String>();
309 310 311 312 313 314

            // new header => reset count.
            self.cnt = 0;
            self.current_header = Some(name);
        }
    }
315 316 317 318 319 320
}

impl DocFolder for Collector {
    fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
        let pushed = match item.name {
            Some(ref name) if name.len() == 0 => false,
321
            Some(ref name) => { self.names.push(name.to_string()); true }
322 323 324 325 326
            None => false
        };
        match item.doc_value() {
            Some(doc) => {
                self.cnt = 0;
327
                markdown::find_testable_code(doc, &mut *self);
328 329 330 331 332 333 334 335 336 337
            }
            None => {}
        }
        let ret = self.fold_item_recur(item);
        if pushed {
            self.names.pop();
        }
        return ret;
    }
}