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

Requirements index · View definition

evidence Valid

Example

In a shallow clone with no history beyond the last commit, each window holds only the files’ contents on disk and in that commit. habeas status --coverage says that coverage history is incomplete and warns about items those contents do not cover. test requirements.references.coverage.history

Rationale

Concrete case. requirements.references.coverage.shallow_clone describes a concrete case: a depth-1 clone. Its expected behavior is that habeas status --coverage says coverage history is incomplete and warns about items that the on-disk and last-commit contents do not cover.

Tests that cover it.

  • tests/coverage.rs#fn[a_shallow_clone_uses_the_history_it_has_and_says_so] builds a project with two commits. The first commit’s record covers fn[parse], and the second commit’s record covers nothing. It clones with --depth 1 and makes three assertions:
    • coverage_warnings(&target).incomplete is true.
    • The warned functions in the clone are parse, render and helper. parse is warned because the only available commit no longer reads it.
    • The full-history original warns only about render and helper, which shows the clone really lacks history.
  • tests/coverage.rs#fn[status_shows_coverage_warnings_and_says_when_history_is_incomplete] covers the user-visible status --coverage output. The coverage_status helper calls status_markdown_with_coverage with the coverage flag set. In the full repo it asserts that “history is incomplete” is absent. In the depth-1 clone it asserts “Coverage history is incomplete” and “function parse has no recent coverage”.
  • tests/coverage.rs#fn[incomplete_history_is_noted_even_with_nothing_to_warn_about] checks that the incomplete note appears in --coverage output even when there are no warnings. It checks that the plain status output omits the note, and that the full repo does not report incomplete.
  • tests/coverage.rs#fn[a_shallow_clone_holding_every_window_is_complete] and tests/coverage.rs#fn[a_linked_worktree_of_a_shallow_clone_knows_where_history_ends] check the boundaries. A clone that holds a record’s whole history is not flagged incomplete, a clone that stops at the commit adding the record is flagged, and a linked worktree of a shallow clone detects the shallow boundary correctly.

Result. The tests assert each stated outcome: incompleteness is reported, uncovered items are warned about, and the warnings use only the contents on disk and in the last commit. The expected values follow from the example. They also show that a deeper history would change the result (parse is not warned in the full repo).

Minor limitation. No test makes the on-disk files differ from the last commit in a shallow clone. That is a small extra case, not a stated outcome, so I don’t count it as a gap.

Inputs

Tests

Input 2: outline tests/coverage.rs fn[a_shallow_clone_uses_the_history_it_has_and_says_so]
// {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"]);
}
Input 3: outline tests/coverage.rs fn[status_shows_coverage_warnings_and_says_when_history_is_incomplete]
  • File: tests/coverage.rs
  • Selector: fn[status_shows_coverage_warnings_and_says_when_history_is_incomplete]
  • Kind: fn
  • tests/coverage.rs:231–283
// {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}"
    );
}
Input 4: outline tests/coverage.rs fn[incomplete_history_is_noted_even_with_nothing_to_warn_about]
  • File: tests/coverage.rs
  • Selector: fn[incomplete_history_is_noted_even_with_nothing_to_warn_about]
  • Kind: fn
  • tests/coverage.rs:347–394
// {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()
    );
}
Input 5: outline tests/coverage.rs fn[a_shallow_clone_holding_every_window_is_complete]
// {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);
}
Input 6: outline tests/coverage.rs fn[a_linked_worktree_of_a_shallow_clone_knows_where_history_ends]
  • File: tests/coverage.rs
  • Selector: fn[a_linked_worktree_of_a_shallow_clone_knows_where_history_ends]
  • Kind: fn
  • tests/coverage.rs:665–699
// {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"]);
}

Other sources

Input 1: requirements selection requirements.references.coverage.shallow_clone
:::{example} coverage.shallow_clone
In a shallow clone with no history beyond the last commit, each window holds
only the files' contents on disk and in that commit. `habeas status --coverage`
says that coverage history is incomplete and warns about items those contents
do not cover. {test}`requirements.references.coverage.history`
:::
Input 7: 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 8: outline tests/coverage.rs fn[coverage_status]
// {cite}`habeas:util`
fn coverage_status(root: &Path) -> String {
    habeas::status_markdown_with_coverage(
        root,
        false,
        None,
        false,
        true,
        &habeas::GithubIssues::offline(),
    )
    .unwrap()
}
Input 9: outline tests/coverage.rs fn[record]
/// 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();
}
Input 10: outline tests/coverage.rs fn[commit]
// {cite}`habeas:util`
fn commit(root: &Path, message: &str) {
    git(root, &["add", "-A"]);
    git(root, &["commit", "--quiet", "--allow-empty", "-m", message]);
}
Input 11: 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()
}
Input 12: outline tests/coverage.rs fn[shallow_clone]
/// 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)
}