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

Requirements index · View definition

evidence Valid

Requirement

A file coverage map MUST identify each file by its workspace-relative path and show its covered and instrumented line counts and percentage. These counts MUST use the union rule of cite requirements.test-coverage.index.summary, restricted to the file and the records represented by that map, including associated child processes. The map MUST visually distinguish coverage levels while also displaying percentages as text. The percentage MUST describe the map’s recorded instrumented lines, rather than all physical source lines.

A listed file with instrumented lines but no positive execution counts MUST show 0%. A listed file with no recorded instrumented lines in otherwise usable coverage MUST show “not measured” without a percentage. Coverage whose completeness cannot be established MUST be identified as unavailable rather than as 0% or “not measured”.

Rationale

I judged the tests adequate. I did not request implementation source or run any searches. All of the evidence comes from the five supplied tests in src/execution_coverage/book.rs. Each obligation of requirements.test-coverage.index.file_map maps to a test with assertions that enforce it.

  • Path, counts and percentage.
    • gaps_union_process_locations_and_omit_per_test_reports asserts the code.rs card shows “2/3 instrumented lines covered”, “66.7%” and --gap-covered: 66.666667%.
    • It also asserts the card links to the file’s notes chapter.
    • gaps_escape_paths_and_do_not_round_positive_coverage_to_zero asserts paths are HTML-escaped. It also asserts 1/2000 shows <0.1% rather than 0.0%, and “1/2000 instrumented lines covered”.
  • Union rule across child processes.
    • The test above gives code.rs a parent process with lines {1:2, 2:0} and a child process with lines {1:0, 3:u64::MAX}.
    • The expected 2/3 requires each location to count once in the denominator. It also requires a location to count as covered when any process has a positive count, and the child’s record to be included.
    • The index summary of 2/4 (50.0%) is also asserted.
  • Percentage over recorded instrumented lines, not physical lines. code.rs has four physical lines but three instrumented ones, and the asserted result is 2/3 (66.7%).
  • Visual distinction plus percentage text. coverage_bars_distinguish_measured_levels_alongside_percentage_text covers 0, 1, 25, 75 and 100% coverage. For each file it asserts the percentage text, the --gap-covered width variable and the gap-coverage-bar element. It also checks the bundled CSS for the width and colour rules.
  • 0% case. zero.rs has an instrumented line with a count of 0. The test asserts “0.0%” and --gap-covered: 0.000000%. maps_distinguish_no_runs_zero_hits_stale_and_incomplete_coverage also checks all-zero hits for code.rs.
  • “Not measured” case. executable.rs is a source file with no recorded instrumented lines, in otherwise usable coverage. The test asserts its row contains “not measured”, has no % and has no coverage bar. A non-executable file (unmeasured.rs) gets no card.
  • Unavailable rather than 0% or “not measured”.
    • maps_distinguish_no_runs_zero_hits_stale_and_incomplete_coverage covers these cases:
      • no runs;
      • empty records;
      • no processes;
      • an attempt diagnostic;
      • a process diagnostic;
      • a missing source;
      • an empty profile;
      • a stale source;
      • a missing child profile;
      • an unreadable attempt;
      • unreadable metadata.
    • In every case it asserts “Coverage unavailable” and that no data-file= cards appear.
    • incomplete_attempts_do_not_label_their_files_as_unmeasured_in_a_partial_map covers a partial map whose files are only in an unavailable or stale attempt. It asserts that no “not measured” label, directory rows or bars appear.

The {cite} of requirements.test-coverage.index.summary is exercised by the union and index assertions above.

Inputs

Tests

