buildl-cli 0.1.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
//! The argument surface: the commands, their flags, and how each becomes a value a build takes.
//!
//! Its own file because the command line is a contract of its own: the names here are what a
//! person types and what a script depends on, and nothing else in the binary reads an argument
//! as text.
//!
//! Responsibilities:
//!
//! - [`Cli`] and [`Command`] — the commands and the flags of each.
//! - [`Color`] — when to colour what is printed, as asked for.
//! - [`GraphArgs`], [`QueryArgs`], [`PlanArgs`], [`BuildArgs`], [`TestArgs`], [`VerifyArgs`],
//!   [`LogArgs`], [`PluginsArgs`], [`RemoteArgs`] and [`Selection`] — the arguments of a command.
//! - [`PluginsCommand`] — the commands under `plugins`.
//! - [`Streams`] — which streams of a run `log` prints.
//! - [`Override`] and [`overrides`] — `--set NAME=VALUE`, and the settings of one run.
//!
//! Non-responsibilities: running a command, which the `run` module does; reading a target
//! argument as a label, which needs the workspace; and the texts of `--help` and of a usage
//! error, which are the argument parser's own.

use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::num::NonZeroUsize;

use buildl::buildl_core::{
    LiveOutput, OnFailure, RemoteJobs, Rerun, Sample, SettingName, SettingValue,
};
use buildl::report::TestBlocks;
use clap::error::ErrorKind;
use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum};

/// A sandboxed, deterministic build system
#[derive(Debug, Parser)]
#[command(name = "buildl", version, propagate_version = true)]
pub(crate) struct Cli {
    /// When to colour what is printed
    #[arg(long, value_enum, value_name = "WHEN", default_value_t = Color::Auto, global = true)]
    pub color: Color,

    /// The command to run.
    #[command(subcommand)]
    pub command: Command,
}

/// When to colour what is printed, as the command line asks.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum Color {
    /// Colour when standard error is a terminal and `NO_COLOR` is unset
    Auto,
    /// Always colour
    Always,
    /// Never colour
    Never,
}

/// One command of the binary.
#[derive(Debug, Subcommand)]
pub(crate) enum Command {
    /// Evaluate every build file twice and run nothing
    Check,
    /// Print the target graph
    Graph(GraphArgs),
    /// Ask what a target needs, or what needs it
    Query(QueryArgs),
    /// Say what a build would run, and why
    Plan(PlanArgs),
    /// Build targets and what they need
    Build(BuildArgs),
    /// Build targets, then build again when a file they read changes
    Watch(BuildArgs),
    /// Run test targets and what they need
    Test(TestArgs),
    /// Re-run cached actions and name those whose outputs differ
    Verify(VerifyArgs),
    /// Print the workspace's ceiling, approver, run settings and what its build wants
    Doctor,
    /// Show what a target printed when it last ran
    Log(LogArgs),
    /// Remove the output directory
    Clean,
    /// Drop rows of targets no longer declared and remove what nothing needs
    Gc,
    /// Work with the plugins `buildl.toml` names
    Plugins(PluginsArgs),
}

/// The arguments of `plugins`.
#[derive(Debug, Args)]
pub(crate) struct PluginsArgs {
    /// What to do with the plugins.
    #[command(subcommand)]
    pub command: PluginsCommand,
}

/// One command of `plugins`.
#[derive(Debug, Subcommand)]
pub(crate) enum PluginsCommand {
    /// Pin every git plugin in buildl.lock and store its files, so a build needs no network
    Fetch,
}

/// The arguments of `graph`.
#[derive(Debug, Args)]
pub(crate) struct GraphArgs {
    /// The form the graph is printed in.
    #[command(flatten)]
    pub format: GraphFormat,
}

/// The form a graph is printed in: one of the two at most, and JSON when neither is named.
#[derive(Debug, Args)]
#[group(multiple = false)]
pub(crate) struct GraphFormat {
    /// Print the graph as canonical JSON, the bytes of .buildl/graph.json (the default)
    #[arg(long)]
    pub json: bool,
    /// Print the graph in Graphviz DOT
    #[arg(long)]
    pub dot: bool,
}

