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.references.coverage.test_module

Requirements index · View definition

evidence Valid

Example

A helper function inside mod tests has no reference and is read by no record. habeas status --coverage does not warn about it. A #[test] function in the same module without a {test} reference is still a validation error. test requirements.references.coverage.report test requirements.references.coverage.tests

Rationale

The example describes a concrete case with two expected outcomes. The first is that an unreferenced helper in mod tests that no record reads gets no coverage warning. The second is that a #[test] function in the same module with no {test} reference is still a validation error. The reviewed tests check both.

  • tests/coverage.rs#fn[a_test_without_a_test_reference_fails_validation_even_in_mod_tests] writes a src/lib.rs with fn parse and a mod tests containing a helper fixture and a #[test] fn checks.
    • It asserts that validation reports the exact error “test function checks has no {test} reference to a requirement or example”, which covers the second outcome.
    • It asserts that no error mentions fixture.
    • It asserts warned(root) == ["parse"], so the helper is not warned about while the ordinary function is. This covers the first outcome.
  • tests/coverage.rs#fn[coverage_lasts_for_three_reauthorings_of_the_record_that_read_an_item] uses the project() fixture, which has fixture inside mod tests. It asserts that warned returns only helper, and later ["parse", "helper"], so fixture is never warned about. It also asserts that warnings produce no validation error.
  • tests/coverage.rs#fn[items_in_out_of_line_tests_modules_are_not_warned_about] covers tests.rs and tests/mod.rs modules. Their fixtures, foo_fixture and bar_fixture, are absent from the warned set, while in_foo, parse, render and helper are warned about.
  • src/coverage_history.rs#mod[tests]/fn[files_declared_as_tests_modules_are_in_a_tests_module] checks the path classification with positive and negative cases, including src/bin/tests.rs and tests/tests.rs.

One small point: the tests call habeas::coverage_warnings through the warned helper instead of running the habeas status --coverage CLI. The helper returns the warning items that the report is built from, so this is an adequate stand-in and not a gap.

No FIXME acknowledgments apply, and I found no unresolved gaps.

Inputs

Tests

Input 2: outline src/coverage_history.rs mod[tests]/fn[files_declared_as_tests_modules_are_in_a_tests_module]
    // {test}`requirements.references.coverage.report`
    // {test}`requirements.references.coverage.test_module`
    #[test]
    fn files_declared_as_tests_modules_are_in_a_tests_module() {
        for path in [
            "src/tests.rs",
            "src/foo/tests.rs",
            "src/foo/tests/mod.rs",
            "src/foo/tests/helpers.rs",
            "crates/a/src/b/tests.rs",
            "src/bin/tool/tests.rs",
            "tests/common/tests.rs",
        ] {
            assert!(in_tests_file(Path::new(path)), "{path}");
        }
        for path in [
            "src/lib.rs",
            "src/main.rs",
            "src/foo.rs",
            "src/foo/mod.rs",
            "src/bin/tests.rs",
            "tests/tests.rs",
            "tests/coverage.rs",
            "tests/common/mod.rs",
            "examples/tests.rs",
            "build.rs",
        ] {
            assert!(!in_tests_file(Path::new(path)), "{path}");
        }
    }
Input 3: 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 4: outline tests/coverage.rs fn[a_test_without_a_test_reference_fails_validation_even_in_mod_tests]
  • File: tests/coverage.rs
  • Selector: fn[a_test_without_a_test_reference_fails_validation_even_in_mod_tests]
  • Kind: fn
  • tests/coverage.rs:296–321
// {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"]);
}
Input 5: outline tests/coverage.rs fn[items_in_out_of_line_tests_modules_are_not_warned_about]
// {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"]);
}

Other sources

Input 1: requirements selection requirements.references.coverage.test_module
:::{example} coverage.test_module
A helper function inside `mod tests` has no reference and is read by no record.
`habeas status --coverage` does not warn about it. A `#[test]` function in the
same module without a `{test}` reference is still a validation error.
{test}`requirements.references.coverage.report`
{test}`requirements.references.coverage.tests`
:::
Input 6: outline tests/coverage.rs fn[project]
/// 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
}
Input 7: outline tests/coverage.rs fn[warned]
/// 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()
}