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.setup_css

Requirements index · View definition

evidence Valid

Requirement

habeas setup MUST preserve existing entries in output.html.additional-css while adding the Habeas stylesheet at most once. It MUST report an error if additional-css is not an array.

Rationale

The tests cover all three obligations in requirements.cli.setup_css.

  1. Preserve existing entries. The fixture helper in tests/setup.rs writes a book.toml with additional-css = ["md/custom.css"]. setup_is_repeatable runs habeas setup and then asserts that the resulting book.toml still contains md/custom.css. It also asserts that md/habeas.css was added.
  2. Add the stylesheet at most once. setup_is_repeatable runs setup twice. It then asserts book.matches("md/habeas.css").count() == 1. The second run starts with the stylesheet already present, so this is the case where a duplicate could appear.
  3. Error when additional-css is not an array. setup_rejects_invalid_book_configuration includes a case with additional-css = "wrong". It asserts that the command fails and that stderr contains output.html.additional-css must be an array.

Both tests carry {test} references to the requirement.

I found no further obligation or boundary in the requirement text that needs its own test.

Inputs

Tests

Input 2: 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}");
}
Input 3: outline tests/setup.rs fn[setup_rejects_invalid_book_configuration]
// {test}`requirements.cli.setup_project`
// {test}`requirements.cli.setup_css`
// {test}`requirements.cli.setup`
#[test]
fn setup_rejects_invalid_book_configuration() {
    let temp = fixture();
    let project = temp.path();
    let binary = env!("CARGO_BIN_EXE_habeas");

    for (book, expected) in [
        ("book = 'wrong'\n", "book must be a table"),
        ("[book]\nsrc = 7\n", "book.src must be a string"),
        ("[book]\nsrc = ''\n", "book.src must be a directory"),
        ("[book]\nsrc = '/outside'\n", "book.src must be a directory"),
        (
            "[book]\nsrc = \"../outside\"\n",
            "book.src must be a directory",
        ),
        (
            "[book]\nsrc = 'md/../outside'\n",
            "book.src must be a directory",
        ),
        (
            "[book]\nsrc = \"md\"\n\n[output.html]\nadditional-css = \"wrong\"\n",
            "output.html.additional-css must be an array",
        ),
        (
            "[book]\nsrc = \"md\"\n\n[output.html]\nfold = true\n",
            "output.html.fold must be a table",
        ),
    ] {
        fs::write(project.join("book.toml"), book).unwrap();
        let output = Command::new(binary)
            .args(["setup", "--path", env!("CARGO_MANIFEST_DIR")])
            .current_dir(project)
            .env("CARGO_NET_OFFLINE", "true")
            .output()
            .unwrap();
        assert!(!output.status.success());
        assert!(
            String::from_utf8_lossy(&output.stderr).contains(expected),
            "{:?}: {}",
            book,
            String::from_utf8_lossy(&output.stderr)
        );
    }

    fs::write(project.join("book.toml"), "[book]\nsrc = \"md\"\n").unwrap();
    fs::remove_file(project.join("md/SUMMARY.md")).unwrap();
    let output = Command::new(binary)
        .args(["setup", "--path", env!("CARGO_MANIFEST_DIR")])
        .current_dir(project)
        .env("CARGO_NET_OFFLINE", "true")
        .output()
        .unwrap();
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("cannot find mdBook SUMMARY.md"));
}

Other sources

Input 1: requirements selection requirements.cli.setup_css
:::{req} setup_css
`habeas setup` MUST preserve existing entries in `output.html.additional-css` while adding the Habeas stylesheet at most once. It MUST report an error if `additional-css` is not an array.
:::
Input 4: outline tests/setup.rs fn[fixture]
// {cite}`habeas:util`
fn fixture() -> TempDir {
    let temp = TempDir::new().unwrap();
    fs::create_dir_all(temp.path().join("src")).unwrap();
    fs::create_dir_all(temp.path().join("md")).unwrap();
    fs::write(
        temp.path().join("Cargo.toml"),
        "[package]\nname = \"sample-project\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
    )
    .unwrap();
    fs::write(temp.path().join("src/main.rs"), "fn main() {}\n").unwrap();
    fs::write(
        temp.path().join("book.toml"),
        "[book]\nsrc = \"md\"\n\n[output.html]\nadditional-css = [\"md/custom.css\"]\n",
    )
    .unwrap();
    fs::write(temp.path().join("md/SUMMARY.md"), "# Summary\n").unwrap();
    temp
}