buildl-cli 0.3.0

The `buildl` binary — the command-line entry point to the buildl library.
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
//! What each command writes, byte for byte, and to which stream.
//!
//! Most tests here build one workspace: an application `//:app` linked from `//:main.o` and
//! `//lib:text`, its documentation `//:docs`, and an alias `//:default`. Its two programs are
//! scripts this file writes into a directory of their own, which is the whole search path the
//! binary is given: `cc`, which copies a source and complains about what the source asks it
//! to, and `mkdoc`, which prints two lines, the second not UTF-8.

use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Output;

use crate::common::{
    append, code, manifest, program, run, run_at_home, stderr, stdout, workspace, write,
};

/// The root build file of the workspace.
const BUILD: &str = r#"local b = buildl
b.subdir("lib")
b.option("mode", { default = "debug" })
b.alias("default", "app")
b.target("app", { deps = { "main.o", "//lib:text" }, run = { "cc", "$deps", "-o", "$out" } })
b.target("docs", { inputs = { "README" }, run = { "mkdoc", "--doc", "$in", "a b" } })
b.target("main.o", { inputs = { "main.c" }, run = { "cc", "-c", "$in", "-o", "$out", "$opt:mode" } })
"#;

/// The build file of `lib/`.
const LIB: &str = "buildl.target(\"text\", { inputs = { \"text.c\" }, run = { \"cc\", \"-c\", \"$in\", \"-o\", \"$out\" } })\n";

/// `cc -c <source> -o <output> [mode]` copies the source, warns about one that holds `unused`
/// and refuses one that holds `broken`; `cc <object> <object> -o <output>` joins the two.
const CC: &str = r#"#!/bin/sh
if [ "$1" = "-c" ]; then
    case "$(/bin/cat "$2")" in
    *broken*)
        echo "$2:3:1: error: expected ';'" >&2
        exit 1
        ;;
    *unused*) echo "$2:4:9: warning: unused variable 'n'" >&2 ;;
    esac
    /bin/cp "$2" "$4"
else
    /bin/cat "$1" "$2" > "$4"
fi
"#;

/// `mkdoc --doc <source> <title>` says what it wrote, the byte 0xE9 included, and copies the
/// source.
const MKDOC: &str = "#!/bin/sh\nprintf 'wrote 3 pages\\ncaf\\351\\n'\n/bin/cp \"$2\" out/docs\n";

/// The workspace, and the directory that holds its two programs.
struct Shop {
    _dir: tempfile::TempDir,
    root: PathBuf,
    _tools: tempfile::TempDir,
    bin: PathBuf,
}

impl Shop {
    fn new() -> Self {
        let (dir, root) = workspace(&[
            ("buildl.toml", &manifest(&["cc", "mkdoc"])),
            ("build.lua", BUILD),
            ("lib/build.lua", LIB),
            ("main.c", "int main(void) { int unused; return 0; }\n"),
            ("lib/text.c", "const char *text = \"text\";\n"),
            ("README", "three pages\n"),
        ]);
        let tools = tempfile::tempdir().unwrap();
        let bin = fs::canonicalize(tools.path()).unwrap();
        program(&bin, "cc", CC);
        program(&bin, "mkdoc", MKDOC);
        Self {
            _dir: dir,
            root,
            _tools: tools,
            bin,
        }
    }

    /// Runs the binary with `args` at the workspace root, its two programs the only ones it
    /// can find.
    fn run(&self, args: &[&str]) -> Output {
        run(&self.root, args, &[("PATH", self.bin.as_os_str())])
    }

    /// Runs the binary with `args` and returns its status, after checking that it wrote
    /// nothing to standard output, and what it wrote to standard error.
    fn built(&self, args: &[&str]) -> (i32, Vec<u8>) {
        let output = self.run(args);
        assert_eq!(stdout(&output), "", "{args:?}");
        (code(&output), output.stderr)
    }
}

#[test]
fn check_prints_one_line_on_stderr_and_nothing_on_stdout() {
    let shop = Shop::new();
    let output = shop.run(&["check"]);
    assert_eq!(code(&output), 0);
    assert_eq!(stderr(&output), "ok: 6 declarations in 2 build files\n");
    assert_eq!(stdout(&output), "");
}

