cargo-nextest 0.9.130

A next-generation test runner for Rust.
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
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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Value enums shared across core commands.

use crate::errors::CargoMessageFormatError;
use clap::ValueEnum;
use nextest_metadata::BuildPlatform;
use nextest_runner::reporter::{FinalStatusLevel, ShowProgress, StatusLevel, TestOutputDisplay};
use std::collections::HashSet;

/// Platform filter options.
#[derive(Copy, Clone, Debug, ValueEnum, Default)]
pub(crate) enum PlatformFilterOpts {
    Target,
    Host,
    #[default]
    Any,
}

impl From<PlatformFilterOpts> for Option<BuildPlatform> {
    fn from(opt: PlatformFilterOpts) -> Self {
        match opt {
            PlatformFilterOpts::Target => Some(BuildPlatform::Target),
            PlatformFilterOpts::Host => Some(BuildPlatform::Host),
            PlatformFilterOpts::Any => None,
        }
    }
}

/// List type options.
#[derive(Copy, Clone, Debug, ValueEnum, Default)]
pub(crate) enum ListType {
    #[default]
    Full,
    BinariesOnly,
}

/// Message format options for list command.
#[derive(Copy, Clone, Debug, ValueEnum, Default)]
pub(crate) enum MessageFormatOpts {
    /// Auto-detect: **human** if stdout is an interactive terminal, **oneline**
    /// otherwise.
    #[default]
    Auto,
    /// A human-readable output format.
    Human,
    /// One test per line.
    Oneline,
    /// JSON with no whitespace.
    Json,
    /// JSON, prettified.
    JsonPretty,
}

impl MessageFormatOpts {
    pub(crate) fn to_output_format(
        self,
        verbose: bool,
        is_terminal: bool,
    ) -> nextest_runner::list::OutputFormat {
        use nextest_runner::list::{OutputFormat, SerializableFormat};

        match self {
            Self::Auto => {
                if is_terminal {
                    OutputFormat::Human { verbose }
                } else {
                    OutputFormat::Oneline { verbose }
                }
            }
            Self::Human => OutputFormat::Human { verbose },
            Self::Oneline => OutputFormat::Oneline { verbose },
            Self::Json => OutputFormat::Serializable(SerializableFormat::Json),
            Self::JsonPretty => OutputFormat::Serializable(SerializableFormat::JsonPretty),
        }
    }

    /// Returns true if this format is human-readable (suitable for paging).
    ///
    /// Machine-readable formats (JSON) should not be paged.
    pub(crate) fn is_human_readable(self) -> bool {
        match self {
            Self::Auto | Self::Human | Self::Oneline => true,
            Self::Json | Self::JsonPretty => false,
        }
    }
}

/// Run ignored test options.
#[derive(Copy, Clone, Debug, ValueEnum)]
pub(crate) enum RunIgnoredOpt {
    /// Run non-ignored tests.
    Default,

    /// Run ignored tests.
    #[clap(alias = "ignored-only")]
    Only,

    /// Run both ignored and non-ignored tests.
    All,
}

impl From<RunIgnoredOpt> for nextest_runner::test_filter::RunIgnored {
    fn from(opt: RunIgnoredOpt) -> Self {
        match opt {
            RunIgnoredOpt::Default => Self::Default,
            RunIgnoredOpt::Only => Self::Only,
            RunIgnoredOpt::All => Self::All,
        }
    }
}

/// No tests behavior options.
#[derive(Clone, Copy, Debug, Default, ValueEnum)]
pub(crate) enum NoTestsBehaviorOpt {
    /// Automatically determine behavior, defaulting to `fail`.
    #[default]
    Auto,

    /// Silently exit with code 0.
    Pass,

    /// Produce a warning and exit with code 0.
    Warn,

    /// Produce an error message and exit with code 4.
    #[clap(alias = "error")]
    Fail,
}

