Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

requirements.cli.mdbook

Requirements index · View definition

evidence Valid

Requirement

The mdbook subcommand MUST act as the mdBook preprocessor for HTML books, rendering requirement definitions and linked reference badges. This project’s book.toml MUST configure cargo habeas mdbook as the habeas preprocessor, using a repository-local Cargo alias for cargo run --release -p habeas --. Consumer books MUST continue to invoke the installed habeas mdbook CLI directly.

Rationale

The tests cover each obligation in requirements.cli.mdbook.

1. The mdbook subcommand acts as the preprocessor, rendering definitions and linked badges.

  • tests/repository.rs#fn[mdbook_preprocessor_renders_definitions_between_stdin_and_stdout] runs the real habeas mdbook binary.
  • It sends a serialized (PreprocessorContext, Book) pair on stdin, using the html renderer. It then parses the book from stdout and checks that the process succeeded.
  • It asserts that the :::{req} directive is gone from the rendered chapter. It also asserts that shields.io badges are present and that exactly two definition badge rows are rendered, each with an image, a detail icon and two links.
  • The {cite} reference to rules.ready becomes a link to the in-page anchor #habeas-req-rules.ready and to the detail page requirements-index/rules.ready.html.
  • The test also checks the generated “Requirements index” chapter and its sub-chapters.

2. This project’s book.toml uses cargo habeas mdbook through a repository-local alias.

  • tests/repository.rs#fn[project_book_uses_the_repository_local_preprocessor_alias] parses the repository’s book.toml. It asserts preprocessor.habeas.command == "cargo habeas mdbook" and renderers == ["html"].
  • It parses .cargo/config.toml and asserts alias.habeas == "run --release -p habeas --". That is the exact alias the text requires.

3. Consumer books keep invoking the installed habeas mdbook directly.

  • tests/setup.rs#fn[setup_is_repeatable] runs setup on a fixture project, twice. It asserts the generated book.toml contains command = "habeas mdbook", so consumer books do not use the cargo alias.

Gaps considered. I found none.

  • The text does not specify behavior for non-HTML renderers, so no test is required for them.
  • Badge details beyond the linked cite are governed by other requirements.

Inputs

Tests

Input 2: outline tests/repository.rs fn[mdbook_preprocessor_renders_definitions_between_stdin_and_stdout]
  • File: tests/repository.rs
  • Selector: fn[mdbook_preprocessor_renders_definitions_between_stdin_and_stdout]
  • Kind: fn
  • tests/repository.rs:160–260
