voom 0.5.0

Fast, safe, parallel build-artifact pruning across every major language ecosystem
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! The command-line surface: flags, output formats, and exit codes.
//!
//! Exit codes are the contract the published git hooks depend on (ADR 0009), so they are
//! asserted against the real binary rather than against the library.

// Helper functions here are not `#[test]` bodies, which is all `allow-expect-in-tests` covers.
// In a test crate a panic is the failure report, which is exactly what these want.
#![allow(clippy::expect_used, clippy::unwrap_used)]

mod support;

use std::fs;
use std::path::Path;

use assert_cmd::Command;
use predicates::prelude::PredicateBooleanExt;
use predicates::str::contains;
use support::snapshot;
use tempfile::TempDir;

/// A tree with one proven Rust artifact, one unproven `target/` holding a file nobody can
/// regenerate, and one opt-in `bin/`.
fn mixed_tree() -> TempDir {
    let root = TempDir::new().expect("a temporary directory");
    let path = root.path();
    fs::create_dir_all(path.join("proven/target/debug")).unwrap();
    fs::write(path.join("proven/Cargo.toml"), b"[package]").unwrap();
    fs::write(path.join("proven/target/debug/app"), vec![0u8; 4096]).unwrap();

    fs::create_dir_all(path.join("unproven/target")).unwrap();
    fs::write(path.join("unproven/target/notes.txt"), b"irreplaceable").unwrap();

    fs::create_dir_all(path.join("gotool/bin")).unwrap();
    fs::write(path.join("gotool/go.mod"), b"module example").unwrap();
    fs::write(path.join("gotool/bin/tool"), b"binary").unwrap();
    root
}

fn voom() -> Command {
    Command::cargo_bin("voom").expect("the binary builds")
}

#[test]
fn should_exit_zero_and_report_nothing_on_a_clean_tree() {
    let root = TempDir::new().unwrap();
    fs::write(root.path().join("README.md"), b"nothing to do").unwrap();

    voom()
        .arg(root.path())
        .assert()
        .success()
        .stdout(contains("0 artifacts"));
}

/// The contract `.pre-commit-hooks.yaml` and `poly-hooks.toml` are committed against:
/// `--dry-run --report --exit-code` fails on findings and deletes nothing.
#[test]
fn should_exit_three_for_the_reporting_hook_when_there_are_findings() {
    let tree = mixed_tree();
    let before = snapshot(tree.path());

    voom()
        .args(["--dry-run", "--report", "--exit-code"])
        .arg(tree.path())
        .assert()
        .code(3);

    assert_eq!(
        snapshot(tree.path()),
        before,
        "the reporting hook must not touch the tree"
    );
}

#[test]
fn should_exit_zero_for_the_reporting_hook_when_there_is_nothing_to_find() {
    let root = TempDir::new().unwrap();
    fs::write(root.path().join("README.md"), b"clean").unwrap();

    voom()
        .args(["--dry-run", "--report", "--exit-code"])
        .arg(root.path())
        .assert()
        .success();
}

#[test]
fn should_exit_two_on_a_usage_error() {
    voom()
        .args(["--min-age", "forever", "."])
        .assert()
        .code(2)
        .stderr(contains("not a duration"));
    voom()
        .args(["--ecosystem", "rustlang", "."])
        .assert()
        .code(2)
        .stderr(contains("rustlang"));
    voom().arg("/no/such/tree").assert().code(2);
}

#[test]
fn should_remove_only_what_a_marker_proves() {
    let tree = mixed_tree();
    voom().arg(tree.path()).assert().success();

    assert!(
        !tree.path().join("proven/target").exists(),
        "the proven artifact is swept"
    );
    assert!(
        tree.path().join("unproven/target/notes.txt").exists(),
        "unproven data survives"
    );
    assert!(
        tree.path().join("gotool/bin/tool").exists(),
        "an opt-in artifact survives"
    );
    assert!(tree.path().join("proven/Cargo.toml").exists(), "source survives");
}

#[test]
fn should_explain_every_skip_under_verbose() {
    let tree = mixed_tree();
    voom()
        .args(["--dry-run", "--verbose"])
        .arg(tree.path())
        .assert()
        .success()
        .stdout(contains("no rust marker"))
        .stdout(contains("not enabled"))
        .stdout(contains("go.bin"));
}