#[test]
fn check_writes_nothing() {
    let shop = Shop::new();
    for args in [&["check"][..], &["graph"][..], &["graph", "--dot"][..]] {
        assert_eq!(code(&shop.run(args)), 0, "{args:?}");
    }
    assert!(!shop.root.join("out").exists());
    assert!(!shop.root.join(".buildl").exists());
}

/// A workspace whose one discovery target, `//:pkgs`, has never run.
fn discovering() -> (tempfile::TempDir, PathBuf) {
    workspace(&[
        ("buildl.toml", &manifest(&["cp"])),
        (
            "build.lua",
            "buildl.discover(\"pkgs\", { inputs = { \"list\" }, run = { \"cp\", \"$in\", \"$out\" } })\n",
        ),
        ("list", "[]\n"),
    ])
}

const NOT_RUN: &str = "//:pkgs has not run; run buildl build\n";

#[test]
fn check_prints_the_not_run_note_on_stderr() {
    let (_dir, root) = discovering();
    let output = run(&root, &["check"], &[]);
    assert_eq!(code(&output), 0);
    assert_eq!(
        stderr(&output),
        format!("ok: 1 declaration in 1 build file\n{NOT_RUN}")
    );
    assert_eq!(stdout(&output), "");
    assert!(!root.join(".buildl").exists());
}

#[test]
fn the_not_run_note_stays_off_standard_output_for_graph_query_and_doctor() {
    let (_dir, root) = discovering();
    for args in [
        &["graph"][..],
        &["graph", "--dot"][..],
        &["query", "deps(//:pkgs)"][..],
        &["query", "--json", "deps(//:pkgs)"][..],
        &["doctor"][..],
    ] {
        let output = run(&root, args, &[]);
        assert_eq!(code(&output), 0, "{args:?}");
        assert_eq!(stderr(&output), NOT_RUN, "{args:?}");
        assert!(!stdout(&output).contains("has not run"), "{args:?}");
    }
    assert!(!root.join(".buildl").exists());
}

#[test]
fn graph_json_equals_the_graph_file() {
    let shop = Shop::new();
    assert_eq!(shop.built(&["build"]).0, 0);
    let file = fs::read(shop.root.join(".buildl/graph.json")).unwrap();
    for args in [&["graph"][..], &["graph", "--json"][..]] {
        let output = shop.run(args);
        assert_eq!(code(&output), 0, "{args:?}");
        assert_eq!(output.stdout.last(), Some(&b'\n'), "{args:?}");
        assert_eq!(
            output.stdout[..output.stdout.len() - 1],
            file[..],
            "{args:?}"
        );
        assert_eq!(stderr(&output), "", "{args:?}");
    }
}

#[test]
fn query_prints_one_label_a_line_for_deps_and_rdeps() {
    let shop = Shop::new();
    for (expression, expected) in [
        ("deps(//:app)", "//:main.o\n//lib:text\n"),
        ("deps(default)", "//:main.o\n//lib:text\n"),
        ("rdeps(//lib:text)", "//:app\n"),
        ("rdeps(//:docs)", ""),
    ] {
        let output = shop.run(&["query", expression]);
        assert_eq!(code(&output), 0, "{expression}");
        assert_eq!(stdout(&output), expected, "{expression}");
        assert_eq!(stderr(&output), "", "{expression}");
    }
    assert!(!shop.root.join("out").exists());
    assert!(!shop.root.join(".buildl").exists());
}

#[test]
fn query_json_is_the_canonical_array_and_one_newline() {
    let shop = Shop::new();
    for (expression, expected) in [
        ("deps(//:app)", "[\"//:main.o\",\"//lib:text\"]\n"),
        ("rdeps(//:main.o)", "[\"//:app\"]\n"),
        ("deps(//:docs)", "[]\n"),
    ] {
        let output = shop.run(&["query", "--json", expression]);
        assert_eq!(code(&output), 0, "{expression}");
        assert_eq!(stdout(&output), expected, "{expression}");
        assert_eq!(stderr(&output), "", "{expression}");
    }
}

