Skip to content

Commit 32687e6

Browse files
Color test output and shorten chapter paths
Currently, the output from `rustdoc --test` is not colored because `rustdoc`'s stdout is not a tty. The output of a failed `rustdoc` run is sent to `mdbook`'s stderr via the `error!()` macro. This commit checks if stderr is a tty using the standard `.is_terminal()` and if so, passes `--color always` to `rustdoc`. The test output from `rustdoc` includes the full path that `rustdoc` was called with. This obscures the path of the file with the error. E.g., ``` ---- /var/folders/9v/90bm7kb10fx3_bprxltb3t1r0000gn/T/mdbook-tnGJxp/lab0/index.md - Lab_0__Getting_Started (line 3) stdout ---- error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `code` --> /var/folders/9v/90bm7kb10fx3_bprxltb3t1r0000gn/T/mdbook-tnGJxp/lab0/index.md:4:6 | 3 | this code has a bug | ^^^^ expected one of 8 possible tokens error: aborting due to previous error ``` This commit runs `rustdoc` in the temp directory and replaces any relative library paths with absolute library paths. This leads to simpler error messages. The one above becomes ``` ---- lab0/index.md - Lab_0__Getting_Started (line 3) stdout ---- error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `code` --> lab0/index.md:4:6 | 3 | this code has a bug | ^^^^ expected one of 8 possible tokens error: aborting due to previous error ``` (with colors, of course).
1 parent b7f4621 commit 32687e6

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 std::string::ToString;
2223
use tempfile::Builder as TempFileBuilder;
@@ -259,10 +260,18 @@ impl MDBook {
259260
/// Run `rustdoc` tests on a specific chapter of the book, linking against the provided libraries.
260261
/// If `chapter` is `None`, all tests will be run.
261262
pub fn test_chapter(&mut self, library_paths: Vec<&str>, chapter: Option<&str>) -> Result<()> {
262-
let library_args: Vec<&str> = (0..library_paths.len())
263-
.map(|_| "-L")
264-
.zip(library_paths.into_iter())
265-
.flat_map(|x| vec![x.0, x.1])
263+
let cwd = std::env::current_dir()?;
264+
let library_args: Vec<OsString> = library_paths
265+
.into_iter()
266+
.flat_map(|path| {
267+
let path = Path::new(path);
268+
let path = if path.is_relative() {
269+
cwd.join(path).into_os_string()
270+
} else {
271+
path.to_path_buf().into_os_string()
272+
};
273+
[OsString::from("-L"), path]
274+
})
266275
.collect();
267276

268277
let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?;
@@ -289,6 +298,7 @@ impl MDBook {
289298
.collect();
290299
let (book, _) = self.preprocess_book(&TestRenderer)?;
291300

301+
let color_output = std::io::stderr().is_terminal();
292302
let mut failed = false;
293303
for item in book.iter() {
294304
if let BookItem::Chapter(ref ch) = *item {
@@ -314,7 +324,10 @@ impl MDBook {
314324
tmpf.write_all(ch.content.as_bytes())?;
315325

316326
let mut cmd = Command::new("rustdoc");
317-
cmd.arg(&path).arg("--test").args(&library_args);
327+
cmd.current_dir(temp_dir.path())
328+
.arg(&chapter_path)
329+
.arg("--test")
330+
.args(&library_args);
318331

319332
if let Some(edition) = self.config.rust.edition {
320333
match edition {
@@ -330,6 +343,10 @@ impl MDBook {
330343
}
331344
}
332345

346+
if color_output {
347+
cmd.args(&["--color", "always"]);
348+
}
349+
333350
debug!("running {:?}", cmd);
334351
let output = cmd.output()?;
335352

0 commit comments

Comments
 (0)