requirements.references.coverage.reauthored
Requirements index · View definition
Example
Function
parsehas no reference, and the evidence ofrequirements.cli.checkread it. The requirement is re-authored and committed twice, and neither new record readsparse. With the default window of 3, the record’s third most recent version still namesparse, sohabeas status --coveragedoes not warn. After a third such commit, no version in the window names it, andhabeas status --coveragewarns aboutparse.![]()
Rationale
The example is concrete. parse has no reference, and the record for requirements.cli.check reads it. The record is re-authored twice with no read of parse, so the window of 3 still holds a version that names parse and there is no warning. After a third such commit there is a warning about parse.
The test tests/coverage.rs#fn[coverage_lasts_for_three_reauthorings_of_the_record_that_read_an_item] carries a {test} reference to the example and follows that scenario step by step:
- Setup:
project()creates a Git repo whosesrc/lib.rsholdsparse,renderandhelperwith no references.record()writesrules.checkreadingfn[parse]andrules.renderreadingfn[render], and the first commit follows. The assertionwarned == ["helper"]showsparseis covered by the record andhelperwas never read. - Two re-authorings: the loop rewrites
evidence/rules.check.jsonwithinputs: []and a changed rationale, so each commit changes the file. After each commit it assertswarned == ["helper"], soparsestays unwarned while the first version is the third most recent. - Third re-authoring: one more rewrite and commit with empty inputs, then
warned == ["parse", "helper"]. This is the expected warning once no version in the window namesparse. - Warning versus failure: the test also asserts that
validate_repositoryreports no error mentioningparse, so the warning does not fail validation.
The test checks warnings through habeas::coverage_warnings and not through the status --coverage output the example names. Source evidence closes that gap. status_markdown_with_coverage in src/commands.rs renders the result of inspected_coverage_warnings, which coverage_warnings also uses. Other tests in the file check the status text: status_shows_coverage_warnings_and_says_when_history_is_incomplete asserts the ## Coverage warnings section and a function … has no recent coverage line for an uncovered item, and that a covered item is absent. I count no unresolved gap.
Inputs
Tests
Input 2: outline tests/coverage.rs fn[coverage_lasts_for_three_reauthorings_of_the_record_that_read_an_item]
- File:
tests/coverage.rs - Selector:
fn[coverage_lasts_for_three_reauthorings_of_the_record_that_read_an_item] - Kind:
fn tests/coverage.rs:98–137
// {test}`requirements.references.coverage.reauthored`
// {test}`requirements.references.coverage.new_function`
// {test}`requirements.references.coverage.test_module`
// {test}`requirements.references.coverage.history`
// {test}`requirements.references.coverage.report`
#[test]
fn coverage_lasts_for_three_reauthorings_of_the_record_that_read_an_item() {
let temp = project();
let root = temp.path();
record(root, "rules.check", &["fn[parse]"]);
record(root, "rules.render", &["fn[render]"]);
commit(root, "first");
// `helper` was never read, and `fixture` is in `mod tests`.
assert_eq!(warned(root), ["helper"]);
for round in 1..=2 {
// Each re-authoring changes the file, so each commit counts.
fs::write(
root.join("evidence/rules.check.json"),
json!({"requirement": "x", "determination": true, "rationale": format!("round {round}"), "inputs": []})
.to_string(),
)
.unwrap();
commit(root, &format!("round {round}"));
assert_eq!(warned(root), ["helper"], "round {round}");
}
fs::write(
root.join("evidence/rules.check.json"),
json!({"requirement": "x", "determination": true, "rationale": "round 3", "inputs": []})
.to_string(),
)
.unwrap();
commit(root, "round 3");
assert_eq!(warned(root), ["parse", "helper"]);
// A warning does not fail validation.
let errors = habeas::validate_repository(root).unwrap().errors;
assert!(
!errors.iter().any(|error| error.contains("parse")),
"{errors:#?}"
);
}
Input 3: outline tests/coverage.rs (whole file)
- File:
tests/coverage.rs - Selector:
(root) - Kind:
file tests/coverage.rs:1–751
//! Rust item coverage warnings from the recent history of evidence files.
mod common;
use std::fs;
use std::path::Path;
use std::process::Command;
use serde_json::json;
use tempfile::TempDir;
/// A Git project whose book defines `rules.check` and `rules.render`, and
/// whose `src/lib.rs` holds three functions with no references.
// {cite}`habeas:util`
fn project() -> TempDir {
let temp = tempfile::tempdir().unwrap();
let root = temp.path();
for directory in ["md", "src", "evidence"] {
fs::create_dir_all(root.join(directory)).unwrap();
}
fs::write(root.join("Cargo.toml"), "[package]\nname = 'fixture'\n").unwrap();
fs::write(root.join("book.toml"), "[book]\nsrc = 'md'\n").unwrap();
fs::write(
root.join("md/rules.md"),
":::{req} check\nChecking MUST work.\n:::\n\n:::{req} render\nRendering MUST work.\n:::\n",
)
.unwrap();
fs::write(
root.join("src/lib.rs"),
"fn parse() {}\n\nfn render() {}\n\nfn helper() {}\n\nmod tests {\n fn fixture() {}\n}\n",
)
.unwrap();
git(root, &["init", "--quiet"]);
git(root, &["config", "user.email", "test@example.com"]);
git(root, &["config", "user.name", "Test"]);
common::coverage_setup(root);
temp
}
// {cite}`habeas:util`
fn git(root: &Path, args: &[&str]) {
let output = Command::new("git")
.current_dir(root)
.args(args)
.output()
.unwrap();
assert!(output.status.success(), "git {args:?}: {output:?}");
}
// {cite}`habeas:util`
fn coverage_status(root: &Path) -> String {
habeas::status_markdown_with_coverage(
root,
false,
None,
false,
true,
&habeas::GithubIssues::offline(),
)
.unwrap()
}
/// Writes `name`'s evidence reading `items` of `src/lib.rs`.
// {cite}`habeas:util`
fn record(root: &Path, name: &str, items: &[&str]) {
let inputs: Vec<_> = items
.iter()
.map(|item| habeas::outline_input(root, "src/lib.rs", item).unwrap())
.collect();
fs::write(
root.join(format!("evidence/{name}.json")),
json!({"requirement": "x", "determination": true, "rationale": "x", "inputs": inputs})
.to_string(),
)
.unwrap();
}
// {cite}`habeas:util`
fn commit(root: &Path, message: &str) {
git(root, &["add", "-A"]);
git(root, &["commit", "--quiet", "--allow-empty", "-m", message]);
}
/// The names of the functions warned about.
// {cite}`habeas:util`
fn warned(root: &Path) -> Vec<String> {
habeas::coverage_warnings(root)
.unwrap()
.items
.into_iter()
.filter_map(|line| {
let name = line.split(": function ").nth(1)?;
Some(name.split(' ').next()?.to_owned())
})
.collect()
}
// {test}`requirements.references.coverage.reauthored`
// {test}`requirements.references.coverage.new_function`
// {test}`requirements.references.coverage.test_module`
// {test}`requirements.references.coverage.history`
// {test}`requirements.references.coverage.report`
#[test]
fn coverage_lasts_for_three_reauthorings_of_the_record_that_read_an_item() {
let temp = project();
let root = temp.path();
record(root, "rules.check", &["fn[parse]"]);
record(root, "rules.render", &["fn[render]"]);
commit(root, "first");
// `helper` was never read, and `fixture` is in `mod tests`.
assert_eq!(warned(root), ["helper"]);
for round in 1..=2 {
// Each re-authoring changes the file, so each commit counts.
fs::write(
root.join("evidence/rules.check.json"),
json!({"requirement": "x", "determination": true, "rationale": format!("round {round}"), "inputs": []})
.to_string(),
)
.unwrap();
commit(root, &format!("round {round}"));
assert_eq!(warned(root), ["helper"], "round {round}");
}
fs::write(
root.join("evidence/rules.check.json"),
json!({"requirement": "x", "determination": true, "rationale": "round 3", "inputs": []})
.to_string(),
)
.unwrap();
commit(root, "round 3");
assert_eq!(warned(root), ["parse", "helper"]);
// A warning does not fail validation.
let errors = habeas::validate_repository(root).unwrap().errors;
assert!(
!errors.iter().any(|error| error.contains("parse")),
"{errors:#?}"
);
}
// {test}`requirements.references.coverage.deleted_requirement`
// {test}`requirements.references.coverage.history`
#[test]
fn deleting_the_only_record_that_read_an_item_warns_at_once() {
let temp = project();
let root = temp.path();
record(root, "rules.check", &["fn[parse]", "fn[helper]"]);
record(root, "rules.render", &["fn[render]"]);
commit(root, "first");
assert!(warned(root).is_empty());
fs::write(
root.join("md/rules.md"),
":::{req} check\nChecking MUST work.\n:::\n",
)
.unwrap();
fs::remove_file(root.join("evidence/rules.render.json")).unwrap();
assert_eq!(warned(root), ["render"]);
}
// {test}`requirements.references.coverage.uncommitted`
// {test}`requirements.references.coverage.history`
#[test]
fn uncommitted_records_count_as_they_are_on_disk() {
let temp = project();
let root = temp.path();
record(root, "rules.check", &["fn[parse]"]);
commit(root, "first");
assert_eq!(warned(root), ["render", "helper"]);
// Re-authored but not committed: the committed version still covers it.
record(root, "rules.check", &[]);
assert_eq!(warned(root), ["render", "helper"]);
// A new record on disk covers what it reads before any commit.
record(root, "rules.render", &["fn[render]"]);
assert_eq!(warned(root), ["helper"]);
}
// {test}`requirements.references.coverage.shallow_clone`
// {test}`requirements.references.coverage.history`
#[test]
fn a_shallow_clone_uses_the_history_it_has_and_says_so() {
let temp = project();
let root = temp.path();
record(root, "rules.check", &["fn[parse]"]);
commit(root, "first");
record(root, "rules.check", &[]);
commit(root, "second");
let clone = tempfile::tempdir().unwrap();
let url = format!("file://{}", root.display());
let target = clone.path().join("shallow");
let output = Command::new("git")
.args(["clone", "--quiet", "--depth", "1", &url])
.arg(&target)
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
// Only the last commit is available, and it no longer reads `parse`.
assert!(habeas::coverage_warnings(&target).unwrap().incomplete);
assert_eq!(warned(&target), ["parse", "render", "helper"]);
// The full history still covers it.
assert_eq!(warned(root), ["render", "helper"]);
}
// {test}`requirements.references.coverage.history`
// {test}`requirements.configuration.loading`
#[test]
fn the_window_follows_the_workspace_setting() {
let temp = project();
let root = temp.path();
fs::write(
root.join("habeas.toml"),
"version = 1\n\n[coverage]\nreauthorings = 1\n",
)
.unwrap();
record(
root,
"rules.check",
&["fn[parse]", "fn[render]", "fn[helper]"],
);
commit(root, "first");
assert!(warned(root).is_empty());
record(root, "rules.check", &["fn[render]", "fn[helper]"]);
commit(root, "second");
assert_eq!(warned(root), ["parse"]);
fs::write(
root.join("habeas.toml"),
"version = 1\n\n[coverage]\nreauthorings = 0\n",
)
.unwrap();
let error = habeas::coverage_warnings(root).unwrap_err().to_string();
assert!(error.contains("coverage.reauthorings"), "{error}");
}
// {test}`requirements.references.coverage.shallow_clone`
// {test}`requirements.references.coverage.history`
// {test}`requirements.references.coverage.report`
#[test]
fn status_shows_coverage_warnings_and_says_when_history_is_incomplete() {
let temp = project();
let root = temp.path();
record(root, "rules.check", &["fn[parse]"]);
commit(root, "first");
let status = habeas::status_markdown(root, false).unwrap();
assert!(
status.contains("2 nodes have coverage warnings. Use `status --coverage` to see them."),
"{status}"
);
assert!(!status.contains("has no recent coverage"), "{status}");
assert!(!status.contains("## Coverage warnings"), "{status}");
let status = coverage_status(root);
assert!(status.contains("## Coverage warnings"), "{status}");
assert!(
status.contains("src/lib.rs:3: function render has no recent coverage"),
"{status}"
);
assert!(
!status.contains("function parse has no recent coverage"),
"{status}"
);
assert!(!status.contains("history is incomplete"), "{status}");
record(root, "rules.check", &[]);
commit(root, "second");
let clone = tempfile::tempdir().unwrap();
let target = clone.path().join("shallow");
let output = Command::new("git")
.args([
"clone",
"--quiet",
"--depth",
"1",
&format!("file://{}", root.display()),
])
.arg(&target)
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
let status = coverage_status(&target);
assert!(
status.contains("Coverage history is incomplete"),
"{status}"
);
assert!(
status.contains("function parse has no recent coverage"),
"{status}"
);
}
// {test}`requirements.references.coverage.history`
#[test]
fn outside_git_only_the_files_on_disk_count_and_history_is_incomplete() {
let temp = project();
let root = temp.path();
fs::remove_dir_all(root.join(".git")).unwrap();
record(root, "rules.check", &["fn[parse]"]);
assert!(habeas::coverage_warnings(root).unwrap().incomplete);
assert_eq!(warned(root), ["render", "helper"]);
}
// {test}`requirements.references.coverage.tests`
// {test}`requirements.references.coverage.test_module`
// {test}`requirements.references.procedure`
#[test]
fn a_test_without_a_test_reference_fails_validation_even_in_mod_tests() {
let temp = project();
let root = temp.path();
fs::write(
root.join("src/lib.rs"),
"fn parse() {}\n\nmod tests {\n fn fixture() {}\n\n #[test]\n fn checks() {}\n}\n",
)
.unwrap();
commit(root, "first");
let errors = habeas::validate_repository(root).unwrap().errors;
assert!(
errors.iter().any(|error| error
== "src/lib.rs:7: test function checks has no {test} reference to a requirement or example"),
"{errors:#?}"
);
// The helper in `mod tests` is neither an error nor a warning.
assert!(
!errors.iter().any(|error| error.contains("fixture")),
"{errors:#?}"
);
assert_eq!(warned(root), ["parse"]);
}
// {test}`requirements.references.coverage.report`
#[test]
fn habeas_author_prints_only_the_coverage_warning_count_on_stderr() {
let temp = project();
let root = temp.path();
// No definitions need assessment, so authoring needs no model.
fs::write(root.join("md/rules.md"), "# Rules\n").unwrap();
commit(root, "first");
let output = Command::new(env!("CARGO_BIN_EXE_habeas"))
.args(["author", "--all"])
.current_dir(root)
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("3 nodes have coverage warnings. Use `status --coverage` to see them."),
"{stderr}"
);
assert!(!stderr.contains("has no recent coverage"), "{stderr}");
assert!(!stderr.contains("history is incomplete"), "{stderr}");
assert!(!String::from_utf8_lossy(&output.stdout).contains("has no recent coverage"));
}
// {test}`requirements.references.coverage.history`
// {test}`requirements.references.coverage.shallow_clone`
// {test}`requirements.references.coverage.report`
#[test]
fn incomplete_history_is_noted_even_with_nothing_to_warn_about() {
let temp = project();
let root = temp.path();
record(
root,
"rules.check",
&["fn[parse]", "fn[render]", "fn[helper]"],
);
commit(root, "first");
commit(root, "second");
let clone = tempfile::tempdir().unwrap();
let target = clone.path().join("shallow");
let output = Command::new("git")
.args(["clone", "--quiet", "--depth", "1"])
.arg(format!("file://{}", root.display()))
.arg(&target)
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
assert!(warned(&target).is_empty());
assert_eq!(
habeas::coverage_warnings(&target).unwrap(),
habeas::CoverageWarnings {
items: Vec::new(),
incomplete: true
}
);
let status = habeas::status_markdown(&target, false).unwrap();
assert!(
status.contains("0 nodes have coverage warnings. Use `status --coverage` to see them."),
"{status}"
);
assert!(!status.contains("history is incomplete"), "{status}");
let status = coverage_status(&target);
assert!(
status.contains("Coverage history is incomplete"),
"{status}"
);
assert!(warned(root).is_empty());
assert_eq!(
habeas::coverage_warnings(root).unwrap(),
habeas::CoverageWarnings::default()
);
}
// {test}`requirements.references.coverage.history`
#[cfg(unix)]
#[test]
fn coverage_history_uses_the_git_library_without_launching_git() {
use std::os::unix::fs::PermissionsExt;
let temp = project();
let root = temp.path();
record(root, "rules.check", &["fn[parse]"]);
commit(root, "read parse");
record(root, "rules.check", &[]);
commit(root, "stop reading parse");
assert_eq!(warned(root), ["render", "helper"]);
let tools = tempfile::tempdir().unwrap();
let tripwire = tools.path().join("git-launched");
let executable = tools.path().join("git");
// Status also asks Git to discover indexed sources. Supply those metadata
// answers, while making every command capable of reading history fail.
let index = Command::new("git")
.args(["ls-files", "--cached", "-z", "--", "."])
.current_dir(root)
.output()
.unwrap();
assert!(index.status.success());
let index_path = tools.path().join("index");
fs::write(&index_path, index.stdout).unwrap();
fs::write(
&executable,
"#!/bin/sh\ncase \"$*\" in\n 'rev-parse --is-inside-work-tree') printf 'true\\n'; exit 0;;\n 'ls-files --cached -z -- .') cat \"$HABEAS_TEST_INDEX\"; exit 0;;\n 'ls-files --others --exclude-standard -z -- .') exit 0;;\nesac\nprintf '%s\\n' \"$*\" >> \"$HABEAS_GIT_TRIPWIRE\"\nexit 97\n",
)
.unwrap();
fs::set_permissions(&executable, fs::Permissions::from_mode(0o755)).unwrap();
let mut paths = vec![tools.path().to_path_buf()];
paths.extend(std::env::split_paths(&std::env::var_os("PATH").unwrap()));
let output = Command::new(env!("CARGO_BIN_EXE_habeas"))
.args(["status", "--coverage"])
.current_dir(root)
.env("PATH", std::env::join_paths(paths).unwrap())
.env("HABEAS_GIT_TRIPWIRE", &tripwire)
.env("HABEAS_TEST_INDEX", &index_path)
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
let status = String::from_utf8(output.stdout).unwrap();
for name in ["render", "helper"] {
assert!(
status.contains(&format!("function {name} has no recent coverage")),
"{status}"
);
}
assert!(
!status.contains("function parse has no recent coverage"),
"{status}"
);
assert!(
!tripwire.exists(),
"coverage history invoked a Git history command"
);
}
// {test}`requirements.references.coverage.report`
// {test}`requirements.references.coverage.history`
#[test]
fn validation_does_not_read_git_history() {
let temp = project();
let root = temp.path();
record(root, "rules.check", &["fn[parse]"]);
commit(root, "first");
let before = habeas::validate_repository(root).unwrap().errors;
// Without the object database, no committed version can be read.
for entry in fs::read_dir(root.join(".git/objects")).unwrap() {
let path = entry.unwrap().path();
if path.file_name().unwrap().len() == 2 {
fs::remove_dir_all(path).unwrap();
}
}
assert_eq!(habeas::validate_repository(root).unwrap().errors, before);
assert!(habeas::coverage_warnings(root).unwrap().incomplete);
}
/// The output of `git` in `root`.
// {cite}`habeas:util`
fn git_output(root: &Path, args: &[&str]) -> String {
let output = Command::new("git")
.current_dir(root)
.args(args)
.output()
.unwrap();
assert!(output.status.success(), "git {args:?}: {output:?}");
String::from_utf8(output.stdout).unwrap().trim().to_owned()
}
/// A shallow clone of `root` holding its last `depth` commits.
// {cite}`habeas:util`
fn shallow_clone(root: &Path, depth: usize) -> (TempDir, std::path::PathBuf) {
let clone = tempfile::tempdir().unwrap();
let target = clone.path().join("shallow");
let output = Command::new("git")
.args(["clone", "--quiet", "--depth", &depth.to_string()])
.arg(format!("file://{}", root.display()))
.arg(&target)
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
(clone, target)
}
// {test}`requirements.references.coverage.history`
#[test]
fn a_merge_counts_only_when_it_differs_from_every_parent() {
let temp = project();
let root = temp.path();
fs::write(
root.join("habeas.toml"),
"version = 1\n\n[coverage]\nreauthorings = 2\n",
)
.unwrap();
record(
root,
"rules.check",
&["fn[parse]", "fn[render]", "fn[helper]"],
);
commit(root, "first");
git(root, &["switch", "--quiet", "-c", "side"]);
record(root, "rules.check", &["fn[render]", "fn[helper]"]);
commit(root, "side");
git(root, &["switch", "--quiet", "-"]);
git(
root,
&["merge", "--quiet", "--no-ff", "-m", "merge", "side"],
);
// The merge takes the side branch's version unchanged, so the window of
// two holds that version and the first, which still reads `parse`.
assert!(warned(root).is_empty(), "{:?}", warned(root));
assert!(!habeas::coverage_warnings(root).unwrap().incomplete);
}
// {test}`requirements.references.coverage.history`
#[test]
fn an_unreadable_historical_version_makes_history_incomplete() {
let temp = project();
let root = temp.path();
record(root, "rules.check", &["fn[parse]"]);
commit(root, "first");
record(root, "rules.check", &[]);
commit(root, "second");
assert_eq!(warned(root), ["render", "helper"]);
assert!(!habeas::coverage_warnings(root).unwrap().incomplete);
let blob = git_output(root, &["rev-parse", "HEAD~1:evidence/rules.check.json"]);
fs::remove_file(root.join(format!(".git/objects/{}/{}", &blob[..2], &blob[2..]))).unwrap();
// The version that read `parse` cannot be read, and that is reported.
let warnings = habeas::coverage_warnings(root).unwrap();
assert!(warnings.incomplete, "{warnings:?}");
assert_eq!(warned(root), ["parse", "render", "helper"]);
}
// {test}`requirements.references.coverage.history`
// {test}`requirements.references.coverage.shallow_clone`
#[test]
fn a_shallow_clone_holding_every_window_is_complete() {
let temp = project();
let root = temp.path();
commit(root, "first");
record(
root,
"rules.check",
&["fn[parse]", "fn[render]", "fn[helper]"],
);
commit(root, "add the record");
commit(root, "unrelated");
// The clone reaches the commit before the record was added, so the
// record's whole history is there.
let (_clone, target) = shallow_clone(root, 3);
assert_eq!(
habeas::coverage_warnings(&target).unwrap(),
habeas::CoverageWarnings::default()
);
let status = habeas::status_markdown(&target, false).unwrap();
assert!(!status.contains("history is incomplete"), "{status}");
// A clone that stops at the commit adding it cannot tell.
let (_clone, target) = shallow_clone(root, 2);
assert!(habeas::coverage_warnings(&target).unwrap().incomplete);
}
// {test}`requirements.references.coverage.report`
// {test}`requirements.references.coverage.test_module`
#[test]
fn items_in_out_of_line_tests_modules_are_not_warned_about() {
let temp = project();
let root = temp.path();
fs::create_dir_all(root.join("src/foo")).unwrap();
fs::create_dir_all(root.join("src/bar/tests")).unwrap();
fs::write(root.join("src/foo.rs"), "mod tests;\n\nfn in_foo() {}\n").unwrap();
fs::write(root.join("src/foo/tests.rs"), "fn foo_fixture() {}\n").unwrap();
fs::write(root.join("src/bar.rs"), "mod tests;\n").unwrap();
fs::write(root.join("src/bar/tests/mod.rs"), "fn bar_fixture() {}\n").unwrap();
commit(root, "first");
let mut names = warned(root);
names.sort();
assert_eq!(names, ["helper", "in_foo", "parse", "render"]);
}
// {test}`requirements.configuration.workspace_structure`
// {test}`requirements.configuration.loading`
// {test}`requirements.references.coverage.history`
#[test]
fn an_invalid_coverage_table_is_a_validation_finding_but_authoring_proceeds() {
let temp = project();
let root = temp.path();
fs::write(root.join("md/rules.md"), "# Rules\n").unwrap();
commit(root, "first");
for (table, setting) in [
("[coverage]\nreauthorings = 0\n", "coverage.reauthorings"),
("[coverage]\nreauthorings = 'x'\n", "coverage.reauthorings"),
("[coverage]\nwindow = 2\n", "coverage.window"),
] {
fs::write(root.join("habeas.toml"), format!("version = 1\n\n{table}")).unwrap();
let errors = habeas::validate_repository(root).unwrap().errors;
assert!(
errors
.iter()
.any(|error| error.contains("habeas.toml") && error.contains(setting)),
"{table}: {errors:#?}"
);
// Authoring does not use `[coverage]`.
let output = Command::new(env!("CARGO_BIN_EXE_habeas"))
.args(["author", "--all"])
.current_dir(root)
.output()
.unwrap();
assert!(output.status.success(), "{table}: {output:?}");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("cannot compute coverage warnings"),
"{stderr}"
);
}
}
// {test}`requirements.references.coverage.examples`
#[test]
fn an_untracked_example_is_not_a_target() {
let temp = project();
let root = temp.path();
fs::create_dir_all(root.join("examples")).unwrap();
fs::write(
root.join("md/rules.md"),
":::{req} check\nChecking MUST work.\n:::\n\n:::{example} demo\nA demo. {cite}`rules.check`\n:::\n",
)
.unwrap();
fs::write(root.join("examples/tracked.rs"), "fn main() {}\n").unwrap();
commit(root, "first");
// Untracked, it is not discovered, so its citation is not found either.
fs::write(
root.join("examples/untracked.rs"),
"// {cite}`rules.demo`\nfn main() {}\n",
)
.unwrap();
let errors = habeas::validate_repository(root).unwrap().errors;
let examples: Vec<_> = errors
.iter()
.filter(|error| error.contains("Cargo example"))
.collect();
assert_eq!(
examples,
["examples/tracked.rs: Cargo example does not {cite} a named example of the book"]
);
}
// {test}`requirements.references.coverage.history`
// {test}`requirements.references.coverage.shallow_clone`
#[test]
fn a_linked_worktree_of_a_shallow_clone_knows_where_history_ends() {
let temp = project();
let root = temp.path();
fs::write(
root.join("habeas.toml"),
"version = 1\n\n[coverage]\nreauthorings = 1\n",
)
.unwrap();
record(root, "rules.check", &["fn[parse]"]);
commit(root, "first");
record(root, "rules.check", &["fn[render]"]);
commit(root, "second");
// The one-commit clone holds the record's whole window of one version;
// so does a linked worktree of it, whose `shallow` file is in the common
// directory rather than its own.
let (clone, target) = shallow_clone(root, 1);
assert!(!habeas::coverage_warnings(&target).unwrap().incomplete);
let worktree = clone.path().join("linked");
git(
&target,
&[
"worktree",
"add",
"--quiet",
"--detach",
worktree.to_str().unwrap(),
],
);
let warnings = habeas::coverage_warnings(&worktree).unwrap();
assert!(!warnings.incomplete, "{warnings:?}");
assert_eq!(warned(&worktree), ["parse", "helper"]);
}
// {test}`requirements.references.coverage.history`
// {test}`requirements.references.coverage.uncommitted`
#[test]
fn an_uncommitted_record_does_not_make_a_full_shallow_history_incomplete() {
let temp = project();
let root = temp.path();
fs::write(
root.join("habeas.toml"),
"version = 1\n\n[coverage]\nreauthorings = 1\n",
)
.unwrap();
record(root, "rules.check", &["fn[parse]"]);
commit(root, "first");
record(root, "rules.check", &["fn[render]"]);
commit(root, "second");
// The one-commit clone holds the committed record's whole window.
let (_clone, target) = shallow_clone(root, 1);
assert!(!habeas::coverage_warnings(&target).unwrap().incomplete);
// A new record that was never committed has no history to miss.
record(&target, "rules.render", &["fn[helper]"]);
let warnings = habeas::coverage_warnings(&target).unwrap();
assert!(!warnings.incomplete, "{warnings:?}");
assert_eq!(warned(&target), ["parse"]);
}
// {test}`requirements.references.coverage.history`
#[cfg(unix)]
#[test]
fn an_unlistable_evidence_directory_makes_history_incomplete() {
use std::os::unix::fs::PermissionsExt;
let temp = project();
let root = temp.path();
record(
root,
"rules.check",
&["fn[parse]", "fn[render]", "fn[helper]"],
);
commit(root, "first");
assert!(!habeas::coverage_warnings(root).unwrap().incomplete);
let hidden = root.join("evidence/hidden");
fs::create_dir_all(&hidden).unwrap();
fs::set_permissions(&hidden, fs::Permissions::from_mode(0o000)).unwrap();
// Permissions do not stop a privileged user, so there is nothing to test.
let listable = fs::read_dir(&hidden).is_ok();
let warnings = (!listable).then(|| habeas::coverage_warnings(root));
fs::set_permissions(&hidden, fs::Permissions::from_mode(0o755)).unwrap();
if let Some(warnings) = warnings {
let warnings = warnings.unwrap();
assert!(warnings.incomplete, "{warnings:?}");
}
}
Other sources
Input 1: requirements selection requirements.references.coverage.reauthored
requirements.references.coverage.reauthored:md/requirements/references.md:281–290
:::{example} coverage.reauthored
Function `parse` has no reference, and the evidence of `requirements.cli.check`
read it. The requirement is re-authored and committed twice, and neither new
record reads `parse`. With the default window of 3, the record's third most
recent version still names `parse`, so `habeas status --coverage` does not warn.
After a third such commit, no version in the window names it, and
`habeas status --coverage` warns about `parse`.
{test}`requirements.references.coverage.history`
{test}`requirements.references.coverage.report`
:::
Input 4: requirements selection requirements.references.coverage.history
requirements.references.coverage.history:md/requirements/references.md:245–260
:::{req} coverage.history
{impl}`requirements.configuration.feature_sections`
An item has recent coverage when it has a reference that covers it under
{cite}`requirements.references.rust_item_coverage`, or when its outline node, or
an outline node containing it, is named by file and selector in the inputs of
any version of an existing evidence record, citation review, or site review in
its coverage window. A file's coverage window MUST be its current contents on
disk, committed or not, together with its contents in each of the last N
commits in its Git history that changed it, read through a Git library rather
than by running `git`. A recorded hash MUST NOT matter here: an earlier version
names the node, whatever its code was then. N MUST be the workspace
`[coverage]` setting `reauthorings`, a positive integer, and 3 when it is not
set. When the history is unavailable, such as outside Git or in a shallow clone
missing those commits, the window MUST be the commits that are available, and
`habeas status --coverage` MUST say that coverage history is incomplete.
:::
Input 5: outline src/commands.rs fn[status_markdown_with_coverage]
- File:
src/commands.rs - Selector:
fn[status_markdown_with_coverage] - Kind:
fn src/commands.rs:49–214
/// Renders status with coverage-warning details when `coverage` is enabled.
// {impl}`requirements.cli.status`
// {impl}`requirements.references.coverage.report`
pub fn status_markdown_with_coverage(
root: &Path,
all: bool,
glob: Option<&str>,
verbose: bool,
coverage: bool,
github: &GithubIssues,
) -> io::Result<String> {
let mut inspection = inspect_repository(root)?;
crate::github::add_issue_findings(&mut inspection, github);
if let Some(glob) = glob {
definition_glob::validate(glob)
.map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?;
if !inspection.definitions.iter().any(|(name, d)| {
d.kind != crate::DefinitionKind::Term && definition_glob::matches(glob, name)
}) {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("no definitions match {glob:?}"),
));
}
}
let mut output = String::from("# Definitions status\n\n");
let mut rows = Vec::new();
let mut findings = String::new();
let mut fixmes = FixmeSummary::default();
let mut associated_errors = BTreeSet::new();
for (name, definition) in &inspection.definitions {
if definition.kind == crate::DefinitionKind::Term {
continue;
}
if glob.is_some_and(|glob| !definition_glob::matches(glob, name)) {
continue;
}
let evidence = LoadedDetailEvidence::load(root, name, &inspection.report);
for error in evidence.errors() {
associated_errors.insert(error.clone());
}
fixmes.add(name, definition.kind.noun(), evidence.fixme_entries());
if !all && evidence.state() == EvidenceState::Valid {
continue;
}
if !all && !verbose && evidence.state() == EvidenceState::Fixme {
continue;
}
rows.push((
format!("`{name}` ({})", definition.kind.noun()),
evidence.state().label(),
));
if !evidence.errors().is_empty() {
findings.push_str(&format!("\n### `{name}`\n\n<pre><code>"));
findings.push_str(&escape_html(&evidence.errors().join("\n\n")));
findings.push_str("</code></pre>\n");
}
}
let fixmes = fixmes.render(verbose);
if rows.is_empty() {
output.push_str(if all {
"No definitions found.\n"
} else if !fixmes.is_empty() {
"No other definitions need attention.\n"
} else {
"All matching definitions have valid evidence.\n"
});
} else {
output.push_str(&render_evidence_table(&rows));
}
output.push_str(&fixmes);
// Site reviews are checked on their own; list those needing attention,
// or every one with `--all`, by requirement, file, and selector.
// {impl}`requirements.references.site_review`
let mut site_rows = Vec::new();
for site in inspection
.sites
.iter()
.filter(|(name, _)| glob.is_none_or(|glob| definition_glob::matches(glob, name)))
.flat_map(|(_, sites)| sites)
{
let evidence =
LoadedDetailEvidence::load_path(root, &site.review_path(), &inspection.report);
for error in evidence.errors() {
associated_errors.insert(error.clone());
}
if !evidence.errors().is_empty() {
findings.push_str(&format!(
"\n### `{}`\n\n<pre><code>",
crate::sites::subject_label(&site.requirement, &site.file, &site.item)
));
findings.push_str(&escape_html(&evidence.errors().join("\n\n")));
findings.push_str("</code></pre>\n");
}
if all || evidence.state() != EvidenceState::Valid {
site_rows.push(format!(
"| `{}` | `{}` | `{}` | {} |\n",
site.requirement,
site.file,
site.selector_label(),
evidence.state().label()
));
}
}
if !site_rows.is_empty() {
output.push_str(
"\n## Site reviews\n\n| Requirement | File | Selector | Evidence |\n| --- | --- | --- | --- |\n",
);
output.push_str(&site_rows.concat());
}
output.push_str(&disagreements(root, &inspection, glob));
let other_findings = inspection
.report
.errors
.iter()
.filter(|error| !associated_errors.contains(error.as_str()))
.collect::<Vec<_>>();
match crate::evidence::untracked_reference_files(&inspection.root) {
Ok(untracked) if untracked.is_empty() => {}
Ok(untracked) => {
output.push_str(
"\n## Untracked files with references\n\n\
Reference discovery considers only files in the Git index, so these \
references are not validated. Stage the files with `git add` to include them.\n\n",
);
for file in untracked {
let file = file.strip_prefix(&inspection.root).unwrap_or(&file);
output.push_str(&format!("- `{}`\n", file.display()));
}
}
Err(error) => output.push_str(&format!(
"\n## Untracked files with references\n\nCannot check untracked files for references: {}\n",
escape_html(&error.to_string())
)),
}
if !findings.is_empty() {
output.push_str("\n## Validation findings\n");
output.push_str(&findings);
}
if !other_findings.is_empty() {
output.push_str("\n## Other validation findings\n\n<pre><code>");
output.push_str(&escape_html(
&other_findings
.into_iter()
.map(String::as_str)
.collect::<Vec<_>>()
.join("\n\n"),
));
output.push_str("</code></pre>\n");
}
// An invalid workspace file, including an invalid `[coverage]` table, is
// already a validation finding; status still reports everything else and
// says why the warnings could not be computed.
match inspected_coverage_warnings(&inspection) {
Ok(warnings) => output.push_str(&coverage_section(&warnings, coverage)),
Err(error) => output.push_str(&format!(
"\n## Coverage warnings\n\nCoverage warnings could not be computed: {}\n",
escape_html(&error.to_string())
)),
}
Ok(output)
}
Input 6: outline src/commands.rs fn[coverage_warnings]
- File:
src/commands.rs - Selector:
fn[coverage_warnings] - Kind:
fn src/commands.rs:238–244
/// The coverage warnings of the project at `root`, as `habeas status` shows
/// them and `habeas author` prints them after it finishes.
// {impl}`requirements.references.coverage.report`
// {impl}`requirements.references.coverage.history`
pub fn coverage_warnings(root: &Path) -> io::Result<CoverageWarnings> {
inspected_coverage_warnings(&crate::evidence::inspect_repository(root)?)
}