runner-run 0.12.0

Universal project task 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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! Integration tests for chain mode dispatch.
//!
//! Each test spawns the `runner` binary against a fixture project under
//! `tests/fixtures/`. The fixtures use `just` (already a dependency the
//! repo expects to be on PATH for development) so the tests don't need
//! to install any package managers.
//!
//! If `just` is not installed, the integration tests skip with a
//! warning rather than failing. Run them locally with `cargo test
//! --test chain_integration`.

use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use std::{io::Read, thread};

fn runner_binary() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_runner"))
}

fn fixture(name: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures")
        .join(name)
}

fn just_available() -> bool {
    Command::new("just")
        .arg("--version")
        .output()
        .is_ok_and(|o| o.status.success())
}

#[test]
fn sequential_chain_runs_in_order() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("chain-sequential").to_str().unwrap(),
            "run",
            "-s",
            "build",
            "test",
            "lint",
        ])
        .output()
        .expect("runner binary spawns");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected success.\nstdout: {stdout}\nstderr: {stderr}",
    );

    let b = stdout
        .find("build-ran")
        .unwrap_or_else(|| panic!("build-ran missing.\nstdout: {stdout}"));
    let t = stdout
        .find("test-ran")
        .unwrap_or_else(|| panic!("test-ran missing.\nstdout: {stdout}"));
    let l = stdout
        .find("lint-ran")
        .unwrap_or_else(|| panic!("lint-ran missing.\nstdout: {stdout}"));
    assert!(b < t && t < l, "order should match -s arg order: {stdout}");
}

#[test]
fn parallel_chain_exit_code_reflects_first_failure() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("chain-parallel-fail").to_str().unwrap(),
            "run",
            "-p",
            "ok-one",
            "fail-mid",
            "ok-two",
        ])
        .output()
        .expect("runner binary spawns");

    assert_eq!(
        output.status.code(),
        Some(7),
        "expected exit 7 from fail-mid task.\nstderr: {}",
        String::from_utf8_lossy(&output.stderr),
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    // The fixture's runner.toml disables parallel grouping on both the CI
    // (`[github].group_parallel`) and non-CI (`[parallel].grouped`) paths, so
    // this deterministically exercises the live line-prefixed muxer
    // regardless of environment. Output is line-prefixed.
    assert!(
        stdout.contains("[ok-one"),
        "expected `[ok-one ]` prefix on ok-one's output. stdout: {stdout}",
    );
    assert!(
        stdout.contains("[fail-mid"),
        "expected `[fail-mid]` prefix on fail-mid's output. stdout: {stdout}",
    );
}

#[test]
fn chain_rejects_mutually_exclusive_mode_flags() {
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("chain-sequential").to_str().unwrap(),
            "run",
            "-s",
            "-p",
            "build",
        ])
        .output()
        .expect("runner binary spawns");

    assert!(
        !output.status.success(),
        "expected clap to reject -s + -p combo",
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("--sequential") || stderr.contains("--parallel"),
        "expected clap conflict diagnostic. stderr: {stderr}",
    );
}

#[test]
fn chain_rejects_whitespace_positional_in_v1() {
    // No `just_available()` gate — the parser rejects the whitespace
    // positional before any task is dispatched, so the test runs
    // regardless of whether `just` is on PATH.
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("chain-sequential").to_str().unwrap(),
            "run",
            "-s",
            "build --release",
        ])
        .output()
        .expect("runner binary spawns");

    assert!(
        !output.status.success(),
        "v1 rejects whitespace positionals",
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("whitespace") || stderr.contains("quoted-bundle"),
        "expected v1 rejection diagnostic. stderr: {stderr}",
    );
}

#[test]
fn install_completion_includes_tasks_and_options() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }

    let output = Command::new(runner_binary())
        .env("COMPLETE", "zsh")
        .env("_CLAP_COMPLETE_INDEX", "4")
        .args([
            "--",
            "runner",
            "--dir",
            fixture("chain-sequential").to_str().unwrap(),
            "install",
            "",
        ])
        .output()
        .expect("runner binary spawns");

    assert!(
        output.status.success(),
        "completion should succeed. stderr: {}",
        String::from_utf8_lossy(&output.stderr),
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("just\x1fbuild"),
        "install completion should include task candidates. stdout: {stdout}",
    );
    assert!(
        stdout.contains("Options\x1f--frozen"),
        "install completion should keep option candidates. stdout: {stdout}",
    );
}