#[test]
fn graph_dot_is_stable() {
    let shop = Shop::new();
    let first = shop.run(&["graph", "--dot"]);
    assert_eq!(code(&first), 0);
    assert_eq!(
        stdout(&first),
        concat!(
            "digraph buildl {\n",
            "  \"//:app\";\n",
            "  \"//:docs\";\n",
            "  \"//:main.o\";\n",
            "  \"//lib:text\";\n",
            "  \"//:app\" -> \"//:main.o\";\n",
            "  \"//:app\" -> \"//lib:text\";\n",
            "  \"//:default\" [shape=plaintext];\n",
            "  \"//:default\" -> \"//:app\" [style=dashed];\n",
            "}\n",
        )
    );
    assert_eq!(shop.run(&["graph", "--dot"]).stdout, first.stdout);
}

#[test]
fn plan_says_why_each_target_would_run() {
    let shop = Shop::new();
    let never = shop.run(&["plan"]);
    assert_eq!(code(&never), 0);
    assert_eq!(
        stdout(&never),
        concat!(
            "run    //:app: never built\n",
            "run    //:docs: never built\n",
            "run    //:main.o: never built\n",
            "run    //lib:text: never built\n",
            "  run programs: cc, mkdoc\n",
            "  write into:   .buildl, out\n",
            "4 to run, 0 conditional, 0 clean\n",
        )
    );

    assert_eq!(shop.built(&["build"]).0, 0);
    append(&shop.root, "main.c", "// grown\n");
    let output = shop.run(&["plan"]);
    assert_eq!(code(&output), 0);
    assert_eq!(
        stdout(&output),
        concat!(
            "maybe  //:app: runs only if //:main.o produces something else\n",
            "clean  //:docs\n",
            "run    //:main.o: input main.c changed\n",
            "clean  //lib:text\n",
            "  run programs: cc, mkdoc\n",
            "  write into:   .buildl, out\n",
            "1 to run, 1 conditional, 2 clean\n",
        )
    );
    assert_eq!(stderr(&output), "");
}

#[test]
fn plan_names_every_difference_of_a_target() {
    let shop = Shop::new();
    assert_eq!(shop.built(&["build"]).0, 0);
    append(&shop.root, "main.c", "// grown\n");
    append(&shop.bin, "cc", "# a newer compiler\n");
    let output = shop.run(&["plan", "--set", "mode=release"]);
    assert_eq!(code(&output), 0);
    assert_eq!(
        stdout(&output),
        concat!(
            "run    //:app: tool cc changed\n",
            "clean  //:docs\n",
            "run    //:main.o: input main.c changed; tool cc changed; setting mode changed\n",
            "run    //lib:text: tool cc changed\n",
            "  run programs: cc, mkdoc\n",
            "  write into:   .buildl, out\n",
            "3 to run, 0 conditional, 1 clean\n",
        )
    );
}

#[test]
fn plan_json_is_one_canonical_line() {
    let shop = Shop::new();
    let first = shop.run(&["plan", "--json"]);
    assert_eq!(code(&first), 0);
    let text = stdout(&first);
    assert!(
        text.starts_with(concat!(
            "{\"attribution\":{\"env\":{},\"network\":{},\"programs\":{},\"unkeyed\":{}},",
            "\"settings\":{\"mode\":\"debug\"},\"stale_discovery\":[],",
            "\"targets\":{\"//:app\":{"
        )),
        "{text}"
    );
    assert!(text.ends_with("}\n"), "{text}");
    assert_eq!(text.lines().count(), 1);
    assert_eq!(shop.run(&["plan", "--json"]).stdout, first.stdout);
}

#[test]
fn build_prints_one_block_per_action_and_a_summary() {
    let shop = Shop::new();
    assert_eq!(
        shop.built(&["build"]),
        (
            0,
            [
                &b"--- //:docs\n"[..],
                b"wrote 3 pages\n",
                b"caf\xe9\n",
                b"--- //:main.o\n",
                b"main.c:4:9: warning: unused variable 'n'\n",
                b"4 built\n",
            ]
            .concat()
        )
    );
}

#[test]
fn a_cache_hit_shows_what_the_action_printed() {
    let shop = Shop::new();
    assert_eq!(shop.built(&["build"]).0, 0);
    assert_eq!(
        shop.built(&["build"]),
        (
            0,
            [
                &b"--- //:docs (cached)\n"[..],
                b"wrote 3 pages\n",
                b"caf\xe9\n",
                b"--- //:main.o (cached)\n",
                b"main.c:4:9: warning: unused variable 'n'\n",
                b"4 cached\n",
            ]
            .concat()
        )
    );
}