/// Test output display options.
#[derive(Clone, Copy, Debug, ValueEnum)]
pub(crate) enum TestOutputDisplayOpt {
    Immediate,
    ImmediateFinal,
    Final,
    Never,
}

impl From<TestOutputDisplayOpt> for TestOutputDisplay {
    fn from(opt: TestOutputDisplayOpt) -> Self {
        match opt {
            TestOutputDisplayOpt::Immediate => TestOutputDisplay::Immediate,
            TestOutputDisplayOpt::ImmediateFinal => TestOutputDisplay::ImmediateFinal,
            TestOutputDisplayOpt::Final => TestOutputDisplay::Final,
            TestOutputDisplayOpt::Never => TestOutputDisplay::Never,
        }
    }
}

/// Status level options.
#[derive(Clone, Copy, Debug, ValueEnum)]
pub(crate) enum StatusLevelOpt {
    None,
    Fail,
    Retry,
    Slow,
    Leak,
    Pass,
    Skip,
    All,
}

impl From<StatusLevelOpt> for StatusLevel {
    fn from(opt: StatusLevelOpt) -> Self {
        match opt {
            StatusLevelOpt::None => StatusLevel::None,
            StatusLevelOpt::Fail => StatusLevel::Fail,
            StatusLevelOpt::Retry => StatusLevel::Retry,
            StatusLevelOpt::Slow => StatusLevel::Slow,
            StatusLevelOpt::Leak => StatusLevel::Leak,
            StatusLevelOpt::Pass => StatusLevel::Pass,
            StatusLevelOpt::Skip => StatusLevel::Skip,
            StatusLevelOpt::All => StatusLevel::All,
        }
    }
}

/// Final status level options.
#[derive(Clone, Copy, Debug, ValueEnum)]
pub(crate) enum FinalStatusLevelOpt {
    None,
    Fail,
    #[clap(alias = "retry")]
    Flaky,
    Slow,
    Skip,
    Pass,
    All,
}

impl From<FinalStatusLevelOpt> for FinalStatusLevel {
    fn from(opt: FinalStatusLevelOpt) -> Self {
        match opt {
            FinalStatusLevelOpt::None => FinalStatusLevel::None,
            FinalStatusLevelOpt::Fail => FinalStatusLevel::Fail,
            FinalStatusLevelOpt::Flaky => FinalStatusLevel::Flaky,
            FinalStatusLevelOpt::Slow => FinalStatusLevel::Slow,
            FinalStatusLevelOpt::Skip => FinalStatusLevel::Skip,
            FinalStatusLevelOpt::Pass => FinalStatusLevel::Pass,
            FinalStatusLevelOpt::All => FinalStatusLevel::All,
        }
    }
}

/// Show progress options.
#[derive(Default, Clone, Copy, Debug, ValueEnum)]
pub(crate) enum ShowProgressOpt {
    /// Automatically choose the best progress display based on whether nextest
    /// is running in an interactive terminal.
    #[default]
    Auto,

    /// Do not display a progress bar or counter.
    None,

    /// Display a progress bar with running tests: default for interactive
    /// terminals.
    #[clap(alias = "running")]
    Bar,

    /// Display a counter next to each completed test.
    Counter,

    /// In interactive terminals, display a progress bar with running tests and
    /// hide successful test output (equivalent to `--show-progress=bar
    /// --status-level=slow --final-status-level=none`). In non-interactive
    /// contexts (piped output, CI), behaves like `auto`: successful test output
    /// is shown normally.
    Only,
}

impl From<ShowProgressOpt> for ShowProgress {
    fn from(opt: ShowProgressOpt) -> Self {
        match opt {
            ShowProgressOpt::Auto => ShowProgress::Auto {
                suppress_success: false,
            },
            ShowProgressOpt::None => ShowProgress::None,
            ShowProgressOpt::Bar => ShowProgress::Running,
            ShowProgressOpt::Counter => ShowProgress::Counter,
            ShowProgressOpt::Only => ShowProgress::Auto {
                suppress_success: true,
            },
        }
    }
}

