summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Huss <eric@huss.org>2024-05-10 18:14:32 +0000
committerGitHub <noreply@github.com>2024-05-10 18:14:32 +0000
commit2420919ca8b30b6d139760ebba5743a33fa3add0 (patch)
tree36302c0b80370df3cd1985bee18fa4b4ccc6eada
parentc671c2e90486b6ec72921a5043dac1c1c5565f76 (diff)
parent32687e64fe61da693cf4e3d72fa586317aba4b22 (diff)
Merge pull request #2259 from stevecheckoway/improve-test-output
Color test output and shorten chapter paths
-rw-r--r--src/book/mod.rs31
1 files changed, 24 insertions, 7 deletions
diff --git a/src/book/mod.rs b/src/book/mod.rs
index c0ab8a54..6adc02ec 100644
--- a/src/book/mod.rs
+++ b/src/book/mod.rs
@@ -15,8 +15,9 @@ pub use self::init::BookBuilder;
pub use self::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem};
use log::{debug, error, info, log_enabled, trace, warn};
-use std::io::Write;
-use std::path::PathBuf;
+use std::ffi::OsString;
+use std::io::{IsTerminal, Write};
+use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::Builder as TempFileBuilder;
use toml::Value;
@@ -264,10 +265,18 @@ impl MDBook {
/// Run `rustdoc` tests on a specific chapter of the book, linking against the provided libraries.
/// If `chapter` is `None`, all tests will be run.
pub fn test_chapter(&mut self, library_paths: Vec<&str>, chapter: Option<&str>) -> Result<()> {
- let library_args: Vec<&str> = (0..library_paths.len())
- .map(|_| "-L")
- .zip(library_paths.into_iter())
- .flat_map(|x| vec![x.0, x.1])
+ let cwd = std::env::current_dir()?;
+ let library_args: Vec<OsString> = library_paths
+ .into_iter()
+ .flat_map(|path| {
+ let path = Path::new(path);
+ let path = if path.is_relative() {
+ cwd.join(path).into_os_string()
+ } else {
+ path.to_path_buf().into_os_string()
+ };
+ [OsString::from("-L"), path]
+ })
.collect();
let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?;
@@ -294,6 +303,7 @@ impl MDBook {
.collect();
let (book, _) = self.preprocess_book(&TestRenderer)?;
+ let color_output = std::io::stderr().is_terminal();
let mut failed = false;
for item in book.iter() {
if let BookItem::Chapter(ref ch) = *item {
@@ -319,7 +329,10 @@ impl MDBook {
tmpf.write_all(ch.content.as_bytes())?;
let mut cmd = Command::new("rustdoc");
- cmd.arg(&path).arg("--test").args(&library_args);
+ cmd.current_dir(temp_dir.path())
+ .arg(&chapter_path)
+ .arg("--test")
+ .args(&library_args);
if let Some(edition) = self.config.rust.edition {
match edition {
@@ -335,6 +348,10 @@ impl MDBook {
}
}
+ if color_output {
+ cmd.args(&["--color", "always"]);
+ }
+
debug!("running {:?}", cmd);
let output = cmd.output()?;