Add library path argument for mdbook test

This commit is contained in:
messense 2017-06-19 22:06:15 +08:00
parent 4974d2cfa1
commit 7c023e2d1d
No known key found for this signature in database
GPG key ID: BB41A8A2C716CCA9
2 changed files with 11 additions and 4 deletions

View file

@ -5,15 +5,18 @@ use get_book_dir;
// Create clap subcommand arguments
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("test").about("Test that code samples compile")
SubCommand::with_name("test")
.about("Test that code samples compile")
.arg_from_usage("-L, --library-path [DIR]... 'directory to add to crate search path'")
}
// test command implementation
pub fn execute(args: &ArgMatches) -> Result<()> {
let library_paths: Vec<&str> = args.values_of("library-path").map(|v| v.collect()).unwrap_or_default();
let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir).read_config()?;
book.test()?;
book.test(library_paths)?;
Ok(())
}

View file

@ -351,9 +351,13 @@ impl MDBook {
self
}
pub fn test(&mut self) -> Result<()> {
pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> {
// read in the chapters
self.parse_summary().chain_err(|| "Couldn't parse summary")?;
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();
for item in self.iter() {
if let BookItem::Chapter(_, ref ch) = *item {
@ -363,7 +367,7 @@ impl MDBook {
println!("[*]: Testing file: {:?}", path);
let output = Command::new("rustdoc").arg(&path).arg("--test").output()?;
let output = Command::new("rustdoc").arg(&path).arg("--test").args(&library_args).output()?;
if !output.status.success() {
bail!(ErrorKind::Subprocess("Rustdoc returned an error".to_string(), output));