requirements.refactor.edited_while_moving
Requirements index · View definition
Example
fn[load]moves tosrc/config/load.rsand its body is also edited. The mapped node’s hash differs from the recorded one, sohabeas refactorleaves every record that read it unchanged and reports each one; those records stay stale untilhabeas authorreassesses them.![]()
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 withsrc/config.rs.author(root)writes current evidence.evidence/rules.load.jsonhas an outline inputsrc/config.rs#fn[load]atinputs[1].move_load(root, " parse();")movesloadintosrc/config/load.rsand appendsparse();to its body.fn[load]therefore moves and is also edited, so its structural hash changes.src/config.rskeeps 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 readsloadisrules.load.json, becauserules.checkreadscheck,verifyanddata.txt. “Each one” is therefore a single record, and it is reported. - The bytes of
evidence/rules.load.jsonequal 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]
- File:
tests/refactor.rs - Selector:
fn[a_function_edited_while_moving_leaves_its_records_stale] - Kind:
fn tests/refactor.rs:243–274
// {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
requirements.refactor.outline_inputs:md/requirements/refactor.md:65–71
:::{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
requirements.refactor.whole_record:md/requirements/refactor.md:93–101
:::{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
requirements.refactor.mapped_back:md/requirements/refactor.md:58–63
:::{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
requirements.refactor.edited_while_moving:md/requirements/refactor.md:185–192
:::{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]
- File:
tests/refactor.rs - Selector:
fn[project] - Kind:
fn tests/refactor.rs:14–38
/// 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]
- File:
tests/refactor.rs - Selector:
fn[author] - Kind:
fn tests/refactor.rs:91–118
/// 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]
- File:
tests/refactor.rs - Selector:
fn[errors] - Kind:
fn tests/refactor.rs:120–123
/// The validation errors.
fn errors(root: &Path) -> Vec<String> {
habeas::validate_repository(root).unwrap().errors
}
Input 9: outline tests/refactor.rs fn[move_load]
- File:
tests/refactor.rs - Selector:
fn[move_load] - Kind:
fn tests/refactor.rs:159–169
/// 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();
}