Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/harness-runtime/src/facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,10 @@ mod tests {
Operation::SoftwareUpdate,
Operation::SoftwareRemove,
Operation::PatchInstructionRegion,
// Performed by the kernel but withheld everywhere until a released
// consumer accepts the name — declaring it earlier would make
// every older reader refuse the whole provider-info answer.
Operation::DetachInstructionRegion,
] {
assert!(
!info.declares(optional),
Expand Down
3 changes: 3 additions & 0 deletions crates/harness-runtime/src/human.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,9 @@ fn effect_lines(harness: &Harness, effect: &Effect<'_>, setup_id: Option<&str>)
Effect::PatchInstruction { path, .. } => {
vec![capture, format!("patch instruction region at {path}")]
}
Effect::DetachInstruction { path, .. } => {
vec![capture, format!("detach instruction region at {path}")]
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions crates/harness-runtime/src/instruction_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ pub fn patch(existing: &str, section: &str) -> (String, bool) {
(splice(existing, desired), true)
}

/// Remove the attachment — the owned prefix and the marked region — keeping
/// every other byte. `None` when `existing` carries no marked region, so a
/// repeated detach is a no-op rather than an error. Ambiguous markers never
/// reach here: `validate` refuses them while the plan is built.
#[must_use]
pub fn remove_region(existing: &str) -> Option<String> {
let region = extract(existing)?;
let begin = existing.find(BEGIN)?;
let owned = owned_prefix(existing);
Some(format!(
"{}{}",
&existing[owned.len()..begin],
&existing[begin + region.len()..]
))
}

/// Keep an existing attachment when a setup writes the same path.
#[must_use]
pub fn preserve_in_replacement(existing: &str, incoming: &str) -> String {
Expand Down Expand Up @@ -231,6 +247,26 @@ mod tests {
assert_eq!(keep_region_on_withdraw("just setup"), None);
}

#[test]
fn remove_region_keeps_every_byte_outside_the_attachment() {
let existing = splice("keep-me\n", SECTION);
assert_eq!(remove_region(&existing).as_deref(), Some("keep-me\n"));
assert_eq!(remove_region("keep-me\n"), None);
assert_eq!(remove_region(""), None);
// A file holding only the attachment detaches to nothing.
assert_eq!(remove_region(SECTION).as_deref(), Some(""));
}

#[test]
fn remove_region_takes_the_owned_frontmatter_with_the_region() {
let section = "---\nalwaysApply: true\n---\n\n:::begin-ai-stp\nhello\n:::end-ai-stp\n";
let (first, _) = patch("", section);
assert_eq!(remove_region(&first).as_deref(), Some(""));
// A body between the fence and the markers is not ours.
let with_body = format!("---\nalwaysApply: true\n---\n\nsetup-body\n{SECTION}");
assert_eq!(remove_region(&with_body).as_deref(), Some("setup-body\n"));
}

#[test]
fn yaml_frontmatter_survives_setup_replace_and_withdraw() {
let section = "---\nalwaysApply: true\n---\n\n:::begin-ai-stp\nhello\n:::end-ai-stp\n";
Expand Down
Loading
Loading