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

Requirements index · View definition

evidence Valid

Requirement

The coverage index MUST combine the most recent recorded test attempt for each nextest binary and test name among the retained runs. “Most recent” MUST refer to when the attempt started, including failed or interrupted attempts. A newer filtered run MUST update only the tests it executes, leaving other compatible test records available to the combined view. A newer attempt with missing or unreadable coverage MUST be shown as unavailable rather than replaced with an older successful attempt. Results from incompatible instrumented build configurations MUST NOT be combined under cite requirements.test-coverage.storage.configuration.

The main combined view MUST use the instrumented build configuration of the most recently started recorded test attempt. Latest-per-test selection MUST occur within that configuration. Records from other configurations MUST be identified as excluded historical coverage. Configuration selection MUST use recorded metadata without building or executing tests.

Rationale

I judged this from the supplied test source only. I did not request implementation source or the record and fixture helpers, so I relied on the tests’ visible assertions. The cited requirements.test-coverage.storage.configuration is context and was not reassessed.

Obligations and the tests that cover them (all in src/execution_coverage/book.rs unless noted)

  1. Latest attempt per nextest binary and test name.

    • selection_distinguishes_binaries_and_selects_latest_attempts_within_the_active_build writes two records with the same test name “same” but different binaries, “first” and “second”. It asserts that saved.selected.len() is 2, that both binaries are selected, and that the index shows 2/2 coverage. Two records with the same test name are therefore not collapsed across binaries.
    • The same test’s second scenario checks that, within a build, the newer of two attempts for a test is the one used.
  2. “Most recent” means attempt start, including failed or interrupted attempts.

    • The second scenario of the selection test writes records newest-first. It makes run-start and UUID ordering disagree with attempt-start ordering, and the result is still 2/2 (100%).
    • The third scenario covers a retry in the same run. The older attempt has the larger UUID, and the later-started attempt still wins.
    • filtered_runs_union_latest_attempts_and_invalidate_with_saved_source adds a newest attempt for “one” with no data. It sets the outcome to “failed” and then to “interrupted”. Each time it asserts “gap counts unavailable”, “missing or unreadable coverage” and “2 selected test attempts”.
  3. A newer filtered run updates only the tests it executes. In filtered_runs_union_latest_attempts_and_invalidate_with_saved_source, “one” and “two” come from separate runs. The combined view is 2/2 with “2 selected test attempts”, so each test’s record stays available.

  4. A newer missing or unreadable attempt is shown as unavailable, not replaced by an older success. unreadable_history_cannot_reintroduce_an_older_success covers four cases:

    • A newest attempt.json that is not valid JSON gives “cannot establish latest-per-test selection” and “gap counts unavailable”, and it asserts that “1/1 (100.0%)” is absent.
    • Missing captured sources give “missing or unreadable coverage”.
    • An unsupported run.json schema gives “Coverage history is incomplete”.
    • A missing raw profile gives “A retained raw profile is missing”.
    • The failed and interrupted no-data cases are covered under item 2.
  5. The main view uses the configuration of the most recently started attempt, and other configurations are excluded and identified.

    • In the selection test’s second scenario, the build-b attempt has a later run start (1000) but an earlier attempt start (35) than the newest build-a attempt (40). The test asserts that build-a is the main view, with 2/2 coverage and the text “1 attempts from a historical build configuration”.
    • In the filtered-runs test, a newest build-b attempt becomes the main view. The test asserts “historical build configuration”, 1/1, and “1 selected test attempts”. Selection is therefore latest-first by attempt start, and configurations are not combined.
  6. Configuration selection uses recorded metadata without building or executing tests. book_rendering_preserves_retained_coverage_and_never_launches_tests in tests/execution_coverage.rs puts a fake cargo on PATH. The fake allows only metadata and writes to a forbidden-command file for anything else. The test runs both coverage-check and the mdbook preprocessor. It asserts that no marker file was created by a test, that no forbidden command ran, that every cargo call started with metadata, and that the retained files were unchanged.

I found no obligation of the subject text left without a test that exercises it and asserts the outcome. All the tests carry {test} references to requirements.test-coverage.index.selection.