Input 3: outline src/execution_coverage/book.rs mod[tests]/fn[gaps_union_process_locations_and_omit_per_test_reports]
    // {test}`requirements.test-coverage.index.file_map`
    // {test}`requirements.test-coverage.index.file_map.table`
    // {test}`requirements.test-coverage.index.file_map.combined`
    // {test}`requirements.test-coverage.index.file_map.test`
    // {test}`requirements.test-coverage.index.summary`
    // {test}`requirements.test-coverage.index.file_map.combined.details`
    // {test}`requirements.test-coverage.index.file_map.notes`
    // {test}`requirements.test-coverage.index.details`
    #[test]
    fn gaps_union_process_locations_and_omit_per_test_reports() {
        let (temp, mut book) = fixture();
        let root = temp.path();
        fs::write(
            root.join("code.rs"),
            "fn one() {}\nfn two() {}\nfn three() {}\n// not instrumented\n",
        )
        .unwrap();
        fs::write(root.join("zero.rs"), "fn zero() {}\n").unwrap();
        fs::write(root.join("unmeasured.rs"), "// no instrumentation\n").unwrap();
        fs::write(root.join("executable.rs"), "fn unmeasured() {}\n").unwrap();
        let path = record(root, "one", 1, "a", &[(1, 2), (2, 0)]);
        let run_path = path.parent().unwrap().parent().unwrap().join("run.json");
        let mut run: Run = load(&run_path).unwrap();
        for file in ["zero.rs", "unmeasured.rs", "executable.rs"] {
            run.sources
                .insert(file.into(), fs::read_to_string(root.join(file)).unwrap());
        }
        save(&run_path, &run).unwrap();
        let mut attempt: Attempt = load(&path.join("attempt.json")).unwrap();
        attempt.processes[0]
            .lines
            .insert("zero.rs".into(), BTreeMap::from([(1, 0)]));
        fs::write(path.join("2-child.profraw"), "child profile fixture").unwrap();
        attempt.processes.push(Process {
            functions: Some(vec![]),
            pid: 2,
            profiles: vec!["2-child.profraw".into()],
            diagnostic: None,
            lines: BTreeMap::from([("code.rs".into(), BTreeMap::from([(1, 0), (3, u64::MAX)]))]),
        });
        save(&path.join("attempt.json"), &attempt).unwrap();
        populate(&mut book, root).unwrap();
        let index = &book.chapters().next().unwrap().content;
        assert!(index.contains("Line coverage: **2/4 (50.0%)**"));
        let map = map_html(index);
        assert!(file_card(map, "code.rs").contains("2/3 instrumented lines covered"));
        assert!(file_card(map, "code.rs").contains("66.7%"));
        assert!(file_card(map, "code.rs").contains("--gap-covered: 66.666667%"));
        let notes_path = super::super::notes::path(Path::new("reports/coverage.md"), "code.rs");
        let href = crate::chapter_href(Path::new("reports/coverage.md"), &notes_path, None);
        assert!(file_card(map, "code.rs").contains(&format!("href=\"{href}\"")));
        let notes = book
            .chapters()
            .find(|chapter| chapter.path.as_ref() == Some(&notes_path))
            .unwrap();
        assert!(notes.content.contains("File: <code>code.rs</code>"));
        assert!(file_card(map, "zero.rs").contains("0.0%"));
        assert!(file_card(map, "zero.rs").contains("--gap-covered: 0.000000%"));
        assert!(
            !map.contains("data-file=\"unmeasured.rs\""),
            "a non-executable file has no gap"
        );
        let unmeasured = file_card(map, "executable.rs")
            .split("</tr>")
            .next()
            .unwrap();
        assert!(unmeasured.contains("not measured"));
        assert!(!unmeasured.contains('%'));
        assert!(!unmeasured.contains("gap-coverage-bar"));
        assert!(map.contains("<th>File</th><th>Coverage</th>"));
        assert_eq!(
            map.matches("class=\"gap-file-row habeas-coverage-file\"")
                .count(),
            3
        );
        let mut html = String::new();
        pulldown_cmark::html::push_html(
            &mut html,
            pulldown_cmark::Parser::new_ext(index, pulldown_cmark::Options::all()),
        );
        assert!(html.contains("Unresolved coverage gaps"));
        assert!(!html.contains("habeas-coverage-test"));
        assert!(!html.contains("Test runs and provenance"));
        assert!(!html.contains("binary / one"));
        assert!(
            book.chapters()
                .skip(1)
                .all(|chapter| chapter.name.starts_with("File notes:"))
        );
    }
