Migrate backends_receive_render_context_via_stdin to BookTest
This commit is contained in:
parent
82000d917f
commit
a38a30da1e
5 changed files with 78 additions and 55 deletions
|
|
@ -4,37 +4,7 @@ use mdbook::config::Config;
|
|||
use mdbook::MDBook;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use tempfile::{Builder as TempFileBuilder, TempDir};
|
||||
|
||||
#[test]
|
||||
fn backends_receive_render_context_via_stdin() {
|
||||
use mdbook::renderer::RenderContext;
|
||||
use std::fs::File;
|
||||
|
||||
let (md, temp) = dummy_book_with_backend("cat-to-file", "renderers/myrenderer", 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());
|
||||
md.build().unwrap();
|
||||
assert!(out_file.exists());
|
||||
|
||||
let got = RenderContext::from_json(File::open(&out_file).unwrap());
|
||||
assert!(got.is_ok());
|
||||
}
|
||||
use tempfile::Builder as TempFileBuilder;
|
||||
|
||||
#[test]
|
||||
fn relative_command_path() {
|
||||
|
|
@ -75,30 +45,6 @@ fn relative_command_path() {
|
|||
do_test("renderers/myrenderer");
|
||||
}
|
||||
|
||||
fn dummy_book_with_backend(
|
||||
name: &str,
|
||||
command: &str,
|
||||
backend_is_optional: bool,
|
||||
) -> (MDBook, TempDir) {
|
||||
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
|
||||
|
||||
let mut config = Config::default();
|
||||
config
|
||||
.set(format!("output.{name}.command"), command)
|
||||
.unwrap();
|
||||
|
||||
if backend_is_optional {
|
||||
config.set(format!("output.{name}.optional"), true).unwrap();
|
||||
}
|
||||
|
||||
let md = MDBook::init(temp.path())
|
||||
.with_config(config)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
(md, temp)
|
||||
}
|
||||
|
||||
fn rust_exe(temp: &Path, name: &str, src: &str) {
|
||||
let rs = temp.join(name).with_extension("rs");
|
||||
fs::write(&rs, src).unwrap();
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
use crate::prelude::*;
|
||||
use mdbook::errors::Result;
|
||||
use mdbook::renderer::{RenderContext, Renderer};
|
||||
use snapbox::IntoData;
|
||||
use std::fs::File;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
struct Spy(Arc<Mutex<Inner>>);
|
||||
|
|
@ -136,3 +138,73 @@ Hello World!
|
|||
"#]]);
|
||||
});
|
||||
}
|
||||
|
||||
// Checks the render context received by the renderer.
|
||||
#[test]
|
||||
fn backends_receive_render_context_via_stdin() {
|
||||
let mut test = BookTest::from_dir("renderer/backends_receive_render_context_via_stdin");
|
||||
test.rust_program(
|
||||
"cat-to-file",
|
||||
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();
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.run("build", |cmd| {
|
||||
cmd.expect_stdout(str![[""]]).expect_stderr(str![[r#"
|
||||
[TIMESTAMP] [INFO] (mdbook::book): Book building has started
|
||||
[TIMESTAMP] [INFO] (mdbook::book): Running the cat-to-file backend
|
||||
[TIMESTAMP] [INFO] (mdbook::renderer): Invoking the "cat-to-file" renderer
|
||||
|
||||
"#]]);
|
||||
})
|
||||
.check_file(
|
||||
"book/out.txt",
|
||||
str![[r##"
|
||||
{
|
||||
"book": {
|
||||
"__non_exhaustive": null,
|
||||
"sections": [
|
||||
{
|
||||
"Chapter": {
|
||||
"content": "# Chapter 1\n",
|
||||
"name": "Chapter 1",
|
||||
"number": [
|
||||
1
|
||||
],
|
||||
"parent_names": [],
|
||||
"path": "chapter_1.md",
|
||||
"source_path": "chapter_1.md",
|
||||
"sub_items": []
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"config": {
|
||||
"book": {
|
||||
"authors": [],
|
||||
"language": "en",
|
||||
"src": "src"
|
||||
},
|
||||
"output": {
|
||||
"cat-to-file": {
|
||||
"command": "./cat-to-file"
|
||||
}
|
||||
}
|
||||
},
|
||||
"destination": "[ROOT]/book",
|
||||
"root": "[ROOT]",
|
||||
"version": "[VERSION]"
|
||||
}
|
||||
"##]]
|
||||
.is_json(),
|
||||
);
|
||||
|
||||
// Can round-trip.
|
||||
let f = File::open(test.dir.join("book/out.txt")).unwrap();
|
||||
RenderContext::from_json(f).unwrap();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
[output.cat-to-file]
|
||||
command = "./cat-to-file"
|
||||
|
||||
|
|
@ -0,0 +1 @@
|
|||
- [Chapter 1](./chapter_1.md)
|
||||
|
|
@ -0,0 +1 @@
|
|||
# Chapter 1
|
||||
Loading…
Add table
Reference in a new issue