Skip to content

Commit 43e690d

Browse files
committed
Add debug option to the testsuite
This adds a basic debug method to the testsuite command to help with diagnosing tests that aren't working as expected.
1 parent 4a2d3a7 commit 43e690d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/testsuite/book_test.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ impl BookTest {
227227
expect_status: StatusCode::Success,
228228
expect_stderr_data: None,
229229
expect_stdout_data: None,
230+
debug: None,
230231
};
231232
f(&mut cmd);
232233
cmd.run();
@@ -272,6 +273,7 @@ pub struct BookCommand {
272273
expect_status: StatusCode,
273274
expect_stderr_data: Option<snapbox::Data>,
274275
expect_stdout_data: Option<snapbox::Data>,
276+
debug: Option<String>,
275277
}
276278

277279
impl BookCommand {
@@ -311,6 +313,21 @@ impl BookCommand {
311313
self
312314
}
313315

316+
/// Use this to debug a command.
317+
///
318+
/// Pass the value that you would normally pass to `RUST_LOG`, and this
319+
/// will enable logging, print the command that runs and its output.
320+
///
321+
/// This will fail if you use it in CI.
322+
#[allow(unused)]
323+
pub fn debug(&mut self, value: &str) -> &mut Self {
324+
if std::env::var_os("CI").is_some() {
325+
panic!("debug is not allowed on CI");
326+
}
327+
self.debug = Some(value.into());
328+
self
329+
}
330+
314331
/// Runs the command, and verifies the output.
315332
fn run(&mut self) {
316333
let mut cmd = Command::new(env!("CARGO_BIN_EXE_mdbook"));
@@ -326,13 +343,20 @@ impl BookCommand {
326343
.env_remove("GIT_COMMITTER_EMAIL")
327344
.env_remove("GIT_COMMITTER_NAME");
328345

346+
if let Some(debug) = &self.debug {
347+
cmd.env("RUST_LOG", debug);
348+
}
349+
329350
for (k, v) in &self.env {
330351
match v {
331352
Some(v) => cmd.env(k, v),
332353
None => cmd.env_remove(k),
333354
};
334355
}
335356

357+
if self.debug.is_some() {
358+
eprintln!("running {cmd:#?}");
359+
}
336360
let output = cmd.output().expect("mdbook should be runnable");
337361
let stdout = std::str::from_utf8(&output.stdout).expect("stdout is not utf8");
338362
let stderr = std::str::from_utf8(&output.stderr).expect("stderr is not utf8");
@@ -353,6 +377,9 @@ impl BookCommand {
353377
},
354378
_ => {}
355379
}
380+
if self.debug.is_some() {
381+
eprintln!("{}", render_output());
382+
}
356383
self.expect_status = StatusCode::Success; // Reset to default.
357384
if let Some(expect_stderr_data) = &self.expect_stderr_data {
358385
if let Err(e) = self.assert.try_eq(

0 commit comments

Comments
 (0)