Input 4: outline src/execution_coverage/book.rs mod[tests]/fn[maps_distinguish_no_runs_zero_hits_stale_and_incomplete_coverage]
    // {test}`requirements.test-coverage.index.file_map`
    // {test}`requirements.test-coverage.index.file_map.table`
    // {test}`requirements.test-coverage.index.file_map.combined`
    // {test}`requirements.test-coverage.index.file_map.test`
    // {test}`requirements.test-coverage.index.file_map.combined.measurement`
    // {test}`requirements.test-coverage.index.summary`
    #[test]
    fn maps_distinguish_no_runs_zero_hits_stale_and_incomplete_coverage() {
        let (temp, book) = fixture();
        let root = temp.path();
        let mut no_runs = book.clone();
        populate(&mut no_runs, root).unwrap();
        let index = &no_runs.chapters().next().unwrap().content;
        assert!(index.contains("Coverage unavailable"));
        assert!(index.contains("gap counts unavailable"));
        assert!(!map_html(index).contains("data-file="));
        assert!(index.contains("Run `habeas coverage`"));

        let empty_path = record(root, "empty", 0, "empty", &[]);
        let mut empty = book.clone();
        populate(&mut empty, root).unwrap();
        let empty_index = &empty.chapters().next().unwrap().content;
        assert!(empty_index.contains("Coverage unavailable"));
        assert!(!empty_index.contains("Line coverage:"));
        assert!(!empty_index.contains("NaN"));
        assert!(!map_html(empty_index).contains("data-file="));
        assert!(empty_index.contains("No usable instrumented lines"));
        fs::remove_dir_all(empty_path.parent().unwrap().parent().unwrap()).unwrap();
        let path = record(root, "zero", 1, "a", &[(1, 0), (2, 0)]);
        let mut zero = book.clone();
        populate(&mut zero, root).unwrap();
        assert!(
            file_card(
                map_html(&zero.chapters().next().unwrap().content),
                "code.rs"
            )
            .contains("0.0%")
        );

        let mut attempt: Attempt = load(&path.join("attempt.json")).unwrap();
        attempt.processes[0]
            .lines
            .get_mut("code.rs")
            .unwrap()
            .insert(1, 1);
        for failure in [
            "no processes",
            "attempt diagnostic",
            "process diagnostic",
            "missing source",
            "empty profile",
        ] {
            let mut missing = attempt.clone();
            match failure {
                "no processes" => missing.processes.clear(),
                "attempt diagnostic" => missing.diagnostics.push("export incomplete".into()),
                "process diagnostic" => {
                    missing.processes[0].diagnostic = Some("unreadable profile".into());
                }
                "missing source" => {
                    missing.processes[0]
                        .lines
                        .insert("uncaptured.rs".into(), BTreeMap::from([(1, 1)]));
                }
                "empty profile" => fs::write(path.join("1-1.profraw"), "").unwrap(),
                _ => unreachable!(),
            }
            save(&path.join("attempt.json"), &missing).unwrap();
            let mut unavailable = book.clone();
            populate(&mut unavailable, root).unwrap();
            let index = &unavailable.chapters().next().unwrap().content;
            assert!(index.contains("Coverage unavailable"), "{failure}");
            assert!(!map_html(index).contains("data-file="), "{failure}");
            assert!(
                index.contains("missing or unreadable coverage"),
                "{failure}"
            );
            fs::write(path.join("1-1.profraw"), "retained profile fixture").unwrap();
        }
        save(&path.join("attempt.json"), &attempt).unwrap();
        fs::write(root.join("code.rs"), "changed source\n").unwrap();
        fs::write(root.join("new.rs"), "fn newly_added() {}\n").unwrap();
        let mut stale = book.clone();
        populate(&mut stale, root).unwrap();
        let index = &stale.chapters().next().unwrap().content;
        assert!(index.contains("Coverage unavailable"));
        assert!(!map_html(index).contains("data-file="));
        assert!(index.contains("1 stale test attempts"));
        attempt.processes.push(Process {
            functions: Some(vec![]),
            pid: 2,
            profiles: vec!["missing-child.profraw".into()],
            diagnostic: None,
            lines: BTreeMap::from([("code.rs".into(), BTreeMap::from([(2, 1)]))]),
        });
        save(&path.join("attempt.json"), &attempt).unwrap();
        let mut incomplete = book.clone();
        populate(&mut incomplete, root).unwrap();
        let index = &incomplete.chapters().next().unwrap().content;
        assert!(index.contains("Coverage unavailable"));
        assert!(!map_html(index).contains("data-file="));
        assert!(index.contains("missing or unreadable coverage"));

        fs::write(root.join("code.rs"), "fn one() {}\nfn two() {}\n").unwrap();
        fs::remove_file(root.join("new.rs")).unwrap();
        let mut unreadable = book.clone();
        populate(&mut unreadable, root).unwrap();
        let index = &unreadable.chapters().next().unwrap().content;
        assert!(index.contains("1 unavailable test attempts"));
        assert!(index.contains("Coverage unavailable"));
        assert!(!map_html(index).contains("data-file="));
        // Unreadable metadata cannot establish latest-per-test selection.
        fs::write(path.join("attempt.json"), "not JSON").unwrap();
        let mut uncertain = book;
        populate(&mut uncertain, root).unwrap();
        let index = &uncertain.chapters().next().unwrap().content;
        assert!(index.contains("Coverage history is incomplete"));
        assert!(index.contains("gap counts unavailable"));
        assert!(!map_html(index).contains("data-file="));
    }
