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.test-coverage.index.render_only

Requirements index · View definition

evidence Valid

Requirement

Building the book MUST NOT run tests or collect coverage.

Rationale

The requirement requirements.test-coverage.index.render_only has one obligation: building the book must not run tests or collect coverage.

Main test: tests/execution_coverage.rs#fn[book_rendering_preserves_retained_coverage_and_never_launches_tests] is marked {test} for this requirement. It runs the real habeas mdbook preprocessor end to end.

  • Setup: it first captures real coverage with a filtered run. The captured test second is modified to write a sentinel file, and the test asserts the sentinel exists. This proves the sentinel really detects a test being executed. The sentinel is then deleted and the retained files are snapshotted.
  • Cargo wrapper: a fake cargo is placed first on PATH. It logs every call. It records any non-metadata subcommand in a “forbidden” file and exits 99. It forwards metadata calls to the real cargo.
  • Rendering step: the preprocessor is fed a book containing a {coverage-index} chapter.
  • Assertions after rendering:
    • The preprocessor succeeds and produces the “Coverage gaps” chapter with “Unresolved coverage gaps”. This shows the render path really does the coverage-index work.
    • Every logged cargo call starts with metadata .
    • The forbidden file does not exist, so no test or coverage command was launched through cargo.
    • The sentinel does not exist, so no captured test ran.
    • retained_files(&storage) equals the pre-render snapshot, so no coverage was collected or rewritten.
  • Standalone check: the same assertions are applied to coverage-check. That check covers a related path but is not required by this subject.

Unit test: src/execution_coverage/book.rs#mod[tests]/fn[filtered_runs_union_latest_attempts_and_invalidate_with_saved_source] is also marked {test} for this requirement. It calls populate on retained data. It shows that rendering works purely from saved attempts: it handles failed, interrupted, stale and other-build cases without launching anything.

Possible gap, judged acceptable: the wrapper only intercepts invocations of cargo. A render that launched a test through some other route would not be caught by the wrapper. The sentinel check would still catch it if that route ran the second test. The retained-files equality check would catch it if it collected new coverage. Together these cover both halves of the obligation: running tests, and collecting coverage. The requirement has no other modes or boundary cases.

Inputs

Tests

Input 2: outline src/execution_coverage/book.rs mod[tests]/fn[filtered_runs_union_latest_attempts_and_invalidate_with_saved_source]
    // {test}`requirements.test-coverage.index`
    // {test}`requirements.test-coverage.processes.reports`
    // {test}`requirements.test-coverage.storage.configuration`
    // {test}`requirements.test-coverage.index.selection`
    // {test}`requirements.test-coverage.index.summary`
    // {test}`requirements.test-coverage.index.details`
    // {test}`requirements.test-coverage.index.freshness`
    // {test}`requirements.test-coverage.index.render_only`
    // {test}`requirements.test-coverage.index.file_map.combined.measurement`
    #[test]
    fn filtered_runs_union_latest_attempts_and_invalidate_with_saved_source() {
        let (temp, book) = fixture();
        let root = temp.path();
        record(root, "one", 1, "build-a", &[(1, 1), (2, 0)]);
        record(root, "two", 2, "build-a", &[(1, 0), (2, 1)]);
        let mut combined = book.clone();
        populate(&mut combined, root).unwrap();
        assert!(
            combined
                .chapters()
                .next()
                .unwrap()
                .content
                .contains("2/2 (100.0%)")
        );
        assert!(
            combined
                .chapters()
                .next()
                .unwrap()
                .content
                .contains("2 selected test attempts")
        );
        assert!(
            !combined
                .chapters()
                .next()
                .unwrap()
                .content
                .contains("{coverage-index}")
        );
        // A failing newest attempt replaces the earlier success, even with no data.
        let newest = record(root, "one", 3, "build-a", &[]);
        let mut attempt: Attempt = load(&newest.join("attempt.json")).unwrap();
        attempt.diagnostics.push("profile missing".into());
        for outcome in ["failed", "interrupted"] {
            attempt.outcome = outcome.into();
            save(&newest.join("attempt.json"), &attempt).unwrap();
            let mut combined = book.clone();
            populate(&mut combined, root).unwrap();
            assert!(
                combined
                    .chapters()
                    .next()
                    .unwrap()
                    .content
                    .contains("gap counts unavailable")
            );
            assert!(
                combined
                    .chapters()
                    .next()
                    .unwrap()
                    .content
                    .contains("missing or unreadable coverage")
            );
            assert!(
                combined
                    .chapters()
                    .next()
                    .unwrap()
                    .content
                    .contains("2 selected test attempts")
            );
        }
        fs::write(root.join("code.rs"), "changed source\n").unwrap();
        let mut stale = book.clone();
        populate(&mut stale, root).unwrap();
        assert!(
            stale
                .chapters()
                .next()
                .unwrap()
                .content
                .contains("gap counts unavailable")
        );
        assert!(
            stale
                .chapters()
                .next()
                .unwrap()
                .content
                .contains("stale coverage; recollect")
        );
        let saved = load_saved(root, Some(root.join("target")));
        assert!(
            saved
                .records
                .iter()
                .all(|record| record.run.sources["code.rs"].contains("fn one() {}"))
        );
        record(root, "one", 4, "build-b", &[(1, 1)]);
        let mut other = book;
        populate(&mut other, root).unwrap();
        assert!(
            other
                .chapters()
                .next()
                .unwrap()
                .content
                .contains("historical build configuration")
        );
        assert!(
            other
                .chapters()
                .next()
                .unwrap()
                .content
                .contains("1/1 (100.0%)")
        );
        assert!(
            other
                .chapters()
                .next()
                .unwrap()
                .content
                .contains("1 selected test attempts")
        );
    }