#[test]
fn should_hide_skip_detail_by_default_but_say_how_to_get_it() {
    let tree = mixed_tree();
    voom()
        .args(["--dry-run"])
        .arg(tree.path())
        .assert()
        .success()
        .stdout(contains("skipped"))
        .stdout(contains("--verbose for why"))
        .stdout(contains("no rust marker").not());
}

#[test]
fn should_emit_parseable_json_with_a_schema_version() {
    let tree = mixed_tree();
    let output = voom()
        .args(["--dry-run", "--format", "json"])
        .arg(tree.path())
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let document: serde_json::Value = serde_json::from_slice(&output).expect("stdout parses as JSON");
    assert_eq!(document["schema_version"], voom::report::SCHEMA_VERSION);
    assert_eq!(document["dry_run"], true);
    assert_eq!(document["totals"]["reclaimed"], 1);
    let reasons: Vec<_> = document["skipped"]
        .as_array()
        .unwrap()
        .iter()
        .map(|skip| skip["reason"].clone())
        .collect();
    assert!(reasons.contains(&serde_json::Value::from("no_marker")));
}

/// Diagnostics go to stderr and results to stdout, so `voom ~ --format json | jq` works
/// without filtering (ADR 0007).
#[test]
fn should_keep_json_on_stdout_free_of_diagnostics() {
    let tree = mixed_tree();
    let assert = voom()
        .args(["--dry-run", "--format", "json"])
        .arg(tree.path())
        .assert()
        .success();
    let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(
        serde_json::from_str::<serde_json::Value>(&stdout).is_ok(),
        "stdout is exactly one JSON document"
    );
}

#[test]
fn should_enable_an_opt_in_artifact_on_request() {
    let tree = mixed_tree();
    voom().args(["--enable", "go.bin"]).arg(tree.path()).assert().success();
    assert!(!tree.path().join("gotool/bin").exists());
}

#[test]
fn should_restrict_to_the_named_ecosystems() {
    let tree = mixed_tree();
    voom().args(["--ecosystem", "node"]).arg(tree.path()).assert().success();
    assert!(tree.path().join("proven/target").exists(), "rust was not selected");
}

#[test]
fn should_hold_everything_when_min_age_covers_it() {
    let tree = mixed_tree();
    let before = snapshot(tree.path());
    voom().args(["--min-age", "30d"]).arg(tree.path()).assert().success();
    assert_eq!(snapshot(tree.path()), before);
}

#[test]
fn should_print_only_the_footer_with_summary() {
    let tree = mixed_tree();
    voom()
        .args(["--dry-run", "--summary"])
        .arg(tree.path())
        .assert()
        .success()
        .stdout(contains("would remove").not())
        .stdout(contains("reclaimable"));
}

/// Tool caches are skipped by location, before classification, because an installed toolchain
/// and a built project are the same shape on disk (ADR 0001). Driven through the binary with a
/// fake `HOME`, which is process-scoped and so leaves other tests alone.
///
/// Unix only, and not because the behaviour is: `dirs::home_dir()` reads `$HOME` on Unix but
/// goes through the Shell known-folder API on Windows, so no environment variable can point it
/// at a fixture there. Faking a real profile directory would mean writing into the actual one.
/// The resolution this exercises takes `home` as an argument and is covered on every platform
/// by the unit tests in `src/caches.rs`; what is Unix-only here is the end-to-end wiring.
#[test]
#[cfg(unix)]
fn should_skip_a_tool_cache_under_home_and_sweep_it_with_the_flag() {
    let home = TempDir::new().unwrap();
    let cache = home.path().join(".npm/_cacache/pkg");
    fs::create_dir_all(cache.join("dist")).unwrap();
    fs::write(cache.join("package.json"), b"{}").unwrap();
    fs::write(cache.join("dist/bundle.js"), b"built").unwrap();

    let project = home.path().join("project");
    fs::create_dir_all(project.join("dist")).unwrap();
    fs::write(project.join("package.json"), b"{}").unwrap();
    fs::write(project.join("dist/bundle.js"), b"built").unwrap();

    voom()
        .env("HOME", home.path())
        .args(["--dry-run", "--format", "json"])
        .arg(home.path())
        .assert()
        .success()
        .stdout(contains("project/dist").and(contains("_cacache").not()));

    voom()
        .env("HOME", home.path())
        .args(["--dry-run", "--caches", "--format", "json"])
        .arg(home.path())
        .assert()
        .success()
        .stdout(contains("project/dist").and(contains("_cacache")));
}