#[test]
fn chain_prevalidates_all_tokens_before_running_any_task() {
    // A chain with a clearly-broken third token (`lint:cargo` — the
    // reversed qualifier we error on) must NOT run `build` and `test`
    // to completion first. The pre-validation in `run_chain` should
    // bail before any sibling dispatches.
    //
    // No `just_available()` gate — `precheck_task` works off
    // `ctx.tasks` (populated from the justfile by the detector) and
    // never spawns the just binary itself. If `just` is missing the
    // tasks table is empty, which would make this test pass for the
    // wrong reason (qualified miss on `build`), so we still assert
    // on the `cargo:lint` hint specifically.
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("chain-sequential").to_str().unwrap(),
            "run",
            "-s",
            "build",
            "test",
            "lint:cargo",
        ])
        .output()
        .expect("runner binary spawns");

    assert!(
        !output.status.success(),
        "chain with reversed-qualifier item must fail",
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cargo:lint"),
        "expected `did you mean cargo:lint?` hint in stderr: {stderr}",
    );
    // The fixture's `build` and `test` recipes echo `build-ran` /
    // `test-ran` (see `tests/fixtures/chain-sequential/justfile`).
    // Their absence proves the pre-validation fired *before* any
    // sibling dispatch.
    assert!(
        !stdout.contains("build-ran"),
        "pre-validation should have skipped `build`. stdout: {stdout}",
    );
    assert!(
        !stdout.contains("test-ran"),
        "pre-validation should have skipped `test`. stdout: {stdout}",
    );
}

#[test]
fn sequential_chain_wraps_steps_in_github_actions_groups() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("chain-sequential").to_str().unwrap(),
            "run",
            "-s",
            "build",
            "test",
        ])
        .env("GITHUB_ACTIONS", "true")
        .output()
        .expect("runner binary spawns");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "expected success. stdout: {stdout}"
    );

    let g_build = stdout
        .find("::group::runner: build")
        .unwrap_or_else(|| panic!("missing build group. stdout: {stdout}"));
    let end_build = g_build
        + stdout[g_build..]
            .find("::endgroup::")
            .unwrap_or_else(|| panic!("build group not closed. stdout: {stdout}"));
    let g_test = stdout
        .find("::group::runner: test")
        .unwrap_or_else(|| panic!("missing test group. stdout: {stdout}"));
    let build_ran = stdout
        .find("build-ran")
        .unwrap_or_else(|| panic!("build-ran missing. stdout: {stdout}"));

    // build's group opens, contains its output, and closes before test's
    // group opens — flat, non-overlapping groups (GitHub Actions can't
    // render nested ones).
    assert!(
        g_build < build_ran && build_ran < end_build && end_build < g_test,
        "expected build group to open, contain build-ran, close, then test group. stdout: {stdout}",
    );
    assert_eq!(
        stdout.matches("::group::runner: ").count(),
        2,
        "expected exactly two groups. stdout: {stdout}",
    );
    assert_eq!(
        stdout.matches("::endgroup::").count(),
        2,
        "expected exactly two endgroups. stdout: {stdout}",
    );
}

#[test]
fn single_task_is_grouped_under_github_actions() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    // A bare single task (no `-s`) still gets one group under GitHub Actions
    // — grouping covers every task run, not just multi-step chains.
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("chain-sequential").to_str().unwrap(),
            "run",
            "build",
        ])
        .env("GITHUB_ACTIONS", "true")
        .output()
        .expect("runner binary spawns");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "expected success. stdout: {stdout}"
    );
    assert!(
        stdout.contains("::group::runner: build"),
        "single task should be wrapped in a group. stdout: {stdout}",
    );
    assert_eq!(
        stdout.matches("::group::").count(),
        1,
        "exactly one group for a single task. stdout: {stdout}",
    );
}

#[test]
fn no_groups_emitted_outside_github_actions() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    // Scrub GITHUB_ACTIONS so this is deterministic even when the test host
    // itself runs under GitHub Actions (mirrors info_deprecation.rs).
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("chain-sequential").to_str().unwrap(),
            "run",
            "-s",
            "build",
            "test",
        ])
        .env_remove("GITHUB_ACTIONS")
        .output()
        .expect("runner binary spawns");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "expected success. stdout: {stdout}"
    );
    assert!(
        !stdout.contains("::group::"),
        "no GHA groups in a normal terminal. stdout: {stdout}",
    );
    assert!(
        !stdout.contains("::endgroup::"),
        "no GHA endgroups in a normal terminal. stdout: {stdout}",
    );
}

#[test]
fn config_opt_out_disables_grouping_under_github_actions() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    // The `github-no-group` fixture ships a runner.toml with
    // `[github] group_output = false`, so even under GitHub Actions no
    // groups are emitted.
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("github-no-group").to_str().unwrap(),
            "run",
            "build",
        ])
        .env("GITHUB_ACTIONS", "true")
        .output()
        .expect("runner binary spawns");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "expected success. stdout: {stdout}"
    );
    assert!(
        !stdout.contains("::group::"),
        "config opt-out must suppress groups. stdout: {stdout}",
    );
}

#[test]
fn github_group_output_false_restores_live_parallel_muxer() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("github-no-group").to_str().unwrap(),
            "run",
            "-p",
            "build",
            "test",
        ])
        .env("GITHUB_ACTIONS", "true")
        .output()
        .expect("runner binary spawns");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "expected success. stdout: {stdout}"
    );
    assert!(
        stdout.contains("[build") && stdout.contains("[test"),
        "GHA group_output=false should restore live prefixes. stdout: {stdout}",
    );
    assert!(
        !stdout.contains("::group::") && !stdout.contains("runner: build"),
        "GHA group_output=false should not emit grouped blocks. stdout: {stdout}",
    );
}

