]> git.lizzy.rs Git - rust.git/blob - src/rustbook/test.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.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::CliResult;
15 use error::CommandResult;
16 use error::Error;
17 use term::Term;
18 use book;
19 use std::old_io::{Command, File};
20 use std::os;
21
22 struct Test;
23
24 pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> {
25     if name == "test" {
26         Some(box Test as Box<Subcommand>)
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 = os::getcwd().unwrap();
39         let src = cwd.clone();
40
41         let summary = File::open(&src.join("SUMMARY.md"));
42         match book::parse_summary(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.output[]),
54                                          String::from_utf8_lossy(&output.error[]))[]);
55                                 return Err(box "Some tests failed." as Box<Error>);
56                             }
57
58                         }
59                         Err(e) => {
60                             let message = format!("Could not execute `rustdoc`: {}", e);
61                             return Err(box message as Box<Error>);
62                         }
63                     }
64                 }
65             }
66             Err(errors) => {
67                 for err in errors {
68                     term.err(&err[]);
69                 }
70                 return Err(box "There was an error." as Box<Error>);
71             }
72         }
73         Ok(()) // lol
74     }
75 }