/// Pointing voom *at* a cache is explicit intent. The skip is a rule about what a walk wanders
/// into, not about what the user asked for, so it must not need the flag.
///
/// Unix only for the reason above — and this one had to be gated rather than left alone,
/// because on Windows the fake `HOME` was ignored, no cache root resolved, and the assertion
/// passed without the skip logic ever running. A test that cannot fail is not a test.
#[test]
#[cfg(unix)]
fn should_sweep_a_cache_named_as_the_scan_root_without_the_flag() {
    let home = TempDir::new().unwrap();
    let cache = home.path().join(".npm/_cacache/pkg");
    fs::create_dir_all(cache.join("dist")).unwrap();
    fs::write(cache.join("package.json"), b"{}").unwrap();
    fs::write(cache.join("dist/bundle.js"), b"built").unwrap();

    voom()
        .env("HOME", home.path())
        .arg(home.path().join(".npm"))
        .assert()
        .success();

    assert!(!cache.join("dist").exists(), "a cache the user named outright is swept");
    assert!(cache.join("package.json").exists(), "and only its build output is");
}

/// A tree with a proven `node_modules/` and a `dist/` beside it.
fn node_tree() -> TempDir {
    let root = TempDir::new().expect("a temporary directory");
    fs::write(root.path().join("package.json"), b"{}").unwrap();
    fs::create_dir_all(root.path().join("node_modules/left-pad")).unwrap();
    fs::write(root.path().join("node_modules/left-pad/index.js"), b"module").unwrap();
    fs::create_dir_all(root.path().join("dist")).unwrap();
    fs::write(root.path().join("dist/bundle.js"), b"bundled").unwrap();
    root
}

/// The whole flag, through the real binary: without it the dependency cache is untouched.
#[test]
fn should_leave_node_modules_alone_without_the_dependency_flag() {
    let tree = node_tree();

    voom().arg(tree.path()).assert().success();

    assert!(
        tree.path().join("node_modules/left-pad/index.js").exists(),
        "an ordinary sweep must not reach a dependency cache"
    );
    assert!(!tree.path().join("dist").exists(), "and still sweeps build output");
}

#[test]
fn should_remove_node_modules_with_the_dependency_flag() {
    let tree = node_tree();

    voom().arg("--clean-dependencies").arg(tree.path()).assert().success();

    assert!(!tree.path().join("node_modules").exists());
    assert!(tree.path().join("package.json").exists(), "the source is untouched");
}

/// `--dry-run` is the same pipeline with the last step withheld, and that has to hold for the
/// one flag that reaches directories nothing else does.
#[test]
fn should_predict_a_dependency_removal_in_a_dry_run() {
    let tree = node_tree();
    let before = snapshot(tree.path());

    voom()
        .args(["--clean-dependencies", "--dry-run"])
        .arg(tree.path())
        .assert()
        .success()
        .stdout(contains("node_modules"));

    assert_eq!(snapshot(tree.path()), before, "a dry run must not touch the tree");
}

#[test]
fn should_print_the_catalog() {
    voom()
        .arg("catalog")
        .assert()
        .success()
        .stdout(contains("Rust (rust)"))
        .stdout(contains("Cargo.toml"))
        .stdout(contains("node.build"));
}

#[test]
fn should_show_the_resolved_configuration_with_its_sources() {
    let root = TempDir::new().unwrap();
    fs::write(root.path().join("voom.toml"), "[keep]\nmin_age = \"7d\"\n").unwrap();

    voom()
        .args(["config", "show"])
        .arg(root.path())
        .assert()
        .success()
        .stdout(contains("voom.toml"))
        .stdout(contains("command line"))
        .stdout(contains("min_age"));
}

#[test]
fn should_reject_a_broken_config_by_naming_the_file() {
    let root = TempDir::new().unwrap();
    fs::write(root.path().join("voom.toml"), "[keep]\nmin_age = \"forever\"\n").unwrap();

    voom()
        .arg(root.path())
        .assert()
        .code(2)
        .stderr(contains("voom.toml"))
        .stderr(contains("forever"));
}

