Skip to content

Commit ae5d06a

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 obfuscates 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 ``` 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 ae5d06a

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/book/mod.rs

+23-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::{Write, IsTerminal};
20+
use std::path::{PathBuf, Path};
2021
use std::process::Command;
2122
use std::string::ToString;
2223
use tempfile::Builder as TempFileBuilder;
@@ -259,10 +260,17 @@ 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.into_iter()
265+
.flat_map(|path| {
266+
let path = Path::new(path);
267+
let path = if path.is_relative() {
268+
cwd.join(path).into_os_string()
269+
} else {
270+
path.to_path_buf().into_os_string()
271+
};
272+
[OsString::from("-L"), path]
273+
})
266274
.collect();
267275

268276
let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?;
@@ -289,6 +297,7 @@ impl MDBook {
289297
.collect();
290298
let (book, _) = self.preprocess_book(&TestRenderer)?;
291299

300+
let color_output = std::io::stderr().is_terminal();
292301
let mut failed = false;
293302
for item in book.iter() {
294303
if let BookItem::Chapter(ref ch) = *item {
@@ -314,7 +323,10 @@ impl MDBook {
314323
tmpf.write_all(ch.content.as_bytes())?;
315324

316325
let mut cmd = Command::new("rustdoc");
317-
cmd.arg(&path).arg("--test").args(&library_args);
326+
cmd.current_dir(temp_dir.path())
327+
.arg(&chapter_path)
328+
.args(&["--test", "-C", "embed-bitcode=no", "--error-format", "human"])
329+
.args(&library_args);
318330

319331
if let Some(edition) = self.config.rust.edition {
320332
match edition {
@@ -330,6 +342,10 @@ impl MDBook {
330342
}
331343
}
332344

345+
if color_output {
346+
cmd.args(&["--color", "always"]);
347+
}
348+
333349
debug!("running {:?}", cmd);
334350
let output = cmd.output()?;
335351

0 commit comments

Comments
 (0)