]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/test.rs
3322794c7781e28827a66df5cb66ebb52a49d445
[rust.git] / src / librustdoc / test.rs
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.
4 //
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.
10
11 #![allow(deprecated)]
12
13 use std::cell::{RefCell, Cell};
14 use std::collections::{HashSet, HashMap};
15 use std::dynamic_lib::DynamicLibrary;
16 use std::env;
17 use std::ffi::OsString;
18 use std::io::prelude::*;
19 use std::io;
20 use std::path::PathBuf;
21 use std::process::Command;
22 use std::rc::Rc;
23 use std::str;
24 use std::sync::{Arc, Mutex};
25
26 use testing;
27 use rustc_lint;
28 use rustc::front::map as hir_map;
29 use rustc::session::{self, config};
30 use rustc::session::config::{get_unstable_features_setting, OutputType};
31 use rustc::session::search_paths::{SearchPaths, PathKind};
32 use rustc_front::lowering::{lower_crate, LoweringContext};
33 use rustc_back::tempdir::TempDir;
34 use rustc_driver::{driver, Compilation};
35 use rustc_metadata::cstore::CStore;
36 use syntax::codemap::CodeMap;
37 use syntax::diagnostic;
38 use syntax::parse::token;
39
40 use core;
41 use clean;
42 use clean::Clean;
43 use fold::DocFolder;
44 use html::markdown;
45 use passes;
46 use visit_ast::RustdocVisitor;
47
48 #[derive(Clone, Default)]
49 pub struct TestOptions {
50     pub no_crate_inject: bool,
51     pub attrs: Vec<String>,
52 }
53
54 pub fn run(input: &str,
55            cfgs: Vec<String>,
56            libs: SearchPaths,
57            externs: core::Externs,
58            mut test_args: Vec<String>,
59            crate_name: Option<String>)
60            -> isize {
61     let input_path = PathBuf::from(input);
62     let input = config::Input::File(input_path.clone());
63
64     let sessopts = config::Options {
65         maybe_sysroot: Some(env::current_exe().unwrap().parent().unwrap()
66                                               .parent().unwrap().to_path_buf()),
67         search_paths: libs.clone(),
68         crate_types: vec!(config::CrateTypeDylib),
69         externs: externs.clone(),
70         unstable_features: get_unstable_features_setting(),
71         ..config::basic_options().clone()
72     };
73
74     let codemap = CodeMap::new();
75     let diagnostic_handler = diagnostic::Handler::new(diagnostic::Auto, None, true);
76     let span_diagnostic_handler =
77     diagnostic::SpanHandler::new(diagnostic_handler, codemap);
78
79     let cstore = Rc::new(CStore::new(token::get_ident_interner()));
80     let cstore_ = ::rustc_driver::cstore_to_cratestore(cstore.clone());
81     let sess = session::build_session_(sessopts,
82                                        Some(input_path.clone()),
83                                        span_diagnostic_handler,
84                                        cstore_);
85     rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
86
87     let mut cfg = config::build_configuration(&sess);
88     cfg.extend(config::parse_cfgspecs(cfgs.clone()));
89     let krate = driver::phase_1_parse_input(&sess, cfg, &input);
90     let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate,
91                                                      "rustdoc-test", None)
92         .expect("phase_2_configure_and_expand aborted in rustdoc!");
93     let krate = driver::assign_node_ids(&sess, krate);
94     let lcx = LoweringContext::new(&sess, Some(&krate));
95     let krate = lower_crate(&lcx, &krate);
96
97     let opts = scrape_test_config(&krate);
98
99     let mut forest = hir_map::Forest::new(krate);
100     let map = hir_map::map_crate(&mut forest);
101
102     let ctx = core::DocContext {
103         map: &map,
104         maybe_typed: core::NotTyped(&sess),
105         input: input,
106         external_paths: RefCell::new(Some(HashMap::new())),
107         external_traits: RefCell::new(None),
108         external_typarams: RefCell::new(None),
109         inlined: RefCell::new(None),
110         populated_crate_impls: RefCell::new(HashSet::new()),
111         deref_trait_did: Cell::new(None),
112     };
113
114     let mut v = RustdocVisitor::new(&ctx, None);
115     v.visit(ctx.map.krate());
116     let mut krate = v.clean(&ctx);
117     match crate_name {
118         Some(name) => krate.name = name,
119         None => {}
120     }
121     let (krate, _) = passes::collapse_docs(krate);
122     let (krate, _) = passes::unindent_comments(krate);
123
124     let mut collector = Collector::new(krate.name.to_string(),
125                                        cfgs,
126                                        libs,
127                                        externs,
128                                        false,
129                                        opts);
130     collector.fold_crate(krate);
131
132     test_args.insert(0, "rustdoctest".to_string());
133
134     testing::test_main(&test_args,
135                        collector.tests.into_iter().collect());
136     0
137 }
138
139 // Look for #![doc(test(no_crate_inject))], used by crates in the std facade
140 fn scrape_test_config(krate: &::rustc_front::hir::Crate) -> TestOptions {
141     use syntax::attr::AttrMetaMethods;
142     use syntax::print::pprust;
143
144     let mut opts = TestOptions {
145         no_crate_inject: false,
146         attrs: Vec::new(),
147     };
148
149     let attrs = krate.attrs.iter()
150                      .filter(|a| a.check_name("doc"))
151                      .filter_map(|a| a.meta_item_list())
152                      .flat_map(|l| l)
153                      .filter(|a| a.check_name("test"))
154                      .filter_map(|a| a.meta_item_list())
155                      .flat_map(|l| l);
156     for attr in attrs {
157         if attr.check_name("no_crate_inject") {
158             opts.no_crate_inject = true;
159         }
160         if attr.check_name("attr") {
161             if let Some(l) = attr.meta_item_list() {
162                 for item in l {
163                     opts.attrs.push(pprust::meta_item_to_string(item));
164                 }
165             }
166         }
167     }
168
169     return opts;
170 }
171
172 fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
173            externs: core::Externs,
174            should_panic: bool, no_run: bool, as_test_harness: bool,
175            opts: &TestOptions) {
176     // the test harness wants its own `main` & top level functions, so
177     // never wrap the test in `fn main() { ... }`
178     let test = maketest(test, Some(cratename), as_test_harness, opts);
179     let input = config::Input::Str(test.to_string());
180     let mut outputs = HashMap::new();
181     outputs.insert(OutputType::Exe, None);
182
183     let sessopts = config::Options {
184         maybe_sysroot: Some(env::current_exe().unwrap().parent().unwrap()
185                                               .parent().unwrap().to_path_buf()),
186         search_paths: libs,
187         crate_types: vec!(config::CrateTypeExecutable),
188         output_types: outputs,
189         externs: externs,
190         cg: config::CodegenOptions {
191             prefer_dynamic: true,
192             .. config::basic_codegen_options()
193         },
194         test: as_test_harness,
195         unstable_features: get_unstable_features_setting(),
196         ..config::basic_options().clone()
197     };
198
199     // Shuffle around a few input and output handles here. We're going to pass
200     // an explicit handle into rustc to collect output messages, but we also
201     // want to catch the error message that rustc prints when it fails.
202     //
203     // We take our thread-local stderr (likely set by the test runner) and replace
204     // it with a sink that is also passed to rustc itself. When this function
205     // returns the output of the sink is copied onto the output of our own thread.
206     //
207     // The basic idea is to not use a default Handler for rustc, and then also
208     // not print things by default to the actual stderr.
209     struct Sink(Arc<Mutex<Vec<u8>>>);
210     impl Write for Sink {
211         fn write(&mut self, data: &[u8]) -> io::Result<usize> {
212             Write::write(&mut *self.0.lock().unwrap(), data)
213         }
214         fn flush(&mut self) -> io::Result<()> { Ok(()) }
215     }
216     struct Bomb(Arc<Mutex<Vec<u8>>>, Box<Write+Send>);
217     impl Drop for Bomb {
218         fn drop(&mut self) {
219             let _ = self.1.write_all(&self.0.lock().unwrap());
220         }
221     }
222     let data = Arc::new(Mutex::new(Vec::new()));
223     let emitter = diagnostic::EmitterWriter::new(box Sink(data.clone()), None);
224     let old = io::set_panic(box Sink(data.clone()));
225     let _bomb = Bomb(data, old.unwrap_or(box io::stdout()));
226
227     // Compile the code
228     let codemap = CodeMap::new();
229     let diagnostic_handler = diagnostic::Handler::with_emitter(true, box emitter);
230     let span_diagnostic_handler =
231         diagnostic::SpanHandler::new(diagnostic_handler, codemap);
232
233     let cstore = Rc::new(CStore::new(token::get_ident_interner()));
234     let cstore_ = ::rustc_driver::cstore_to_cratestore(cstore.clone());
235     let sess = session::build_session_(sessopts,
236                                        None,
237                                        span_diagnostic_handler,
238                                        cstore_);
239     rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
240
241     let outdir = TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir");
242     let out = Some(outdir.path().to_path_buf());
243     let mut cfg = config::build_configuration(&sess);
244     cfg.extend(config::parse_cfgspecs(cfgs));
245     let libdir = sess.target_filesearch(PathKind::All).get_lib_path();
246     let mut control = driver::CompileController::basic();
247     if no_run {
248         control.after_analysis.stop = Compilation::Stop;
249     }
250     driver::compile_input(sess, &cstore, cfg, &input, &out, &None, None, control);
251
252     if no_run { return }
253
254     // Run the code!
255     //
256     // We're careful to prepend the *target* dylib search path to the child's
257     // environment to ensure that the target loads the right libraries at
258     // runtime. It would be a sad day if the *host* libraries were loaded as a
259     // mistake.
260     let mut cmd = Command::new(&outdir.path().join("rust_out"));
261     let var = DynamicLibrary::envvar();
262     let newpath = {
263         let path = env::var_os(var).unwrap_or(OsString::new());
264         let mut path = env::split_paths(&path).collect::<Vec<_>>();
265         path.insert(0, libdir.clone());
266         env::join_paths(path).unwrap()
267     };
268     cmd.env(var, &newpath);
269
270     match cmd.output() {
271         Err(e) => panic!("couldn't run the test: {}{}", e,
272                         if e.kind() == io::ErrorKind::PermissionDenied {
273                             " - maybe your tempdir is mounted with noexec?"
274                         } else { "" }),
275         Ok(out) => {
276             if should_panic && out.status.success() {
277                 panic!("test executable succeeded when it should have failed");
278             } else if !should_panic && !out.status.success() {
279                 panic!("test executable failed:\n{}\n{}",
280                        str::from_utf8(&out.stdout).unwrap_or(""),
281                        str::from_utf8(&out.stderr).unwrap_or(""));
282             }
283         }
284     }
285 }
286
287 pub fn maketest(s: &str, cratename: Option<&str>, dont_insert_main: bool,
288                 opts: &TestOptions) -> String {
289     let (crate_attrs, everything_else) = partition_source(s);
290
291     let mut prog = String::new();
292
293     // First push any outer attributes from the example, assuming they
294     // are intended to be crate attributes.
295     prog.push_str(&crate_attrs);
296
297     // Next, any attributes for other aspects such as lints.
298     for attr in &opts.attrs {
299         prog.push_str(&format!("#![{}]\n", attr));
300     }
301
302     // Don't inject `extern crate std` because it's already injected by the
303     // compiler.
304     if !s.contains("extern crate") && !opts.no_crate_inject && cratename != Some("std") {
305         match cratename {
306             Some(cratename) => {
307                 if s.contains(cratename) {
308                     prog.push_str(&format!("extern crate {};\n", cratename));
309                 }
310             }
311             None => {}
312         }
313     }
314     if dont_insert_main || s.contains("fn main") {
315         prog.push_str(&everything_else);
316     } else {
317         prog.push_str("fn main() {\n    ");
318         prog.push_str(&everything_else.replace("\n", "\n    "));
319         prog.push_str("\n}");
320     }
321
322     info!("final test program: {}", prog);
323
324     return prog
325 }
326
327 fn partition_source(s: &str) -> (String, String) {
328     use rustc_unicode::str::UnicodeStr;
329
330     let mut after_header = false;
331     let mut before = String::new();
332     let mut after = String::new();
333
334     for line in s.lines() {
335         let trimline = line.trim();
336         let header = trimline.is_whitespace() ||
337             trimline.starts_with("#![feature");
338         if !header || after_header {
339             after_header = true;
340             after.push_str(line);
341             after.push_str("\n");
342         } else {
343             before.push_str(line);
344             before.push_str("\n");
345         }
346     }
347
348     return (before, after);
349 }
350
351 pub struct Collector {
352     pub tests: Vec<testing::TestDescAndFn>,
353     names: Vec<String>,
354     cfgs: Vec<String>,
355     libs: SearchPaths,
356     externs: core::Externs,
357     cnt: usize,
358     use_headers: bool,
359     current_header: Option<String>,
360     cratename: String,
361     opts: TestOptions,
362 }
363
364 impl Collector {
365     pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: core::Externs,
366                use_headers: bool, opts: TestOptions) -> Collector {
367         Collector {
368             tests: Vec::new(),
369             names: Vec::new(),
370             cfgs: cfgs,
371             libs: libs,
372             externs: externs,
373             cnt: 0,
374             use_headers: use_headers,
375             current_header: None,
376             cratename: cratename,
377             opts: opts,
378         }
379     }
380
381     pub fn add_test(&mut self, test: String,
382                     should_panic: bool, no_run: bool, should_ignore: bool,
383                     as_test_harness: bool) {
384         let name = if self.use_headers {
385             let s = self.current_header.as_ref().map(|s| &**s).unwrap_or("");
386             format!("{}_{}", s, self.cnt)
387         } else {
388             format!("{}_{}", self.names.join("::"), self.cnt)
389         };
390         self.cnt += 1;
391         let cfgs = self.cfgs.clone();
392         let libs = self.libs.clone();
393         let externs = self.externs.clone();
394         let cratename = self.cratename.to_string();
395         let opts = self.opts.clone();
396         debug!("Creating test {}: {}", name, test);
397         self.tests.push(testing::TestDescAndFn {
398             desc: testing::TestDesc {
399                 name: testing::DynTestName(name),
400                 ignore: should_ignore,
401                 // compiler failures are test failures
402                 should_panic: testing::ShouldPanic::No,
403             },
404             testfn: testing::DynTestFn(Box::new(move|| {
405                 runtest(&test,
406                         &cratename,
407                         cfgs,
408                         libs,
409                         externs,
410                         should_panic,
411                         no_run,
412                         as_test_harness,
413                         &opts);
414             }))
415         });
416     }
417
418     pub fn register_header(&mut self, name: &str, level: u32) {
419         if self.use_headers && level == 1 {
420             // we use these headings as test names, so it's good if
421             // they're valid identifiers.
422             let name = name.chars().enumerate().map(|(i, c)| {
423                     if (i == 0 && c.is_xid_start()) ||
424                         (i != 0 && c.is_xid_continue()) {
425                         c
426                     } else {
427                         '_'
428                     }
429                 }).collect::<String>();
430
431             // new header => reset count.
432             self.cnt = 0;
433             self.current_header = Some(name);
434         }
435     }
436 }
437
438 impl DocFolder for Collector {
439     fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
440         let current_name = match item.name {
441             Some(ref name) if !name.is_empty() => Some(name.clone()),
442             _ => typename_if_impl(&item)
443         };
444
445         let pushed = if let Some(name) = current_name {
446             self.names.push(name);
447             true
448         } else {
449             false
450         };
451
452         if let Some(doc) = item.doc_value() {
453             self.cnt = 0;
454             markdown::find_testable_code(doc, &mut *self);
455         }
456
457         let ret = self.fold_item_recur(item);
458         if pushed {
459             self.names.pop();
460         }
461
462         return ret;
463
464         // FIXME: it would be better to not have the escaped version in the first place
465         fn unescape_for_testname(mut s: String) -> String {
466             // for refs `&foo`
467             if s.contains("&amp;") {
468                 s = s.replace("&amp;", "&");
469
470                 // `::&'a mut Foo::` looks weird, let's make it `::<&'a mut Foo>`::
471                 if let Some('&') = s.chars().nth(0) {
472                     s = format!("<{}>", s);
473                 }
474             }
475
476             // either `<..>` or `->`
477             if s.contains("&gt;") {
478                 s.replace("&gt;", ">")
479                  .replace("&lt;", "<")
480             } else {
481                 s
482             }
483         }
484
485         fn typename_if_impl(item: &clean::Item) -> Option<String> {
486             if let clean::ItemEnum::ImplItem(ref impl_) = item.inner {
487                 let path = impl_.for_.to_string();
488                 let unescaped_path = unescape_for_testname(path);
489                 Some(unescaped_path)
490             } else {
491                 None
492             }
493         }
494     }
495 }