// {test}`requirements.cli.mdbook`
// {test}`requirements.index.chapter.default`
// {test}`requirements.index.detail_pages`
// {test}`requirements.index.navigation`
#[test]
fn mdbook_preprocessor_renders_definitions_between_stdin_and_stdout() {
    let temp = TempDir::new().unwrap();
    let root = temp.path();
    fs::create_dir_all(root.join("md")).unwrap();
    fs::write(root.join("Cargo.toml"), "[package]\nname = \"fixture\"\n").unwrap();
    fs::write(root.join("book.toml"), "[book]\nsrc = \"md\"\n").unwrap();
    let content =
        ":::{req} ready\nReady MUST work.\n:::\n\n:::{req} cites\nSee {cite}`rules.ready`.\n:::\n"
            .to_owned();
    fs::write(root.join("md/rules.md"), &content).unwrap();
    let mut book = Book::new();
    book.push_item(Chapter::new("Rules", content, "rules.md", Vec::new()));
    let context = PreprocessorContext::new(root.to_path_buf(), Config::default(), "html".into());

    let mut child = Command::new(env!("CARGO_BIN_EXE_habeas"))
        .arg("mdbook")
        .current_dir(root)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    child
        .stdin
        .take()
        .unwrap()
        .write_all(&serde_json::to_vec(&(&context, &book)).unwrap())
        .unwrap();
    let output = child.wait_with_output().unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let rendered: Book = serde_json::from_slice(&output.stdout).unwrap();
    let BookItem::Chapter(chapter) = &rendered.items[0] else {
        panic!("{rendered:?}");
    };
    assert!(!chapter.content.contains(":::{req}"), "{}", chapter.content);
    assert!(
        chapter.content.contains("https://img.shields.io/badge/"),
        "{}",
        chapter.content
    );
    assert!(
        chapter.content.contains("cite") && chapter.content.contains("rules.ready"),
        "{}",
        chapter.content
    );
    let badge_rows = chapter
        .content
        .split("<span class=\"habeas-definition-badge\">")
        .skip(1)
        .map(|row| row.split("</span>").next().unwrap())
        .collect::<Vec<_>>();
    assert_eq!(badge_rows.len(), 2, "{}", chapter.content);
    for row in badge_rows {
        assert!(row.contains("<img "), "{row}");
        assert!(row.contains("class=\"habeas-detail-icon\""), "{row}");
        assert_eq!(row.matches("</a>").count(), 2, "{row}");
    }
    assert!(chapter.sub_items.is_empty());
    assert!(
        chapter.content.contains("href=\"#habeas-req-rules.ready\""),
        "{}",
        chapter.content
    );
    assert!(
        chapter
            .content
            .contains("href=\"requirements-index/rules.ready.html\""),
        "{}",
        chapter.content
    );
    assert_eq!(rendered.items.len(), 2);
    let BookItem::Chapter(index) = &rendered.items[1] else {
        panic!("{rendered:?}");
    };
    assert_eq!(index.name, "Requirements index");
    assert_eq!(
        index.path.as_deref(),
        Some(Path::new("requirements-index.md"))
    );
    assert!(index.source_path.is_none());
    assert_eq!(
        index
            .sub_items
            .iter()
            .filter_map(|item| match item {
                BookItem::Chapter(detail) => Some(detail.name.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>(),
        ["Coverage gaps", "rules.ready", "rules.cites"]
    );
}
Input 3: outline tests/repository.rs fn[project_book_uses_the_repository_local_preprocessor_alias]
// {test}`requirements.cli.mdbook`
// {test}`requirements.layout.project`
#[test]
fn project_book_uses_the_repository_local_preprocessor_alias() {
    let book = repository_toml("book.toml");
    assert_eq!(book["book"]["src"].as_str(), Some("md"));
    let preprocessor = &book["preprocessor"]["habeas"];
    assert_eq!(
        preprocessor["command"].as_str(),
        Some("cargo habeas mdbook")
    );
    assert_eq!(
        preprocessor["renderers"],
        toml::Value::Array(vec!["html".into()])
    );
    let cargo = repository_toml(".cargo/config.toml");
    assert_eq!(
        cargo["alias"]["habeas"].as_str(),
        Some("run --release -p habeas --")
    );

    let root = Path::new(env!("CARGO_MANIFEST_DIR"));
    assert!(root.join("md/SUMMARY.md").is_file());
    assert!(root.join("src/lib.rs").is_file());
    assert!(root.join("src/main.rs").is_file());
}
Input 4: outline tests/setup.rs fn[setup_is_repeatable]
// {test}`requirements.cli.setup`
// {test}`requirements.cli.setup_css`
// {test}`requirements.cli.setup_local_path`
// {test}`requirements.cli.mdbook`
// {test}`requirements.index.detail_pages`
#[test]
fn setup_is_repeatable() {
    let temp = fixture();
    let project = temp.path();
    let local_crate = env!("CARGO_MANIFEST_DIR");

    run(project, &["setup", "--path", local_crate]);
    assert!(!project.join(".agents/skills").exists());
    run(project, &["setup", "--path", local_crate]);

    let cargo = fs::read_to_string(project.join("Cargo.toml")).unwrap();
    assert!(cargo.contains("[dev-dependencies]"));
    assert!(cargo.contains("habeas"));
    assert!(cargo.contains("path = "));
    let book = fs::read_to_string(project.join("book.toml")).unwrap();
    assert!(book.contains("command = \"habeas mdbook\""));
    assert!(book.contains("md/habeas.css"));
    assert!(book.contains("md/custom.css"));
    assert_eq!(book.matches("md/habeas.css").count(), 1);
    assert_eq!(
        fs::read_to_string(project.join("tests/habeas.rs")).unwrap(),
        "#[test]\nfn habeas() {\n    habeas::validate();\n}\n"
    );
    assert!(project.join("md/habeas.css").is_file());
    assert!(!project.join(".agents/skills").exists());
    let book: toml_edit::DocumentMut = book.parse().unwrap();
    assert_eq!(
        book["output"]["html"]["fold"]["enable"].as_bool(),
        Some(true)
    );
    assert_eq!(book.to_string().matches("enable").count(), 1, "{book}");
}

Other sources

Input 1: requirements selection requirements.cli.mdbook
:::{req} mdbook
The `mdbook` subcommand MUST act as the mdBook preprocessor for HTML books, rendering requirement definitions and linked reference badges. This project's `book.toml` MUST configure `cargo habeas mdbook` as the `habeas` preprocessor, using a repository-local Cargo alias for `cargo run --release -p habeas --`. Consumer books MUST continue to invoke the installed `habeas mdbook` CLI directly.
:::