-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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_.
- Loading branch information
1 parent
8dce553
commit 9de0522
Showing
1 changed file
with
33 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -26,6 +26,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]>") | ||
|
@@ -42,7 +56,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(); | ||
|
||
|
@@ -106,14 +121,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() | ||
} | ||
|
@@ -125,12 +148,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); | ||
} |