cargo-reclaim 0.2.2

Safe Cargo cleanup for target directories, stale artifacts, and Cargo home caches
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::error::Error;
use std::fs;
use std::path::PathBuf;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use cargo_reclaim::{
    ApplyEntryStatus, ArtifactClass, InventoryOptions, PathKind, PathSnapshot, Plan, PlanAction,
    PlanCommandKind, PlanEditRequest, PlanEntry, PlanId, PlanInput, PlanInvocation,
    PlanPersistenceError, PlannerOptions, PolicyKind, SavePlanOptions, ScannerOptions,
    TargetEvidence, edit_persisted_plan, execute_persisted_plan_apply, persist_plan, snapshot_path,
    validate_persisted_plan_for_apply,
};

#[test]
fn apply_validation_revalidates_delete_candidates_without_deleting() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_validate")?;
    let file = temp.path.join("target/debug/incremental/cache.bin");
    fs::create_dir_all(file.parent().expect("file parent"))?;
    fs::write(&file, b"abc")?;
    let document = persisted_plan_for_path(&file, 3)?;

    let report =
        validate_persisted_plan_for_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.totals.would_delete_count, 1);
    assert_eq!(report.totals.would_delete_bytes, 3);
    assert!(report.dry_run);
    assert!(file.is_file());
    Ok(())
}

#[test]
fn apply_validation_reports_stale_plan_when_path_changes() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_stale")?;
    let file = temp.path.join("target/debug/incremental/cache.bin");
    fs::create_dir_all(file.parent().expect("file parent"))?;
    fs::write(&file, b"abc")?;
    let document = persisted_plan_for_path(&file, 3)?;
    fs::write(&file, b"changed")?;

    let report =
        validate_persisted_plan_for_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.totals.would_delete_count, 0);
    assert_eq!(report.totals.stale_skip_count, 1);
    assert!(report.entries[0].reason.contains("skip_stale_plan"));
    Ok(())
}

#[test]
fn apply_validation_reports_stale_plan_when_same_size_content_changes() -> Result<(), Box<dyn Error>>
{
    let temp = TestTemp::new("apply_same_size_stale")?;
    let file = temp.path.join("target/debug/incremental/cache.bin");
    fs::create_dir_all(file.parent().expect("file parent"))?;
    fs::write(&file, b"abc")?;
    let mut document = persisted_plan_for_path(&file, 3)?;
    document.body.plan.entries[0].snapshot.modified = None;
    document.id = PlanId::from_body(&document.body)?;
    fs::write(&file, b"xyz")?;

    let report =
        validate_persisted_plan_for_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.totals.would_delete_count, 0);
    assert_eq!(report.totals.stale_skip_count, 1);
    assert!(
        report.entries[0]
            .reason
            .contains("content fingerprint changed")
    );
    Ok(())
}

#[test]
fn apply_validation_rejects_expired_plans() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_expired")?;
    let file = temp.path.join("target/debug/incremental/cache.bin");
    fs::create_dir_all(file.parent().expect("file parent"))?;
    fs::write(&file, b"abc")?;
    let document = persisted_plan_for_path(&file, 3)?;

    assert!(matches!(
        validate_persisted_plan_for_apply(&document, UNIX_EPOCH + Duration::from_secs(5_000)),
        Err(PlanPersistenceError::PlanExpired)
    ));
    Ok(())
}

#[test]
fn apply_execution_deletes_revalidated_file() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_execute_file")?;
    let file = temp.path.join("target/debug/incremental/cache.bin");
    fs::create_dir_all(file.parent().expect("file parent"))?;
    fs::write(&file, b"abc")?;
    let document = persisted_plan_for_path(&file, 3)?;

    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert!(!report.dry_run);
    assert_eq!(report.entries[0].status, ApplyEntryStatus::Deleted);
    assert_eq!(report.totals.applied_count, 1);
    assert_eq!(report.totals.applied_bytes, 3);
    assert_eq!(report.totals.failed_count, 0);
    assert!(!file.exists());
    Ok(())
}