Inputs

Tests

Input 3: outline src/execution_coverage/book.rs mod[tests]/fn[selection_distinguishes_binaries_and_selects_latest_attempts_within_the_active_build]
    // {test}`requirements.test-coverage.index.selection`
    #[test]
    fn selection_distinguishes_binaries_and_selects_latest_attempts_within_the_active_build() {
        let (temp, mut book) = fixture();
        for (binary, started, counts) in [
            ("first", 1, [(1, 1), (2, 0)]),
            ("second", 2, [(1, 0), (2, 1)]),
        ] {
            let dir = record(temp.path(), "same", started, "build-a", &counts);
            let path = dir.join("attempt.json");
            let mut attempt: Attempt = load(&path).unwrap();
            attempt.binary = binary.into();
            save(&path, &attempt).unwrap();
        }
        populate(&mut book, temp.path()).unwrap();
        let index = &book.chapters().next().unwrap().content;
        assert!(index.contains("2/2 (100.0%)"));
        let saved = load_saved(temp.path(), Some(temp.path().join("target")));
        assert_eq!(saved.selected.len(), 2);
        let binaries = saved
            .selected
            .iter()
            .map(|&i| saved.records[i].attempt.binary.as_str())
            .collect::<BTreeSet<_>>();
        assert_eq!(binaries, BTreeSet::from(["first", "second"]));

        let (temp, mut book) = fixture();
        // Write newest first; run-start and UUID ordering deliberately disagree
        // with attempt-start ordering.
        for (test, start, config, counts) in [
            ("two", 40, "build-a", [(2, 1)]),
            ("one", 30, "build-a", [(1, 1)]),
            ("one", 35, "build-b", [(1, 0)]),
            ("two", 10, "build-a", [(2, 0)]),
        ] {
            let attempt = record(temp.path(), test, start, config, &counts);
            let dir = attempt.parent().unwrap().parent().unwrap();
            let path = dir.join("run.json");
            let mut run: Run = load(&path).unwrap();
            run.started = if config == "build-b" { 1000 } else { start };
            run.id = uuid::Uuid::from_u128(100 - start).to_string();
            save(&path, &run).unwrap();
            fs::rename(dir, dir.parent().unwrap().join(&run.id)).unwrap();
        }
        populate(&mut book, temp.path()).unwrap();
        let index = &book.chapters().next().unwrap().content;
        assert!(index.contains("2/2 (100.0%)"));
        assert!(index.contains("1 attempts from a historical build configuration"));
        let saved = load_saved(temp.path(), Some(temp.path().join("target")));
        let tests = saved
            .selected
            .iter()
            .map(|&i| saved.records[i].attempt.test.as_str())
            .collect::<BTreeSet<_>>();
        assert_eq!(tests, BTreeSet::from(["one", "two"]));

        // A retry in the same run must also use attempt-start ordering.
        let (temp, mut book) = fixture();
        let newest = record(temp.path(), "one", 20, "build-a", &[(1, 1)]);
        let mut older: Attempt = load(&newest.join("attempt.json")).unwrap();
        older.started = 10;
        older.id = uuid::Uuid::from_u128(u128::MAX).to_string();
        older.attempt = "older".into();
        older.processes[0].lines = BTreeMap::from([("code.rs".into(), BTreeMap::from([(1, 0)]))]);
        let older_dir = newest.parent().unwrap().join(&older.id);
        fs::create_dir(&older_dir).unwrap();
        fs::copy(newest.join("1-1.profraw"), older_dir.join("1-1.profraw")).unwrap();
        save(&older_dir.join("attempt.json"), &older).unwrap();
        populate(&mut book, temp.path()).unwrap();
        assert!(
            book.chapters()
                .next()
                .unwrap()
                .content
                .contains("1/1 (100.0%)")
        );
    }
