Clean up test backends_receive_render_context_via_stdin

This commit is contained in:
Eric Huss 2024-02-05 09:53:50 -08:00
parent 53eccf7047
commit 3387cf373d
1 changed files with 15 additions and 17 deletions

View File

@ -39,29 +39,27 @@ fn alternate_backend_with_arguments() {
md.build().unwrap(); md.build().unwrap();
} }
/// Get a command which will pipe `stdin` to the provided file.
#[cfg(not(windows))]
fn tee_command<P: AsRef<Path>>(out_file: P) -> String {
let out_file = out_file.as_ref();
if cfg!(windows) {
format!("cmd.exe /c \"type > {}\"", out_file.display())
} else {
format!("tee {}", out_file.display())
}
}
#[test] #[test]
#[cfg(not(windows))]
fn backends_receive_render_context_via_stdin() { fn backends_receive_render_context_via_stdin() {
use mdbook::renderer::RenderContext; use mdbook::renderer::RenderContext;
use std::fs::File; use std::fs::File;
let temp = TempFileBuilder::new().prefix("output").tempdir().unwrap(); let (md, temp) = dummy_book_with_backend("cat-to-file", "renderers/myrenderer", false);
let out_file = temp.path().join("out.txt");
let cmd = tee_command(&out_file);
let (md, _temp) = dummy_book_with_backend("cat-to-file", &cmd, false); let renderers = temp.path().join("renderers");
fs::create_dir(&renderers).unwrap();
rust_exe(
&renderers,
"myrenderer",
r#"fn main() {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
std::fs::write("out.txt", s).unwrap();
}"#,
);
let out_file = temp.path().join("book/out.txt");
assert!(!out_file.exists()); assert!(!out_file.exists());
md.build().unwrap(); md.build().unwrap();