2019-05-26 01:50:41 +07:00
|
|
|
use crate::get_book_dir;
|
2022-01-19 10:10:53 -06:00
|
|
|
use clap::{arg, App, Arg, ArgMatches};
|
2018-07-23 12:45:01 -05:00
|
|
|
use mdbook::errors::Result;
|
|
|
|
|
use mdbook::MDBook;
|
2017-06-26 01:24:33 +02:00
|
|
|
|
2017-06-27 07:59:50 +02:00
|
|
|
// Create clap subcommand arguments
|
2022-01-18 15:56:45 -06:00
|
|
|
pub fn make_subcommand<'help>() -> App<'help> {
|
2022-01-18 16:19:40 -06:00
|
|
|
App::new("test")
|
2018-08-02 15:48:22 -05:00
|
|
|
.about("Tests that a book's Rust code samples compile")
|
2022-01-19 10:10:53 -06:00
|
|
|
.arg(
|
|
|
|
|
Arg::new("dest-dir")
|
|
|
|
|
.short('d')
|
|
|
|
|
.long("dest-dir")
|
|
|
|
|
.value_name("dest-dir")
|
|
|
|
|
.help(
|
|
|
|
|
"Output directory for the book{n}\
|
|
|
|
|
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`.",
|
|
|
|
|
),
|
2022-08-25 19:13:51 -07:00
|
|
|
).arg(
|
|
|
|
|
Arg::new("chapter")
|
|
|
|
|
.short('c')
|
|
|
|
|
.long("chapter")
|
|
|
|
|
.value_name("chapter")
|
|
|
|
|
.help(
|
|
|
|
|
"Only test the specified chapter{n}\
|
|
|
|
|
Where the name of the chapter is defined in the SUMMARY.md file.{n}\
|
|
|
|
|
Use the special name \"?\" to the list of chapter names."
|
|
|
|
|
)
|
2018-08-02 15:48:22 -05:00
|
|
|
)
|
2022-01-19 10:10:53 -06:00
|
|
|
.arg(arg!([dir]
|
|
|
|
|
"Root directory for the book{n}\
|
|
|
|
|
(Defaults to the Current Directory when omitted)"
|
|
|
|
|
))
|
2022-01-18 16:17:36 -06:00
|
|
|
.arg(Arg::new("library-path")
|
2022-01-18 15:56:45 -06:00
|
|
|
.short('L')
|
2018-08-02 15:48:22 -05:00
|
|
|
.long("library-path")
|
|
|
|
|
.value_name("dir")
|
|
|
|
|
.takes_value(true)
|
2022-01-18 15:56:45 -06:00
|
|
|
.use_delimiter(true)
|
2018-08-02 15:48:22 -05:00
|
|
|
.require_delimiter(true)
|
2022-01-18 16:22:27 -06:00
|
|
|
.multiple_values(true)
|
|
|
|
|
.multiple_occurrences(true)
|
2022-01-18 16:21:53 -06:00
|
|
|
.forbid_empty_values(true)
|
2018-08-02 15:48:22 -05:00
|
|
|
.help("A comma-separated list of directories to add to {n}the crate search path when building tests"))
|
2017-06-27 07:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-26 01:24:33 +02:00
|
|
|
// test command implementation
|
2017-06-27 07:59:50 +02:00
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
2018-08-02 20:22:49 -05:00
|
|
|
let library_paths: Vec<&str> = args
|
|
|
|
|
.values_of("library-path")
|
2019-05-07 01:20:58 +07:00
|
|
|
.map(std::iter::Iterator::collect)
|
2018-07-23 12:45:01 -05:00
|
|
|
.unwrap_or_default();
|
2022-08-25 19:13:51 -07:00
|
|
|
let chapter: Option<&str> = args.value_of("chapter");
|
|
|
|
|
|
2017-06-26 01:24:33 +02: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 01:24:33 +02:00
|
|
|
|
2018-08-02 15:48:22 -05:00
|
|
|
if let Some(dest_dir) = args.value_of("dest-dir") {
|
|
|
|
|
book.config.build.build_dir = dest_dir.into();
|
|
|
|
|
}
|
2022-08-25 19:13:51 -07:00
|
|
|
match chapter {
|
|
|
|
|
Some(_) => book.test_chapter(library_paths, chapter),
|
|
|
|
|
None => book.test(library_paths),
|
|
|
|
|
}?;
|
2017-06-26 01:24:33 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|