mdBook/tests/alternative_backends.rs

115 lines
2.7 KiB
Rust
Raw Normal View History

//! Integration tests to make sure alternative backends work.
use mdbook::config::Config;
use mdbook::MDBook;
2018-07-23 17:45:01 +00:00
#[cfg(not(windows))]
use std::path::Path;
use tempfile::{Builder as TempFileBuilder, TempDir};
#[test]
fn passing_alternate_backend() {
2019-12-31 10:16:59 +00:00
let (md, _temp) = dummy_book_with_backend("passing", success_cmd(), false);
md.build().unwrap();
}
#[test]
fn failing_alternate_backend() {
2019-12-31 10:16:59 +00:00
let (md, _temp) = dummy_book_with_backend("failing", fail_cmd(), false);
md.build().unwrap_err();
}
2018-01-24 17:15:29 +00:00
#[test]
2019-12-31 10:16:59 +00:00
fn missing_backends_are_fatal() {
let (md, _temp) = dummy_book_with_backend("missing", "trduyvbhijnorgevfuhn", false);
assert!(md.build().is_err());
}
2018-01-24 17:15:29 +00:00
2019-12-31 10:16:59 +00:00
#[test]
fn missing_optional_backends_are_not_fatal() {
let (md, _temp) = dummy_book_with_backend("missing", "trduyvbhijnorgevfuhn", true);
2018-01-24 17:15:29 +00:00
assert!(md.build().is_ok());
}
#[test]
fn alternate_backend_with_arguments() {
2019-12-31 10:16:59 +00:00
let (md, _temp) = dummy_book_with_backend("arguments", "echo Hello World!", false);
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]
#[cfg(not(windows))]
fn backends_receive_render_context_via_stdin() {
use mdbook::renderer::RenderContext;
2018-07-23 17:45:01 +00:00
use std::fs::File;
let temp = TempFileBuilder::new().prefix("output").tempdir().unwrap();
let out_file = temp.path().join("out.txt");
let cmd = tee_command(&out_file);
2019-12-31 10:16:59 +00:00
let (md, _temp) = dummy_book_with_backend("cat-to-file", &cmd, false);
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());
}
2019-12-31 10:16:59 +00:00
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.{}.command", name), command)
.unwrap();
2019-12-31 10:16:59 +00:00
if backend_is_optional {
config
.set(format!("output.{}.optional", name), true)
.unwrap();
}
let md = MDBook::init(temp.path())
.with_config(config)
.build()
.unwrap();
(md, temp)
}
fn fail_cmd() -> &'static str {
if cfg!(windows) {
r#"cmd.exe /c "exit 1""#
} else {
"false"
}
}
fn success_cmd() -> &'static str {
if cfg!(windows) {
r#"cmd.exe /c "exit 0""#
} else {
"true"
}
}