2022-07-04 23:16:31 +08:00
|
|
|
use super::command_prelude::*;
|
2019-05-26 01:50:41 +07:00
|
|
|
use crate::get_book_dir;
|
2025-07-21 11:37:46 -07:00
|
|
|
use anyhow::Result;
|
2024-02-24 12:04:57 -08:00
|
|
|
use clap::ArgAction;
|
2018-07-23 12:45:01 -05:00
|
|
|
use clap::builder::NonEmptyStringValueParser;
|
|
|
|
|
use mdbook::MDBook;
|
2022-07-04 23:16:31 +08:00
|
|
|
use std::path::PathBuf;
|
2017-06-26 01:24:33 +02:00
|
|
|
|
2017-06-27 07:59:50 +02:00
|
|
|
// Create clap subcommand arguments
|
2022-07-04 23:16:31 +08:00
|
|
|
pub fn make_subcommand() -> Command {
|
|
|
|
|
Command::new("test")
|
2018-08-02 15:48:22 -05:00
|
|
|
.about("Tests that a book's Rust code samples compile")
|
2022-07-04 23:16:31 +08:00
|
|
|
// FIXME: --dest-dir is unused by the test command, it should be removed
|
|
|
|
|
.arg_dest_dir()
|
|
|
|
|
.arg_root_dir()
|
2022-01-19 10:10:53 -06:00
|
|
|
.arg(
|
2022-08-25 19:13:51 -07:00
|
|
|
Arg::new("chapter")
|
|
|
|
|
.short('c')
|
|
|
|
|
.long("chapter")
|
2022-07-04 23:16:31 +08:00
|
|
|
.value_name("chapter"),
|
|
|
|
|
)
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::new("library-path")
|
|
|
|
|
.short('L')
|
|
|
|
|
.long("library-path")
|
|
|
|
|
.value_name("dir")
|
|
|
|
|
.value_delimiter(',')
|
|
|
|
|
.value_parser(NonEmptyStringValueParser::new())
|
|
|
|
|
.action(ArgAction::Append)
|
2022-08-25 19:13:51 -07:00
|
|
|
.help(
|
2022-07-04 23:16:31 +08:00
|
|
|
"A comma-separated list of directories to add to the crate \
|
|
|
|
|
search path when building tests",
|
|
|
|
|
),
|
2018-08-02 15:48:22 -05:00
|
|
|
)
|
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
|
2022-07-04 23:16:31 +08:00
|
|
|
.get_many("library-path")
|
|
|
|
|
.map(|it| it.map(String::as_str).collect())
|
2018-07-23 12:45:01 -05:00
|
|
|
.unwrap_or_default();
|
2022-07-04 23:16:31 +08:00
|
|
|
|
|
|
|
|
let chapter: Option<&str> = args.get_one::<String>("chapter").map(|s| s.as_str());
|
2022-08-25 19:13:51 -07:00
|
|
|
|
2017-06-26 01:24:33 +02:00
|
|
|
let book_dir = get_book_dir(args);
|
2023-05-13 09:44:11 -07:00
|
|
|
let mut book = MDBook::load(book_dir)?;
|
2017-06-26 01:24:33 +02:00
|
|
|
|
2022-07-04 23:16:31 +08:00
|
|
|
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
|
|
|
|
|
book.config.build.build_dir = dest_dir.to_path_buf();
|
2018-08-02 15:48:22 -05:00
|
|
|
}
|
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(())
|
|
|
|
|
}
|