diff options
| author | Chris Krycho <hello@chriskrycho.com> | 2024-10-18 12:49:00 -0600 |
|---|---|---|
| committer | Chris Krycho <hello@chriskrycho.com> | 2024-11-23 08:57:24 -0700 |
| commit | f7409e8afb119e6cbb040b5e9a7cb07796638fc2 (patch) | |
| tree | dcd6eadd35ca731d1ba86a96f9401e88ed5ec28e | |
| parent | 5e3607626da6a9ddb41c626bd1c1b1ade69bb342 (diff) | |
| download | rust-f7409e8afb119e6cbb040b5e9a7cb07796638fc2.tar.gz rust-f7409e8afb119e6cbb040b5e9a7cb07796638fc2.zip | |
Add support for `--library-path` to `rustbook test`
This makes it possible to test an mdbook which has dependencies other than the direct crate for the book itself, e.g. the `trpl` crate used in _The Rust Programming Language_.
| -rw-r--r-- | src/tools/rustbook/src/main.rs | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/tools/rustbook/src/main.rs b/src/tools/rustbook/src/main.rs index f905b9277ff..dc89362538b 100644 --- a/src/tools/rustbook/src/main.rs +++ b/src/tools/rustbook/src/main.rs @@ -31,6 +31,20 @@ fn main() { (Defaults to the current directory when omitted)") .value_parser(clap::value_parser!(PathBuf)); + // Note: we don't parse this into a `PathBuf` because it is comma separated + // strings *and* we will ultimately pass it into `MDBook::test()`, which + // accepts `Vec<&str>`. Although it is a bit annoying that `-l/--lang` and + // `-L/--library-path` are so close, this is the same set of arguments we + // would pass when invoking mdbook on the CLI, so making them match when + // invoking rustbook makes for good consistency. + let library_path_arg = arg!( + -L --"library-path" <PATHS> + "A comma-separated list of directories to add to the crate search\n\ + path when building tests" + ) + .required(false) + .value_parser(parse_library_paths); + let matches = Command::new("rustbook") .about("Build a book with mdBook") .author("Steve Klabnik <steve@steveklabnik.com>") @@ -48,7 +62,8 @@ fn main() { .subcommand( Command::new("test") .about("Tests that a book's Rust code samples compile") - .arg(dir_arg), + .arg(dir_arg) + .arg(library_path_arg), ) .get_matches(); @@ -113,8 +128,12 @@ pub fn build(args: &ArgMatches) -> Result3<()> { fn test(args: &ArgMatches) -> Result3<()> { let book_dir = get_book_dir(args); + let library_paths = args + .try_get_one::<Vec<String>>("library-path")? + .map(|v| v.iter().map(|s| s.as_str()).collect::<Vec<&str>>()) + .unwrap_or_default(); let mut book = load_book(&book_dir)?; - book.test(vec![]) + book.test(library_paths) } fn get_book_dir(args: &ArgMatches) -> PathBuf { @@ -132,6 +151,10 @@ fn load_book(book_dir: &Path) -> Result3<MDBook> { Ok(book) } +fn parse_library_paths(input: &str) -> Result<Vec<String>, String> { + Ok(input.split(",").map(String::from).collect()) +} + fn handle_error(error: mdbook::errors::Error) -> ! { eprintln!("Error: {}", error); @@ -139,5 +162,5 @@ fn handle_error(error: mdbook::errors::Error) -> ! { eprintln!("\tCaused By: {}", cause); } - ::std::process::exit(101); + std::process::exit(101); } |
