]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/test.rs
Replace all ~"" with "".to_owned()
[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 use std::cell::RefCell;
12 use std::char;
13 use std::io;
14 use std::io::{Process, TempDir};
15 use std::local_data;
16 use std::os;
17 use std::str;
18 use std::strbuf::StrBuf;
19
20 use collections::HashSet;
21 use testing;
22 use rustc::back::link;
23 use rustc::driver::driver;
24 use rustc::driver::session;
25 use rustc::metadata::creader::Loader;
26 use syntax::ast;
27 use syntax::codemap::{CodeMap, dummy_spanned};
28 use syntax::diagnostic;
29 use syntax::parse::token;
30
31 use core;
32 use clean;
33 use clean::Clean;
34 use fold::DocFolder;
35 use html::markdown;
36 use passes;
37 use visit_ast::RustdocVisitor;
38
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());
43
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()
49     };
50
51
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);
56
57     let sess = driver::build_session_(sessopts,
58                                       Some(input_path),
59                                       span_diagnostic_handler);
60
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_))
65     }));
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());
69
70     let ctx = @core::DocContext {
71         krate: krate,
72         maybe_typed: core::NotTyped(sess),
73     };
74     local_data::set(super::ctxtkey, ctx);
75
76     let mut v = RustdocVisitor::new(ctx, None);
77     v.visit(&ctx.krate);
78     let krate = v.clean();
79     let (krate, _) = passes::unindent_comments(krate);
80     let (krate, _) = passes::collapse_docs(krate);
81
82     let mut collector = Collector::new(krate.name.to_owned(),
83                                        libs,
84                                        false,
85                                        false);
86     collector.fold_crate(krate);
87
88     test_args.unshift("rustdoctest".to_owned());
89
90     testing::test_main(test_args.as_slice(),
91                        collector.tests.move_iter().collect());
92     0
93 }
94
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);
99
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()
108         },
109         ..session::basic_options().clone()
110     };
111
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.
115     //
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.
120     //
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);
125     let w2 = w1.clone();
126     let old = io::stdio::set_stderr(~w1);
127     spawn(proc() {
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();
131     });
132     let emitter = diagnostic::EmitterWriter::new(~w2);
133
134     // Compile the code
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);
139
140     let sess = driver::build_session_(sessopts,
141                                       None,
142                                       span_diagnostic_handler);
143
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);
148
149     if no_run { return }
150
151     // Run the code!
152     let exe = outdir.path().join("rust_out");
153     let out = Process::output(exe.as_str().unwrap(), []);
154     match out {
155         Err(e) => fail!("couldn't run the test: {}{}", e,
156                         if e.kind == io::PermissionDenied {
157                             " - maybe your tempdir is mounted with noexec?"
158                         } else { "" }),
159         Ok(out) => {
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()));
165             }
166         }
167     }
168 }
169
170 fn maketest(s: &str, cratename: &str, loose_feature_gating: bool) -> ~str {
171     let mut prog = StrBuf::from_str(r"
172 #![deny(warnings)]
173 #![allow(unused_variable, dead_assignment, unused_mut, attribute_usage, dead_code)]
174 ");
175
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");
180     }
181
182     if !s.contains("extern crate") {
183         if s.contains(cratename) {
184             prog.push_str(format!("extern crate {};\n", cratename));
185         }
186     }
187     if s.contains("fn main") {
188         prog.push_str(s);
189     } else {
190         prog.push_str("fn main() {\n");
191         prog.push_str(s);
192         prog.push_str("\n}");
193     }
194
195     return prog.into_owned();
196 }
197
198 pub struct Collector {
199     pub tests: Vec<testing::TestDescAndFn>,
200     names: Vec<~str>,
201     libs: HashSet<Path>,
202     cnt: uint,
203     use_headers: bool,
204     current_header: Option<~str>,
205     cratename: ~str,
206
207     loose_feature_gating: bool
208 }
209
210 impl Collector {
211     pub fn new(cratename: ~str, libs: HashSet<Path>,
212                use_headers: bool, loose_feature_gating: bool) -> Collector {
213         Collector {
214             tests: Vec::new(),
215             names: Vec::new(),
216             libs: libs,
217             cnt: 0,
218             use_headers: use_headers,
219             current_header: None,
220             cratename: cratename,
221
222             loose_feature_gating: loose_feature_gating
223         }
224     }
225
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)
230         } else {
231             format!("{}_{}", self.names.connect("::"), self.cnt)
232         };
233         self.cnt += 1;
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
243             },
244             testfn: testing::DynTestFn(proc() {
245                 runtest(test, cratename, libs, should_fail, no_run, loose_feature_gating);
246             }),
247         });
248     }
249
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)) {
257                         c
258                     } else {
259                         '_'
260                     }
261                 }).collect::<~str>();
262
263             // new header => reset count.
264             self.cnt = 0;
265             self.current_header = Some(name);
266         }
267     }
268 }
269
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 }
275             None => false
276         };
277         match item.doc_value() {
278             Some(doc) => {
279                 self.cnt = 0;
280                 markdown::find_testable_code(doc, &mut *self);
281             }
282             None => {}
283         }
284         let ret = self.fold_item_recur(item);
285         if pushed {
286             self.names.pop();
287         }
288         return ret;
289     }
290 }