/// The arguments of `query`.
#[derive(Debug, Args)]
pub(crate) struct QueryArgs {
    /// deps(TARGET) for what TARGET needs, or rdeps(TARGET) for what needs it; TARGET as //dir:name, :name or name
    #[arg(value_name = "EXPR")]
    pub expression: String,
    /// Print the answer as canonical JSON
    #[arg(long)]
    pub json: bool,
}

/// What `plan`, `build` and `test` share: which targets, under which settings.
#[derive(Debug, Args)]
pub(crate) struct Selection {
    /// Targets, as //dir:name, :name or name; with none, plan and build take every target but tests, and test every test
    #[arg(value_name = "TARGET")]
    pub targets: Vec<String>,
    /// Give a build setting a value for this run; may be repeated
    #[arg(long = "set", value_name = "NAME=VALUE", value_parser = parse_override)]
    pub overrides: Vec<Override>,
}

/// The arguments of `plan`.
#[derive(Debug, Args)]
pub(crate) struct PlanArgs {
    /// Which targets, under which settings.
    #[command(flatten)]
    pub selection: Selection,
    /// Print the plan as canonical JSON
    #[arg(long)]
    pub json: bool,
}

/// The arguments of `build` and of `watch`, which takes the flags of a build.
#[derive(Debug, Args)]
pub(crate) struct BuildArgs {
    /// Which targets, under which settings.
    #[command(flatten)]
    pub selection: Selection,
    /// After a failure, still build everything that does not depend on it
    #[arg(short = 'k', long)]
    pub keep_going: bool,
    /// Show one running action's output as it is printed
    #[arg(long)]
    pub stream: bool,
    /// What the run does with the remote cache.
    #[command(flatten)]
    pub remote: RemoteArgs,
}

/// The remote cache flags `build`, `test` and `watch` share.
#[derive(Debug, Args)]
#[expect(
    clippy::struct_excessive_bools,
    reason = "each bool is one independent flag of the command line, which clap reads as a bool"
)]
pub(crate) struct RemoteArgs {
    /// Upload what this run produced to the remote cache
    #[arg(long = "remote-upload")]
    pub upload: bool,
    /// Fail the run when a call to the remote cache fails, instead of warning and building locally
    #[arg(long = "remote-strict")]
    pub strict: bool,
    /// Do not use the remote cache in this run, and run nothing on the remote, whatever the manifest and variables say
    #[arg(long = "no-remote")]
    pub no_remote: bool,
    /// Run the actions the remote cache has no result for on the remote, whatever the manifest and variables say
    #[arg(long = "remote-execute")]
    pub remote_execute: bool,
    /// Run every action here, and keep using the remote cache, whatever the manifest and variables say
    #[arg(long = "no-remote-execute")]
    pub no_remote_execute: bool,
    /// How many actions may be with the remote at once, from 1 to 1024, for this run
    #[arg(long = "remote-jobs", value_name = "N", value_parser = parse_remote_jobs)]
    pub remote_jobs: Option<RemoteJobs>,
}

/// A `--remote-jobs` count: a whole number from 1 to 1024.
fn parse_remote_jobs(raw: &str) -> Result<RemoteJobs, String> {
    RemoteJobs::parse(raw).map_err(|error| error.to_string())
}

/// The arguments of `test`.
#[derive(Debug, Args)]
#[expect(
    clippy::struct_excessive_bools,
    reason = "each bool is one independent flag of the command line, which clap reads as a bool"
)]
pub(crate) struct TestArgs {
    /// Which targets, under which settings.
    #[command(flatten)]
    pub selection: Selection,
    /// Start nothing new after the first failure
    #[arg(long)]
    pub stop: bool,
    /// Show one running action's output as it is printed
    #[arg(long)]
    pub stream: bool,
    /// Run the selected tests even when nothing they depend on changed
    #[arg(long)]
    pub rerun: bool,
    /// Show what every test printed, not only what a failing one did
    #[arg(long)]
    pub verbose: bool,
    /// What the run does with the remote cache.
    #[command(flatten)]
    pub remote: RemoteArgs,
}