Input 3: outline tests/execution_coverage.rs fn[book_rendering_preserves_retained_coverage_and_never_launches_tests]
// {test}`requirements.test-coverage.index.render_only`
// {test}`requirements.test-coverage.index.selection`
// {test}`requirements.test-coverage.check`
#[cfg(unix)]
#[test]
fn book_rendering_preserves_retained_coverage_and_never_launches_tests() {
    use mdbook_preprocessor::book::{Book, Chapter};
    use std::io::Write;
    use std::os::unix::fs::PermissionsExt;
    use std::process::Stdio;
    let temp = fixture();
    let root = temp.path();
    let marker = root.join("test-executed");
    let source = root.join("src/lib.rs");
    fs::write(
        &source,
        fs::read_to_string(&source).unwrap().replace(
            "fn second() {",
            &format!("fn second() {{ std::fs::write({marker:?}, b\"executed\").unwrap();"),
        ),
    )
    .unwrap();
    assert_success(&coverage(root, &["-E", "test(second)"]));
    assert!(
        marker.exists(),
        "the captured test must exercise the execution sentinel"
    );
    fs::remove_file(&marker).unwrap();
    let storage = root.join("target/habeas/coverage");
    let before = retained_files(&storage);
    let bin = root.join("read-only-tools");
    fs::create_dir(&bin).unwrap();
    let forbidden = root.join("forbidden-command");
    let calls = root.join("metadata-calls");
    fs::write(&calls, "").unwrap();
    let cargo = bin.join("cargo");
    fs::write(&cargo, "#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"$HABEAS_CARGO_CALL_LOG\"\nif [ \"$1\" != metadata ]; then\n  printf '%s\\n' \"$*\" >> \"$HABEAS_FORBIDDEN_COMMAND\"\n  exit 99\nfi\nexec \"$HABEAS_REAL_CARGO\" \"$@\"\n").unwrap();
    fs::set_permissions(&cargo, fs::Permissions::from_mode(0o755)).unwrap();
    let mut paths = vec![bin];
    paths.extend(std::env::split_paths(&std::env::var_os("PATH").unwrap()));
    let tool_path = std::env::join_paths(&paths).unwrap();
    let checked = Command::new(env!("CARGO_BIN_EXE_habeas"))
        .arg("coverage-check")
        .current_dir(root)
        .env("PATH", &tool_path)
        .env("HABEAS_REAL_CARGO", env!("CARGO"))
        .env("HABEAS_CARGO_CALL_LOG", &calls)
        .env("HABEAS_FORBIDDEN_COMMAND", &forbidden)
        .env("CARGO_NET_OFFLINE", "true")
        .env_remove("CARGO_TARGET_DIR")
        .env_remove("CARGO_BUILD_TARGET_DIR")
        .output()
        .unwrap();
    assert!(
        !checked.status.success(),
        "filtered coverage leaves unmeasured functions"
    );
    assert!(
        String::from_utf8_lossy(&checked.stdout).contains("File execution coverage"),
        "{}",
        String::from_utf8_lossy(&checked.stderr)
    );
    assert!(!marker.exists(), "standalone coverage-check ran a test");
    assert!(
        !forbidden.exists(),
        "standalone coverage-check launched compilation or collection"
    );
    assert_eq!(
        retained_files(&storage),
        before,
        "standalone coverage-check modified captured data"
    );
    let mut book = Book::new();
    book.push_item(Chapter::new(
        "Coverage",
        ":::{coverage-index}\n:::\n".into(),
        "coverage.md",
        vec![],
    ));
    let input = serde_json::json!([{
        "root": root, "config": {"book":{"src":"md"}}, "renderer":"html", "mdbook_version":"0.5.3"
    }, book]);
    let mut child = Command::new(env!("CARGO_BIN_EXE_habeas"))
        .arg("mdbook")
        .current_dir(root)
        .env("PATH", &tool_path)
        .env("HABEAS_REAL_CARGO", env!("CARGO"))
        .env("HABEAS_CARGO_CALL_LOG", &calls)
        .env("HABEAS_FORBIDDEN_COMMAND", &forbidden)
        .env("CARGO_NET_OFFLINE", "true")
        .env_remove("CARGO_TARGET_DIR")
        .env_remove("CARGO_BUILD_TARGET_DIR")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    child
        .stdin
        .take()
        .unwrap()
        .write_all(input.to_string().as_bytes())
        .unwrap();
    let output = child.wait_with_output().unwrap();
    assert_success(&output);
    let rendered: Book = serde_json::from_slice(&output.stdout).unwrap();
    assert!(
        rendered
            .chapters()
            .find(|chapter| chapter.name == "Coverage gaps")
            .unwrap()
            .content
            .contains("Unresolved coverage gaps")
    );
    assert!(
        fs::read_to_string(calls)
            .unwrap()
            .lines()
            .all(|line| line.starts_with("metadata "))
    );
    assert!(
        !forbidden.exists(),
        "rendering invoked a test or coverage command"
    );
    assert!(!marker.exists(), "rendering ran a captured test binary");
    assert_eq!(
        retained_files(&storage),
        before,
        "rendering modified collected coverage"
    );
}

Other sources

Input 1: requirements selection requirements.test-coverage.index.render_only
:::{req} index.render_only
Building the book MUST NOT run tests or collect coverage.
:::