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.refactor.moved_requirement

Requirements index · View definition

evidence Valid

Example

requirements.cli.check moves to the core chapter, becoming requirements.core.check; the {impl}, {test}, and {cite} references to it still use the old name. habeas refactor --map requirements.cli.check=requirements.core.check rewrites those references, renames its evidence file, rewrites the requirements inputs of records that read it, and leaves unchanged a record whose input selects requirements.cli.*, because that selection no longer matches what it recorded. cite requirements.refactor.reference_rewrite cite requirements.refactor.record_files cite requirements.refactor.definition_inputs

Rationale

tests/refactor.rs#fn[a_moved_requirement_renames_its_references_and_evidence] carries a {test} reference to requirements.refactor.moved_requirement. It covers the example’s scenario and asserts each stated outcome. It uses rules.load → core.load in place of requirements.cli.check → requirements.core.check.

Setup

  • author writes evidence for rules.load and rules.check.
  • rules.load has a requirements input that selects rules.load exactly.
  • rules.check has a requirements input that selects the glob rules.*, recorded as matching both rules.check and rules.load.
  • The book’s rules.md and core.md are rewritten so that load has moved to the core chapter.
  • The sources still use the old name. src/config.rs carries the {impl} and {test} references, which the report counts as two. md/notes.md carries a {cite}.

Assertions

  • References rewritten: the command succeeds, the report lists src/config.rs (2) and md/notes.md (1), and notes.md now reads {cite}core.load. `config.rs` contains `// {impl}`core.load and // {test}core.load``.
  • Nothing else changes: a before/after snapshot diff is exactly evidence/core.load.json, evidence/rules.load.json, md/notes.md and src/config.rs. The glob record rules.check.json is not among the changed files.
  • Evidence file renamed: the report shows evidence/rules.load.json -> evidence/core.load.json, the old file no longer exists, and the new record exists.
  • Requirements input rewritten: record["inputs"][0] equals requirements_input("core.load", ["core.load"], LOAD_TEXT), and the rationale contains as `core.load` asks.
  • Glob record left unchanged: the report says evidence/rules.check.json: inputs[0]: rules.* no longer selects what it recorded. The remaining validation errors all start with evidence/rules.check.json, so only that record is stale.

The test also carries {test} references to the three cited requirements (reference_rewrite, record_files, definition_inputs). It exercises each of them in the example’s scenario.

Inputs

Tests

Input 5: outline tests/refactor.rs fn[a_moved_requirement_renames_its_references_and_evidence]
// {test}`requirements.refactor.moved_requirement`
// {test}`requirements.refactor.reference_rewrite`
// {test}`requirements.refactor.record_files`
// {test}`requirements.refactor.definition_inputs`
// {test}`requirements.refactor.subject_text`
// {test}`requirements.refactor.rationale.names`
// {test}`requirements.refactor.command`
// {test}`requirements.refactor.mappings`
#[test]
fn a_moved_requirement_renames_its_references_and_evidence() {
    let temp = project();
    let root = temp.path();
    author(root);
    fs::write(
        root.join("md/notes.md"),
        "Loading follows {cite}`rules.load`.\n",
    )
    .unwrap();
    // `load` moves to the `core` chapter; references still name the old name.
    fs::write(
        root.join("md/rules.md"),
        "# Rules\n\n:::{req} check\n:impl: data.txt\nThe data MUST be checked.\n:::\n",
    )
    .unwrap();
    fs::write(
        root.join("md/core.md"),
        "# Core\n\n:::{req} load\nConfiguration MUST load.\n:::\n",
    )
    .unwrap();
    let before = snapshot(root);
    let output = refactor(root, &["--map", "rules.load=core.load"]);
    assert!(output.status.success(), "{output:?}");
    let report = stdout(&output);
    assert!(report.contains("- src/config.rs (2)"), "{report}");
    assert!(report.contains("- md/notes.md (1)"), "{report}");
    assert_eq!(
        fs::read_to_string(root.join("md/notes.md")).unwrap(),
        "Loading follows {cite}`core.load`.\n"
    );
    // Only the rewritten references and the moved evidence changed.
    let after = snapshot(root);
    let differing = before
        .keys()
        .chain(after.keys())
        .filter(|file| before.get(*file) != after.get(*file))
        .cloned()
        .collect::<std::collections::BTreeSet<_>>();
    assert_eq!(
        differing.into_iter().collect::<Vec<_>>(),
        [
            "evidence/core.load.json",
            "evidence/rules.load.json",
            "md/notes.md",
            "src/config.rs",
        ]
    );
    assert!(
        report.contains("- evidence/rules.load.json -> evidence/core.load.json"),
        "{report}"
    );
    // The glob no longer matches what it recorded, so that record is stale.
    assert!(
        report.contains(
            "- evidence/rules.check.json: inputs[0]: rules.* no longer selects what it recorded"
        ),
        "{report}"
    );
    assert!(!root.join("evidence/rules.load.json").exists());
    let source = fs::read_to_string(root.join("src/config.rs")).unwrap();
    assert!(source.contains("// {impl}`core.load`") && source.contains("// {test}`core.load`"));
    let record: Value =
        serde_json::from_str(&fs::read_to_string(root.join("evidence/core.load.json")).unwrap())
            .unwrap();
    assert_eq!(
        record["inputs"][0],
        requirements_input("core.load", &["core.load"], &LOAD_TEXT)
    );
    assert!(
        record["rationale"]
            .as_str()
            .unwrap()
            .contains("as `core.load` asks")
    );
    let remaining = errors(root);
    assert!(
        remaining
            .iter()
            .all(|error| error.starts_with("evidence/rules.check.json")),
        "{remaining:#?}"
    );
}

Referenced definitions

Input 2: requirements selection requirements.refactor.reference_rewrite
:::{req} reference_rewrite
For a definition-name mapping, `habeas refactor` MUST rewrite each `{impl}`,
`{test}`, `{cite}`, `{fixme}`, and `{term}` reference that names an old name,
in the book and in the sources reference discovery considers. For a path or
node mapping, it MUST rewrite each `:impl:` or `:test:` anchor that names an
old file or node. It MUST change nothing else in those files.
:::
Input 3: requirements selection requirements.refactor.record_files
:::{req} record_files
For each changed record, `habeas refactor` MUST move its evidence file,
citation review file under {cite}`requirements.references.citation_review`, or
site review file under {cite}`requirements.references.site_review` to the path
its mapped subject, file, and selector determine. It MUST NOT overwrite an
existing file; it MUST leave that record unchanged and report the conflict.
:::
Input 4: requirements selection requirements.refactor.definition_inputs
:::{req} definition_inputs
A requirements or definition input MUST be rewritten only if the
{term}`requirements.refactor.mapped_back` current text of each definition it
records equals the recorded text and, for a requirements input, the mapped
selection's current matches are exactly the recorded matches with the mapping
applied.
:::
Input 6: definition requirements.refactor.mapped_back
:::{definition} mapped_back
:phrase: mapped-back

The *mapped-back* form of current text or a current address is the text or
address with each new side of the mappings replaced by its old side.
:::

Other sources

Input 1: requirements selection requirements.refactor.moved_requirement
:::{example} moved_requirement
`requirements.cli.check` moves to the `core` chapter, becoming
`requirements.core.check`; the `{impl}`, `{test}`, and `{cite}` references to
it still use the old name. `habeas refactor --map requirements.cli.check=requirements.core.check`
rewrites those references, renames its evidence file, rewrites the requirements
inputs of records that read it, and leaves unchanged a record whose input
selects `requirements.cli.*`, because that selection no longer matches what it
recorded. {cite}`requirements.refactor.reference_rewrite`
{cite}`requirements.refactor.record_files`
{cite}`requirements.refactor.definition_inputs`
:::
Input 7: outline tests/refactor.rs fn[author]
/// Writes current evidence for both requirements.
fn author(root: &Path) {
    let outline = |file: &str, item: &str| habeas::outline_input(root, file, item).unwrap();
    write_evidence(
        root,
        "rules.load",
        "`src/config.rs#fn[load]` loads it, as `rules.load` asks; `verify` tests it in `src/config.rs`.",
        vec![
            requirements_input("rules.load", &["rules.load"], &LOAD_TEXT),
            outline("src/config.rs", "fn[load]"),
            outline("src/config.rs", "fn[verify]"),
        ],
    );
    let mut both = CHECK_TEXT.to_vec();
    both.push("");
    both.extend(LOAD_TEXT);
    write_evidence(
        root,
        "rules.check",
        "`data.txt` is checked by `src/config.rs#fn[check]`.",
        vec![
            requirements_input("rules.*", &["rules.check", "rules.load"], &both),
            outline("data.txt", ""),
            outline("src/config.rs", "fn[check]"),
            outline("src/config.rs", "fn[verify]"),
        ],
    );
}