Skip to content

Commit 2420919

Browse files
authored
Merge pull request #2259 from stevecheckoway/improve-test-output
Color test output and shorten chapter paths
2 parents c671c2e + 32687e6 commit 2420919

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/book/mod.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ pub use self::init::BookBuilder;
1515
pub use self::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem};
1616

1717
use log::{debug, error, info, log_enabled, trace, warn};
18-
use std::io::Write;
19-
use std::path::PathBuf;
18+
use std::ffi::OsString;
19+
use std::io::{IsTerminal, Write};
20+
use std::path::{Path, PathBuf};
2021
use std::process::Command;
2122
use tempfile::Builder as TempFileBuilder;
2223
use toml::Value;
@@ -264,10 +265,18 @@ impl MDBook {
264265
/// Run `rustdoc` tests on a specific chapter of the book, linking against the provided libraries.
265266
/// If `chapter` is `None`, all tests will be run.
266267
pub fn test_chapter(&mut self, library_paths: Vec<&str>, chapter: Option<&str>) -> Result<()> {
267-
let library_args: Vec<&str> = (0..library_paths.len())
268-
.map(|_| "-L")
269-
.zip(library_paths.into_iter())
270-
.flat_map(|x| vec![x.0, x.1])
268+
let cwd = std::env::current_dir()?;
269+
let library_args: Vec<OsString> = library_paths
270+
.into_iter()
271+
.flat_map(|path| {
272+
let path = Path::new(path);
273+
let path = if path.is_relative() {
274+
cwd.join(path).into_os_string()
275+
} else {
276+
path.to_path_buf().into_os_string()
277+
};
278+
[OsString::from("-L"), path]
279+
})
271280
.collect();
272281

273282
let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?;
@@ -294,6 +303,7 @@ impl MDBook {
294303
.collect();
295304
let (book, _) = self.preprocess_book(&TestRenderer)?;
296305

306+
let color_output = std::io::stderr().is_terminal();
297307
let mut failed = false;
298308
for item in book.iter() {
299309
if let BookItem::Chapter(ref ch) = *item {
@@ -319,7 +329,10 @@ impl MDBook {
319329
tmpf.write_all(ch.content.as_bytes())?;
320330

321331
let mut cmd = Command::new("rustdoc");
322-
cmd.arg(&path).arg("--test").args(&library_args);
332+
cmd.current_dir(temp_dir.path())
333+
.arg(&chapter_path)
334+
.arg("--test")
335+
.args(&library_args);
323336

324337
if let Some(edition) = self.config.rust.edition {
325338
match edition {
@@ -335,6 +348,10 @@ impl MDBook {
335348
}
336349
}
337350

351+
if color_output {
352+
cmd.args(&["--color", "always"]);
353+
}
354+
338355
debug!("running {:?}", cmd);
339356
let output = cmd.output()?;
340357

0 commit comments

Comments
 (0)