summaryrefslogtreecommitdiffstats
path: root/vendor/mdbook/src/cmd/test.rs
blob: 3efe130b11ea22ec959f50d3c5534c1d61b8a948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use super::command_prelude::*;
use crate::get_book_dir;
use clap::builder::NonEmptyStringValueParser;
use clap::{Arg, ArgAction, ArgMatches, Command};
use mdbook::errors::Result;
use mdbook::MDBook;
use std::path::PathBuf;

// Create clap subcommand arguments
pub fn make_subcommand() -> Command {
    Command::new("test")
        .about("Tests that a book's Rust code samples compile")
        // FIXME: --dest-dir is unused by the test command, it should be removed
        .arg_dest_dir()
        .arg_root_dir()
        .arg(
            Arg::new("chapter")
                .short('c')
                .long("chapter")
                .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(
                    "A comma-separated list of directories to add to the crate \
                    search path when building tests",
                ),
        )
}

// test command implementation
pub fn execute(args: &ArgMatches) -> Result<()> {
    let library_paths: Vec<&str> = args
        .get_many("library-path")
        .map(|it| it.map(String::as_str).collect())
        .unwrap_or_default();

    let chapter: Option<&str> = args.get_one::<String>("chapter").map(|s| s.as_str());

    let book_dir = get_book_dir(args);
    let mut book = MDBook::load(&book_dir)?;

    if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
        book.config.build.build_dir = dest_dir.to_path_buf();
    }
    match chapter {
        Some(_) => book.test_chapter(library_paths, chapter),
        None => book.test(library_paths),
    }?;

    Ok(())
}