mmz 0.14.0

memoized command runner
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
use std::path::Path;

use super::{Invocation, execute};
use crate::clock::Clock;
use crate::disable::Disable;

/// [`super::run`] with the run-anyway switch pinned off rather than read from
/// the environment, so a suite run under `MMZ_DISABLE=1` (a forced `just check`)
/// still tests the skip these cases assert.
fn run(argv: &[String], cwd: &Path) -> crate::error::Result<u8> {
    run_with(argv, cwd, Disable::off())
}

fn run_with(argv: &[String], cwd: &Path, disable: Disable) -> crate::error::Result<u8> {
    execute(&Invocation {
        argv,
        cwd,
        clock: Clock::resolve()?,
        disable,
    })
}

fn write_manifest(dir: &std::path::Path, body: &str) {
    let cfg = dir.join(".mmz");
    std::fs::create_dir_all(&cfg).expect("mkdir .mmz");
    std::fs::write(cfg.join("config.yaml"), body).expect("write manifest");
}

#[test]
fn skips_second_run_when_inputs_unchanged() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    std::fs::write(base.join("a.txt"), b"one").expect("input");
    write_manifest(
        base,
        "scopes:\n  src: [\"*.txt\"]\ncommands:\n  - name: sh\n    inputs: [src]\n",
    );

    let argv = [
        "sh".to_owned(),
        "-c".to_owned(),
        "printf x >> runs.log".to_owned(),
    ];
    assert_eq!(run(&argv, base).expect("run"), 0);
    assert_eq!(run(&argv, base).expect("run"), 0);
    assert_eq!(
        std::fs::read(base.join("runs.log")).expect("log").len(),
        1,
        "skipped once"
    );

    std::fs::write(base.join("a.txt"), b"two").expect("rewrite");
    assert_eq!(run(&argv, base).expect("run"), 0);
    assert_eq!(
        std::fs::read(base.join("runs.log")).expect("log").len(),
        2,
        "input change re-runs"
    );
}

fn run_twice(base: &std::path::Path) {
    let argv = ["sh".to_owned(), "-c".to_owned(), "exit 0".to_owned()];
    assert_eq!(run(&argv, base).expect("first run records"), 0);
    assert_eq!(run(&argv, base).expect("second run is a hit"), 0);
}

#[test]
fn on_hit_paths_are_exercised_on_a_cache_hit() {
    // Per-command on_hit (overrides absent global) with a macro: the hit path
    // resolves and prints the notice.
    let rule = tempfile::tempdir().expect("tempdir");
    std::fs::write(rule.path().join("a.txt"), b"one").expect("input");
    write_manifest(
        rule.path(),
        "scopes:\n  src: [\"*.txt\"]\ncommands:\n  - name: sh\n    inputs: [src]\n    on_hit: \"skip {cache:command}\"\n",
    );
    run_twice(rule.path());

    // An empty global on_hit suppresses the line without erroring.
    let blank = tempfile::tempdir().expect("tempdir");
    std::fs::write(blank.path().join("a.txt"), b"one").expect("input");
    write_manifest(
        blank.path(),
        "scopes:\n  src: [\"*.txt\"]\non_hit: \"\"\ncommands:\n  - name: sh\n    inputs: [src]\n",
    );
    run_twice(blank.path());
}

#[test]
fn propagates_exit_code_and_reruns_after_failure() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    std::fs::write(base.join("a.txt"), b"one").expect("input");
    write_manifest(
        base,
        "scopes:\n  src: [\"*.txt\"]\ncommands:\n  - name: sh\n    inputs: [src]\n",
    );

    let argv = ["sh".to_owned(), "-c".to_owned(), "exit 3".to_owned()];
    assert_eq!(run(&argv, base).expect("run"), 3, "exit code propagates");
    assert_eq!(
        run(&argv, base).expect("run"),
        3,
        "failure was not cached as fresh"
    );
}

/// A rule keyed on `*.txt` that declares the artifact `out/artifact.bin`.
fn write_producer(dir: &std::path::Path) {
    std::fs::write(dir.join("a.txt"), b"one").expect("input");
    std::fs::create_dir_all(dir.join("out")).expect("mkdir out");
    write_manifest(
        dir,
        concat!(
            "scopes:\n  src: [\"*.txt\"]\n",
            "commands:\n  - name: sh\n    inputs: [src]\n",
            "    outputs:\n      - out/artifact.bin\n",
        ),
    );
}

fn build_argv() -> [String; 3] {
    [
        "sh".to_owned(),
        "-c".to_owned(),
        "printf x >> runs.log; printf built > out/artifact.bin".to_owned(),
    ]
}

fn runs(base: &std::path::Path) -> usize {
    std::fs::read(base.join("runs.log")).map_or(0, |bytes| bytes.len())
}