#[test]
fn apply_execution_deletes_revalidated_directory() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_execute_dir")?;
    let directory = temp.path.join("target/debug/incremental");
    fs::create_dir_all(directory.join("session"))?;
    fs::write(directory.join("session/cache.bin"), b"abc")?;
    let document = persisted_plan_for_directory(&directory, 3, false)?;

    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.entries[0].status, ApplyEntryStatus::Deleted);
    assert_eq!(report.totals.applied_count, 1);
    assert!(!directory.exists());
    Ok(())
}

#[test]
fn apply_execution_reports_measured_deleted_bytes_for_shallow_directory()
-> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_execute_dir_measured_bytes")?;
    let directory = temp.path.join("target/debug/incremental");
    let expected_deleted_bytes = 7;
    fs::create_dir_all(directory.join("session"))?;
    fs::write(directory.join("session/cache.bin"), b"abc")?;
    fs::write(directory.join("session/other.bin"), b"defg")?;
    let document = persisted_plan_for_directory(&directory, 0, false)?;

    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.entries[0].status, ApplyEntryStatus::Deleted);
    assert_eq!(report.entries[0].size_bytes, 0);
    assert_eq!(
        report.entries[0].deleted_bytes,
        Some(expected_deleted_bytes)
    );
    assert_eq!(report.totals.applied_bytes, expected_deleted_bytes);
    assert!(!directory.exists());
    Ok(())
}

#[test]
fn apply_execution_deletes_revalidated_whole_target() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_execute_whole_target")?;
    write_manifest(&temp.path)?;
    let target = temp.path.join("target");
    fs::create_dir_all(target.join("debug/incremental"))?;
    fs::write(target.join("debug/incremental/cache.bin"), b"abc")?;
    let document = persisted_whole_target_plan_for_project(&target, temp.path.join("Cargo.toml"))?;

    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.entries[0].status, ApplyEntryStatus::Deleted);
    assert_eq!(report.totals.applied_count, 1);
    assert!(!target.exists());
    Ok(())
}

#[test]
fn apply_execution_skips_whole_target_when_project_manifest_is_missing()
-> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_whole_target_missing_manifest")?;
    write_manifest(&temp.path)?;
    let manifest = temp.path.join("Cargo.toml");
    let target = temp.path.join("target");
    fs::create_dir_all(target.join("debug/incremental"))?;
    fs::write(target.join("debug/incremental/cache.bin"), b"abc")?;
    let document = persisted_whole_target_plan_for_project(&target, &manifest)?;
    fs::remove_file(manifest)?;

    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.entries[0].status, ApplyEntryStatus::SkipStalePlan);
    assert!(report.entries[0].reason.contains("project manifest"));
    assert!(target.is_dir());
    Ok(())
}

#[test]
fn apply_execution_skips_whole_target_when_marker_is_missing() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_whole_target_missing_marker")?;
    let target = temp.path.join("target");
    fs::create_dir_all(target.join("debug/incremental"))?;
    fs::write(
        target.join("CACHEDIR.TAG"),
        b"Signature: 8a477f597d28d172789f06886806bc55",
    )?;
    fs::write(target.join("debug/incremental/cache.bin"), b"abc")?;
    let document =
        persisted_whole_target_plan(&target, TargetEvidence::strong_marker("CACHEDIR.TAG")?)?;
    let marker_size = fs::metadata(target.join("CACHEDIR.TAG"))?.len();
    fs::remove_file(target.join("CACHEDIR.TAG"))?;
    fs::write(
        target.join("same-size-placeholder"),
        vec![b'x'; marker_size as usize],
    )?;

    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.entries[0].status, ApplyEntryStatus::SkipStalePlan);
    assert!(report.entries[0].reason.contains("skip_stale_plan"));
    assert!(target.is_dir());
    Ok(())
}

#[test]
#[cfg(unix)]
fn apply_execution_skips_whole_target_replaced_by_symlink() -> Result<(), Box<dyn Error>> {
    use std::os::unix::fs::symlink;

    let temp = TestTemp::new("apply_whole_target_symlink")?;
    write_manifest(&temp.path)?;
    let target = temp.path.join("target");
    fs::create_dir_all(target.join("debug/incremental"))?;
    fs::write(target.join("debug/incremental/cache.bin"), b"abc")?;
    let document = persisted_whole_target_plan_for_project(&target, temp.path.join("Cargo.toml"))?;
    fs::remove_dir_all(&target)?;
    fs::create_dir(temp.path.join("replacement"))?;
    symlink(temp.path.join("replacement"), &target)?;

    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.entries[0].status, ApplyEntryStatus::SkipStalePlan);
    assert!(report.entries[0].reason.contains("symlink"));
    assert!(target.exists());
    Ok(())
}