/// Two runs over an unchanged tree produce byte-identical output, which is what makes the
/// report diffable and therefore reviewable.
#[test]
fn should_produce_identical_output_across_two_dry_runs() {
    let tree = mixed_tree();
    let render = |tree: &Path| {
        let assert = voom()
            .args(["--dry-run", "--verbose", "--color", "never"])
            .arg(tree)
            .assert()
            .success();
        let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
        // Wall-clock time is the only thing that legitimately varies between two runs over one
        // tree: the headline's total, and the stage breakdown under it. Both are dropped by
        // name rather than by a substring that an artifact path could also contain.
        stdout
            .lines()
            .filter(|line| !line.contains(" reclaimable in ") && !line.contains(" reclaimed in "))
            .filter(|line| !line.trim_start().starts_with("scan "))
            .collect::<Vec<_>>()
            .join("\n")
    };
    assert_eq!(render(tree.path()), render(tree.path()));
}

#[test]
fn should_suppress_color_when_asked() {
    let tree = mixed_tree();
    let assert = voom()
        .args(["--dry-run", "--color", "never"])
        .arg(tree.path())
        .assert()
        .success();
    let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(!stdout.contains('\u{1b}'), "no ANSI escapes when color is off");
}

/// The argument lists the two committed hook manifests actually publish.
///
/// Extracted from the files rather than retyped, because the point is that *those* files keep
/// working. A retyped copy would pass while the published manifest passed something voom no
/// longer accepts, which is the failure this guards. Extraction is a line scan rather than a
/// YAML and a TOML parser: both lines have a fixed shape, and the helper fails loudly if it
/// stops matching rather than quietly returning nothing.
fn published_hook_args(manifest: &str, marker: &str) -> Vec<String> {
    let line = manifest
        .lines()
        .find(|line| line.trim_start().starts_with(marker))
        .unwrap_or_else(|| panic!("no line starting `{marker}` — the manifest format changed"));
    let list = line
        .split_once('[')
        .and_then(|(_, rest)| rest.rsplit_once(']'))
        .unwrap_or_else(|| panic!("no argument list in `{line}`"))
        .0;
    let args: Vec<String> = list
        .split(',')
        .map(|arg| arg.trim().trim_matches('"').to_owned())
        .filter(|arg| !arg.is_empty())
        .collect();
    assert!(!args.is_empty(), "no arguments parsed from `{line}`");
    args
}

/// ADR 0009's contract, against the manifests as committed: report, delete nothing, exit 3 so
/// the commit fails. These files are a release surface — consumers pin them by git revision —
/// so a flag voom stopped accepting would break every consumer at their next hook run, not
/// ours.
#[test]
fn should_honour_the_published_reporting_hook_arguments() {
    let manifests = [
        published_hook_args(include_str!("../.pre-commit-hooks.yaml"), "args:"),
        published_hook_args(include_str!("../poly-hooks.toml"), "args ="),
    ];

    for args in manifests {
        assert!(
            args.iter().any(|arg| arg == "--exit-code"),
            "the reporting hook must ask for the findings exit code: {args:?}"
        );

        let tree = mixed_tree();
        let before = snapshot(tree.path());
        voom().current_dir(tree.path()).args(&args).assert().code(3);
        assert_eq!(snapshot(tree.path()), before, "the reporting hook deletes nothing");

        let clean = TempDir::new().unwrap();
        fs::write(clean.path().join("README.md"), b"clean").unwrap();
        voom().current_dir(clean.path()).args(&args).assert().success();
    }
}

/// An unanchored removal has to be visible in the machine-readable output too, because a hook
/// consuming JSON is exactly the caller that cannot see the human column (ADR 0004).
#[test]
fn should_mark_an_unanchored_removal_in_json() {
    let root = TempDir::new().unwrap();
    fs::create_dir_all(root.path().join("junk")).unwrap();
    fs::write(root.path().join("junk/leftovers.o"), b"output").unwrap();
    fs::write(
        root.path().join("voom.toml"),
        format!("include = ['{}']\n", root.path().join("junk").display()),
    )
    .unwrap();

    voom()
        .args(["--dry-run", "--format", "json"])
        .arg(root.path())
        .assert()
        .success()
        .stdout(contains("\"source\": \"config-include\"").and(contains("\"ecosystem\": null")));
}

