use std::{fs, process::Command};
use googletest::prelude::*;
use super::support::{fixture, run, stderr};
#[gtest]
fn generates_markdown_without_node() -> Result<()> {
let directory = fixture()?;
let output = run(directory.path(), &[])?;
verify_true!(output.status.success())?;
let readme = fs::read_to_string(directory.path().join("README.md")).or_fail()?;
verify_eq!(
readme,
concat!(
"<!-- @generated by markupwright -- do not edit by hand. ",
"Edit the source template and run `markupwright`. -->\n\n",
"# fixture\n\n",
"Fixture workspace\n\n",
"This content came from the input template.\n",
)
)
}
#[gtest]
fn workspace_templates_override_builtins_at_runtime() -> Result<()> {
let directory = fixture()?;
let templates = directory.path().join(".markupwright/templates");
fs::create_dir_all(&templates).or_fail()?;
fs::write(
templates.join("readme-root.md.j2"),
"CUSTOM {{ name }}\n\n{% block content %}{% endblock %}\n",
)
.or_fail()?;
verify_true!(run(directory.path(), &[])?.status.success())?;
let readme = fs::read_to_string(directory.path().join("README.md")).or_fail()?;
verify_that!(readme.as_str(), contains_substring("CUSTOM fixture"))?;
verify_that!(
readme.as_str(),
contains_substring("This content came from the input template.")
)
}
#[gtest]
fn custom_runtime_templates_can_be_extended() -> Result<()> {
let directory = fixture()?;
let templates = directory.path().join(".markupwright/templates");
fs::create_dir_all(&templates).or_fail()?;
fs::write(
templates.join("custom.md.j2"),
"# Custom base\n\n{% block content %}{% endblock %}\n",
)
.or_fail()?;
fs::write(
directory.path().join("README.md.in"),
"{% extends \"custom.md.j2\" %}\n{% block content %}Runtime extension.{% endblock %}\n",
)
.or_fail()?;
verify_true!(run(directory.path(), &[])?.status.success())?;
let readme = fs::read_to_string(directory.path().join("README.md")).or_fail()?;
verify_that!(readme.as_str(), contains_substring("# Custom base"))?;
verify_that!(readme.as_str(), contains_substring("Runtime extension."))
}
#[gtest]
fn generated_header_is_a_runtime_template() -> Result<()> {
let directory = fixture()?;
let templates = directory.path().join(".markupwright/templates");
fs::create_dir_all(&templates).or_fail()?;
fs::write(
templates.join("header.md.j2"),
"<!-- generated by the fixture -->\n",
)
.or_fail()?;
verify_true!(run(directory.path(), &[])?.status.success())?;
let readme = fs::read_to_string(directory.path().join("README.md")).or_fail()?;
verify_that!(
readme.as_str(),
starts_with("<!-- generated by the fixture -->")
)?;
verify_that!(
readme.as_str(),
not(contains_substring("@generated by markupwright"))
)
}
#[gtest]
fn normalizes_rendered_markdown_to_lf() -> Result<()> {
let directory = fixture()?;
let templates = directory.path().join(".markupwright/templates");
fs::create_dir_all(&templates).or_fail()?;
fs::write(
templates.join("readme-root.md.j2"),
"# {{ name }}\r\n\r\n{% block content %}{% endblock %}\r\n",
)
.or_fail()?;
verify_true!(run(directory.path(), &[])?.status.success())?;
let readme = fs::read_to_string(directory.path().join("README.md")).or_fail()?;
verify_false!(readme.contains('\r'))
}
#[gtest]
fn explicit_root_argument_selects_the_workspace() -> Result<()> {
let directory = fixture()?;
let output = Command::new(env!("CARGO_BIN_EXE_markupwright"))
.arg("--root")
.arg(directory.path())
.env_remove("MARKUPWRIGHT_ROOT")
.output()
.or_fail()?;
verify_true!(output.status.success())?;
verify_true!(directory.path().join("README.md").is_file())
}
#[gtest]
fn formats_prose_spacing_without_changing_code_fences() -> Result<()> {
let directory = fixture()?;
fs::write(
directory.path().join("README.md.in"),
"alpha\n\n\nbeta\n\n```text\none\n\n\ntwo\n```\n",
)
.or_fail()?;
verify_true!(run(directory.path(), &[])?.status.success())?;
let readme = fs::read_to_string(directory.path().join("README.md")).or_fail()?;
verify_that!(readme.as_str(), contains_substring("alpha\n\nbeta"))?;
verify_that!(readme.as_str(), contains_substring("one\n\n\ntwo"))
}
#[gtest]
fn unknown_helper_arguments_fail_rendering() -> Result<()> {
let directory = fixture()?;
fs::write(
directory.path().join("README.md.in"),
"{{ tree(depth=1, unknown=true) }}\n",
)
.or_fail()?;
let output = run(directory.path(), &[])?;
let stderr = stderr(&output)?;
verify_false!(output.status.success())?;
verify_that!(
stderr.as_str(),
contains_substring("unknown keyword argument")
)
}
#[gtest]
fn non_utf8_runtime_templates_fail_loading() -> Result<()> {
let directory = fixture()?;
let templates = directory.path().join(".markupwright/templates");
fs::create_dir_all(&templates).or_fail()?;
fs::write(templates.join("readme-root.md.j2"), [0xff]).or_fail()?;
let output = run(directory.path(), &[])?;
let stderr = stderr(&output)?;
verify_false!(output.status.success())?;
verify_that!(stderr.as_str(), contains_substring("not UTF-8 text"))
}