markupwright 0.1.2

Template-driven workspace documentation generator
Documentation
use std::{fs, process::Command};

use googletest::prelude::*;

use super::fixtures::{preview, run, stderr, workspace};

#[gtest]
fn generates_markdown_without_node() -> Result<()> {
    let directory = workspace()?;
    let output = run(directory.path(), &[])?;

    verify_true!(output.status.success())?;
    let readme = fs::read_to_string(directory.path().join("README.md")).or_fail()?;

    let expected = format!(
        concat!(
            "<!-- @generated by markupwright v{} -- 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",
        ),
        env!("CARGO_PKG_VERSION")
    );

    verify_eq!(readme, expected)
}

#[gtest]
fn workspace_templates_override_builtins_at_runtime() -> Result<()> {
    let directory = workspace()?;
    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 workspace_base_template_customizes_domain_templates() -> Result<()> {
    let directory = preview().or_fail()?;
    let templates = directory.path().join(".markupwright/templates");

    fs::create_dir_all(&templates).or_fail()?;
    fs::write(
        templates.join("readme-base.md.j2"),
        "# Branded {{ name }}\n",
    )
    .or_fail()?;
    fs::write(
        templates.join("instructions-base.md.j2"),
        "# Agent notes for {{ name }}\n",
    )
    .or_fail()?;

    verify_true!(run(directory.path(), &[])?.status.success())?;
    let cargo_readme =
        fs::read_to_string(directory.path().join("crates/core/README.md")).or_fail()?;
    let node_instructions =
        fs::read_to_string(directory.path().join("apps/web/CLAUDE.md")).or_fail()?;

    verify_that!(
        cargo_readme.as_str(),
        contains_substring("# Branded preview-core")
    )?;
    verify_that!(
        cargo_readme.as_str(),
        contains_substring("## Cargo features")
    )?;
    verify_that!(
        cargo_readme.as_str(),
        contains_substring("The core crate holds the types")
    )?;
    verify_that!(
        node_instructions.as_str(),
        contains_substring("# Agent notes for preview-web")
    )?;
    verify_that!(node_instructions.as_str(), contains_substring("## Scripts"))?;
    verify_that!(
        node_instructions.as_str(),
        contains_substring("Use server components by default")
    )
}

#[gtest]
fn custom_runtime_templates_can_be_extended() -> Result<()> {
    let directory = workspace()?;
    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 = workspace()?;
    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 = workspace()?;
    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 = workspace()?;
    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 = workspace()?;

    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 = workspace()?;

    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 = workspace()?;
    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"))
}