]> git.proxmox.com Git - rustc.git/blob - src/rustbook/test.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / rustbook / test.rs
1 // Copyright 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 //! Implementation of the `test` subcommand. Just a stub for now.
12
13 use subcommand::Subcommand;
14 use error::{err, CliResult, CommandResult};
15 use term::Term;
16 use book;
17
18 use std::fs::File;
19 use std::env;
20 use std::process::Command;
21
22 struct Test;
23
24 pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> {
25 if name == "test" {
26 Some(Box::new(Test))
27 } else {
28 None
29 }
30 }
31
32 impl Subcommand for Test {
33 fn parse_args(&mut self, _: &[String]) -> CliResult<()> {
34 Ok(())
35 }
36 fn usage(&self) {}
37 fn execute(&mut self, term: &mut Term) -> CommandResult<()> {
38 let cwd = env::current_dir().unwrap();
39 let src = cwd.clone();
40
41 let mut summary = try!(File::open(&src.join("SUMMARY.md")));
42 match book::parse_summary(&mut summary, &src) {
43 Ok(book) => {
44 for (_, item) in book.iter() {
45 let output_result = Command::new("rustdoc")
46 .arg(&item.path)
47 .arg("--test")
48 .output();
49 match output_result {
50 Ok(output) => {
51 if !output.status.success() {
52 term.err(&format!("{}\n{}",
53 String::from_utf8_lossy(&output.stdout),
54 String::from_utf8_lossy(&output.stderr)));
55 return Err(err("some tests failed"));
56 }
57
58 }
59 Err(e) => {
60 let message = format!("could not execute `rustdoc`: {}", e);
61 return Err(err(&message))
62 }
63 }
64 }
65 }
66 Err(errors) => {
67 for err in errors {
68 term.err(&err[..]);
69 }
70 return Err(err("there was an error"))
71 }
72 }
73 Ok(()) // lol
74 }
75 }