2019-05-26 02:50:41 +08:00
|
|
|
use crate::get_book_dir;
|
2018-08-03 04:48:22 +08:00
|
|
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
2018-07-24 01:45:01 +08:00
|
|
|
use mdbook::errors::Result;
|
|
|
|
use mdbook::MDBook;
|
2017-06-26 07:24:33 +08:00
|
|
|
|
2017-06-27 13:59:50 +08:00
|
|
|
// Create clap subcommand arguments
|
|
|
|
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
2017-06-19 22:06:15 +08:00
|
|
|
SubCommand::with_name("test")
|
2018-08-03 04:48:22 +08:00
|
|
|
.about("Tests that a book's Rust code samples compile")
|
2018-08-02 06:59:40 +08:00
|
|
|
.arg_from_usage(
|
2018-08-03 04:48:22 +08:00
|
|
|
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
|
2018-09-06 23:24:42 +08:00
|
|
|
Relative paths are interpreted relative to the book's root directory.{n}\
|
|
|
|
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'",
|
2018-08-02 06:59:40 +08:00
|
|
|
)
|
2018-08-03 04:48:22 +08:00
|
|
|
.arg_from_usage(
|
|
|
|
"[dir] 'Root directory for the book{n}\
|
|
|
|
(Defaults to the Current Directory when omitted)'",
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("library-path")
|
|
|
|
.short("L")
|
|
|
|
.long("library-path")
|
|
|
|
.value_name("dir")
|
|
|
|
.takes_value(true)
|
|
|
|
.require_delimiter(true)
|
|
|
|
.multiple(true)
|
|
|
|
.empty_values(false)
|
|
|
|
.help("A comma-separated list of directories to add to {n}the crate search path when building tests"))
|
2017-06-27 13:59:50 +08:00
|
|
|
}
|
|
|
|
|
2017-06-26 07:24:33 +08:00
|
|
|
// test command implementation
|
2017-06-27 13:59:50 +08:00
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
2018-08-03 09:22:49 +08:00
|
|
|
let library_paths: Vec<&str> = args
|
|
|
|
.values_of("library-path")
|
2019-05-07 02:20:58 +08:00
|
|
|
.map(std::iter::Iterator::collect)
|
2018-07-24 01:45:01 +08:00
|
|
|
.unwrap_or_default();
|
2017-06-26 07:24:33 +08:00
|
|
|
let book_dir = get_book_dir(args);
|
2017-11-18 20:41:04 +08:00
|
|
|
let mut book = MDBook::load(&book_dir)?;
|
2017-06-26 07:24:33 +08:00
|
|
|
|
2018-08-03 04:48:22 +08:00
|
|
|
if let Some(dest_dir) = args.value_of("dest-dir") {
|
|
|
|
book.config.build.build_dir = dest_dir.into();
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:06:15 +08:00
|
|
|
book.test(library_paths)?;
|
2017-06-26 07:24:33 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|