Skip to content

Commit

Permalink
Add support for --library-path to rustbook test
Browse files Browse the repository at this point in the history
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_.
  • Loading branch information
chriskrycho committed Oct 26, 2024
1 parent 2dc8145 commit 907b0fe
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/tools/rustbook/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::env;
use std::path::{Path, PathBuf};

use clap::{ArgMatches, Command, arg, crate_version};
use mdbook::MDBook;
use clap::{arg, crate_version, ArgMatches, Command};
use mdbook::errors::Result as Result3;
use mdbook::MDBook;
use mdbook_i18n_helpers::preprocessors::Gettext;
use mdbook_spec::Spec;
use mdbook_trpl_listing::TrplListing;
Expand Down Expand Up @@ -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"
"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 <[email protected]>")
Expand All @@ -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();

Expand Down Expand Up @@ -113,14 +128,22 @@ 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 {
if let Some(p) = args.get_one::<PathBuf>("dir") {
// Check if path is relative from current dir, or absolute...
if p.is_relative() { env::current_dir().unwrap().join(p) } else { p.to_path_buf() }
if p.is_relative() {
env::current_dir().unwrap().join(p)
} else {
p.to_path_buf()
}
} else {
env::current_dir().unwrap()
}
Expand All @@ -132,12 +155,16 @@ 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);

for cause in error.chain().skip(1) {
eprintln!("\tCaused By: {}", cause);
}

::std::process::exit(101);
std::process::exit(101);
}

0 comments on commit 907b0fe

Please sign in to comment.