#[test]
fn apply_execution_skips_stale_paths_without_deleting() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_execute_stale")?;
    let file = temp.path.join("target/debug/incremental/cache.bin");
    fs::create_dir_all(file.parent().expect("file parent"))?;
    fs::write(&file, b"abc")?;
    let document = persisted_plan_for_path(&file, 3)?;
    fs::write(&file, b"changed")?;

    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.entries[0].status, ApplyEntryStatus::SkipStalePlan);
    assert_eq!(report.totals.applied_count, 0);
    assert_eq!(report.totals.stale_skip_count, 1);
    assert!(file.is_file());
    Ok(())
}

#[test]
fn apply_execution_skips_entries_requiring_confirmation() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_execute_confirmation")?;
    let directory = temp.path.join("target");
    fs::create_dir_all(directory.join("debug/incremental"))?;
    fs::write(directory.join("debug/incremental/cache.bin"), b"abc")?;
    let document = persisted_plan_for_directory(&directory, 3, true)?;

    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(
        report.entries[0].status,
        ApplyEntryStatus::NotPlannedForDeletion
    );
    assert_eq!(report.totals.applied_count, 0);
    assert!(directory.is_dir());
    Ok(())
}

#[test]
fn apply_execution_deletes_confirmation_entry_after_selection() -> Result<(), Box<dyn Error>> {
    let temp = TestTemp::new("apply_execute_selected_confirmation")?;
    let directory = temp.path.join("target/debug/incremental");
    fs::create_dir_all(directory.join("session"))?;
    fs::write(directory.join("session/cache.bin"), b"abc")?;
    let mut document = persisted_plan_for_directory(&directory, 3, true)?;

    edit_persisted_plan(
        &mut document,
        &PlanEditRequest::new_with_indices(Vec::new(), Vec::new(), vec![1], Vec::new())?,
        UNIX_EPOCH + Duration::from_secs(1_100),
    )?;
    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;

    assert_eq!(report.entries[0].status, ApplyEntryStatus::Deleted);
    assert_eq!(report.totals.applied_count, 1);
    assert!(!directory.exists());
    Ok(())
}

#[test]
#[cfg(unix)]
fn apply_execution_reports_delete_failures() -> Result<(), Box<dyn Error>> {
    use std::os::unix::fs::PermissionsExt;

    let temp = TestTemp::new("apply_execute_failed")?;
    let directory = temp.path.join("target/debug/incremental");
    fs::create_dir_all(directory.join("session"))?;
    fs::write(directory.join("session/cache.bin"), b"abc")?;
    let document = persisted_plan_for_directory(&directory, 3, false)?;

    fs::set_permissions(&directory, fs::Permissions::from_mode(0o555))?;
    let report = execute_persisted_plan_apply(&document, UNIX_EPOCH + Duration::from_secs(1_100))?;
    fs::set_permissions(&directory, fs::Permissions::from_mode(0o755))?;

    assert_eq!(report.entries[0].status, ApplyEntryStatus::DeleteFailed);
    assert_eq!(report.totals.failed_count, 1);
    assert!(directory.is_dir());
    Ok(())
}

fn persisted_plan_for_path(
    path: impl Into<PathBuf>,
    size_bytes: u64,
) -> Result<cargo_reclaim::PersistedPlan, Box<dyn Error>> {
    let path = path.into();
    let modified = fs::metadata(&path)?.modified().ok();
    let snapshot = PathSnapshot::with_details(path, size_bytes, PathKind::File, modified)?;
    let entry = PlanEntry::new(
        snapshot,
        ArtifactClass::Incremental,
        TargetEvidence::project_context("Cargo.toml")?,
        PlanAction::Delete,
        "derived intermediate output",
        false,
    )?;
    let plan = Plan::new(PlanInput::from_root(".")?, vec![entry]);
    Ok(persist_plan(
        &plan,
        SavePlanOptions {
            created_at: UNIX_EPOCH + Duration::from_secs(1_000),
            expires_at: UNIX_EPOCH + Duration::from_secs(2_000),
            interactive_selection_modified: false,
            invocation: PlanInvocation::new(
                PlanCommandKind::Plan,
                PolicyKind::Balanced,
                &ScannerOptions::default(),
                &InventoryOptions::default(),
                &PlannerOptions::default(),
            ),
        },
    )?)
}

