mdbook/src/cmd/test.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.5 KiB
Rust
Raw Normal View History

2022-07-04 23:16:31 +08:00
use super::command_prelude::*;
use crate::get_book_dir;
use anyhow::Result;
2024-02-24 12:04:57 -08:00
use clap::ArgAction;
use clap::builder::NonEmptyStringValueParser;
2025-07-21 21:42:58 -07:00
use mdbook_driver::MDBook;
// 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
.arg_root_dir()
.arg(
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)
.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
)
}
// test command implementation
pub fn execute(args: &ArgMatches) -> Result<()> {
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())
.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());
let book_dir = get_book_dir(args);
2023-05-13 09:44:11 -07:00
let mut book = MDBook::load(book_dir)?;
match chapter {
Some(_) => book.test_chapter(library_paths, chapter),
None => book.test(library_paths),
}?;
Ok(())
}