Debian Codegraph @

src/cmd/test.rs

raw
1 use super::command_prelude::*;
2 use crate::get_book_dir;
3 use anyhow::Result;
4 use clap::ArgAction;
5 use clap::builder::NonEmptyStringValueParser;
6 use mdbook_driver::MDBook;
7
8 // Create clap subcommand arguments
9 pub fn make_subcommand() -> Command {
10 Command::new("test")
11 .about("Tests that a book's Rust code samples compile")
12 .arg_root_dir()
13 .arg(
14 Arg::new("chapter")
15 .short('c')
16 .long("chapter")
17 .value_name("chapter"),
18 )
19 .arg(
20 Arg::new("library-path")
21 .short('L')
22 .long("library-path")
23 .value_name("dir")
24 .value_delimiter(',')
25 .value_parser(NonEmptyStringValueParser::new())
26 .action(ArgAction::Append)
27 .help(
28 "A comma-separated list of directories to add to the crate \
29 search path when building tests",
30 ),
31 )
32 }
33
34 // test command implementation
35 pub fn execute(args: &ArgMatches) -> Result<()> {
36 let library_paths: Vec<&str> = args
37 .get_many("library-path")
38 .map(|it| it.map(String::as_str).collect())
39 .unwrap_or_default();
40
41 let chapter: Option<&str> = args.get_one::<String>("chapter").map(|s| s.as_str());
42
43 let book_dir = get_book_dir(args);
44 let mut book = MDBook::load(book_dir)?;
45
46 match chapter {
47 Some(_) => book.test_chapter(library_paths, chapter),
48 None => book.test(library_paths),
49 }?;
50
51 Ok(())
52 }