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
49 changes: 33 additions & 16 deletions crates/socket-patch-cli/tests/remove/remove_failure_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,31 +1051,48 @@ fn remove_detached_skip_rollback_refused() {

/// Plant `.socket/blobs`, `.socket/diffs`, `.socket/packages` as regular
/// FILES so every cleanup read_dir errors: the remove must still succeed
/// (repair's posture: warn and continue, never fatal), with one stderr
/// warning per store.
/// (repair's posture: warn and continue, never fatal), with one
/// `cleanup_failed` run warning per store. The human `Warning:` lines are
/// checked in `remove_render.rs`.
#[test]
fn remove_cleanup_failures_warn_not_fatal() {
let tmp = tempfile::tempdir().expect("tempdir");
let purl = "pkg:npm/__covgap_cleanup__@1.0.0";
let socket =
write_manifest_files_empty(tmp.path(), purl, "13131313-1313-4131-8131-131313131313");
for name in ["blobs", "diffs", "packages"] {
std::fs::write(socket.join(name), b"not a directory").unwrap();
}
let socket = plant_unsweepable_stores(tmp.path(), purl);

let (code, stdout, stderr) = run_remove(tmp.path(), &[purl, "--yes", "--skip-rollback"], &[]);
let (code, stdout, stderr) = run_remove(
tmp.path(),
&[purl, "--json", "--yes", "--skip-rollback"],
&[],
);
assert_eq!(
code, 0,
"cleanup failures must never fail the remove; stdout=\n{stdout}\nstderr=\n{stderr}"
);
for store in ["blob", "diffs", "packages"] {
assert!(
stderr
.lines()
.any(|l| l.starts_with("Warning:") && l.contains(&format!("{store} cleanup"))),
"the {store}-sweep failure must warn; got:\n{stderr}"
);
}
let v = parse_json_envelope(&stdout);
assert_eq!(v["status"], "success", "envelope={v}");
assert_eq!(
warning_codes(&v),
["cleanup_failed"; 3],
"one warning per failed sweep; envelope={v}"
);
// One warning per pass, not one pass warned three times. Which store
// each names is wording, checked in `remove_render.rs`.
let mut details: Vec<&str> = v["warnings"]
.as_array()
.expect("warnings array")
.iter()
.filter_map(|w| w["detail"].as_str())
.collect();
details.sort_unstable();
details.dedup();
assert_eq!(details.len(), 3, "envelope={v}");
assert_eq!(v["summary"]["removed"], 1, "envelope={v}");
assert_eq!(event_purls(&v, "removed"), [purl], "envelope={v}");
assert!(
!stderr.contains("Warning:"),
"--json must not also print the warnings; stderr=\n{stderr}"
);
let manifest: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(socket.join("manifest.json")).unwrap())
.unwrap();
Expand Down
11 changes: 11 additions & 0 deletions crates/socket-patch-cli/tests/remove/remove_fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ pub(crate) fn write_manifest_files_empty(root: &Path, purl: &str, uuid: &str) ->
socket
}

/// A one-entry manifest for `purl` whose `.socket/blobs`, `.socket/diffs`
/// and `.socket/packages` are regular FILES, so every cleanup sweep fails.
/// Returns the `.socket` dir.
pub(crate) fn plant_unsweepable_stores(root: &Path, purl: &str) -> PathBuf {
let socket = write_manifest_files_empty(root, purl, "13131313-1313-4131-8131-131313131313");
for name in ["blobs", "diffs", "packages"] {
std::fs::write(socket.join(name), b"not a directory").unwrap();
}
socket
}

/// Two-entry `files: {}` manifest (purl1/uuid1, purl2/uuid2).
pub(crate) fn write_two_entry_manifest(
root: &Path,
Expand Down
21 changes: 21 additions & 0 deletions crates/socket-patch-cli/tests/remove/remove_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,24 @@ mod pty {
);
}
}

/// Human-mode twin of `remove_cleanup_failures_warn_not_fatal`: each
/// failed sweep prints one `Warning:` line on stderr naming its store,
/// and the remove still succeeds.
#[test]
fn remove_cleanup_failures_print_one_warning_per_store() {
let tmp = tempfile::tempdir().expect("tempdir");
let purl = "pkg:npm/__covgap_cleanup__@1.0.0";
plant_unsweepable_stores(tmp.path(), purl);

let (code, stdout, stderr) = run_remove(tmp.path(), &[purl, "--yes", "--skip-rollback"], &[]);
assert_eq!(code, 0, "stdout=\n{stdout}\nstderr=\n{stderr}");
for store in ["blob", "diffs", "packages"] {
let prefix = format!("Warning: {store} cleanup failed: ");
let warnings = stderr.lines().filter(|l| l.starts_with(&prefix)).count();
assert_eq!(
warnings, 1,
"the {store} sweep must warn once; got:\n{stderr}"
);
}
}
Loading