/// The arguments of `verify`.
#[derive(Debug, Args)]
pub(crate) struct VerifyArgs {
    /// Targets, as //dir:name, :name or name; with none, every target but tests
    #[arg(value_name = "TARGET")]
    pub targets: Vec<String>,
    /// Give a build setting a value for this run; may be repeated
    #[arg(long = "set", value_name = "NAME=VALUE", value_parser = parse_override)]
    pub overrides: Vec<Override>,
    /// Re-run only the N cached actions whose keys sort lowest: the same ones while their keys stay the same
    #[arg(long, value_name = "N")]
    sample: Option<NonZeroUsize>,
}

/// The arguments of `log`.
#[derive(Debug, Args)]
pub(crate) struct LogArgs {
    /// The target, as //dir:name, :name or name
    #[arg(value_name = "TARGET")]
    pub target: String,
    /// Which stream to print.
    #[command(flatten)]
    flags: StreamFlags,
}

/// The stream of a run `log` prints: one of the two at most, and both when neither is named.
#[derive(Debug, Args)]
#[group(multiple = false)]
pub(crate) struct StreamFlags {
    /// Print only what the run wrote to standard output
    #[arg(long)]
    pub stdout: bool,
    /// Print only what the run wrote to standard error, on standard output
    #[arg(long)]
    pub stderr: bool,
}

/// Which of a run's streams `log` prints.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Streams {
    /// Standard output on standard output and standard error on standard error
    Both,
    /// Only what the run wrote to standard output
    Stdout,
    /// Only what the run wrote to standard error, on standard output
    Stderr,
}

impl LogArgs {
    /// The streams the command line asks for.
    pub(crate) const fn streams(&self) -> Streams {
        if self.flags.stdout {
            Streams::Stdout
        } else if self.flags.stderr {
            Streams::Stderr
        } else {
            Streams::Both
        }
    }
}

impl VerifyArgs {
    /// How many of the cached actions the verify re-runs.
    pub(crate) const fn sample(&self) -> Sample {
        match self.sample {
            Some(count) => Sample::Lowest(count),
            None => Sample::All,
        }
    }
}

impl BuildArgs {
    /// What the build does once a target has failed.
    pub(crate) const fn on_failure(&self) -> OnFailure {
        if self.keep_going {
            OnFailure::KeepGoing
        } else {
            OnFailure::Stop
        }
    }

    /// Whether one running action's output is shown as it is printed.
    pub(crate) const fn live(&self) -> LiveOutput {
        if self.stream {
            LiveOutput::OneAction
        } else {
            LiveOutput::Off
        }
    }
}

impl TestArgs {
    /// What the run does once a test has failed.
    pub(crate) const fn on_failure(&self) -> OnFailure {
        if self.stop {
            OnFailure::Stop
        } else {
            OnFailure::KeepGoing
        }
    }

    /// Whether one running action's output is shown as it is printed.
    pub(crate) const fn live(&self) -> LiveOutput {
        if self.stream {
            LiveOutput::OneAction
        } else {
            LiveOutput::Off
        }
    }

    /// Whether the selected tests run again whatever their keys say.
    pub(crate) const fn rerun(&self) -> Rerun {
        if self.rerun { Rerun::Tests } else { Rerun::Off }
    }

    /// Which tests get a block in the report.
    pub(crate) const fn blocks(&self) -> TestBlocks {
        if self.verbose {
            TestBlocks::Every
        } else {
            TestBlocks::Failed
        }
    }
}

/// One `--set NAME=VALUE`.
#[derive(Debug, Clone)]
pub(crate) struct Override {
    /// The setting.
    pub name: SettingName,
    /// The value it is given for this run.
    pub value: SettingValue,
}