/// Ignore overrides options.
#[derive(Clone, Copy, Debug, ValueEnum)]
pub(crate) enum IgnoreOverridesOpt {
    Retries,
    All,
}

/// Message format for run command (experimental).
#[derive(Clone, Copy, Debug, ValueEnum, Default)]
pub(crate) enum MessageFormat {
    /// The default output format.
    #[default]
    Human,
    /// Output test information in the same format as libtest.
    LibtestJson,
    /// Output test information in the same format as libtest, with a `nextest` subobject that
    /// includes additional metadata.
    LibtestJsonPlus,
}

/// Configuration for how to invoke Cargo and handle its output.
///
/// This type controls:
///
/// - What `--message-format` arguments to pass to Cargo.
/// - Whether to forward JSON output to stdout.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum CargoMessageFormat {
    /// Human-readable diagnostics rendered by Cargo.
    ///
    /// Cargo args: `json-render-diagnostics`
    ///
    /// Note: `--cargo-message-format short` also maps to this variant, since
    /// `cargo test --message-format short` produces the same output as human.
    #[default]
    Human,

    /// JSON output forwarded to stdout.
    ///
    /// Cargo args: combinations of `json`, `json-diagnostic-short`,
    /// `json-diagnostic-rendered-ansi`, and/or `json-render-diagnostics`
    Json {
        /// Whether to also render diagnostics to stderr via Cargo.
        ///
        /// When true, Cargo uses `json-render-diagnostics`, which:
        /// - Renders compiler messages to stderr
        /// - **Removes** `compiler-message` entries from the JSON stdout
        ///
        /// When false, `compiler-message` entries remain in the JSON output.
        render_diagnostics: bool,

        /// Whether to use short diagnostic format.
        ///
        /// Cargo arg: `json-diagnostic-short`
        short: bool,

        /// Whether to include ANSI color codes in the `rendered` field.
        ///
        /// Cargo arg: `json-diagnostic-rendered-ansi`
        ansi: bool,
    },
}

impl CargoMessageFormat {
    /// Returns the `--message-format` argument(s) to pass to Cargo.
    pub(crate) fn cargo_arg(&self) -> &'static str {
        match self {
            Self::Human => "json-render-diagnostics",
            Self::Json {
                render_diagnostics: false,
                short: false,
                ansi: false,
            } => "json",
            Self::Json {
                render_diagnostics: false,
                short: true,
                ansi: false,
            } => "json-diagnostic-short",
            Self::Json {
                render_diagnostics: false,
                short: false,
                ansi: true,
            } => "json-diagnostic-rendered-ansi",
            Self::Json {
                render_diagnostics: false,
                short: true,
                ansi: true,
            } => "json-diagnostic-short,json-diagnostic-rendered-ansi",
            Self::Json {
                render_diagnostics: true,
                short: false,
                ansi: false,
            } => "json-render-diagnostics",
            Self::Json {
                render_diagnostics: true,
                short: true,
                ansi: false,
            } => "json-render-diagnostics,json-diagnostic-short",
            Self::Json {
                render_diagnostics: true,
                short: false,
                ansi: true,
            } => "json-render-diagnostics,json-diagnostic-rendered-ansi",
            Self::Json {
                render_diagnostics: true,
                short: true,
                ansi: true,
            } => "json-render-diagnostics,json-diagnostic-short,json-diagnostic-rendered-ansi",
        }
    }

    /// Returns whether JSON should be forwarded to stdout.
    pub(crate) fn forward_json(&self) -> bool {
        matches!(self, Self::Json { .. })
    }
}

