1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::cell::RefCell;
14 use std::io::{Process, TempDir};
18 use std::strbuf::StrBuf;
20 use collections::HashSet;
22 use rustc::back::link;
23 use rustc::driver::driver;
24 use rustc::driver::session;
25 use rustc::metadata::creader::Loader;
27 use syntax::codemap::{CodeMap, dummy_spanned};
28 use syntax::diagnostic;
29 use syntax::parse::token;
37 use visit_ast::RustdocVisitor;
39 pub fn run(input: &str, cfgs: Vec<~str>,
40 libs: HashSet<Path>, mut test_args: Vec<~str>) -> int {
41 let input_path = Path::new(input);
42 let input = driver::FileInput(input_path.clone());
44 let sessopts = session::Options {
45 maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
46 addl_lib_search_paths: RefCell::new(libs.clone()),
47 crate_types: vec!(session::CrateTypeDylib),
48 ..session::basic_options().clone()
52 let codemap = CodeMap::new();
53 let diagnostic_handler = diagnostic::default_handler();
54 let span_diagnostic_handler =
55 diagnostic::mk_span_handler(diagnostic_handler, codemap);
57 let sess = driver::build_session_(sessopts,
59 span_diagnostic_handler);
61 let mut cfg = driver::build_configuration(&sess);
62 cfg.extend(cfgs.move_iter().map(|cfg_| {
63 let cfg_ = token::intern_and_get_ident(cfg_);
64 @dummy_spanned(ast::MetaWord(cfg_))
66 let krate = driver::phase_1_parse_input(&sess, cfg, &input);
67 let (krate, _) = driver::phase_2_configure_and_expand(&sess, &mut Loader::new(&sess), krate,
68 &from_str("rustdoc-test").unwrap());
70 let ctx = @core::DocContext {
72 maybe_typed: core::NotTyped(sess),
74 local_data::set(super::ctxtkey, ctx);
76 let mut v = RustdocVisitor::new(ctx, None);
78 let krate = v.clean();
79 let (krate, _) = passes::unindent_comments(krate);
80 let (krate, _) = passes::collapse_docs(krate);
82 let mut collector = Collector::new(krate.name.to_owned(),
86 collector.fold_crate(krate);
88 test_args.unshift("rustdoctest".to_owned());
90 testing::test_main(test_args.as_slice(),
91 collector.tests.move_iter().collect());
95 fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
96 no_run: bool, loose_feature_gating: bool) {
97 let test = maketest(test, cratename, loose_feature_gating);
98 let input = driver::StrInput(test);
100 let sessopts = session::Options {
101 maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
102 addl_lib_search_paths: RefCell::new(libs),
103 crate_types: vec!(session::CrateTypeExecutable),
104 output_types: vec!(link::OutputTypeExe),
105 cg: session::CodegenOptions {
106 prefer_dynamic: true,
107 .. session::basic_codegen_options()
109 ..session::basic_options().clone()
112 // Shuffle around a few input and output handles here. We're going to pass
113 // an explicit handle into rustc to collect output messages, but we also
114 // want to catch the error message that rustc prints when it fails.
116 // We take our task-local stderr (likely set by the test runner), and move
117 // it into another task. This helper task then acts as a sink for both the
118 // stderr of this task and stderr of rustc itself, copying all the info onto
119 // the stderr channel we originally started with.
121 // The basic idea is to not use a default_handler() for rustc, and then also
122 // not print things by default to the actual stderr.
123 let (tx, rx) = channel();
124 let w1 = io::ChanWriter::new(tx);
126 let old = io::stdio::set_stderr(~w1);
128 let mut p = io::ChanReader::new(rx);
129 let mut err = old.unwrap_or(~io::stderr() as ~Writer:Send);
130 io::util::copy(&mut p, &mut err).unwrap();
132 let emitter = diagnostic::EmitterWriter::new(~w2);
135 let codemap = CodeMap::new();
136 let diagnostic_handler = diagnostic::mk_handler(~emitter);
137 let span_diagnostic_handler =
138 diagnostic::mk_span_handler(diagnostic_handler, codemap);
140 let sess = driver::build_session_(sessopts,
142 span_diagnostic_handler);
144 let outdir = TempDir::new("rustdoctest").expect("rustdoc needs a tempdir");
145 let out = Some(outdir.path().clone());
146 let cfg = driver::build_configuration(&sess);
147 driver::compile_input(sess, cfg, &input, &out, &None);
152 let exe = outdir.path().join("rust_out");
153 let out = Process::output(exe.as_str().unwrap(), []);
155 Err(e) => fail!("couldn't run the test: {}{}", e,
156 if e.kind == io::PermissionDenied {
157 " - maybe your tempdir is mounted with noexec?"
160 if should_fail && out.status.success() {
161 fail!("test executable succeeded when it should have failed");
162 } else if !should_fail && !out.status.success() {
163 fail!("test executable failed:\n{}",
164 str::from_utf8(out.error.as_slice()));
170 fn maketest(s: &str, cratename: &str, loose_feature_gating: bool) -> ~str {
171 let mut prog = StrBuf::from_str(r"
173 #![allow(unused_variable, dead_assignment, unused_mut, attribute_usage, dead_code)]
176 if loose_feature_gating {
177 // FIXME #12773: avoid inserting these when the tutorial & manual
178 // etc. have been updated to not use them so prolifically.
179 prog.push_str("#![feature(macro_rules, globs, struct_variant, managed_boxes) ]\n");
182 if !s.contains("extern crate") {
183 if s.contains(cratename) {
184 prog.push_str(format!("extern crate {};\n", cratename));
187 if s.contains("fn main") {
190 prog.push_str("fn main() {\n");
192 prog.push_str("\n}");
195 return prog.into_owned();
198 pub struct Collector {
199 pub tests: Vec<testing::TestDescAndFn>,
204 current_header: Option<~str>,
207 loose_feature_gating: bool
211 pub fn new(cratename: ~str, libs: HashSet<Path>,
212 use_headers: bool, loose_feature_gating: bool) -> Collector {
218 use_headers: use_headers,
219 current_header: None,
220 cratename: cratename,
222 loose_feature_gating: loose_feature_gating
226 pub fn add_test(&mut self, test: ~str, should_fail: bool, no_run: bool, should_ignore: bool) {
227 let name = if self.use_headers {
228 let s = self.current_header.as_ref().map(|s| s.as_slice()).unwrap_or("");
229 format!("{}_{}", s, self.cnt)
231 format!("{}_{}", self.names.connect("::"), self.cnt)
234 let libs = self.libs.clone();
235 let cratename = self.cratename.to_owned();
236 let loose_feature_gating = self.loose_feature_gating;
237 debug!("Creating test {}: {}", name, test);
238 self.tests.push(testing::TestDescAndFn {
239 desc: testing::TestDesc {
240 name: testing::DynTestName(name),
241 ignore: should_ignore,
242 should_fail: false, // compiler failures are test failures
244 testfn: testing::DynTestFn(proc() {
245 runtest(test, cratename, libs, should_fail, no_run, loose_feature_gating);
250 pub fn register_header(&mut self, name: &str, level: u32) {
251 if self.use_headers && level == 1 {
252 // we use these headings as test names, so it's good if
253 // they're valid identifiers.
254 let name = name.chars().enumerate().map(|(i, c)| {
255 if (i == 0 && char::is_XID_start(c)) ||
256 (i != 0 && char::is_XID_continue(c)) {
261 }).collect::<~str>();
263 // new header => reset count.
265 self.current_header = Some(name);
270 impl DocFolder for Collector {
271 fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
272 let pushed = match item.name {
273 Some(ref name) if name.len() == 0 => false,
274 Some(ref name) => { self.names.push(name.to_owned()); true }
277 match item.doc_value() {
280 markdown::find_testable_code(doc, &mut *self);
284 let ret = self.fold_item_recur(item);