/// Reads one `--set` argument, split at its first `=`: a value may hold more of them.
fn parse_override(raw: &str) -> Result<Override, String> {
    let (name, value) = raw
        .split_once('=')
        .ok_or_else(|| "expected NAME=VALUE".to_owned())?;
    Ok(Override {
        name: SettingName::parse(name).map_err(|error| error.to_string())?,
        value: SettingValue::parse(value).map_err(|error| error.to_string())?,
    })
}

/// The settings given a value for this run, by name.
///
/// # Errors
///
/// Returns a usage error when a setting is named twice: which of the two values is meant is
/// never guessed.
pub(crate) fn overrides(
    given: Vec<Override>,
) -> Result<BTreeMap<SettingName, SettingValue>, clap::Error> {
    let mut settings = BTreeMap::new();
    for Override { name, value } in given {
        match settings.entry(name) {
            Entry::Vacant(free) => {
                free.insert(value);
            }
            Entry::Occupied(taken) => {
                return Err(Cli::command().error(
                    ErrorKind::ArgumentConflict,
                    format!("setting {} is given more than once", taken.key()),
                ));
            }
        }
    }
    Ok(settings)
}

#[cfg(test)]
mod tests {
    #![expect(
        clippy::unwrap_used,
        clippy::panic,
        reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
    )]

    use std::iter;
    use std::num::NonZeroUsize;

    use buildl::buildl_core::{LiveOutput, OnFailure, RemoteJobs, Rerun, Sample};
    use buildl::report::TestBlocks;
    use clap::error::ErrorKind;
    use clap::{CommandFactory, Parser};

    use super::{
        BuildArgs, Cli, Color, Command, LogArgs, PlanArgs, PluginsCommand, Streams, TestArgs,
        VerifyArgs, overrides,
    };

    /// The command line `args` after the program's name, parsed.
    fn parsed(args: &[&str]) -> Cli {
        Cli::try_parse_from(iter::once(&"buildl").chain(args)).unwrap()
    }

    /// The kind of the error the command line `args` is refused with.
    fn refused(args: &[&str]) -> ErrorKind {
        Cli::try_parse_from(iter::once(&"buildl").chain(args))
            .unwrap_err()
            .kind()
    }

    /// The arguments of the `build` command line `args`.
    fn build(args: &[&str]) -> BuildArgs {
        match parsed(args).command {
            Command::Build(build) => build,
            other => panic!("expected build, got {other:?}"),
        }
    }

    /// The arguments of the `test` command line `args`.
    fn test(args: &[&str]) -> TestArgs {
        match parsed(args).command {
            Command::Test(test) => test,
            other => panic!("expected test, got {other:?}"),
        }
    }

    /// The arguments of the `watch` command line `args`.
    fn watch(args: &[&str]) -> BuildArgs {
        match parsed(args).command {
            Command::Watch(watch) => watch,
            other => panic!("expected watch, got {other:?}"),
        }
    }

    /// The arguments of the `plan` command line `args`.
    fn plan(args: &[&str]) -> PlanArgs {
        match parsed(args).command {
            Command::Plan(plan) => plan,
            other => panic!("expected plan, got {other:?}"),
        }
    }

    /// The arguments of the `log` command line `args`.
    fn log(args: &[&str]) -> LogArgs {
        match parsed(args).command {
            Command::Log(log) => log,
            other => panic!("expected log, got {other:?}"),
        }
    }

    /// The arguments of the `verify` command line `args`.
    fn verify(args: &[&str]) -> VerifyArgs {
        match parsed(args).command {
            Command::Verify(verify) => verify,
            other => panic!("expected verify, got {other:?}"),
        }
    }

    #[test]
    fn the_definition_is_consistent() {
        Cli::command().debug_assert();
    }

    #[test]
    fn each_command_parses() {
        assert!(matches!(parsed(&["check"]).command, Command::Check));
        assert!(matches!(parsed(&["graph"]).command, Command::Graph(_)));
        assert!(matches!(parsed(&["plan"]).command, Command::Plan(_)));
        assert!(matches!(parsed(&["build"]).command, Command::Build(_)));
        assert!(matches!(parsed(&["test"]).command, Command::Test(_)));
        assert!(matches!(parsed(&["verify"]).command, Command::Verify(_)));
        assert!(matches!(parsed(&["log", "app"]).command, Command::Log(_)));
    }

    #[test]
    fn plugins_fetch_parses_and_plugins_alone_is_refused() {
        let Command::Plugins(plugins) = parsed(&["plugins", "fetch"]).command else {
            panic!("expected plugins");
        };
        assert!(matches!(plugins.command, PluginsCommand::Fetch));
        assert_eq!(
            refused(&["plugins"]),
            ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
        );
        assert_eq!(
            refused(&["plugins", "update"]),
            ErrorKind::InvalidSubcommand
        );
    }

    #[test]
    fn log_streams_map_to_their_values() {
        assert_eq!(log(&["log", "app"]).streams(), Streams::Both);
        assert_eq!(log(&["log", "app", "--stdout"]).streams(), Streams::Stdout);
        assert_eq!(log(&["log", "--stderr", "app"]).streams(), Streams::Stderr);
        assert_eq!(log(&["log", "//lib:app"]).target, "//lib:app");
    }

    #[test]
    fn log_takes_one_stream_flag_and_one_target() {
        assert_eq!(
            refused(&["log", "app", "--stdout", "--stderr"]),
            ErrorKind::ArgumentConflict
        );
        assert_eq!(refused(&["log"]), ErrorKind::MissingRequiredArgument);
    }

    #[test]
    fn targets_are_kept_as_written_and_in_order() {
        let args = plan(&["plan", "//lib:text", "app", ":docs"]);
        assert_eq!(args.selection.targets, ["//lib:text", "app", ":docs"]);
        assert_eq!(plan(&["plan"]).selection.targets, Vec::<String>::new());
    }

    #[test]
    fn set_may_be_repeated() {
        let args = plan(&["plan", "--set", "a=b", "--set", "c=d=e", "--set", "f="]);
        let settings = overrides(args.selection.overrides).unwrap();
        let settings: Vec<(&str, &str)> = settings
            .iter()
            .map(|(name, value)| (name.as_str(), value.as_str()))
            .collect();
        assert_eq!(settings, [("a", "b"), ("c", "d=e"), ("f", "")]);
    }

    #[test]
    fn a_setting_given_twice_is_refused() {
        let args = plan(&["plan", "--set", "a=b", "--set", "a=c"]);
        let error = overrides(args.selection.overrides).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::ArgumentConflict);
        assert!(error.use_stderr());
        assert!(
            error
                .to_string()
                .starts_with("error: setting a is given more than once\n")
        );
    }

    #[test]
    fn set_without_a_value_part_is_refused() {
        let error = Cli::try_parse_from(["buildl", "plan", "--set", "novalue"]).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::ValueValidation);
        assert!(error.to_string().starts_with(
            "error: invalid value 'novalue' for '--set <NAME=VALUE>': expected NAME=VALUE\n"
        ));
    }

    #[test]
    fn a_setting_name_outside_its_grammar_is_refused() {
        assert_eq!(
            refused(&["plan", "--set", "bad name=x"]),
            ErrorKind::ValueValidation
        );
    }

    #[test]
    fn json_and_dot_exclude_each_other() {
        assert_eq!(
            refused(&["graph", "--json", "--dot"]),
            ErrorKind::ArgumentConflict
        );
        let Command::Graph(dot) = parsed(&["graph", "--dot"]).command else {
            panic!("expected graph");
        };
        assert!(dot.format.dot && !dot.format.json);
        let Command::Graph(neither) = parsed(&["graph"]).command else {
            panic!("expected graph");
        };
        assert!(!neither.format.dot && !neither.format.json);
    }

    #[test]
    fn check_takes_no_target() {
        assert_eq!(refused(&["check", "extra"]), ErrorKind::UnknownArgument);
        assert_eq!(refused(&["graph", "extra"]), ErrorKind::UnknownArgument);
        assert_eq!(
            refused(&["check", "--set", "a=b"]),
            ErrorKind::UnknownArgument
        );
    }

    #[test]
    fn keep_going_and_stream_are_flags() {
        let quiet = build(&["build"]);
        assert_eq!(quiet.on_failure(), OnFailure::Stop);
        assert_eq!(quiet.live(), LiveOutput::Off);

        let long = build(&["build", "--keep-going", "--stream"]);
        assert_eq!(long.on_failure(), OnFailure::KeepGoing);
        assert_eq!(long.live(), LiveOutput::OneAction);

        assert_eq!(build(&["build", "-k"]).on_failure(), OnFailure::KeepGoing);
        assert_eq!(build(&["build", "-k", "app"]).selection.targets, ["app"]);
        assert_eq!(
            refused(&["plan", "--keep-going"]),
            ErrorKind::UnknownArgument
        );
    }

    #[test]
    fn test_flags_map_to_their_values() {
        let quiet = test(&["test"]);
        assert_eq!(quiet.on_failure(), OnFailure::KeepGoing);
        assert_eq!(quiet.live(), LiveOutput::Off);
        assert_eq!(quiet.rerun(), Rerun::Off);
        assert_eq!(quiet.blocks(), TestBlocks::Failed);

        let every = test(&["test", "--stop", "--stream", "--rerun", "--verbose"]);
        assert_eq!(every.on_failure(), OnFailure::Stop);
        assert_eq!(every.live(), LiveOutput::OneAction);
        assert_eq!(every.rerun(), Rerun::Tests);
        assert_eq!(every.blocks(), TestBlocks::Every);

        let named = test(&["test", "//lib:unit", "app", ":docs"]);
        assert_eq!(named.selection.targets, ["//lib:unit", "app", ":docs"]);
    }

    #[test]
    fn the_flags_parse_on_build_and_test() {
        let none = build(&["build"]).remote;
        assert!(!none.upload && !none.strict && !none.no_remote);

        let upload = build(&["build", "--remote-upload"]).remote;
        assert!(upload.upload && !upload.strict && !upload.no_remote);
        let strict = build(&["build", "--remote-strict"]).remote;
        assert!(!strict.upload && strict.strict && !strict.no_remote);
        let off = build(&["build", "--no-remote"]).remote;
        assert!(!off.upload && !off.strict && off.no_remote);

        let none = test(&["test"]).remote;
        assert!(!none.upload && !none.strict && !none.no_remote);
        let upload = test(&["test", "--remote-upload"]).remote;
        assert!(upload.upload && !upload.strict && !upload.no_remote);
        let strict = test(&["test", "--remote-strict"]).remote;
        assert!(!strict.upload && strict.strict && !strict.no_remote);
        let off = test(&["test", "--no-remote", "app"]).remote;
        assert!(!off.upload && !off.strict && off.no_remote);

        assert_eq!(
            refused(&["plan", "--remote-upload"]),
            ErrorKind::UnknownArgument
        );
    }

    #[test]
    fn build_test_and_watch_take_the_execution_flags() {
        for args in [
            build(&["build"]).remote,
            test(&["test"]).remote,
            watch(&["watch"]).remote,
        ] {
            assert!(!args.remote_execute && !args.no_remote_execute);
            assert_eq!(args.remote_jobs, None);
        }

        let on = [
            build(&["build", "--remote-execute", "--remote-jobs", "16"]).remote,
            test(&["test", "--remote-execute", "--remote-jobs", "16", "app"]).remote,
            watch(&["watch", "--remote-execute", "--remote-jobs", "16"]).remote,
        ];
        for args in on {
            assert!(args.remote_execute && !args.no_remote_execute);
            assert_eq!(args.remote_jobs, Some(RemoteJobs::parse("16").unwrap()));
        }

        assert!(
            build(&["build", "--no-remote-execute"])
                .remote
                .no_remote_execute
        );
        assert!(
            test(&["test", "--no-remote-execute"])
                .remote
                .no_remote_execute
        );
        assert!(
            watch(&["watch", "--no-remote-execute"])
                .remote
                .no_remote_execute
        );

        for bad in ["0", "1025", "many", ""] {
            assert_eq!(
                refused(&["build", "--remote-jobs", bad]),
                ErrorKind::ValueValidation,
                "{bad:?}"
            );
        }
        assert_eq!(
            refused(&["plan", "--remote-execute"]),
            ErrorKind::UnknownArgument
        );
    }

    #[test]
    fn watch_parses_builds_flags() {
        let args = watch(&[
            "watch",
            "-k",
            "--stream",
            "--set",
            "a=b",
            "//x",
            "--remote-upload",
            "--remote-strict",
        ]);
        assert_eq!(args.on_failure(), OnFailure::KeepGoing);
        assert_eq!(args.live(), LiveOutput::OneAction);
        assert_eq!(args.selection.targets, ["//x"]);
        assert_eq!(overrides(args.selection.overrides).unwrap().len(), 1);
        assert!(args.remote.upload && args.remote.strict && !args.remote.no_remote);
        assert!(watch(&["watch", "--no-remote"]).remote.no_remote);
    }

    #[test]
    fn watch_with_no_target_is_the_whole_workspace() {
        let args = watch(&["watch"]);
        assert_eq!(args.selection.targets, Vec::<String>::new());
        assert_eq!(args.on_failure(), OnFailure::Stop);
        assert_eq!(args.live(), LiveOutput::Off);
    }

    #[test]
    fn test_takes_no_keep_going() {
        assert_eq!(refused(&["test", "-k"]), ErrorKind::UnknownArgument);
        assert_eq!(
            refused(&["test", "--keep-going"]),
            ErrorKind::UnknownArgument
        );
    }

    #[test]
    fn the_color_flag_follows_the_command() {
        assert_eq!(parsed(&["check"]).color, Color::Auto);
        assert_eq!(parsed(&["--color", "never", "check"]).color, Color::Never);
        assert_eq!(parsed(&["check", "--color", "always"]).color, Color::Always);
        assert_eq!(
            parsed(&["build", "-k", "--color", "never", "app"]).color,
            Color::Never
        );
        assert_eq!(
            refused(&["plan", "--color", "sometimes"]),
            ErrorKind::InvalidValue
        );
    }

    #[test]
    fn there_is_no_flag_for_a_start_directory() {
        assert_eq!(refused(&["-C", "/tmp", "plan"]), ErrorKind::UnknownArgument);
    }

    #[test]
    fn verify_takes_targets_and_settings_as_build_does() {
        let args = verify(&["verify", "//lib:text", "app", "--set", "a=b"]);
        assert_eq!(args.targets, ["//lib:text", "app"]);
        let settings = overrides(args.overrides).unwrap();
        assert_eq!(settings.len(), 1);
        assert_eq!(verify(&["verify"]).targets, Vec::<String>::new());
    }

    #[test]
    fn verify_samples_everything_unless_told_a_number() {
        assert_eq!(verify(&["verify"]).sample(), Sample::All);
        assert_eq!(
            verify(&["verify", "--sample", "3"]).sample(),
            Sample::Lowest(NonZeroUsize::new(3).unwrap())
        );
    }

    #[test]
    fn verify_refuses_a_sample_that_is_not_a_positive_number() {
        for raw in ["0", "two", ""] {
            assert_eq!(
                refused(&["verify", "--sample", raw]),
                ErrorKind::ValueValidation,
                "{raw:?}"
            );
        }
        assert_eq!(
            refused(&["verify", "--sample=-1"]),
            ErrorKind::ValueValidation
        );
        assert_eq!(refused(&["verify", "--sample"]), ErrorKind::InvalidValue);
    }

    #[test]
    fn verify_takes_no_build_flags() {
        assert_eq!(refused(&["verify", "-k"]), ErrorKind::UnknownArgument);
        assert_eq!(refused(&["verify", "--stream"]), ErrorKind::UnknownArgument);
    }
}