#[test]
fn a_deleted_output_re_runs_a_rule_whose_inputs_never_moved() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_producer(base);

    assert_eq!(run(&build_argv(), base).expect("run"), 0);
    assert_eq!(runs(base), 1, "first run executes");
    assert_eq!(run(&build_argv(), base).expect("run"), 0);
    assert_eq!(runs(base), 1, "artifact present, inputs unchanged: skipped");

    // The `cargo clean` case: the artifact goes, every input stays.
    std::fs::remove_file(base.join("out/artifact.bin")).expect("delete artifact");
    assert_eq!(run(&build_argv(), base).expect("run"), 0);
    assert_eq!(runs(base), 2, "the voided record re-runs the command");
    assert_eq!(run(&build_argv(), base).expect("run"), 0);
    assert_eq!(runs(base), 2, "and the fresh artifact restores the skip");
}

#[test]
fn a_success_without_the_declared_output_errors_and_records_nothing() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_producer(base);

    let liar = ["sh".to_owned(), "-c".to_owned(), "exit 0".to_owned()];
    let err = run(&liar, base).expect_err("a success without its output is refused");
    assert!(
        matches!(&err, crate::Error::MissingOutput { path, .. } if path == "out/artifact.bin"),
        "the error names the missing artifact: {err}"
    );

    let records: Vec<_> = std::fs::read_dir(base.join(".mmz/cache"))
        .map(|entries| entries.filter_map(Result::ok).collect())
        .unwrap_or_default();
    assert!(
        records.is_empty(),
        "no record is written, so the rule cannot skip on a claim it did not honour"
    );

    // The next honest run still executes and records normally.
    assert_eq!(run(&build_argv(), base).expect("run"), 0);
    assert_eq!(runs(base), 1);
    assert_eq!(run(&build_argv(), base).expect("run"), 0);
    assert_eq!(runs(base), 1, "recorded once the artifact really landed");
}

#[test]
fn a_failing_run_is_recorded_as_a_failure_not_an_output_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_producer(base);

    let argv = ["sh".to_owned(), "-c".to_owned(), "exit 3".to_owned()];
    assert_eq!(
        run(&argv, base).expect("a failing run keeps its own exit code"),
        3,
        "the command's failure is the story, not its absent output"
    );
}

#[test]
fn a_glob_in_outputs_is_a_manifest_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    std::fs::write(base.join("a.txt"), b"one").expect("input");
    write_manifest(
        base,
        concat!(
            "scopes:\n  src: [\"*.txt\"]\n",
            "commands:\n  - name: sh\n    inputs: [src]\n",
            "    outputs:\n      - \"out/*.bin\"\n",
        ),
    );
    let argv = ["sh".to_owned(), "-c".to_owned(), "exit 0".to_owned()];
    assert!(
        matches!(run(&argv, base), Err(crate::Error::InvalidOutput { .. })),
        "a pattern is refused at load, not left to never match"
    );
}

#[test]
fn missing_manifest_is_an_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let argv = ["sh".to_owned(), "-c".to_owned(), "exit 0".to_owned()];
    assert!(
        matches!(run(&argv, dir.path()), Err(crate::Error::NoManifest { .. })),
        "no manifest is fatal, not passthrough"
    );
}

#[test]
fn invalid_manifest_is_an_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_manifest(base, "commands:\n  - name: sh\n    inputs: [ghost]\n");
    let argv = ["sh".to_owned(), "-c".to_owned(), "exit 0".to_owned()];
    assert!(run(&argv, base).is_err(), "invalid manifest is fatal");
}

#[test]
fn no_match_errors_under_strict_but_passes_through_when_relaxed() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let argv = ["sh".to_owned(), "-c".to_owned(), "exit 5".to_owned()];

    write_manifest(base, "commands:\n  - name: cargo\n");
    assert!(
        matches!(run(&argv, base), Err(crate::Error::NoMatch { .. })),
        "strict default errors on no match"
    );

    write_manifest(base, "commands:\n  - name: cargo\nstrict: []\n");
    assert_eq!(run(&argv, base).expect("run"), 5, "relaxed runs unmemoized");
}

#[test]
fn empty_input_set_errors_under_strict() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_manifest(
        base,
        "scopes:\n  none: [\"*.none\"]\ncommands:\n  - name: sh\n    inputs: [none]\n",
    );
    let argv = ["sh".to_owned(), "-c".to_owned(), "exit 0".to_owned()];
    assert!(
        matches!(run(&argv, base), Err(crate::Error::NoInputs { .. })),
        "strict default errors on empty input set"
    );
}

#[test]
fn empty_input_set_runs_every_time_when_relaxed() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_manifest(
        base,
        "scopes:\n  none: [\"*.none\"]\ncommands:\n  - name: sh\n    inputs: [none]\nstrict: [no_match]\n",
    );
    let argv = [
        "sh".to_owned(),
        "-c".to_owned(),
        "printf x >> runs.log".to_owned(),
    ];
    assert_eq!(run(&argv, base).expect("run"), 0);
    assert_eq!(run(&argv, base).expect("run"), 0);
    assert_eq!(
        std::fs::read(base.join("runs.log")).expect("log").len(),
        2,
        "relaxed no-inputs never memoizes"
    );
}