/// The workspace after a full build, with `//:docs` unable to start and `//lib:text` broken.
fn broken() -> Shop {
    let shop = Shop::new();
    assert_eq!(shop.built(&["build"]).0, 0);
    program(&shop.bin, "mkdoc", "#!/nonexistent/interpreter\n");
    append(&shop.root, "lib/text.c", "broken\n");
    shop
}

/// What a build of [`broken`] reports about `//:docs`.
fn docs_failed(bin: &Path) -> String {
    format!(
        concat!(
            "FAILED //:docs: infrastructure failure (the command could not be started)\n",
            "    exec strategy: //:docs: running {}: No such file or directory (os error 2)\n",
            "    declared in build.lua\n",
            "    command: mkdoc --doc README \"a b\"\n",
        ),
        bin.join("mkdoc").display()
    )
}

#[test]
fn a_failure_report_names_the_class_the_file_and_the_command() {
    let shop = broken();
    let (status, said) = shop.built(&["build", "--keep-going"]);
    assert_eq!(status, 3);
    assert_eq!(
        String::from_utf8(said).unwrap(),
        [
            "--- //:main.o (cached)\n",
            "main.c:4:9: warning: unused variable 'n'\n",
            &docs_failed(&shop.bin),
            "--- //lib:text\n",
            "lib/text.c:3:1: error: expected ';'\n",
            "FAILED //lib:text: action failure (exit status 1)\n",
            "    declared in lib/build.lua\n",
            "    command: cc -c lib/text.c -o out/lib/text\n",
            "2 failed, 1 skipped, 1 cached\n",
        ]
        .concat()
    );
}

#[test]
fn without_keep_going_the_build_stops_at_its_first_failure() {
    let shop = broken();
    let (status, said) = shop.built(&["build"]);
    assert_eq!(status, 3);
    // With one worker `//:docs` fails first, so `//lib:text` is never begun and is skipped
    // with `//:app`.
    assert_eq!(
        String::from_utf8(said).unwrap(),
        [
            "--- //:main.o (cached)\n",
            "main.c:4:9: warning: unused variable 'n'\n",
            &docs_failed(&shop.bin),
            "1 failed, 2 skipped, 1 cached\n",
        ]
        .concat()
    );
}

#[test]
fn stream_shows_the_action_output() {
    let (_dir, root) = workspace(&[
        ("buildl.toml", &manifest(&["sh"])),
        (
            "build.lua",
            concat!(
                "buildl.target(\"loud\", { run = { \"sh\", \"-c\", \"echo one; echo two; exit 3\" } })\n",
                "buildl.target(\"partial\", { run = { \"sh\", \"-c\", \"printf partial; echo > out/partial\" } })\n",
            ),
        ),
    ]);
    let output = run(&root, &["build", "--stream", "--keep-going"], &[]);
    assert_eq!(code(&output), 1);
    assert_eq!(
        stderr(&output),
        concat!(
            "--- //:loud\n",
            "one\n",
            "two\n",
            "FAILED //:loud: action failure (exit status 3)\n",
            "    declared in build.lua\n",
            "    command: sh -c \"echo one; echo two; exit 3\"\n",
            "--- //:partial\n",
            "partial\n",
            "1 failed, 1 built\n",
        )
    );
    assert_eq!(stdout(&output), "");
}

#[test]
fn colour_is_written_only_when_asked_for() {
    let (_dir, root) = workspace(&[
        ("buildl.toml", &manifest(&["false"])),
        (
            "build.lua",
            "buildl.target(\"bad\", { run = { \"false\" } })\n",
        ),
    ]);
    let terminal = [("TERM", OsStr::new("xterm-256color"))];
    let plain = run(&root, &["build"], &terminal);
    assert!(!plain.stderr.contains(&0x1b), "{}", stderr(&plain));

    let asked = run(
        &root,
        &["build", "--color", "always"],
        &[("NO_COLOR", OsStr::new("1"))],
    );
    assert_eq!(code(&asked), 1);
    assert!(
        stderr(&asked)
            .starts_with("\x1b[1;31mFAILED\x1b[0m //:bad: action failure (exit status 1)\n"),
        "{}",
        stderr(&asked)
    );
}