fn persisted_plan_for_directory(
    path: impl Into<PathBuf>,
    size_bytes: u64,
    requires_confirmation: bool,
) -> Result<cargo_reclaim::PersistedPlan, Box<dyn Error>> {
    let path = path.into();
    let modified = fs::metadata(&path)?.modified().ok();
    let snapshot = PathSnapshot::with_details(path, size_bytes, PathKind::Directory, modified)?;
    let entry = PlanEntry::new(
        snapshot,
        ArtifactClass::Incremental,
        TargetEvidence::project_context("Cargo.toml")?,
        if requires_confirmation {
            PlanAction::RequiresConfirmation
        } else {
            PlanAction::Delete
        },
        "derived intermediate output",
        requires_confirmation,
    )?;
    let plan = Plan::new(PlanInput::from_root(".")?, vec![entry]);
    Ok(persist_plan(
        &plan,
        SavePlanOptions {
            created_at: UNIX_EPOCH + Duration::from_secs(1_000),
            expires_at: UNIX_EPOCH + Duration::from_secs(2_000),
            interactive_selection_modified: false,
            invocation: PlanInvocation::new(
                PlanCommandKind::Plan,
                PolicyKind::Balanced,
                &ScannerOptions::default(),
                &InventoryOptions::default(),
                &PlannerOptions::default(),
            ),
        },
    )?)
}

fn persisted_whole_target_plan_for_project(
    target: impl Into<PathBuf>,
    manifest: impl Into<PathBuf>,
) -> Result<cargo_reclaim::PersistedPlan, Box<dyn Error>> {
    persisted_whole_target_plan(target, TargetEvidence::project_context(manifest.into())?)
}

fn persisted_whole_target_plan(
    target: impl Into<PathBuf>,
    evidence: TargetEvidence,
) -> Result<cargo_reclaim::PersistedPlan, Box<dyn Error>> {
    let target = target.into();
    let snapshot = snapshot_path(&target, &InventoryOptions::default())?;
    let entry = PlanEntry::new(
        snapshot,
        ArtifactClass::WholeTarget,
        evidence,
        PlanAction::Delete,
        "aggressive policy permits confirmed whole-target deletion",
        false,
    )?;
    let plan = Plan::new(PlanInput::from_root(".")?, vec![entry]);
    Ok(persist_plan(
        &plan,
        SavePlanOptions {
            created_at: UNIX_EPOCH + Duration::from_secs(1_000),
            expires_at: UNIX_EPOCH + Duration::from_secs(2_000),
            interactive_selection_modified: true,
            invocation: PlanInvocation::new(
                PlanCommandKind::Plan,
                PolicyKind::Aggressive,
                &ScannerOptions::default(),
                &InventoryOptions::default(),
                &PlannerOptions {
                    whole_target_mode: cargo_reclaim::WholeTargetMode::DeleteConfirmed,
                    ..PlannerOptions::default()
                },
            ),
        },
    )?)
}

fn write_manifest(path: &std::path::Path) -> Result<(), Box<dyn Error>> {
    fs::write(path.join("Cargo.toml"), "[package]\nname = \"sample\"\n")?;
    Ok(())
}

struct TestTemp {
    path: PathBuf,
}

impl TestTemp {
    fn new(name: &str) -> Result<Self, Box<dyn Error>> {
        let unique = SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos();
        let path = std::env::temp_dir().join(format!(
            "cargo_reclaim_{name}_{}_{}",
            std::process::id(),
            unique
        ));
        fs::create_dir(&path)?;
        Ok(Self { path })
    }
}

impl Drop for TestTemp {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.path);
    }
}