test.rs 9.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2013 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.

11
use std::cell::RefCell;
12
use std::char;
13
use std::io;
14
use std::io::{Process, TempDir};
15 16 17 18
use std::local_data;
use std::os;
use std::str;

19
use collections::HashSet;
L
Liigo Zhuang 已提交
20
use testing;
A
Alex Crichton 已提交
21
use rustc::back::link;
22 23
use rustc::driver::driver;
use rustc::driver::session;
24
use rustc::metadata::creader::Loader;
25
use syntax::diagnostic;
26
use syntax::codemap::CodeMap;
27 28 29 30 31 32 33 34 35

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

36
pub fn run(input: &str, libs: HashSet<Path>, mut test_args: Vec<~str>) -> int {
37 38
    let input_path = Path::new(input);
    let input = driver::FileInput(input_path.clone());
39

E
Eduard Burtescu 已提交
40
    let sessopts = session::Options {
E
Eduard Burtescu 已提交
41 42
        maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
        addl_lib_search_paths: RefCell::new(libs.clone()),
43
        crate_types: vec!(session::CrateTypeDylib),
E
Eduard Burtescu 已提交
44
        ..session::basic_options().clone()
45 46 47
    };


E
Eduard Burtescu 已提交
48
    let codemap = CodeMap::new();
49
    let diagnostic_handler = diagnostic::default_handler();
50
    let span_diagnostic_handler =
E
Eduard Burtescu 已提交
51
    diagnostic::mk_span_handler(diagnostic_handler, codemap);
52 53

    let sess = driver::build_session_(sessopts,
54
                                      Some(input_path),
55 56
                                      span_diagnostic_handler);

E
Eduard Burtescu 已提交
57 58
    let cfg = driver::build_configuration(&sess);
    let krate = driver::phase_1_parse_input(&sess, cfg, &input);
E
Eduard Burtescu 已提交
59
    let (krate, _) = driver::phase_2_configure_and_expand(&sess, &mut Loader::new(&sess), krate,
E
Eduard Burtescu 已提交
60
                                                          &from_str("rustdoc-test").unwrap());
61 62

    let ctx = @core::DocContext {
63
        krate: krate,
E
Eduard Burtescu 已提交
64
        maybe_typed: core::NotTyped(sess),
65 66 67
    };
    local_data::set(super::ctxtkey, ctx);

A
Alex Crichton 已提交
68
    let mut v = RustdocVisitor::new(ctx, None);
69 70 71 72
    v.visit(&ctx.krate);
    let krate = v.clean();
    let (krate, _) = passes::unindent_comments(krate);
    let (krate, _) = passes::collapse_docs(krate);
73

74 75 76 77
    let mut collector = Collector::new(krate.name.to_owned(),
                                       libs,
                                       false,
                                       false);
78
    collector.fold_crate(krate);
79

80
    test_args.unshift(~"rustdoctest");
81

82 83
    testing::test_main(test_args.as_slice(),
                       collector.tests.move_iter().collect());
84 85 86
    0
}

87
fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
88 89
           no_run: bool, loose_feature_gating: bool) {
    let test = maketest(test, cratename, loose_feature_gating);
90
    let input = driver::StrInput(test);
91

E
Eduard Burtescu 已提交
92
    let sessopts = session::Options {
E
Eduard Burtescu 已提交
93 94
        maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
        addl_lib_search_paths: RefCell::new(libs),
95 96
        crate_types: vec!(session::CrateTypeExecutable),
        output_types: vec!(link::OutputTypeExe),
97 98 99 100
        cg: session::CodegenOptions {
            prefer_dynamic: true,
            .. session::basic_codegen_options()
        },
E
Eduard Burtescu 已提交
101
        ..session::basic_options().clone()
102 103
    };

104 105 106 107 108 109 110 111 112 113 114
    // 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.
115 116
    let (tx, rx) = channel();
    let w1 = io::ChanWriter::new(tx);
117 118 119
    let w2 = w1.clone();
    let old = io::stdio::set_stderr(~w1);
    spawn(proc() {
120
        let mut p = io::ChanReader::new(rx);
121
        let mut err = old.unwrap_or(~io::stderr() as ~Writer:Send);
122 123 124 125 126
        io::util::copy(&mut p, &mut err).unwrap();
    });
    let emitter = diagnostic::EmitterWriter::new(~w2);

    // Compile the code
E
Eduard Burtescu 已提交
127
    let codemap = CodeMap::new();
128
    let diagnostic_handler = diagnostic::mk_handler(~emitter);
129
    let span_diagnostic_handler =
E
Eduard Burtescu 已提交
130
        diagnostic::mk_span_handler(diagnostic_handler, codemap);
131 132

    let sess = driver::build_session_(sessopts,
133
                                      None,
134 135 136 137
                                      span_diagnostic_handler);

    let outdir = TempDir::new("rustdoctest").expect("rustdoc needs a tempdir");
    let out = Some(outdir.path().clone());
E
Eduard Burtescu 已提交
138
    let cfg = driver::build_configuration(&sess);
139 140
    driver::compile_input(sess, cfg, &input, &out, &None);

141 142
    if no_run { return }

143
    // Run the code!
144
    let exe = outdir.path().join("rust_out");
145
    let out = Process::output(exe.as_str().unwrap(), []);
146
    match out {
147 148 149 150
        Err(e) => fail!("couldn't run the test: {}{}", e,
                        if e.kind == io::PermissionDenied {
                            " - maybe your tempdir is mounted with noexec?"
                        } else { "" }),
A
Alex Crichton 已提交
151
        Ok(out) => {
152 153 154 155
            if should_fail && out.status.success() {
                fail!("test executable succeeded when it should have failed");
            } else if !should_fail && !out.status.success() {
                fail!("test executable failed:\n{}", str::from_utf8(out.error));
156 157 158 159 160
            }
        }
    }
}

