requirements.test-coverage.index.navigation
Requirements index · View definition
Requirement
The requirements index MUST link to its execution-coverage subchapter under
.
Rationale
The tests adequately check requirements.test-coverage.index.navigation.
Obligation. The requirements index must link to its execution-coverage subchapter.
Test. src/execution_coverage/book.rs#mod[tests]/fn[generated_reports_inherit_the_requirements_index_hierarchy] carries the {test} reference. It builds a requirements-index chapter at reports.md and runs the production populate_in. It then asserts that the index chapter’s content contains [Coverage gaps](reports/coverage.html). That checks the link text and the href, which is relative to the index path. It also asserts that the generated coverage.md chapter is the index’s first sub-item and sits at reports/coverage.md. The test runs twice, once with a numbered index and once with an unnumbered one. The link is asserted in both runs.
Implementation. populate_in appends the link to the matching chapter’s content with chapter_href(parent, &index_path, None). It returns an error if the index chapter is missing. The assertion would catch a missing link, a wrong href or wrong link text.
Scope. Whether the index is explicitly placed or automatically generated is part of the cited requirements.test-coverage.index. Other tests cover that requirement, so I did not reassess it here. The link is added in populate_in, which is the same path the test exercises.
I found no gaps and no acknowledgments.
Inputs
Implementation
Input 4: outline src/execution_coverage/book.rs fn[populate_in]
- File:
src/execution_coverage/book.rs - Selector:
fn[populate_in] - Kind:
fn src/execution_coverage/book.rs:526–691
// {impl}`requirements.test-coverage.index.navigation`
// {impl}`requirements.test-coverage.index.source`
// {impl}`requirements.test-coverage.index.guidance`
// {impl}`requirements.test-coverage.index.summary`
// {impl}`requirements.test-coverage.index.details`
// {impl}`requirements.test-coverage.index.file_map.test`
// {impl}`requirements.test-coverage.index.file_map.combined.details`
pub(super) fn populate_in(
book: &mut Book,
project: &Path,
target: Option<PathBuf>,
parent: &Path,
) -> io::Result<()> {
let index_path = parent.with_extension("").join("coverage.md");
let saved = load_saved(project, target);
let root = workspace_root(project).unwrap_or_else(|_| project.to_path_buf());
let policy = super::check::check_saved(&root, &saved)?;
let SavedCoverage {
records,
selected,
diagnostics,
uncertain_history,
current_files,
discovery_failed: _,
} = saved;
let note_files = if policy.measured {
policy
.files
.iter()
.filter(|file| !file.obligations.is_empty())
.map(|file| file.file.clone())
.collect()
} else {
current_files
};
let mut summary = String::from("# Coverage gaps\n\n");
summary.push_str(&gaps_map(&policy, &index_path, selected.len()));
if policy.measured {
let totals = union(selected.iter().map(|&index| &records[index]));
let covered = totals.values().filter(|&&count| count > 0).count();
summary.push_str(&format!("Line coverage: **{covered}/{} ({:.1}%)** from the union of instrumented file-and-line locations.\n\n", totals.len(), 100.0 * covered as f64 / totals.len() as f64));
}
if uncertain_history {
summary.push_str("Coverage history is incomplete: cannot establish latest-per-test selection. Combined totals are unavailable until the affected build artifacts are repaired or removed.\n\n");
}
if records.is_empty() {
summary.push_str("No test coverage is available. Run `habeas coverage` (or `cargo habeas coverage` in this repository), then rebuild the book.\n\n");
}
let stale = selected
.iter()
.filter(|&&index| !records[index].fresh)
.count();
let incomplete = selected
.iter()
.filter(|&&index| records[index].fresh && !readable(&records[index]))
.count();
let historical = records.last().map_or(0, |latest| {
records
.iter()
.filter(|record| record.run.configuration != latest.run.configuration)
.count()
});
if stale > 0 || incomplete > 0 || historical > 0 {
summary.push_str(&format!(
"**Excluded coverage:** {stale} stale test attempts; {incomplete} unavailable test attempts; {historical} attempts from a historical build configuration.\n\n"
));
}
for diagnostic in diagnostics {
summary.push_str(&format!(
"**Coverage diagnostic:** {}\n\n",
escaped(&diagnostic)
));
}
let existing: BTreeSet<_> = book
.chapters()
.filter_map(|chapter| chapter.path.clone())
.collect();
let mut generated = BTreeSet::from([index_path.clone()]);
if existing.contains(&index_path) {
return Err(io::Error::other(format!(
"coverage index path conflicts with an existing chapter: {}",
index_path.display()
)));
}
let mut details = Vec::new();
let notes_root = workspace_root(project).unwrap_or_else(|_| project.to_path_buf());
for chapter in super::notes::pages(¬es_root, ¬e_files, &index_path) {
let path = chapter
.path
.as_ref()
.expect("file notes have a render path");
if existing.contains(path) || !generated.insert(path.clone()) {
return Err(io::Error::other(format!(
"coverage report path conflicts with an existing chapter: {}",
path.display()
)));
}
details.push(BookItem::Chapter(chapter));
}
summary.push_str("## Collect coverage and check CI policy\n\n");
summary.push_str(super::GUIDANCE);
summary.push('\n');
let mut coverage = Some(Chapter::new("Coverage gaps", summary, &index_path, vec![]));
let mut found = false;
book.for_each_chapter_mut(|chapter| {
if chapter.path.as_deref() == Some(parent) {
found = true;
chapter.content.push_str(&format!(
"\n\n[Coverage gaps]({}) — uncovered obligations and their explanations.\n",
crate::chapter_href(parent, &index_path, None)
));
let mut page = coverage.take().expect("requirements index occurs once");
page.source_path = None;
page.parent_names = chapter.parent_names.clone();
page.parent_names.push(chapter.name.clone());
page.number = chapter.number.as_ref().map(|number| {
let mut parts = number.to_vec();
parts.push(1);
SectionNumber::new(parts)
});
for (offset, item) in details.iter_mut().enumerate() {
let BookItem::Chapter(child) = item else {
unreachable!()
};
child.parent_names = page.parent_names.clone();
child.parent_names.push(page.name.clone());
child.number = page.number.as_ref().map(|number| {
let mut parts = number.to_vec();
parts.push(offset as u32 + 1);
SectionNumber::new(parts)
});
}
page.sub_items.append(&mut details);
fn renumber(chapter: &mut Chapter, number: Vec<u32>) {
chapter.number = Some(SectionNumber::new(number.clone()));
let mut next = 1;
for item in &mut chapter.sub_items {
if let BookItem::Chapter(child) = item {
let mut parts = number.clone();
parts.push(next);
renumber(child, parts);
next += 1;
}
}
}
if let Some(number) = &chapter.number {
let mut next = 2;
for item in &mut chapter.sub_items {
if let BookItem::Chapter(child) = item {
let mut parts = number.to_vec();
parts.push(next);
renumber(child, parts);
next += 1;
}
}
}
chapter.sub_items.insert(0, BookItem::Chapter(page));
}
});
if !found {
return Err(io::Error::other(
"requirements index chapter disappeared before coverage generation",
));
}
Ok(())
}
Tests
Input 3: outline src/execution_coverage/book.rs mod[tests]/fn[generated_reports_inherit_the_requirements_index_hierarchy]
- File:
src/execution_coverage/book.rs - Selector:
mod[tests]/fn[generated_reports_inherit_the_requirements_index_hierarchy] - Kind:
fn src/execution_coverage/book.rs:870–970
// {test}`requirements.test-coverage.index`
// {test}`requirements.test-coverage.index.navigation`
// {test}`requirements.test-coverage.index.source`
// {test}`requirements.test-coverage.index.guidance`
// {test}`requirements.test-coverage.check.guidance`
// {test}`requirements.test-coverage.check.exceptions.setup`
// {test}`requirements.test-coverage.index.details`
#[test]
fn generated_reports_inherit_the_requirements_index_hierarchy() {
for number in [Some(SectionNumber::new(vec![3, 2])), None] {
let (temp, mut book) = fixture();
let BookItem::Chapter(parent) = &mut book.items[0] else {
unreachable!()
};
parent.path = Some("reports.md".into());
parent.name = "Requirements index".into();
parent.content = "# Requirements index\n".into();
parent.parent_names = vec!["Reports".into()];
parent.number = number.clone();
parent.sub_items.push(BookItem::Chapter(Chapter::new(
"Guide",
"# Guide\n".into(),
"reports/guide.md",
vec![],
)));
record(temp.path(), "one", 1, "a", &[(1, 1)]);
// Build fixture configuration before invoking the production path.
fs::create_dir(temp.path().join("md")).unwrap();
fs::write(temp.path().join("book.toml"), "[book]\nsrc='md'\n").unwrap();
fs::write(temp.path().join("md/SUMMARY.md"), "# Summary\n").unwrap();
super::populate_in(
&mut book,
temp.path(),
Some(temp.path().join("target")),
Path::new("reports.md"),
)
.unwrap();
let BookItem::Chapter(parent) = &book.items[0] else {
unreachable!()
};
assert!(
parent
.content
.contains("[Coverage gaps](reports/coverage.html)")
);
assert_eq!(parent.number, number);
let BookItem::Chapter(coverage) = &parent.sub_items[0] else {
unreachable!()
};
assert_eq!(coverage.parent_names, ["Reports", "Requirements index"]);
assert_eq!(
coverage.path.as_deref(),
Some(Path::new("reports/coverage.md"))
);
assert!(coverage.source_path.is_none());
assert_eq!(
coverage.number,
number.as_ref().map(|_| SectionNumber::new(vec![3, 2, 1]))
);
assert_eq!(parent.sub_items.len(), 2);
for (offset, item) in coverage.sub_items.iter().enumerate() {
let BookItem::Chapter(page) = item else {
unreachable!()
};
assert_eq!(
page.parent_names,
["Reports", "Requirements index", "Coverage gaps"]
);
assert_eq!(
page.number,
number
.as_ref()
.map(|_| SectionNumber::new(vec![3, 2, 1, offset as u32 + 1]))
);
assert!(page.source_path.is_none());
assert!(page.content.contains("../coverage.html"));
}
for guidance in [
"habeas test",
"habeas coverage -- --workspace",
"habeas coverage-check",
"static validation test needs no prior coverage",
"80%",
"habeas:coverage",
"current accepted review",
"No project declaration or special configuration",
"after it succeeds,\nrun `habeas coverage-check` as a separate step",
"`habeas test` collects coverage for the whole workspace,\nthen checks it",
"substantive prose justification",
"fenced commentary or a Rust doc comment",
"citation review with `habeas author`",
"Ordinary partly covered functions\nneed no exception when their file meets its target",
] {
assert!(
coverage.content.contains(guidance),
"missing generated guidance: {guidance}"
);
}
assert!(!coverage.content.contains(":::{coverage-index}"));
}
}
Referenced definitions
Input 2: requirements selection requirements.test-coverage.index
requirements.test-coverage.index:md/requirements/test-coverage.md:158–163
:::{req} index
`habeas mdbook` MUST automatically generate exactly one execution-coverage
subchapter under the requirements index of
{cite}`requirements.index.chapter`, whether that requirements index is
explicitly placed or automatically generated.
:::
Other sources
Input 1: requirements selection requirements.test-coverage.index.navigation
requirements.test-coverage.index.navigation:md/requirements/test-coverage.md:165–168
:::{req} index.navigation
The requirements index MUST link to its execution-coverage subchapter under
{cite}`requirements.test-coverage.index`.
:::