requirements.refactor.invalid_mappings
Requirements index · View definition
Requirement
habeas refactorMUST fail with a usage error, before changing anything, for a malformed mapping, a mapping whose old side could be read as both a path and a definition name, two mappings with overlapping old sides, or a mapping whose new side does not exist.
Rationale
The reviewed tests cover every failure class in requirements.refactor.invalid_mappings. Each case asserts a usage error and that nothing changed.
How the main test asserts the outcome. tests/refactor.rs#fn[invalid_mappings_fail_with_a_usage_error_and_change_nothing] runs the real habeas refactor binary once per case. For each case it asserts:
- exit code 2;
- a case-specific message in stderr;
Usage:in stderr;- that
snapshot(root)equals the pre-run snapshot.snapshotis a byte-for-byte map of every file under the project root.
It also runs refactor with no arguments and asserts exit code 2.
Obligation to scenario mapping
- Malformed mapping:
rules.loadhas no=.=core.loadhas an empty old side.rules.check=andsrc/config.rs=have an empty new side.#fn[load]=src/config.rs#fn[load]has a node with no file (“a node needs a file”).rules.check=rules.checkhas identical sides (“different”).nothing.here=data.txtnames nothing recorded (“names no recorded definition, file, or node”).
- Old side readable as both a path and a definition name:
- The test creates the directory
rules.load, which is also a definition name. - It then maps
rules.load=core.loadand expects “ambiguous”.
- The test creates the directory
- Overlapping old sides:
- Path overlap:
srcandsrc/config.rs. - Definition overlap:
rules.checkandrules. - Node overlap:
src/config.rs#fn[load]andsrc/config.rs#, the whole-file node.
- Path overlap:
- New side that does not exist:
- Definition:
core.check. - File:
src/gone.rs. - Node:
src/config.rs#fn[gone].
- Definition:
- Before changing anything:
- The snapshot comparison runs after every failing invocation.
- The fixture has real evidence authored, so there is something that could be rewritten.
Library-level test. src/refactor/tests.rs#fn[library_errors_preserve_usage_messages_and_filesystem_failure_kinds] checks that a malformed mapping gives RefactorError::Usage containing “expected Io error, not a usage error, and that no evidence directory is created. This adds to the malformed-mapping and no-change coverage.
Gaps considered. I found no scenario that is both named by the requirement and untested. Duplicate identical old sides and a valid mapping paired with an invalid one are not tested. The requirement does not call these out, and the overlap cases already exercise the same all-or-nothing, validate-before-write behavior. There are no acknowledgments or {fixme} references to account for.
Inputs
Tests
Input 2: outline src/refactor/tests.rs fn[library_errors_preserve_usage_messages_and_filesystem_failure_kinds]
- File:
src/refactor/tests.rs - Selector:
fn[library_errors_preserve_usage_messages_and_filesystem_failure_kinds] - Kind:
fn src/refactor/tests.rs:31–60
// {test}`requirements.refactor.invalid_mappings`
#[test]
fn library_errors_preserve_usage_messages_and_filesystem_failure_kinds() {
let root = tempfile::tempdir().unwrap();
fs::create_dir(root.path().join("md")).unwrap();
fs::write(root.path().join("book.toml"), "[book]\nsrc = 'md'\n").unwrap();
fs::write(
root.path().join("md/rules.md"),
":::{req} ready\nReady.\n:::\n",
)
.unwrap();
let error = refactor(root.path(), &["bad-mapping".into()], false).unwrap_err();
assert!(matches!(error, RefactorError::Usage(_)));
assert!(
error.to_string().contains("expected <old>=<new>"),
"{error}"
);
let missing = root.path().join("missing-project");
let cause = fs::canonicalize(&missing).unwrap_err();
let error = refactor(&missing, &["old=new".into()], false).unwrap_err();
assert_eq!(error.to_string(), cause.to_string());
match error {
RefactorError::Io(error) => assert_eq!(error.kind(), io::ErrorKind::NotFound),
RefactorError::Usage(message) => {
panic!("filesystem failure became a usage error: {message}")
}
}
assert!(!root.path().join("evidence").exists());
assert!(!missing.exists());
}
Input 3: outline tests/refactor.rs fn[invalid_mappings_fail_with_a_usage_error_and_change_nothing]
- File:
tests/refactor.rs - Selector:
fn[invalid_mappings_fail_with_a_usage_error_and_change_nothing] - Kind:
fn tests/refactor.rs:419–486
// {test}`requirements.refactor.invalid_mappings`
// {test}`requirements.refactor.mappings`
#[test]
fn invalid_mappings_fail_with_a_usage_error_and_change_nothing() {
let temp = project();
let root = temp.path();
author(root);
fs::create_dir_all(root.join("rules.load")).unwrap();
let before = snapshot(root);
for (args, message) in [
(vec!["--map", "rules.load"], "expected <old>=<new>"),
(vec!["--map", "=core.load"], "expected <old>=<new>"),
(vec!["--map", "rules.check="], "expected <old>=<new>"),
(vec!["--map", "src/config.rs="], "expected <old>=<new>"),
(vec!["--map", "rules.check=rules.check"], "different"),
(vec!["--map", "rules.load=core.load"], "ambiguous"),
(
vec!["--map", "rules.check=core.check"],
"no definition named core.check",
),
(
vec!["--map", "src/config.rs=src/gone.rs"],
"src/gone.rs does not exist",
),
(
vec!["--map", "src/config.rs#fn[load]=src/config.rs#fn[gone]"],
"does not exist",
),
(
vec!["--map", "src=md", "--map", "src/config.rs=data.txt"],
"overlap",
),
(
vec![
"--map",
"rules.check=rules.load",
"--map",
"rules=rules.load",
],
"mappings of rules.check and rules overlap",
),
(
vec![
"--map",
"src/config.rs#fn[load]=src/config.rs#fn[check]",
"--map",
"src/config.rs#=src/config.rs#fn[verify]",
],
"overlap",
),
(
vec!["--map", "#fn[load]=src/config.rs#fn[load]"],
"a node needs a file",
),
(
vec!["--map", "nothing.here=data.txt"],
"names no recorded definition, file, or node",
),
] {
let output = refactor(root, &args);
assert_eq!(output.status.code(), Some(2), "{args:?}: {output:?}");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains(message), "{args:?}: {stderr}");
assert!(stderr.contains("Usage:"), "{stderr}");
assert_eq!(snapshot(root), before, "{args:?}");
}
assert_eq!(refactor(root, &[]).status.code(), Some(2));
}
Other sources
Input 1: requirements selection requirements.refactor.invalid_mappings
requirements.refactor.invalid_mappings:md/requirements/refactor.md:41–46
:::{req} invalid_mappings
`habeas refactor` MUST fail with a usage error, before changing anything, for a
malformed mapping, a mapping whose old side could be read as both a path and a
definition name, two mappings with overlapping old sides, or a mapping whose
new side does not exist.
:::
Input 4: 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 5: 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 6: outline tests/refactor.rs fn[refactor]
- File:
tests/refactor.rs - Selector:
fn[refactor] - Kind:
fn tests/refactor.rs:125–134
// {cite}`habeas:util`
/// Runs `habeas refactor` in `root`.
fn refactor(root: &Path, args: &[&str]) -> std::process::Output {
Command::new(env!("CARGO_BIN_EXE_habeas"))
.arg("refactor")
.args(args)
.current_dir(root)
.output()
.unwrap()
}
Input 7: outline tests/refactor.rs fn[snapshot]
- File:
tests/refactor.rs - Selector:
fn[snapshot] - Kind:
fn tests/refactor.rs:136–152
/// Every file under `root`, by relative path.
fn snapshot(root: &Path) -> BTreeMap<String, Vec<u8>> {
let mut files = BTreeMap::new();
let mut pending = vec![root.to_owned()];
while let Some(directory) = pending.pop() {
for entry in fs::read_dir(directory).unwrap() {
let path = entry.unwrap().path();
if path.is_dir() {
pending.push(path);
} else {
let relative = path.strip_prefix(root).unwrap().display().to_string();
files.insert(relative, fs::read(&path).unwrap());
}
}
}
files
}