Added a round-trip test to make sure parse_input() is always correct
This commit is contained in:
parent
89ea60e7a5
commit
f6a7432569
|
@ -534,7 +534,7 @@ dependencies = [
|
||||||
"shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"staticfile 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"staticfile 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"tempfile 3.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tempfile 3.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"toml-query 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"toml-query 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"walkdir 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"walkdir 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ws 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
"ws 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -1209,7 +1209,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "0.4.6"
|
version = "0.4.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde 1.0.72 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 1.0.72 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -1224,7 +1224,7 @@ dependencies = [
|
||||||
"is-match 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"is-match 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1558,7 +1558,7 @@ dependencies = [
|
||||||
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
|
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
|
||||||
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
|
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
|
||||||
"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b"
|
"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b"
|
||||||
"checksum toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a0263c6c02c4db6c8f7681f9fd35e90de799ebd4cfdeab77a38f4ff6b3d8c0d9"
|
"checksum toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "4a2ecc31b0351ea18b3fe11274b8db6e4d82bce861bbb22e6dbed40417902c65"
|
||||||
"checksum toml-query 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab234a943a2363ad774020e2f9474a38a85bc4396bace01a96380144aef17db3"
|
"checksum toml-query 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab234a943a2363ad774020e2f9474a38a85bc4396bace01a96380144aef17db3"
|
||||||
"checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
|
"checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
|
||||||
"checksum typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887"
|
"checksum typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887"
|
||||||
|
|
|
@ -26,7 +26,7 @@ pulldown-cmark = "0.1.2"
|
||||||
lazy_static = "1.0"
|
lazy_static = "1.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
env_logger = "0.5"
|
env_logger = "0.5"
|
||||||
toml = "0.4"
|
toml = "0.4.8"
|
||||||
memchr = "2.0"
|
memchr = "2.0"
|
||||||
open = "1.1"
|
open = "1.1"
|
||||||
regex = "1.0.0"
|
regex = "1.0.0"
|
||||||
|
|
|
@ -65,9 +65,13 @@ impl CmdPreprocessor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_input<W: Write>(&self, writer: W, book: &Book, ctx: &PreprocessorContext) -> Result<()> {
|
fn write_input<W: Write>(
|
||||||
serde_json::to_writer(writer, &(ctx, book))
|
&self,
|
||||||
.map_err(Into::into)
|
writer: W,
|
||||||
|
book: &Book,
|
||||||
|
ctx: &PreprocessorContext,
|
||||||
|
) -> Result<()> {
|
||||||
|
serde_json::to_writer(writer, &(ctx, book)).map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The command this `Preprocessor` will invoke.
|
/// The command this `Preprocessor` will invoke.
|
||||||
|
@ -105,7 +109,12 @@ impl Preprocessor for CmdPreprocessor {
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::inherit())
|
.stderr(Stdio::inherit())
|
||||||
.spawn()
|
.spawn()
|
||||||
.chain_err(|| format!("Unable to start the \"{}\" preprocessor. Is it installed?", self.name()))?;
|
.chain_err(|| {
|
||||||
|
format!(
|
||||||
|
"Unable to start the \"{}\" preprocessor. Is it installed?",
|
||||||
|
self.name()
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
self.write_input_to_child(&mut child, &book, ctx);
|
self.write_input_to_child(&mut child, &book, ctx);
|
||||||
|
|
||||||
|
@ -114,13 +123,21 @@ impl Preprocessor for CmdPreprocessor {
|
||||||
.chain_err(|| "Error waiting for the preprocessor to complete")?;
|
.chain_err(|| "Error waiting for the preprocessor to complete")?;
|
||||||
|
|
||||||
trace!("{} exited with output: {:?}", self.cmd, output);
|
trace!("{} exited with output: {:?}", self.cmd, output);
|
||||||
ensure!(output.status.success(), "The preprocessor exited unsuccessfully");
|
ensure!(
|
||||||
|
output.status.success(),
|
||||||
|
"The preprocessor exited unsuccessfully"
|
||||||
|
);
|
||||||
|
|
||||||
serde_json::from_slice(&output.stdout).chain_err(|| "Unable to parse the preprocessed book")
|
serde_json::from_slice(&output.stdout)
|
||||||
|
.chain_err(|| "Unable to parse the preprocessed book")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn supports_renderer(&self, renderer: &str) -> bool {
|
fn supports_renderer(&self, renderer: &str) -> bool {
|
||||||
debug!("Checking if the \"{}\" preprocessor supports \"{}\"", self.name(), renderer);
|
debug!(
|
||||||
|
"Checking if the \"{}\" preprocessor supports \"{}\"",
|
||||||
|
self.name(),
|
||||||
|
renderer
|
||||||
|
);
|
||||||
|
|
||||||
let mut cmd = match self.command() {
|
let mut cmd = match self.command() {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
|
@ -152,3 +169,36 @@ impl Preprocessor for CmdPreprocessor {
|
||||||
outcome.unwrap_or(false)
|
outcome.unwrap_or(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::path::Path;
|
||||||
|
use MDBook;
|
||||||
|
|
||||||
|
fn book_example() -> MDBook {
|
||||||
|
let example =
|
||||||
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("book-example");
|
||||||
|
MDBook::load(example).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn round_trip_write_and_parse_input() {
|
||||||
|
let cmd = CmdPreprocessor::new("test".to_string(), "test".to_string());
|
||||||
|
let md = book_example();
|
||||||
|
let ctx = PreprocessorContext::new(
|
||||||
|
md.root.clone(),
|
||||||
|
md.config.clone(),
|
||||||
|
"some-renderer".to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut buffer = Vec::new();
|
||||||
|
cmd.write_input(&mut buffer, &md.book, &ctx).unwrap();
|
||||||
|
|
||||||
|
let (got_ctx, got_book) =
|
||||||
|
CmdPreprocessor::parse_input(buffer.as_slice()).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(got_book, md.book);
|
||||||
|
assert_eq!(got_ctx, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue