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

Requirements index · View definition

evidence Valid

Example

fn[load] moves to src/config/load.rs and its body is also edited. The mapped node’s hash differs from the recorded one, so habeas refactor leaves every record that read it unchanged and reports each one; those records stay stale until habeas author reassesses them. cite requirements.refactor.outline_inputs cite requirements.refactor.whole_record

Rationale

The example is covered by tests/refactor.rs#fn[a_function_edited_while_moving_leaves_its_records_stale]. It carries a {test} reference to requirements.refactor.edited_while_moving.

Setup.

  • project() builds a fixture with src/config.rs.
  • author(root) writes current evidence. evidence/rules.load.json has an outline input src/config.rs#fn[load] at inputs[1].
  • move_load(root, " parse();") moves load into src/config/load.rs and appends parse(); to its body. fn[load] therefore moves and is also edited, so its structural hash changes. src/config.rs keeps the rest of the file.

Action. The test runs habeas refactor --map src/config.rs#fn[load]=src/config/load.rs#fn[load].

Assertions.

  • The command succeeds.
  • The report contains - evidence/rules.load.json: inputs[1]: src/config/load.rs#fn[load] no longer has the recorded hash. This matches the example: the mapped node’s hash differs from the recorded one, and the affected record is reported. The only record that reads load is rules.load.json, because rules.check reads check, verify and data.txt. “Each one” is therefore a single record, and it is reported.
  • The bytes of evidence/rules.load.json equal the bytes read before the refactor, so the record is left unchanged.
  • errors(root) is non-empty. The record stays stale and validation reports errors until it is reassessed.

The cited requirements.refactor.outline_inputs and requirements.refactor.whole_record are context. The test also carries {test} references to them, and the reported failure of the hash check and the unchanged record match what they require. I found no gaps.

Inputs

Tests

Input 4: outline tests/refactor.rs fn[a_function_edited_while_moving_leaves_its_records_stale]
// {test}`requirements.refactor.edited_while_moving`
// {test}`requirements.refactor.outline_inputs`
// {test}`requirements.refactor.whole_record`
// {test}`requirements.refactor.command`
#[test]
fn a_function_edited_while_moving_leaves_its_records_stale() {
    let temp = project();
    let root = temp.path();
    author(root);
    move_load(root, " parse();");
    let before = fs::read(root.join("evidence/rules.load.json")).unwrap();
    let output = refactor(
        root,
        &[
            "--map",
            "src/config.rs#fn[load]=src/config/load.rs#fn[load]",
        ],
    );
    assert!(output.status.success(), "{output:?}");
    let report = stdout(&output);
    assert!(
        report.contains(
            "- evidence/rules.load.json: inputs[1]: src/config/load.rs#fn[load] no longer has the recorded hash"
        ),
        "{report}"
    );
    assert_eq!(
        fs::read(root.join("evidence/rules.load.json")).unwrap(),
        before
    );
    assert!(!errors(root).is_empty());
}

Referenced definitions

Input 2: requirements selection requirements.refactor.outline_inputs
:::{req} outline_inputs
An outline input MUST be rewritten only if the mapped node exists and its
structural hash, computed under
{cite}`requirements.references.structural_hash` over the
{term}`requirements.refactor.mapped_back` text of the node, equals the recorded
hash.
:::
Input 3: requirements selection requirements.refactor.whole_record
:::{req} whole_record
A record MUST be changed only if every rewrite a mapping calls for in it
verifies under {cite}`requirements.refactor.outline_inputs`,
{cite}`requirements.refactor.definition_inputs`,
{cite}`requirements.refactor.subject_text`, and
{cite}`requirements.refactor.reference_digest`; otherwise the whole record MUST
be left unchanged and reported with the check that failed. Records that no
mapping affects MUST be left byte-identical.
:::
Input 5: 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.edited_while_moving
:::{example} edited_while_moving
`fn[load]` moves to `src/config/load.rs` and its body is also edited. The
mapped node's hash differs from the recorded one, so `habeas refactor` leaves
every record that read it unchanged and reports each one; those records stay
stale until `habeas author` reassesses them.
{cite}`requirements.refactor.outline_inputs`
{cite}`requirements.refactor.whole_record`
:::
Input 6: outline tests/refactor.rs fn[project]
/// A project with the `rules` chapter and `src/config.rs` implementing and
/// testing `rules.load`.
// {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"),
        concat!(
            "# Rules\n\n",
            ":::{req} load\nConfiguration MUST load.\n:::\n\n",
            ":::{req} check\n:impl: data.txt\nThe data MUST be checked.\n:::\n",
        ),
    )
    .unwrap();
    fs::write(root.join("data.txt"), "checked data\n").unwrap();
    fs::write(root.join("src/config.rs"), config_source("")).unwrap();
    common::coverage_setup(root);
    temp
}
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]"),
        ],
    );
}
Input 8: outline tests/refactor.rs fn[errors]
/// The validation errors.
fn errors(root: &Path) -> Vec<String> {
    habeas::validate_repository(root).unwrap().errors
}
Input 9: outline tests/refactor.rs fn[move_load]
/// Moves `load` unchanged to `src/config/load.rs`.
fn move_load(root: &Path, extra: &str) {
    fs::create_dir_all(root.join("src/config")).unwrap();
    fs::write(
        root.join("src/config/load.rs"),
        LOAD_ALONE.replace("read();", &format!("read();{extra}")),
    )
    .unwrap();
    let rest = config_source("").replace(LOAD_ALONE, "");
    fs::write(root.join("src/config.rs"), rest).unwrap();
}