/// A workspace whose one target, `//:both`, prints a line on each stream, built once.
fn logged() -> (tempfile::TempDir, PathBuf) {
    let (dir, root) = workspace(&[
        ("buildl.toml", &manifest(&["sh"])),
        (
            "build.lua",
            "buildl.target(\"both\", { run = { \"sh\", \"-c\", \"echo out; echo err >&2\" }, outputs = {} })\n",
        ),
    ]);
    assert_eq!(code(&run(&root, &["build"], &[])), 0);
    (dir, root)
}

/// Whether `line` is `//:both: built at ` and a UTC time to the second, as the header of a
/// last run is written, with its newline.
fn is_header(line: &str) -> bool {
    let Some(time) = line
        .strip_prefix("//:both: built at ")
        .and_then(|rest| rest.strip_suffix(" UTC\n"))
    else {
        return false;
    };
    let shape = "dddd-dd-dd dd:dd:dd";
    time.len() == shape.len()
        && time
            .chars()
            .zip(shape.chars())
            .all(|(got, want)| match want {
                'd' => got.is_ascii_digit(),
                other => got == other,
            })
}

#[test]
fn log_prints_each_stream_on_its_own_channel() {
    let (_dir, root) = logged();
    let output = run(&root, &["log", "both"], &[]);
    assert_eq!(code(&output), 0);
    assert_eq!(stdout(&output), "out\n");
    let said = stderr(&output);
    let (header, rest) = said.split_once('\n').unwrap();
    assert!(is_header(&format!("{header}\n")), "{said}");
    assert_eq!(rest, "err\n");
}

#[test]
fn log_stdout_prints_only_stdout() {
    let (_dir, root) = logged();
    let output = run(&root, &["log", "both", "--stdout"], &[]);
    assert_eq!(code(&output), 0);
    assert_eq!(stdout(&output), "out\n");
    assert!(is_header(&stderr(&output)), "{}", stderr(&output));
}

#[test]
fn log_stderr_prints_stored_stderr_on_stdout() {
    let (_dir, root) = logged();
    let output = run(&root, &["log", "both", "--stderr"], &[]);
    assert_eq!(code(&output), 0);
    assert_eq!(stdout(&output), "err\n");
    assert!(is_header(&stderr(&output)), "{}", stderr(&output));
}

#[test]
fn log_header_is_on_stderr() {
    let (_dir, root) = logged();
    let said = stderr(&run(&root, &["log", "both"], &[]));
    let header = said.lines().next().unwrap();
    assert!(is_header(&format!("{header}\n")), "{said}");
}

/// Two test targets, `//:pass`, which prints `ok` and passes, and `//:fail`, which prints `bad`
/// and fails.
const TESTS: &str = r#"buildl.test("pass", { run = { "sh", "-c", "echo ok" } })
buildl.test("fail", { run = { "sh", "-c", "echo bad; exit 1" } })
"#;

/// A workspace of [`TESTS`] under a manifest that allows `sh`.
fn tests() -> (tempfile::TempDir, PathBuf) {
    workspace(&[("buildl.toml", &manifest(&["sh"])), ("build.lua", TESTS)])
}

/// The block `buildl build fail` prints for `//:fail`: what it wrote to standard error without
/// the summary line that ends it.
fn failing_block() -> String {
    let (_dir, root) = tests();
    let said = stderr(&run(&root, &["build", "fail"], &[]));
    said.strip_suffix("1 failed\n").unwrap().to_owned()
}

#[test]
fn test_prints_only_failing_tests_and_the_test_line() {
    let (_dir, root) = tests();
    let output = run(&root, &["test"], &[]);
    assert_eq!(stdout(&output), "");
    let said = stderr(&output);
    assert_eq!(
        said,
        [failing_block(), "tests: 1 passed, 1 failed\n".to_owned()].concat()
    );
    assert!(!said.contains("ok"), "{said}");
}

#[test]
fn test_verbose_prints_every_test() {
    let (_dir, root) = tests();
    let output = run(&root, &["test", "--verbose"], &[]);
    assert_eq!(stdout(&output), "");
    assert_eq!(
        stderr(&output),
        [
            failing_block(),
            "--- //:pass\nok\n".to_owned(),
            "tests: 1 passed, 1 failed\n".to_owned()
        ]
        .concat()
    );
}