/// Cargo message format CLI options.
///
/// Controls the format of Cargo's build output and whether to forward JSON
/// messages to stdout.
///
/// JSON modifiers can be combined:
/// `json,json-diagnostic-short,json-diagnostic-rendered-ansi`
#[derive(Clone, Copy, Debug, ValueEnum, Default, PartialEq, Eq, Hash)]
pub(crate) enum CargoMessageFormatOpt {
    /// Render diagnostics in the default human-readable format.
    #[default]
    Human,

    /// Alias for `human`.
    Short,

    /// Emit JSON messages to stdout.
    Json,

    /// Ensure the `rendered` field of JSON messages contains the "short"
    /// rendering from rustc.
    JsonDiagnosticShort,

    /// Ensure the `rendered` field of JSON messages contains ANSI color codes.
    JsonDiagnosticRenderedAnsi,

    /// Output JSON messages with human-readable diagnostics.
    JsonRenderDiagnostics,
}

impl CargoMessageFormatOpt {
    /// Returns the string representation of this option.
    fn as_str(self) -> &'static str {
        match self {
            Self::Human => "human",
            Self::Short => "short",
            Self::Json => "json",
            Self::JsonDiagnosticShort => "json-diagnostic-short",
            Self::JsonDiagnosticRenderedAnsi => "json-diagnostic-rendered-ansi",
            Self::JsonRenderDiagnostics => "json-render-diagnostics",
        }
    }

    /// Combines CLI options into a single domain model.
    pub(crate) fn combine(opts: &[Self]) -> Result<CargoMessageFormat, CargoMessageFormatError> {
        let mut base_format: Option<Self> = None;
        let mut short = false;
        let mut ansi = false;
        let mut render_diagnostics = false;
        let mut seen = HashSet::new();

        for &opt in opts {
            if !seen.insert(opt) {
                return Err(CargoMessageFormatError::Duplicate {
                    option: opt.as_str(),
                });
            }

            match opt {
                Self::Human | Self::Short | Self::Json => {
                    if let Some(existing) = base_format {
                        return Err(CargoMessageFormatError::ConflictingBaseFormats {
                            first: existing.as_str(),
                            second: opt.as_str(),
                        });
                    }
                    base_format = Some(opt);
                }
                Self::JsonDiagnosticShort => short = true,
                Self::JsonDiagnosticRenderedAnsi => ansi = true,
                Self::JsonRenderDiagnostics => render_diagnostics = true,
            }
        }

        let has_json_modifiers = short || ansi || render_diagnostics;

        match base_format {
            None if has_json_modifiers => {
                // JSON modifiers without explicit base imply JSON.
                Ok(CargoMessageFormat::Json {
                    render_diagnostics,
                    short,
                    ansi,
                })
            }
            None => {
                // No options specified, use default.
                Ok(CargoMessageFormat::Human)
            }
            Some(fmt @ Self::Human) | Some(fmt @ Self::Short) => {
                if has_json_modifiers {
                    return Err(CargoMessageFormatError::JsonModifierWithNonJson {
                        modifiers: json_modifiers_str(short, ansi, render_diagnostics),
                        base: fmt.as_str(),
                    });
                }
                // Both human and short map to Human (short produces same output
                // as human for `cargo test`).
                Ok(CargoMessageFormat::Human)
            }
            Some(Self::Json) => Ok(CargoMessageFormat::Json {
                render_diagnostics,
                short,
                ansi,
            }),
            Some(_) => unreachable!(),
        }
    }
}