#[test]
fn parallel_chain_grouped_under_github_actions() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    // Default `[github].group_parallel` buffers each task and emits it as its
    // own ::group:: block under GitHub Actions — no live `[task]` prefixes.
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("chain-sequential").to_str().unwrap(),
            "run",
            "-p",
            "build",
            "test",
        ])
        .env("GITHUB_ACTIONS", "true")
        .output()
        .expect("runner binary spawns");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "expected success. stdout: {stdout}"
    );
    assert!(
        !stdout.contains("[build"),
        "grouped parallel output must not use live prefixes. stdout: {stdout}",
    );

    // Each task's output sits inside its own group; completion order between
    // the two is nondeterministic, so assert per-task containment, not order.
    let g_build = stdout
        .find("::group::runner: build")
        .unwrap_or_else(|| panic!("missing build group. stdout: {stdout}"));
    let build_ran = stdout
        .find("build-ran")
        .unwrap_or_else(|| panic!("build-ran missing. stdout: {stdout}"));
    assert!(
        g_build < build_ran,
        "build output must sit inside build's group. stdout: {stdout}",
    );
    assert!(
        stdout.contains("::group::runner: test"),
        "missing test group. stdout: {stdout}",
    );
    assert_eq!(
        stdout.matches("::group::runner: ").count(),
        2,
        "expected exactly two groups. stdout: {stdout}",
    );
    assert_eq!(
        stdout.matches("::endgroup::").count(),
        2,
        "expected exactly two endgroups. stdout: {stdout}",
    );
}

#[test]
fn parallel_chain_grouped_with_plain_headers_outside_github_actions() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    // Grouping is not GitHub-specific: outside Actions each block gets a plain
    // `runner: <task>` header and no ::group:: workflow-command bloat.
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("parallel-grouped").to_str().unwrap(),
            "run",
            "-p",
            "build",
            "test",
        ])
        .env_remove("GITHUB_ACTIONS")
        .output()
        .expect("runner binary spawns");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "expected success. stdout: {stdout}"
    );
    assert!(
        !stdout.contains("::group::") && !stdout.contains("::endgroup::"),
        "no workflow-command syntax outside GitHub Actions. stdout: {stdout}",
    );
    assert!(
        !stdout.contains("[build"),
        "grouped parallel output must not use live prefixes. stdout: {stdout}",
    );

    // Each task gets a plain header block, with its output underneath.
    let h_build = stdout
        .find("runner: build")
        .unwrap_or_else(|| panic!("missing build header. stdout: {stdout}"));
    let build_ran = stdout
        .find("build-ran")
        .unwrap_or_else(|| panic!("build-ran missing. stdout: {stdout}"));
    assert!(
        h_build < build_ran,
        "build output must follow its header. stdout: {stdout}",
    );
    assert!(
        stdout.contains("runner: test"),
        "missing test header. stdout: {stdout}",
    );
}

#[test]
fn parallel_grouped_preserves_child_stderr_stream() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    let output = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("parallel-grouped").to_str().unwrap(),
            "run",
            "-p",
            "build",
            "err",
        ])
        .env_remove("GITHUB_ACTIONS")
        .output()
        .expect("runner binary spawns");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected success. stdout: {stdout}; stderr: {stderr}"
    );
    assert!(
        stderr.contains("err-ran"),
        "child stderr must stay on stderr. stderr: {stderr}",
    );
    assert!(
        !stdout.contains("err-ran"),
        "child stderr must not be replayed to stdout. stdout: {stdout}",
    );
}

#[test]
fn parallel_grouped_does_not_wait_forever_on_inherited_stdout() {
    if !just_available() {
        eprintln!("skipping: `just` not found on PATH");
        return;
    }
    let mut child = Command::new(runner_binary())
        .args([
            "--dir",
            fixture("parallel-grouped").to_str().unwrap(),
            "run",
            "-p",
            "hold-open",
            "build",
        ])
        .env_remove("GITHUB_ACTIONS")
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("runner binary spawns");

    let started = Instant::now();
    let status = loop {
        if let Some(status) = child.try_wait().expect("runner status checks") {
            break status;
        }
        if started.elapsed() > Duration::from_secs(2) {
            let _ = child.kill();
            let _ = child.wait();
            panic!("grouped parallel waited on inherited stdout for too long");
        }
        thread::sleep(Duration::from_millis(50));
    };

    let mut stdout = String::new();
    if let Some(mut pipe) = child.stdout.take() {
        pipe.read_to_string(&mut stdout)
            .expect("stdout should be readable");
    }
    let mut stderr = String::new();
    if let Some(mut pipe) = child.stderr.take() {
        pipe.read_to_string(&mut stderr)
            .expect("stderr should be readable");
    }

    assert!(status.success(), "stdout: {stdout}; stderr: {stderr}");
    assert!(
        stdout.contains("foreground-ran"),
        "completed task output should still flush. stdout: {stdout}",
    );
}