Input 5: outline src/execution_coverage/book.rs mod[tests]/fn[incomplete_attempts_do_not_label_their_files_as_unmeasured_in_a_partial_map]
    // {test}`requirements.test-coverage.index.file_map`
    // {test}`requirements.test-coverage.index.file_map.table`
    // {test}`requirements.test-coverage.index.file_map.combined`
    // {test}`requirements.test-coverage.index.file_map.combined.measurement`
    // {test}`requirements.test-coverage.index.freshness`
    // {test}`requirements.test-coverage.index.file_map.minimum`
    // {test}`requirements.test-coverage.index.file_map.minimum.statuses`
    #[test]
    fn incomplete_attempts_do_not_label_their_files_as_unmeasured_in_a_partial_map() {
        let (temp, book) = fixture();
        let root = temp.path();
        fs::create_dir_all(root.join("src/nested")).unwrap();
        fs::write(root.join("src/nested/other.rs"), "fn other() {}\n").unwrap();
        let good = record(root, "good", 1, "a", &[(1, 1)]);
        let bad = record(root, "bad", 2, "a", &[]);
        let mut attempt: Attempt = load(&bad.join("attempt.json")).unwrap();
        attempt.processes[0].lines =
            BTreeMap::from([("src/nested/other.rs".into(), BTreeMap::from([(1, 1)]))]);
        save(&bad.join("attempt.json"), &attempt).unwrap();
        for path in [&good, &bad] {
            let run_path = path.parent().unwrap().parent().unwrap().join("run.json");
            let mut run: Run = load(&run_path).unwrap();
            run.sources
                .insert("src/nested/other.rs".into(), "fn other() {}\n".into());
            save(&run_path, &run).unwrap();
        }
        fs::remove_file(bad.join("1-1.profraw")).unwrap();
        let mut partial = book.clone();
        populate(&mut partial, root).unwrap();
        let index = &partial.chapters().next().unwrap().content;
        assert!(index.contains("Coverage unavailable"));
        assert!(!map_html(index).contains("data-file="));
        assert!(!map_html(index).contains("not measured"));
        assert!(!map_html(index).contains("class=\"gap-directory\""));
        assert!(!map_html(index).contains("data-min-covered="));
        assert!(!map_html(index).contains("gap-coverage-bar\""));
        assert!(index.contains("1 unavailable test attempts"));
        // A stale selected attempt leaves the same unknown per-file state.
        let run_path = bad.parent().unwrap().parent().unwrap().join("run.json");
        let mut run: Run = load(&run_path).unwrap();
        run.inputs
            .insert("missing-build-input".into(), "old-hash".into());
        save(&run_path, &run).unwrap();
        let mut stale = book;
        populate(&mut stale, root).unwrap();
        let index = &stale.chapters().next().unwrap().content;
        assert!(index.contains("Coverage unavailable"));
        assert!(!map_html(index).contains("data-file="));
        assert!(!map_html(index).contains("class=\"gap-directory\""));
        assert!(!map_html(index).contains("data-min-covered="));
        assert!(!map_html(index).contains("not measured"));
        assert!(index.contains("1 stale test attempts"));
    }
Input 6: outline src/execution_coverage/book.rs mod[tests]/fn[gaps_escape_paths_and_do_not_round_positive_coverage_to_zero]
    // {test}`requirements.test-coverage.index.file_map`
    // {test}`requirements.test-coverage.index.file_map.table`
    #[test]
    fn gaps_escape_paths_and_do_not_round_positive_coverage_to_zero() {
        let policy = super::super::check::PolicyReport {
            report: super::super::check::CheckReport {
                passed: false,
                text: String::new(),
            },
            files: vec![super::super::check::FilePolicy {
                file: "src/<unsafe>\".rs".into(),
                covered: 1,
                total: 2000,
                obligations: vec![super::super::check::Obligation {
                    selector: String::new(),
                    line: 1,
                    measurement: "below target".into(),
                    exception: None,
                }],
            }],
            counts_complete: true,
            measured: true,
            findings: vec![],
        };
        let map = gaps_map(&policy, Path::new("coverage.md"), 1);
        assert!(map.contains("src/&lt;unsafe&gt;&quot;.rs"));
        assert!(!map.contains("<unsafe>"));
        assert!(map.contains("&lt;0.1%"));
        assert!(map.contains("1/2000 instrumented lines covered"));
        assert!(!map.contains(">0.0%<"));
    }