161
fn maketest(s: &str, cratename: &str, loose_feature_gating: bool) -> ~str {
162 163 164
    let mut prog = ~r"
#[deny(warnings)];
#[allow(unused_variable, dead_assignment, unused_mut, attribute_usage, dead_code)];
165 166 167

// FIXME: remove when ~[] disappears from tests.
#[allow(deprecated_owned_vector)];
168
";
169 170 171 172 173 174 175

    if loose_feature_gating {
        // FIXME #12773: avoid inserting these when the tutorial & manual
        // etc. have been updated to not use them so prolifically.
        prog.push_str("#[ feature(macro_rules, globs, struct_variant, managed_boxes) ];\n");
    }

176 177 178 179
    if !s.contains("extern crate") {
        if s.contains(cratename) {
            prog.push_str(format!("extern crate {};\n", cratename));
        }
180 181 182 183 184 185 186 187 188
    }
    if s.contains("fn main") {
        prog.push_str(s);
    } else {
        prog.push_str("fn main() {\n");
        prog.push_str(s);
        prog.push_str("\n}");
    }

189
    return prog;
190 191 192
}

pub struct Collector {
193 194
    tests: Vec<testing::TestDescAndFn>,
    priv names: Vec<~str>,
E
Eduard Burtescu 已提交
195
    priv libs: HashSet<Path>,
196
    priv cnt: uint,
197 198
    priv use_headers: bool,
    priv current_header: Option<~str>,
199
    priv cratename: ~str,
200 201

    priv loose_feature_gating: bool
202 203 204
}

impl Collector {
E
Eduard Burtescu 已提交
205
    pub fn new(cratename: ~str, libs: HashSet<Path>,
206
               use_headers: bool, loose_feature_gating: bool) -> Collector {
207
        Collector {
208 209
            tests: Vec::new(),
            names: Vec::new(),
210 211 212 213
            libs: libs,
            cnt: 0,
            use_headers: use_headers,
            current_header: None,
214 215 216
            cratename: cratename,

            loose_feature_gating: loose_feature_gating
217 218 219 220 221 222 223 224 225 226
        }
    }

    pub fn add_test(&mut self, test: ~str, should_fail: bool, no_run: bool) {
        let name = if self.use_headers {
            let s = self.current_header.as_ref().map(|s| s.as_slice()).unwrap_or("");
            format!("{}_{}", s, self.cnt)
        } else {
            format!("{}_{}", self.names.connect("::"), self.cnt)
        };
227
        self.cnt += 1;
E
Eduard Burtescu 已提交
228
        let libs = self.libs.clone();
229
        let cratename = self.cratename.to_owned();
230
        let loose_feature_gating = self.loose_feature_gating;
231
        debug!("Creating test {}: {}", name, test);
L
Liigo Zhuang 已提交
232 233 234
        self.tests.push(testing::TestDescAndFn {
            desc: testing::TestDesc {
                name: testing::DynTestName(name),
235 236
                ignore: false,
                should_fail: false, // compiler failures are test failures
237
            },
L
Liigo Zhuang 已提交
238
            testfn: testing::DynTestFn(proc() {
239
                runtest(test, cratename, libs, should_fail, no_run, loose_feature_gating);
240 241 242
            }),
        });
    }
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

    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)| {
                    if (i == 0 && char::is_XID_start(c)) ||
                        (i != 0 && char::is_XID_continue(c)) {
                        c
                    } else {
                        '_'
                    }
                }).collect::<~str>();

            // new header => reset count.
            self.cnt = 0;
            self.current_header = Some(name);
        }
    }
262 263 264 265 266 267 268 269 270 271 272 273
}

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,
            Some(ref name) => { self.names.push(name.to_owned()); true }
            None => false
        };
        match item.doc_value() {
            Some(doc) => {
                self.cnt = 0;
274
                markdown::find_testable_code(doc, &mut *self);
275 276 277 278 279 280 281 282 283 284
            }
            None => {}
        }
        let ret = self.fold_item_recur(item);
        if pushed {
            self.names.pop();
        }
        return ret;
    }
}