From 4bcc075ad82c12094316c96b7cf9adbfb64d4b0f Mon Sep 17 00:00:00 2001 From: Rick Weber Date: Wed, 23 Feb 2022 14:04:42 -0800 Subject: [PATCH] Add --verbose logging and --extern flags to test subcommand --- src/book/mod.rs | 25 +++++++++++++++++++++++-- src/cmd/test.rs | 27 ++++++++++++++++++++++++++- tests/testing.rs | 4 ++-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 3370d92c..80587e5a 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -245,13 +245,21 @@ impl MDBook { } /// Run `rustdoc` tests on the book, linking against the provided libraries. - pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> { + pub fn test(&mut self, library_paths: Vec<&str>, externs: Vec<&str>, verbose: bool) -> Result<()> { let library_args: Vec<&str> = (0..library_paths.len()) .map(|_| "-L") .zip(library_paths.into_iter()) .flat_map(|x| vec![x.0, x.1]) .collect(); + let extern_args: Vec<&str> = (0..externs.len()) + .map(|_| "--extern") + .zip(externs.into_iter()) + .flat_map(|x| vec![x.0, x.1]) + .collect(); + + let verbose_arg = if verbose { vec!["-v"] } else { vec![] }; + let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?; // FIXME: Is "test" the proper renderer name to use here? @@ -279,7 +287,12 @@ impl MDBook { tmpf.write_all(ch.content.as_bytes())?; let mut cmd = Command::new("rustdoc"); - cmd.arg(&path).arg("--test").args(&library_args); + cmd + .arg(&path) + .arg("--test") + .args(&library_args) + .args(&extern_args) + .args(&verbose_arg); if let Some(edition) = self.config.rust.edition { match edition { @@ -295,6 +308,14 @@ impl MDBook { } } + let command = cmd + .get_args() + .map(|x| x.to_string_lossy().into_owned()) + .collect::>() + .join(" "); + + info!("Running `rustdoc {}`", command); + let output = cmd.output()?; if !output.status.success() { diff --git a/src/cmd/test.rs b/src/cmd/test.rs index 02f982a4..7901a631 100644 --- a/src/cmd/test.rs +++ b/src/cmd/test.rs @@ -3,6 +3,13 @@ use clap::{arg, App, Arg, ArgMatches}; use mdbook::errors::Result; use mdbook::MDBook; +const EXTERN_HELP: &str = "Specify the name and location of an external crate. This argument +gets passed down to rustdoc. To see the set of --extern and --library-path +arguments needed to use a given crate, create a simple example project that +uses said crate and run `cargo doc -v.` + +Example: --extern my_crate=/path/to/crate.rlib"; + // Create clap subcommand arguments pub fn make_subcommand<'help>() -> App<'help> { App::new("test") @@ -33,6 +40,20 @@ pub fn make_subcommand<'help>() -> App<'help> { .multiple_occurrences(true) .forbid_empty_values(true) .help("A comma-separated list of directories to add to {n}the crate search path when building tests")) + .arg(Arg::with_name("extern") + .long("extern") + .value_name("file") + .takes_value(true) + .require_delimiter(false) + .multiple(true) + .empty_values(false) + .help(EXTERN_HELP)) + .arg(Arg::with_name("verbose") + .long("verbose") + .short("v") + .takes_value(false) + .multiple(false) + .help("Enables verbose logging with the test command.")) } // test command implementation @@ -43,12 +64,16 @@ pub fn execute(args: &ArgMatches) -> Result<()> { .unwrap_or_default(); let book_dir = get_book_dir(args); let mut book = MDBook::load(&book_dir)?; + let externs: Vec<&str> = args + .values_of("extern") + .map(std::iter::Iterator::collect) + .unwrap_or_default(); if let Some(dest_dir) = args.value_of("dest-dir") { book.config.build.build_dir = dest_dir.into(); } - book.test(library_paths)?; + book.test(library_paths, externs, args.is_present("verbose"))?; Ok(()) } diff --git a/tests/testing.rs b/tests/testing.rs index 2b2c0fd0..369fec69 100644 --- a/tests/testing.rs +++ b/tests/testing.rs @@ -9,7 +9,7 @@ fn mdbook_can_correctly_test_a_passing_book() { let temp = DummyBook::new().with_passing_test(true).build().unwrap(); let mut md = MDBook::load(temp.path()).unwrap(); - let result = md.test(vec![]); + let result = md.test(vec![], vec![], false); assert!( result.is_ok(), "Tests failed with {}", @@ -22,5 +22,5 @@ fn mdbook_detects_book_with_failing_tests() { let temp = DummyBook::new().with_passing_test(false).build().unwrap(); let mut md = MDBook::load(temp.path()).unwrap(); - assert!(md.test(vec![]).is_err()); + assert!(md.test(vec![], vec![], false).is_err()); }