/// Returns a formatted string listing all JSON modifiers that are set.
fn json_modifiers_str(short: bool, ansi: bool, render_diagnostics: bool) -> &'static str {
    match (short, ansi, render_diagnostics) {
        (true, false, false) => "`json-diagnostic-short`",
        (false, true, false) => "`json-diagnostic-rendered-ansi`",
        (false, false, true) => "`json-render-diagnostics`",
        (true, true, false) => "`json-diagnostic-short` and `json-diagnostic-rendered-ansi`",
        (true, false, true) => "`json-diagnostic-short` and `json-render-diagnostics`",
        (false, true, true) => "`json-diagnostic-rendered-ansi` and `json-render-diagnostics`",
        (true, true, true) => {
            "`json-diagnostic-short`, `json-diagnostic-rendered-ansi`, \
             and `json-render-diagnostics`"
        }
        (false, false, false) => unreachable!("at least one modifier must be set"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use CargoMessageFormatOpt::*;

    /// Helper to assert successful combination.
    fn assert_combine(opts: &[CargoMessageFormatOpt], expected: CargoMessageFormat) {
        let result = CargoMessageFormatOpt::combine(opts);
        assert_eq!(result, Ok(expected), "opts={opts:?}");
    }

    /// Helper to assert conflicting base formats error.
    fn assert_conflicting(
        opts: &[CargoMessageFormatOpt],
        first: &'static str,
        second: &'static str,
    ) {
        let result = CargoMessageFormatOpt::combine(opts);
        assert_eq!(
            result,
            Err(CargoMessageFormatError::ConflictingBaseFormats { first, second }),
            "opts={opts:?}"
        );
    }

    /// Helper to assert JSON modifier with non-JSON base error.
    fn assert_modifier_with_non_json(
        opts: &[CargoMessageFormatOpt],
        modifiers: &'static str,
        base: &'static str,
    ) {
        let result = CargoMessageFormatOpt::combine(opts);
        assert_eq!(
            result,
            Err(CargoMessageFormatError::JsonModifierWithNonJson { modifiers, base }),
            "opts={opts:?}"
        );
    }

    /// Helper to assert duplicate error.
    fn assert_duplicate(opts: &[CargoMessageFormatOpt], option: &'static str) {
        let result = CargoMessageFormatOpt::combine(opts);
        assert_eq!(
            result,
            Err(CargoMessageFormatError::Duplicate { option }),
            "opts={opts:?}"
        );
    }

    #[test]
    fn test_cargo_message_format_opt_combine() {
        // ---
        // Empty input
        // ---
        assert_combine(&[], CargoMessageFormat::Human);

        // ---
        // Single base formats
        // ---

        assert_combine(&[Human], CargoMessageFormat::Human);

        // Short maps to Human (cargo test --message-format short produces the
        // same output as human).
        assert_combine(&[Short], CargoMessageFormat::Human);

        assert_combine(
            &[Json],
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: false,
                ansi: false,
            },
        );

        // ---
        // Single JSON modifiers (imply Json base)
        // ---

        assert_combine(
            &[JsonDiagnosticShort],
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: true,
                ansi: false,
            },
        );

        assert_combine(
            &[JsonDiagnosticRenderedAnsi],
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: false,
                ansi: true,
            },
        );

        assert_combine(
            &[JsonRenderDiagnostics],
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: false,
                ansi: false,
            },
        );

        // ---
        // Json base + one modifier
        // ---

        assert_combine(
            &[Json, JsonDiagnosticShort],
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: true,
                ansi: false,
            },
        );

        assert_combine(
            &[Json, JsonDiagnosticRenderedAnsi],
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: false,
                ansi: true,
            },
        );

        assert_combine(
            &[Json, JsonRenderDiagnostics],
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: false,
                ansi: false,
            },
        );

        // ---
        // Json base + two modifiers
        // ---

        assert_combine(
            &[Json, JsonDiagnosticShort, JsonDiagnosticRenderedAnsi],
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: true,
                ansi: true,
            },
        );

        assert_combine(
            &[Json, JsonDiagnosticShort, JsonRenderDiagnostics],
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: true,
                ansi: false,
            },
        );

        assert_combine(
            &[Json, JsonDiagnosticRenderedAnsi, JsonRenderDiagnostics],
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: false,
                ansi: true,
            },
        );

        // ---
        // Json base + all three modifiers
        // ---

        assert_combine(
            &[
                Json,
                JsonDiagnosticShort,
                JsonDiagnosticRenderedAnsi,
                JsonRenderDiagnostics,
            ],
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: true,
                ansi: true,
            },
        );

        // ---
        // Two modifiers without explicit Json base
        // ---

        assert_combine(
            &[JsonDiagnosticShort, JsonDiagnosticRenderedAnsi],
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: true,
                ansi: true,
            },
        );

        assert_combine(
            &[JsonDiagnosticShort, JsonRenderDiagnostics],
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: true,
                ansi: false,
            },
        );

        assert_combine(
            &[JsonDiagnosticRenderedAnsi, JsonRenderDiagnostics],
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: false,
                ansi: true,
            },
        );

        // ---
        // Three modifiers without explicit Json base
        // ---

        assert_combine(
            &[
                JsonDiagnosticShort,
                JsonDiagnosticRenderedAnsi,
                JsonRenderDiagnostics,
            ],
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: true,
                ansi: true,
            },
        );

        // ---
        // Order independence: same combinations in different orders
        // ---

        assert_combine(
            &[JsonDiagnosticRenderedAnsi, JsonDiagnosticShort],
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: true,
                ansi: true,
            },
        );

        assert_combine(
            &[JsonDiagnosticShort, Json],
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: true,
                ansi: false,
            },
        );

        assert_combine(
            &[
                JsonRenderDiagnostics,
                JsonDiagnosticShort,
                JsonDiagnosticRenderedAnsi,
                Json,
            ],
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: true,
                ansi: true,
            },
        );

        // ---
        // Error: conflicting base formats
        // ---

        assert_conflicting(&[Human, Json], "human", "json");
        assert_conflicting(&[Human, Short], "human", "short");
        assert_conflicting(&[Short, Json], "short", "json");
        assert_conflicting(&[Json, Human], "json", "human");
        assert_conflicting(&[Json, Short], "json", "short");
        assert_conflicting(&[Short, Human], "short", "human");

        // ---
        // Error: JSON modifier with Human base
        // ---

        assert_modifier_with_non_json(
            &[Human, JsonDiagnosticShort],
            "`json-diagnostic-short`",
            "human",
        );
        assert_modifier_with_non_json(
            &[Human, JsonDiagnosticRenderedAnsi],
            "`json-diagnostic-rendered-ansi`",
            "human",
        );
        assert_modifier_with_non_json(
            &[Human, JsonRenderDiagnostics],
            "`json-render-diagnostics`",
            "human",
        );
        assert_modifier_with_non_json(
            &[Human, JsonDiagnosticShort, JsonDiagnosticRenderedAnsi],
            "`json-diagnostic-short` and `json-diagnostic-rendered-ansi`",
            "human",
        );
        assert_modifier_with_non_json(
            &[Human, JsonDiagnosticShort, JsonRenderDiagnostics],
            "`json-diagnostic-short` and `json-render-diagnostics`",
            "human",
        );
        assert_modifier_with_non_json(
            &[Human, JsonDiagnosticRenderedAnsi, JsonRenderDiagnostics],
            "`json-diagnostic-rendered-ansi` and `json-render-diagnostics`",
            "human",
        );
        assert_modifier_with_non_json(
            &[
                Human,
                JsonDiagnosticShort,
                JsonDiagnosticRenderedAnsi,
                JsonRenderDiagnostics,
            ],
            "`json-diagnostic-short`, `json-diagnostic-rendered-ansi`, \
             and `json-render-diagnostics`",
            "human",
        );

        // ---
        // Error: JSON modifier with Short base
        // ---

        assert_modifier_with_non_json(
            &[Short, JsonDiagnosticShort],
            "`json-diagnostic-short`",
            "short",
        );
        assert_modifier_with_non_json(
            &[Short, JsonDiagnosticRenderedAnsi],
            "`json-diagnostic-rendered-ansi`",
            "short",
        );
        assert_modifier_with_non_json(
            &[Short, JsonRenderDiagnostics],
            "`json-render-diagnostics`",
            "short",
        );
        assert_modifier_with_non_json(
            &[Short, JsonDiagnosticShort, JsonDiagnosticRenderedAnsi],
            "`json-diagnostic-short` and `json-diagnostic-rendered-ansi`",
            "short",
        );
        assert_modifier_with_non_json(
            &[Short, JsonDiagnosticShort, JsonRenderDiagnostics],
            "`json-diagnostic-short` and `json-render-diagnostics`",
            "short",
        );
        assert_modifier_with_non_json(
            &[Short, JsonDiagnosticRenderedAnsi, JsonRenderDiagnostics],
            "`json-diagnostic-rendered-ansi` and `json-render-diagnostics`",
            "short",
        );
        assert_modifier_with_non_json(
            &[
                Short,
                JsonDiagnosticShort,
                JsonDiagnosticRenderedAnsi,
                JsonRenderDiagnostics,
            ],
            "`json-diagnostic-short`, `json-diagnostic-rendered-ansi`, \
             and `json-render-diagnostics`",
            "short",
        );

        // ---
        // Error: duplicate options
        // ---

        assert_duplicate(&[Human, Human], "human");
        assert_duplicate(&[Short, Short], "short");
        assert_duplicate(&[Json, Json], "json");
        assert_duplicate(
            &[JsonDiagnosticShort, JsonDiagnosticShort],
            "json-diagnostic-short",
        );
        assert_duplicate(
            &[JsonDiagnosticRenderedAnsi, JsonDiagnosticRenderedAnsi],
            "json-diagnostic-rendered-ansi",
        );
        assert_duplicate(
            &[JsonRenderDiagnostics, JsonRenderDiagnostics],
            "json-render-diagnostics",
        );

        // Duplicates with other options present.
        assert_duplicate(&[Json, JsonDiagnosticShort, Json], "json");
        assert_duplicate(
            &[
                JsonDiagnosticShort,
                JsonDiagnosticRenderedAnsi,
                JsonDiagnosticShort,
            ],
            "json-diagnostic-short",
        );
    }

    #[test]
    fn test_cargo_message_format_to_cargo_arg() {
        // Test the domain model's to_cargo_arg method.
        assert_eq!(
            CargoMessageFormat::Human.cargo_arg(),
            "json-render-diagnostics"
        );

        assert_eq!(
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: false,
                ansi: false
            }
            .cargo_arg(),
            "json"
        );
        assert_eq!(
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: true,
                ansi: false
            }
            .cargo_arg(),
            "json-diagnostic-short"
        );
        assert_eq!(
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: false,
                ansi: true
            }
            .cargo_arg(),
            "json-diagnostic-rendered-ansi"
        );
        assert_eq!(
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: true,
                ansi: true
            }
            .cargo_arg(),
            "json-diagnostic-short,json-diagnostic-rendered-ansi"
        );
        assert_eq!(
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: false,
                ansi: false
            }
            .cargo_arg(),
            "json-render-diagnostics"
        );
        assert_eq!(
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: true,
                ansi: false
            }
            .cargo_arg(),
            "json-render-diagnostics,json-diagnostic-short"
        );
        assert_eq!(
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: false,
                ansi: true
            }
            .cargo_arg(),
            "json-render-diagnostics,json-diagnostic-rendered-ansi"
        );
        assert_eq!(
            CargoMessageFormat::Json {
                render_diagnostics: true,
                short: true,
                ansi: true
            }
            .cargo_arg(),
            "json-render-diagnostics,json-diagnostic-short,json-diagnostic-rendered-ansi"
        );
    }

    #[test]
    fn test_cargo_message_format_predicates() {
        // Test forward_json.
        assert!(!CargoMessageFormat::Human.forward_json());
        assert!(
            CargoMessageFormat::Json {
                render_diagnostics: false,
                short: false,
                ansi: false
            }
            .forward_json()
        );
    }
}