#[test]
fn test_stop_starts_nothing_after_the_first_failure() {
    let (_dir, root) = tests();
    let output = run(&root, &["test", "--stop"], &[]);
    assert_eq!(code(&output), 1);
    let said = stderr(&output);
    assert!(said.ends_with("tests: 1 failed, 1 skipped\n"), "{said}");
    assert!(!said.contains("ok"), "{said}");
}

#[test]
fn test_stream_shows_a_passing_test_as_it_runs() {
    let (_dir, root) = tests();
    let output = run(&root, &["test", "--stream", "pass"], &[]);
    assert_eq!(code(&output), 0);
    assert_eq!(stdout(&output), "");
    assert_eq!(stderr(&output), "--- //:pass\nok\ntests: 1 passed\n");
}

#[test]
fn test_rerun_line() {
    let (_dir, root) = tests();
    assert_eq!(code(&run(&root, &["test", "pass"], &[])), 0);
    let output = run(&root, &["test", "--rerun", "pass"], &[]);
    assert_eq!(code(&output), 0);
    assert_eq!(stdout(&output), "");
    assert_eq!(stderr(&output), "tests (rerun): 1 passed\n");
}

/// A workspace of two targets, `//:hello` and `//:quiet` that needs it, not built.
fn pair() -> (tempfile::TempDir, PathBuf) {
    workspace(&[
        ("buildl.toml", &manifest(&["cp"])),
        (
            "build.lua",
            "buildl.target(\"hello\", { inputs = { \"hello.txt\" }, run = { \"cp\", \"$in\", \"$out\" } })\nbuildl.target(\"quiet\", { deps = { \"hello\" }, run = { \"cp\", \"$deps\", \"$out\" } })\n",
        ),
        ("hello.txt", "hello\n"),
    ])
}

/// Whether `line` is what `gc` says after dropping one row: a count of blobs and a count of
/// bytes it removed, each in the plural or the singular, and a newline.
fn is_gc_line(line: &str) -> bool {
    let Some(rest) = line.strip_prefix("gc: dropped 1 row, removed ") else {
        return false;
    };
    let Some(rest) = rest
        .strip_suffix(" bytes)\n")
        .or_else(|| rest.strip_suffix(" byte)\n"))
    else {
        return false;
    };
    let Some((blobs, bytes)) = rest.split_once(" (") else {
        return false;
    };
    let digits = |text: &str| !text.is_empty() && text.bytes().all(|byte| byte.is_ascii_digit());
    blobs
        .strip_suffix(" blobs")
        .or_else(|| blobs.strip_suffix(" blob"))
        .is_some_and(digits)
        && digits(bytes)
}

#[test]
fn clean_says_it_removed_out_and_prints_nothing_on_stdout() {
    let (_dir, root) = pair();
    assert_eq!(code(&run(&root, &["build"], &[])), 0);
    let output = run(&root, &["clean"], &[]);
    assert_eq!(code(&output), 0);
    assert_eq!(stdout(&output), "");
    assert_eq!(stderr(&output), "clean: removed out\n");
    assert!(!root.join("out").exists());
}

#[test]
fn clean_without_out_says_it_holds_nothing() {
    let (_dir, root) = pair();
    let output = run(&root, &["clean"], &[]);
    assert_eq!(code(&output), 0);
    assert_eq!(stdout(&output), "");
    assert_eq!(stderr(&output), "clean: out holds nothing\n");
}

#[test]
fn gc_reports_rows_blobs_and_bytes() {
    let (_dir, root) = pair();
    assert_eq!(code(&run(&root, &["build"], &[])), 0);
    write(
        &root,
        "build.lua",
        "buildl.target(\"hello\", { inputs = { \"hello.txt\" }, run = { \"cp\", \"$in\", \"$out\" } })\n",
    );
    let output = run(&root, &["gc"], &[]);
    assert_eq!(code(&output), 0);
    assert_eq!(stdout(&output), "");
    assert!(is_gc_line(&stderr(&output)), "{}", stderr(&output));
}