Input 7: outline src/execution_coverage/book.rs mod[tests]/fn[coverage_bars_distinguish_measured_levels_alongside_percentage_text]
    // {test}`requirements.test-coverage.index.file_map`
    // {test}`requirements.test-coverage.index.file_map.table`
    #[test]
    fn coverage_bars_distinguish_measured_levels_alongside_percentage_text() {
        let mut policy = super::super::check::PolicyReport {
            report: super::super::check::CheckReport {
                passed: false,
                text: String::new(),
            },
            files: vec![],
            counts_complete: true,
            measured: true,
            findings: vec![],
        };
        for covered in [0, 1, 25, 75, 100] {
            policy.files.push(super::super::check::FilePolicy {
                file: format!("src/level-{covered}.rs"),
                covered,
                total: 100,
                obligations: vec![super::super::check::Obligation {
                    selector: "fn[unexecuted]".into(),
                    line: 1,
                    measurement: "zero execution".into(),
                    exception: None,
                }],
            });
        }
        let map = gaps_map(&policy, Path::new("coverage.md"), 1);
        for covered in [0, 1, 25, 75, 100] {
            let file = format!("src/level-{covered}.rs");
            let row = file_card(&map, &file).split("</tr>").next().unwrap();
            assert!(row.contains(&format!("<code>{file}</code>")));
            assert!(row.contains(&format!(">{covered}.0%")));
            assert!(row.contains(&format!("--gap-covered: {covered}.000000%")));
            assert!(row.contains("class=\"gap-coverage-bar\" aria-hidden=\"true\""));
            assert_eq!(map.matches(&format!("data-file=\"{file}\"")).count(), 1);
        }
        let css = include_str!("../../assets/coverage-gaps.css");
        assert!(css.contains(".gap-coverage-bar::before"));
        assert!(css.contains("width: var(--gap-covered)"));
        assert!(css.contains("background: var(--gap-link)"));
    }

Referenced definitions

Input 2: requirements selection requirements.test-coverage.index.summary
:::{req} index.summary
The coverage index MUST display the combined view selected under
{cite}`requirements.test-coverage.index.selection` and
{cite}`requirements.test-coverage.index.freshness`. It MUST show the number of
selected test attempts and summarize coverage excluded because it is stale,
unavailable, or from a historical build configuration. Detailed test identities,
run provenance, and per-test execution data MUST remain in the saved coverage
artifacts rather than being included in the generated book.

For first-party Rust files under
{cite}`requirements.references.rust_item_coverage`, the summary MUST compute
line coverage from the union of instrumented file-and-line locations in the
selected fresh records, including their associated child-process records.
Each location MUST count once in the denominator and count as covered when any
selected record has a positive execution count for it. Percentages MUST be
computed from those union counts rather than averaged across process reports.
A view with no instrumented lines MUST show coverage as unavailable. The index
MUST visibly identify incomplete coverage and MUST NOT imply that every current
test has coverage merely because all available records are valid.
:::

Other sources

Input 1: requirements selection requirements.test-coverage.index.file_map
:::{req} index.file_map
A file coverage map MUST identify each file by its workspace-relative path and
show its covered and instrumented line counts and percentage. These counts MUST
use the union rule of {cite}`requirements.test-coverage.index.summary`,
restricted to the file and the records represented by that map, including
associated child processes. The map MUST visually distinguish coverage levels
while also displaying percentages as text. The percentage MUST describe the
map's recorded instrumented lines, rather than all physical source lines.

A listed file with instrumented lines but no positive execution counts MUST
show 0%. A listed file with no recorded instrumented lines in otherwise usable
coverage MUST show "not measured" without a percentage. Coverage whose
completeness cannot be established MUST be identified as unavailable rather
than as 0% or "not measured".
:::