/// A parametric rule fanned over `src/**/*.rs`. The executed command appends
/// a byte to `<file>.ran` via sh's `$1`, so a run is observable while a
/// cache hit leaves the counter untouched.
fn write_fan(base: &std::path::Path) {
    std::fs::create_dir_all(base.join("src")).expect("mkdir src");
    std::fs::write(base.join("src/a.rs"), b"a").expect("a");
    std::fs::write(base.join("src/b.rs"), b"b").expect("b");
    write_manifest(
        base,
        "scopes:\n  targets: [\"src/**/*.rs\"]\ncommands:\n  - name: 'sh -c echo>>\"$1\".ran sh {targets}'\n",
    );
}

fn fan_argv(file: &str) -> [String; 5] {
    [
        "sh".to_owned(),
        "-c".to_owned(),
        "echo>>\"$1\".ran".to_owned(),
        "sh".to_owned(),
        file.to_owned(),
    ]
}

fn ran_count(base: &std::path::Path, file: &str) -> usize {
    std::fs::read(base.join(format!("{file}.ran"))).map_or(0, |bytes| bytes.len())
}

#[test]
fn parametric_rule_busts_only_its_own_file() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_fan(base);

    assert_eq!(run(&fan_argv("src/a.rs"), base).expect("run a"), 0);
    assert_eq!(ran_count(base, "src/a.rs"), 1, "a ran once");
    assert_eq!(run(&fan_argv("src/a.rs"), base).expect("run a"), 0);
    assert_eq!(ran_count(base, "src/a.rs"), 1, "a's second run is a hit");

    // b has its own record, unaffected by a.
    assert_eq!(run(&fan_argv("src/b.rs"), base).expect("run b"), 0);
    assert_eq!(ran_count(base, "src/b.rs"), 1, "b ran once");
    assert_eq!(ran_count(base, "src/a.rs"), 1, "a untouched by b");

    // Editing b busts only b; a stays fresh (tight per-file scoping).
    std::fs::write(base.join("src/b.rs"), b"edited").expect("edit b");
    assert_eq!(run(&fan_argv("src/a.rs"), base).expect("run a"), 0);
    assert_eq!(
        ran_count(base, "src/a.rs"),
        1,
        "sibling edit did not bust a"
    );
    assert_eq!(run(&fan_argv("src/b.rs"), base).expect("run b"), 0);
    assert_eq!(ran_count(base, "src/b.rs"), 2, "b's own edit re-ran it");
}

#[test]
fn off_domain_file_falls_through_to_no_match() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_fan(base);
    // A .txt path is not in the `src/**/*.rs` domain, so no rule matches.
    let argv = fan_argv("src/c.txt");
    assert!(
        matches!(run(&argv, base), Err(crate::Error::NoMatch { .. })),
        "an off-domain file does not fan a record"
    );
}

#[test]
fn colliding_parametric_expansions_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    std::fs::write(base.join("a.rs"), b"x").expect("a");
    write_manifest(
        base,
        "scopes:\n  wide: [\"*.rs\"]\n  narrow: [\"a.rs\"]\ncommands:\n  - name: \"do {wide}\"\n  - name: \"do {narrow}\"\n",
    );
    let argv = ["do".to_owned(), "a.rs".to_owned()];
    assert!(
        matches!(
            run(&argv, base),
            Err(crate::Error::CollidingIdentity { .. })
        ),
        "two rules claiming `do a.rs` is a loud error, not a silent winner"
    );
}

#[test]
fn malformed_macro_is_a_config_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_manifest(base, "commands:\n  - name: \"do {a} {b}\"\n");
    let argv = ["do".to_owned(), "x".to_owned()];
    assert!(
        matches!(run(&argv, base), Err(crate::Error::MacroSyntax { .. })),
        "a two-macro name is rejected at load"
    );
}

#[test]
fn a_disabled_run_executes_a_fresh_rule_and_leaves_its_record_a_pass() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    std::fs::write(base.join("a.txt"), b"one").expect("input");
    write_manifest(
        base,
        "scopes:\n  src: [\"*.txt\"]\ncommands:\n  - name: sh\n    inputs: [src]\n",
    );
    let argv = [
        "sh".to_owned(),
        "-c".to_owned(),
        "printf x >> runs.log".to_owned(),
    ];

    assert_eq!(run_with(&argv, base, Disable::off()).expect("run"), 0);
    assert_eq!(run_with(&argv, base, Disable::on()).expect("forced"), 0);
    assert_eq!(runs(base), 2, "forced past a fresh record");

    assert_eq!(run_with(&argv, base, Disable::off()).expect("run"), 0);
    assert_eq!(
        runs(base),
        2,
        "the forced run left a record the next run honours"
    );
}