/// `--one-file-system` is a safety control with three spellings, and the tri-state clap
/// configuration behind it is easy to break silently. Testing the crossing itself would need a
/// second filesystem; testing that every documented spelling parses does not, and a flag that
/// stops parsing is a flag that stops guarding.
#[test]
fn should_accept_every_spelling_of_the_filesystem_boundary_flag() {
    let tree = mixed_tree();
    for args in [
        vec!["--dry-run"],
        vec!["--dry-run", "--one-file-system"],
        vec!["--dry-run", "--one-file-system=true"],
        vec!["--dry-run", "--one-file-system=false"],
    ] {
        voom()
            .args(&args)
            .arg(tree.path())
            .assert()
            .success()
            .stdout(contains("would remove"));
    }
}

/// Every `uvx` invocation the hook manifests publish must name the package and the command
/// separately.
///
/// `voom` was taken on `PyPI`, so the distribution is `voom-cli` while the command it installs is
/// `voom` (ADR 0010). `uvx voom-cli` therefore does not run voom — uv looks for a command
/// matching the package name, finds none, and refuses with a message pointing at `--from`. The
/// manifests pinned the short form, so the uvx channel of both published hooks would have
/// failed for every user on first run.
///
/// This asymmetry is permanent, which is why it is worth a test rather than a careful edit.
#[test]
fn should_invoke_uvx_with_the_package_named_separately_from_the_command() {
    let manifest = include_str!("../poly-hooks.toml");
    let invocations: Vec<&str> = manifest
        .lines()
        .map(str::trim)
        .filter(|line| line.contains("uvx ") && (line.starts_with("run =") || line.starts_with("install =")))
        .collect();

    assert!(
        invocations.len() >= 2,
        "poly-hooks.toml publishes a uvx channel; the scan found {} invocations",
        invocations.len()
    );

    for line in invocations {
        assert!(
            line.contains("uvx --from voom-cli"),
            "`{line}` runs uvx without --from, so uv will look for a command named `voom-cli` \
             and fail; the package is voom-cli and the command is voom"
        );
    }
}

/// `--force` lives entirely after the rails, and `--dry-run` withholds the step it would act
/// on, so the two together have to be a strict no-op rather than a usage error — a hook alias
/// that carries both must keep working.
#[test]
fn should_accept_force_and_leave_a_dry_run_byte_identical() {
    let tree = mixed_tree();
    let before = support::snapshot(tree.path());

    voom()
        .args(["--dry-run", "--force"])
        .arg(tree.path())
        .assert()
        .success()
        .stdout(contains("would remove"));

    assert_eq!(
        support::snapshot(tree.path()),
        before,
        "a forced dry run changes nothing at all"
    );
}

/// `--caches` only opens tool-cache locations to the walk; removing one takes `--clean-caches`
/// (ADR 0012). Asking for the first while meaning the second is the ordinary mistake, and it is
/// silent — the run simply scans far more and reports almost nothing. See F1 in the gap analysis
/// that prompted this: `--caches` over a real home directory bought 22 MB for 78% more scan time.
#[test]
fn should_note_that_caches_alone_removes_no_cache() {
    let tree = mixed_tree();
    voom()
        .args(["--dry-run", "--caches"])
        .arg(tree.path())
        .assert()
        .success()
        .stderr(contains("--clean-caches"));
}

#[test]
fn should_not_note_anything_when_a_cache_is_actually_named() {
    let tree = mixed_tree();
    voom()
        .args(["--dry-run", "--caches", "--clean-caches", "uv"])
        .arg(tree.path())
        .assert()
        .success()
        .stderr(contains("--clean-caches").not());
}

#[test]
fn should_keep_the_caches_note_off_stdout() {
    let tree = mixed_tree();
    let assert = voom()
        .args(["--dry-run", "--caches", "--format", "json"])
        .arg(tree.path())
        .assert()
        .success();
    let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(
        serde_json::from_str::<serde_json::Value>(&stdout).is_ok(),
        "the advisory must not contaminate a piped JSON document, got: {stdout}"
    );
}