Skip to content

Commit f7409e8

Browse files
committed
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_.
1 parent 5e36076 commit f7409e8

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/tools/rustbook/src/main.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ fn main() {
3131
(Defaults to the current directory when omitted)")
3232
.value_parser(clap::value_parser!(PathBuf));
3333

34+
// Note: we don't parse this into a `PathBuf` because it is comma separated
35+
// strings *and* we will ultimately pass it into `MDBook::test()`, which
36+
// accepts `Vec<&str>`. Although it is a bit annoying that `-l/--lang` and
37+
// `-L/--library-path` are so close, this is the same set of arguments we
38+
// would pass when invoking mdbook on the CLI, so making them match when
39+
// invoking rustbook makes for good consistency.
40+
let library_path_arg = arg!(
41+
-L --"library-path" <PATHS>
42+
"A comma-separated list of directories to add to the crate search\n\
43+
path when building tests"
44+
)
45+
.required(false)
46+
.value_parser(parse_library_paths);
47+
3448
let matches = Command::new("rustbook")
3549
.about("Build a book with mdBook")
3650
.author("Steve Klabnik <[email protected]>")
@@ -48,7 +62,8 @@ fn main() {
4862
.subcommand(
4963
Command::new("test")
5064
.about("Tests that a book's Rust code samples compile")
51-
.arg(dir_arg),
65+
.arg(dir_arg)
66+
.arg(library_path_arg),
5267
)
5368
.get_matches();
5469

@@ -113,8 +128,12 @@ pub fn build(args: &ArgMatches) -> Result3<()> {
113128

114129
fn test(args: &ArgMatches) -> Result3<()> {
115130
let book_dir = get_book_dir(args);
131+
let library_paths = args
132+
.try_get_one::<Vec<String>>("library-path")?
133+
.map(|v| v.iter().map(|s| s.as_str()).collect::<Vec<&str>>())
134+
.unwrap_or_default();
116135
let mut book = load_book(&book_dir)?;
117-
book.test(vec![])
136+
book.test(library_paths)
118137
}
119138

120139
fn get_book_dir(args: &ArgMatches) -> PathBuf {
@@ -132,12 +151,16 @@ fn load_book(book_dir: &Path) -> Result3<MDBook> {
132151
Ok(book)
133152
}
134153

154+
fn parse_library_paths(input: &str) -> Result<Vec<String>, String> {
155+
Ok(input.split(",").map(String::from).collect())
156+
}
157+
135158
fn handle_error(error: mdbook::errors::Error) -> ! {
136159
eprintln!("Error: {}", error);
137160

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

142-
::std::process::exit(101);
165+
std::process::exit(101);
143166
}

0 commit comments

Comments
 (0)