Input 4: 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 5: outline src/execution_coverage/book.rs mod[tests]/fn[unreadable_history_cannot_reintroduce_an_older_success]
    // {test}`requirements.test-coverage.index.selection`
    // {test}`requirements.test-coverage.index.unavailable`
    // {test}`requirements.test-coverage.index.freshness`
    // {test}`requirements.test-coverage.storage.outcomes`
    // {test}`requirements.test-coverage.index.file_map.combined.measurement`
    #[test]
    fn unreadable_history_cannot_reintroduce_an_older_success() {
        let (temp, mut book) = fixture();
        record(temp.path(), "one", 1, "a", &[(1, 1)]);
        let newest = record(temp.path(), "one", 2, "a", &[(1, 0)]);
        fs::write(newest.join("attempt.json"), "not JSON").unwrap();
        populate(&mut book, temp.path()).unwrap();
        let index = &book.chapters().next().unwrap().content;
        assert!(index.contains("cannot establish latest-per-test selection"));
        assert!(index.contains("gap counts unavailable"));
        assert!(!index.contains("1/1 (100.0%)"));
        let (temp, mut book) = fixture();
        let newest = record(temp.path(), "one", 1, "a", &[(1, 1)]);
        let path = newest.parent().unwrap().parent().unwrap().join("run.json");
        let mut run: Run = load(&path).unwrap();
        run.sources.clear();
        save(&path, &run).unwrap();
        populate(&mut book, temp.path()).unwrap();
        assert!(
            book.chapters()
                .next()
                .unwrap()
                .content
                .contains("missing or unreadable coverage")
        );
        assert!(
            book.chapters()
                .next()
                .unwrap()
                .content
                .contains("Matching captured source is missing")
        );
        // Unsupported metadata cannot establish compatibility for combined totals.
        let (temp, mut book) = fixture();
        let attempt = record(temp.path(), "one", 1, "a", &[(1, 1)]);
        let path = attempt.parent().unwrap().parent().unwrap().join("run.json");
        let mut run: Run = load(&path).unwrap();
        run.schema = 99;
        save(&path, &run).unwrap();
        populate(&mut book, temp.path()).unwrap();
        let index = &book.chapters().next().unwrap().content;
        assert!(index.contains("Coverage history is incomplete"));
        assert!(index.contains("gap counts unavailable"));
        // A retained result with an unreadable/missing raw profile is unavailable.
        let (temp, mut book) = fixture();
        let attempt = record(temp.path(), "one", 1, "a", &[(1, 1)]);
        fs::remove_file(attempt.join("1-1.profraw")).unwrap();
        populate(&mut book, temp.path()).unwrap();
        assert!(
            book.chapters()
                .next()
                .unwrap()
                .content
                .contains("missing or unreadable coverage")
        );
        assert!(
            book.chapters()
                .next()
                .unwrap()
                .content
                .contains("gap counts unavailable")
        );
        assert!(
            book.chapters()
                .next()
                .unwrap()
                .content
                .contains("A retained raw profile is missing")
        );
    }
Input 6: 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"
    );
}

Referenced definitions

Input 2: requirements selection requirements.test-coverage.storage.configuration
:::{req} storage.configuration
Each coverage record MUST identify the recorded toolchain, compilation target,
Cargo build profile, enabled features, and instrumentation-affecting compiler
settings. These settings MUST determine build-configuration compatibility for
the combined view. A nextest scheduling profile alone MUST NOT make otherwise
compatible build configurations incompatible.
:::

Other sources

Input 1: requirements selection requirements.test-coverage.index.selection
:::{req} index.selection
The coverage index MUST combine the most recent recorded test attempt for each
nextest binary and test name among the retained runs. "Most recent" MUST refer
to when the attempt started, including failed or interrupted attempts. A newer
filtered run MUST update only the tests it executes, leaving other compatible
test records available to the combined view. A newer attempt with missing or
unreadable coverage MUST be shown as unavailable rather than replaced with an
older successful attempt. Results from incompatible instrumented build
configurations MUST NOT be combined under
{cite}`requirements.test-coverage.storage.configuration`.

The main combined view MUST use the instrumented build configuration of the
most recently started recorded test attempt. Latest-per-test selection MUST
occur within that configuration. Records from other configurations MUST be
identified as excluded historical coverage. Configuration selection MUST use
recorded metadata without building or executing tests.
:::