Skip to content

Commit 9d4a38c

Browse files
Add option to pass in STDIN data for a BookTest
1 parent f63432c commit 9d4a38c

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

tests/testsuite/book_test.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
33
use mdbook::book::BookBuilder;
44
use mdbook::MDBook;
5-
use snapbox::IntoData;
5+
use snapbox::{cmd::Command, IntoData};
66
use std::collections::BTreeMap;
77
use std::path::{Path, PathBuf};
8-
use std::process::Command;
98
use std::sync::atomic::{AtomicU32, Ordering};
109

1110
/// Test number used for generating unique temp directory names.
@@ -227,6 +226,7 @@ impl BookTest {
227226
expect_status: StatusCode::Success,
228227
expect_stderr_data: None,
229228
expect_stdout_data: None,
229+
stdin_data: None,
230230
};
231231
f(&mut cmd);
232232
cmd.run();
@@ -272,6 +272,7 @@ pub struct BookCommand {
272272
expect_status: StatusCode,
273273
expect_stderr_data: Option<snapbox::Data>,
274274
expect_stdout_data: Option<snapbox::Data>,
275+
stdin_data: Option<snapbox::Data>,
275276
}
276277

277278
impl BookCommand {
@@ -299,6 +300,12 @@ impl BookCommand {
299300
self
300301
}
301302

303+
/// Sets the stdin data to use when running the command.
304+
pub fn stdin(&mut self, input: impl snapbox::IntoData) -> &mut Self {
305+
self.stdin_data = Some(input.into_data());
306+
self
307+
}
308+
302309
/// Adds arguments to the command to run.
303310
pub fn args(&mut self, args: &[&str]) -> &mut Self {
304311
self.args.extend(args.into_iter().map(|t| t.to_string()));
@@ -314,7 +321,8 @@ impl BookCommand {
314321
/// Runs the command, and verifies the output.
315322
fn run(&mut self) {
316323
let mut cmd = Command::new(env!("CARGO_BIN_EXE_mdbook"));
317-
cmd.current_dir(&self.dir)
324+
cmd = cmd
325+
.current_dir(&self.dir)
318326
.args(&self.args)
319327
.env_remove("RUST_LOG")
320328
// Don't read the system git config which is out of our control.
@@ -328,11 +336,15 @@ impl BookCommand {
328336

329337
for (k, v) in &self.env {
330338
match v {
331-
Some(v) => cmd.env(k, v),
332-
None => cmd.env_remove(k),
339+
Some(v) => cmd = cmd.env(k, v),
340+
None => cmd = cmd.env_remove(k),
333341
};
334342
}
335343

344+
if let Some(stdin_data) = &self.stdin_data {
345+
cmd = cmd.stdin(stdin_data);
346+
}
347+
336348
let output = cmd.output().expect("mdbook should be runnable");
337349
let stdout = std::str::from_utf8(&output.stdout).expect("stdout is not utf8");
338350
let stderr = std::str::from_utf8(&output.stderr).expect("stderr is not utf8");

0 commit comments

Comments
 (0)