#[test]
fn doctor_prints_the_workspace_authority() {
    let manifest = format!(
        "{}\n[approver]\ndefault = \"interactive\"\n",
        manifest(&["cp"])
    );
    let (_dir, root) = workspace(&[
        ("buildl.toml", &manifest),
        (
            "build.lua",
            concat!(
                "buildl.target(\"copy\", { inputs = { \"a.txt\" }, run = { \"cp\", \"$in\", \"$out\" } })\n",
                "buildl.target(\"fetch\", { run = { \"curl\", \"-o\", \"$out\" }, network = true })\n",
            ),
        ),
        ("a.txt", "a\n"),
    ]);
    let home_dir = tempfile::tempdir().unwrap();
    let home = fs::canonicalize(home_dir.path()).unwrap();

    let output = run_at_home(&root, &["doctor"], &home);

    assert_eq!(code(&output), 0, "{}", stderr(&output));
    assert_eq!(stderr(&output), "");
    assert_eq!(
        stdout(&output),
        format!(
            concat!(
                "workspace  {}\n",
                "approver   interactive\n",
                "execution  workers 1, timeout 10m, strategy bare\n",
                "remote: none\n",
                "ceiling\n",
                "  proc.run     cp\n",
                "  env.read     -\n",
                "  env.unkeyed  -\n",
                "  fs.read      .\n",
                "  fs.write     .buildl, out\n",
                "wants               ceiling  approved\n",
                "  run cp            allowed  no\n",
                "  run curl          REFUSED  -\n",
                "  write .buildl     allowed  no\n",
                "  write out         allowed  no\n",
                "  network //:fetch  -        no\n",
            ),
            root.display()
        )
    );
}

/// A workspace whose one target, `//:greet`, is declared by the path plugin `hello`.
fn plugin_workspace() -> (tempfile::TempDir, PathBuf) {
    let manifest = format!(
        "{}\n[plugins.hello]\npath = \"plugins/hello\"\nproc.run = [\"cp\"]\n",
        manifest(&["cp"])
    );
    workspace(&[
        ("buildl.toml", &manifest),
        (
            "build.lua",
            "local hello = buildl.use(\"hello\")\nhello.greet(\"greet\")\n",
        ),
        ("hello.txt", "hello\n"),
        (
            "plugins/hello/extension.toml",
            "[extension]\nname = \"hello\"\nversion = \"0.1.0\"\nentry = \"main.lua\"\napi = 1\n\n[capabilities]\nproc.run = [\"cp\"]\n",
        ),
        (
            "plugins/hello/main.lua",
            concat!(
                "local module = {}\n",
                "function module.greet(name)\n",
                "  buildl.target(name, { inputs = { \"hello.txt\" }, run = { \"cp\", \"$in\", \"$out\" } })\n",
                "end\n",
                "return module\n",
            ),
        ),
    ])
}

#[test]
fn plan_names_the_plugin_behind_a_want() {
    let (_dir, root) = plugin_workspace();

    let output = run(&root, &["plan"], &[]);

    assert_eq!(code(&output), 0, "{}", stderr(&output));
    assert!(
        stdout(&output).contains("declared by plugin hello@0.1.0 for //:greet\n"),
        "{}",
        stdout(&output)
    );
}

#[test]
fn doctor_shows_plugin_attribution_and_exits_0() {
    let (_dir, root) = plugin_workspace();
    let home_dir = tempfile::tempdir().unwrap();
    let home = fs::canonicalize(home_dir.path()).unwrap();

    let output = run_at_home(&root, &["doctor"], &home);

    assert_eq!(code(&output), 0, "{}", stderr(&output));
    assert!(
        stdout(&output).contains("declared by plugin hello@0.1.0 for //:greet\n"),
        "{}",
        stdout(&output)
    );
}

#[test]
fn graph_json_shows_the_plugin_chain() {
    let (_dir, root) = plugin_workspace();

    let output = run(&root, &["graph", "--json"], &[]);

    assert_eq!(code(&output), 0, "{}", stderr(&output));
    assert!(
        stdout(&output).contains(r#""origin":{"Plugins":"#),
        "{}",
        stdout(&output)
    );
    assert!(
        stdout(&output).contains(r#""name":"hello""#),
        "{}",
        stdout(&output)
    );
}