nextest-runner 0.114.0

Core runner logic for cargo nextest.
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
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Consolidated display configuration: resolves all display decisions (progress
//! bar, counter, status levels) in a single place.

use super::{ShowProgress, status_level::StatusLevels};
use crate::reporter::displayer::{FinalStatusLevel, StatusLevel};

/// Unresolved display configuration passed to the displayer.
///
/// The displayer resolves this into a [`ResolvedDisplay`] based on the
/// progress display mode, terminal interactivity, and CI detection.
pub(crate) struct DisplayConfig {
    /// The progress display mode.
    pub(crate) show_progress: ShowProgress,

    /// Whether test output capture is disabled (`--no-capture`).
    pub(crate) no_capture: bool,

    /// Explicit override (from CLI `--status-level`), or `None` to use
    /// context-dependent defaults.
    pub(crate) status_level: Option<StatusLevel>,

    /// Explicit override (from CLI `--final-status-level`).
    pub(crate) final_status_level: Option<FinalStatusLevel>,

    /// Status level from the nextest profile, used as the default when no
    /// explicit override is set and no context-specific default applies.
    pub(crate) profile_status_level: StatusLevel,

    /// Final status level from the nextest profile, used as the default when
    /// no explicit override is set and no context-specific default applies.
    pub(crate) profile_final_status_level: FinalStatusLevel,
}

/// The fully resolved display decisions.
///
/// Produced by [`DisplayConfig::resolve()`].
pub(crate) struct ResolvedDisplay {
    /// What kind of progress indicator to show.
    pub(crate) progress_display: ProgressDisplay,

    /// The resolved status levels.
    pub(crate) status_levels: StatusLevels,
}

/// The kind of progress indicator to display.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ProgressDisplay {
    /// Show a progress bar with running tests.
    Bar,

    /// Show a counter (e.g. "(1/10)") on each status line.
    Counter,

    /// No progress indicator.
    None,
}

impl DisplayConfig {
    /// Creates a config with explicit status level overrides and no profile
    /// defaults.
    ///
    /// Used by replay, where status levels are always explicitly set.
    pub(crate) fn with_overrides(
        show_progress: ShowProgress,
        no_capture: bool,
        status_level: StatusLevel,
        final_status_level: FinalStatusLevel,
    ) -> Self {
        DisplayConfig {
            show_progress,
            no_capture,
            status_level: Some(status_level),
            final_status_level: Some(final_status_level),
            // These are unused because the overrides above are always Some,
            // but we set them to the same values for consistency.
            profile_status_level: status_level,
            profile_final_status_level: final_status_level,
        }
    }

    /// Resolves this configuration into concrete display decisions.
    ///
    /// Resolution depends on `is_terminal` and `is_ci`, both of which are
    /// passed in (rather than queried) for testability.
    ///
    /// The behavioral table:
    ///
    /// | `show_progress`                      | `bar_available` | bar | counter | status defaults |
    /// |--------------------------------------|-----------------|-----|---------|-----------------|
    /// | `Auto { suppress_success: false }`   | true            | yes | no      | profile         |
    /// | `Auto { suppress_success: true }`    | true            | yes | no      | Slow / None     |
    /// | `Auto { suppress_success: true }`    | false           | no  | yes     | profile         |
    /// | `Auto { suppress_success: false }`   | false           | no  | yes     | profile         |
    /// | `Running`                            | true            | yes | no      | profile         |
    /// | `Running`                            | false           | no  | no      | profile         |
    /// | `Counter`                            | *               | no  | yes     | profile         |
    /// | `None`                               | *               | no  | no      | profile         |
    ///
    /// In no-capture mode, the status level is raised to at least
    /// [`StatusLevel::Pass`].
    pub(crate) fn resolve(&self, is_terminal: bool, is_ci: bool) -> ResolvedDisplay {
        // The progress bar requires all three conditions:
        // - Capture enabled: with --no-capture, stderr is passed directly to
        //   child processes, making a progress bar incompatible. (A future
        //   pty-based approach could lift this, but would require curses-like
        //   handling for partial lines.)
        // - Not CI: some CI environments pretend to be terminals but don't
        //   render progress bars correctly.
        // - Terminal output: indicatif requires a real terminal target.
        let bar_available = !self.no_capture && !is_ci && is_terminal;
        let (sl, fsl) = (self.profile_status_level, self.profile_final_status_level);

        let mut resolved = match (self.show_progress, bar_available) {
            // Auto + interactive: show bar, profile defaults.
            (
                ShowProgress::Auto {
                    suppress_success: false,
                },
                true,
            ) => ResolvedDisplay {
                progress_display: ProgressDisplay::Bar,
                status_levels: self.apply_overrides(sl, fsl),
            },
            // Auto + suppress_success + interactive: show bar, hide successful output.
            (
                ShowProgress::Auto {
                    suppress_success: true,
                },
                true,
            ) => ResolvedDisplay {
                progress_display: ProgressDisplay::Bar,
                status_levels: self.apply_overrides(StatusLevel::Slow, FinalStatusLevel::None),
            },
            // Auto + suppress_success + non-interactive: suppress_success
            // is irrelevant without a progress bar; use profile defaults.
            (
                ShowProgress::Auto {
                    suppress_success: true,
                },
                false,
            ) => {
                tracing::debug!(
                    is_terminal,
                    is_ci,
                    no_capture = self.no_capture,
                    "suppress_success requested but progress bar is unavailable; \
                     using profile defaults",
                );
                ResolvedDisplay {
                    progress_display: ProgressDisplay::Counter,
                    status_levels: self.apply_overrides(sl, fsl),
                }
            }
            // Auto + non-interactive: show counter, profile defaults.
            (
                ShowProgress::Auto {
                    suppress_success: false,
                },
                false,
            ) => ResolvedDisplay {
                progress_display: ProgressDisplay::Counter,
                status_levels: self.apply_overrides(sl, fsl),
            },
            // Running + interactive: show bar.
            (ShowProgress::Running, true) => ResolvedDisplay {
                progress_display: ProgressDisplay::Bar,
                status_levels: self.apply_overrides(sl, fsl),
            },
            // Running + non-interactive: no visible progress.
            (ShowProgress::Running, false) => ResolvedDisplay {
                progress_display: ProgressDisplay::None,
                status_levels: self.apply_overrides(sl, fsl),
            },
            // Counter: always counter, never bar.
            (ShowProgress::Counter, _) => ResolvedDisplay {
                progress_display: ProgressDisplay::Counter,
                status_levels: self.apply_overrides(sl, fsl),
            },
            // None: no progress display of any kind.
            (ShowProgress::None, _) => ResolvedDisplay {
                progress_display: ProgressDisplay::None,
                status_levels: self.apply_overrides(sl, fsl),
            },
        };

        // In no-capture mode, raise status level to at least Pass.
        if self.no_capture {
            resolved.status_levels.status_level =
                resolved.status_levels.status_level.max(StatusLevel::Pass);
        }

        resolved
    }

    fn apply_overrides(
        &self,
        default_sl: StatusLevel,
        default_fsl: FinalStatusLevel,
    ) -> StatusLevels {
        StatusLevels {
            status_level: self.status_level.unwrap_or(default_sl),
            final_status_level: self.final_status_level.unwrap_or(default_fsl),
        }
    }
}

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

    /// The profile defaults used in resolve tests.
    const DEFAULT_SL: StatusLevel = StatusLevel::Pass;
    const DEFAULT_FSL: FinalStatusLevel = FinalStatusLevel::Flaky;

    fn make_config(
        show_progress: ShowProgress,
        no_capture: bool,
        status_level: Option<StatusLevel>,
        final_status_level: Option<FinalStatusLevel>,
    ) -> DisplayConfig {
        DisplayConfig {
            show_progress,
            no_capture,
            status_level,
            final_status_level,
            profile_status_level: DEFAULT_SL,
            profile_final_status_level: DEFAULT_FSL,
        }
    }

    // -- Non-suppress modes use profile defaults --

    /// For all ShowProgress variants *except* `Auto { suppress_success: true }` with
    /// a progress bar, the resolved status levels should use profile defaults
    /// (when no CLI overrides are set).
    #[test]
    fn resolve_uses_profile_defaults() {
        let non_suppress_modes = [
            ShowProgress::Auto {
                suppress_success: false,
            },
            ShowProgress::None,
            ShowProgress::Counter,
            ShowProgress::Running,
        ];

        for show_progress in non_suppress_modes {
            // Interactive terminal, not CI.
            let config = make_config(show_progress, false, None, None);
            let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
            assert_eq!(
                resolved.status_levels.status_level, DEFAULT_SL,
                "show_progress={show_progress:?}, is_terminal=true, is_ci=false"
            );
            assert_eq!(
                resolved.status_levels.final_status_level, DEFAULT_FSL,
                "show_progress={show_progress:?}, is_terminal=true, is_ci=false"
            );

            // Non-interactive (piped).
            let config = make_config(show_progress, false, None, None);
            let resolved = config.resolve(/* is_terminal */ false, /* is_ci */ false);
            assert_eq!(
                resolved.status_levels.status_level, DEFAULT_SL,
                "show_progress={show_progress:?}, is_terminal=false, is_ci=false"
            );
            assert_eq!(
                resolved.status_levels.final_status_level, DEFAULT_FSL,
                "show_progress={show_progress:?}, is_terminal=false, is_ci=false"
            );

            // CI environment.
            let config = make_config(show_progress, false, None, None);
            let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ true);
            assert_eq!(
                resolved.status_levels.status_level, DEFAULT_SL,
                "show_progress={show_progress:?}, is_terminal=true, is_ci=true"
            );
            assert_eq!(
                resolved.status_levels.final_status_level, DEFAULT_FSL,
                "show_progress={show_progress:?}, is_terminal=true, is_ci=true"
            );
        }

        // Auto { suppress_success: true } without a progress bar also uses profile
        // defaults (non-interactive).
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: true,
            },
            false,
            None,
            None,
        );
        let resolved = config.resolve(/* is_terminal */ false, /* is_ci */ false);
        assert_eq!(resolved.status_levels.status_level, DEFAULT_SL);
        assert_eq!(resolved.status_levels.final_status_level, DEFAULT_FSL);
        assert_eq!(resolved.progress_display, ProgressDisplay::Counter);

        // Auto { suppress_success: true } in CI also uses profile defaults.
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: true,
            },
            false,
            None,
            None,
        );
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ true);
        assert_eq!(resolved.status_levels.status_level, DEFAULT_SL);
        assert_eq!(resolved.status_levels.final_status_level, DEFAULT_FSL);
        assert_eq!(resolved.progress_display, ProgressDisplay::Counter);
    }

    /// `Auto { suppress_success: true }` with a progress bar hides successful output.
    #[test]
    fn resolve_suppress_success_with_progress_bar() {
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: true,
            },
            false,
            None,
            None,
        );
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
        assert_eq!(
            resolved.status_levels.status_level,
            StatusLevel::Slow,
            "suppress_success + progress bar should default to Slow"
        );
        assert_eq!(
            resolved.status_levels.final_status_level,
            FinalStatusLevel::None,
            "suppress_success + progress bar should default to None"
        );
        assert_eq!(resolved.progress_display, ProgressDisplay::Bar);
    }

    // -- CLI overrides --

    /// Explicit CLI overrides always win, even in suppress_success with a progress
    /// bar.
    #[test]
    fn resolve_cli_overrides_win() {
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: true,
            },
            false,
            Some(StatusLevel::Skip),
            Some(FinalStatusLevel::Pass),
        );

        // suppress_success + progress bar: CLI override should still win.
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
        assert_eq!(
            resolved.status_levels.status_level,
            StatusLevel::Skip,
            "CLI override wins over suppress_success defaults"
        );
        assert_eq!(
            resolved.status_levels.final_status_level,
            FinalStatusLevel::Pass,
            "CLI override wins over suppress_success defaults"
        );

        // Normal auto mode: CLI override should also win.
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: false,
            },
            false,
            Some(StatusLevel::Skip),
            Some(FinalStatusLevel::Pass),
        );
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
        assert_eq!(resolved.status_levels.status_level, StatusLevel::Skip);
        assert_eq!(
            resolved.status_levels.final_status_level,
            FinalStatusLevel::Pass
        );

        // Non-auto mode: CLI override should also win.
        let config = make_config(
            ShowProgress::Running,
            false,
            Some(StatusLevel::Skip),
            Some(FinalStatusLevel::Pass),
        );
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
        assert_eq!(resolved.status_levels.status_level, StatusLevel::Skip);
        assert_eq!(
            resolved.status_levels.final_status_level,
            FinalStatusLevel::Pass
        );
    }

    /// Partial CLI overrides: only the overridden level uses the explicit
    /// value, the other falls back to the contextual default.
    #[test]
    fn resolve_partial_cli_overrides() {
        // Override only status_level.
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: true,
            },
            false,
            Some(StatusLevel::All),
            None,
        );
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
        assert_eq!(
            resolved.status_levels.status_level,
            StatusLevel::All,
            "CLI override for status_level"
        );
        assert_eq!(
            resolved.status_levels.final_status_level,
            FinalStatusLevel::None,
            "suppress_success default for final_status_level"
        );

        // Override only final_status_level.
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: true,
            },
            false,
            None,
            Some(FinalStatusLevel::All),
        );
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
        assert_eq!(
            resolved.status_levels.status_level,
            StatusLevel::Slow,
            "suppress_success default for status_level"
        );
        assert_eq!(
            resolved.status_levels.final_status_level,
            FinalStatusLevel::All,
            "CLI override for final_status_level"
        );
    }

    // -- no_capture mode --

    /// No-capture mode raises status_level to at least Pass.
    #[test]
    fn resolve_no_capture_raises_status_level() {
        // Without CLI override, profile default Pass is unchanged.
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: false,
            },
            true,
            None,
            None,
        );
        let resolved = config.resolve(/* is_terminal */ false, /* is_ci */ false);
        assert_eq!(
            resolved.status_levels.status_level,
            StatusLevel::Pass,
            "no_capture raises to at least Pass (default was Pass)"
        );

        // With suppress_success + bar available: Slow is raised to Pass.
        // Note: no_capture prevents the bar from being available, so
        // bar_available=false and suppress_success is irrelevant. The profile default
        // (Pass) is used.
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: true,
            },
            true,
            None,
            None,
        );
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
        assert_eq!(
            resolved.status_levels.status_level,
            StatusLevel::Pass,
            "no_capture prevents bar, so profile default Pass is used"
        );
        // With no_capture, bar is not available so suppress_success falls through to
        // the Auto non-interactive branch which uses profile defaults.
        assert_eq!(
            resolved.status_levels.final_status_level, DEFAULT_FSL,
            "no_capture prevents bar, so profile final default is used"
        );
        assert_eq!(
            resolved.progress_display,
            ProgressDisplay::Counter,
            "no_capture prevents bar"
        );

        // With CLI override below Pass, no_capture raises it.
        let config = make_config(ShowProgress::Running, true, Some(StatusLevel::Fail), None);
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
        assert_eq!(
            resolved.status_levels.status_level,
            StatusLevel::Pass,
            "no_capture raises CLI override Fail to Pass"
        );

        // With CLI override above Pass, no_capture preserves it.
        let config = make_config(ShowProgress::Running, true, Some(StatusLevel::All), None);
        let resolved = config.resolve(/* is_terminal */ true, /* is_ci */ false);
        assert_eq!(
            resolved.status_levels.status_level,
            StatusLevel::All,
            "no_capture preserves CLI override All"
        );
    }

    // -- Progress bar and counter decisions --

    /// Auto mode: bar in interactive terminal, counter otherwise.
    #[test]
    fn resolve_auto_progress_decisions() {
        // Interactive terminal.
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: false,
            },
            false,
            None,
            None,
        );
        let resolved = config.resolve(true, false);
        assert_eq!(resolved.progress_display, ProgressDisplay::Bar);

        // Non-interactive.
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: false,
            },
            false,
            None,
            None,
        );
        let resolved = config.resolve(false, false);
        assert_eq!(resolved.progress_display, ProgressDisplay::Counter);

        // CI pretending to be a terminal.
        let config = make_config(
            ShowProgress::Auto {
                suppress_success: false,
            },
            false,
            None,
            None,
        );
        let resolved = config.resolve(true, true);
        assert_eq!(resolved.progress_display, ProgressDisplay::Counter);
    }

    /// Running mode: bar in interactive terminal, nothing otherwise.
    #[test]
    fn resolve_running_progress_decisions() {
        // Interactive terminal.
        let config = make_config(ShowProgress::Running, false, None, None);
        let resolved = config.resolve(true, false);
        assert_eq!(resolved.progress_display, ProgressDisplay::Bar);

        // Non-interactive.
        let config = make_config(ShowProgress::Running, false, None, None);
        let resolved = config.resolve(false, false);
        assert_eq!(resolved.progress_display, ProgressDisplay::None);

        // CI pretending to be a terminal: no bar, no counter.
        let config = make_config(ShowProgress::Running, false, None, None);
        let resolved = config.resolve(true, true);
        assert_eq!(resolved.progress_display, ProgressDisplay::None);
    }

    /// Counter mode: always counter, never bar.
    #[test]
    fn resolve_counter_progress_decisions() {
        let config = make_config(ShowProgress::Counter, false, None, None);
        let resolved = config.resolve(true, false);
        assert_eq!(resolved.progress_display, ProgressDisplay::Counter);

        let config = make_config(ShowProgress::Counter, false, None, None);
        let resolved = config.resolve(false, false);
        assert_eq!(resolved.progress_display, ProgressDisplay::Counter);
    }

    /// None mode: no progress display.
    #[test]
    fn resolve_none_progress_decisions() {
        let config = make_config(ShowProgress::None, false, None, None);
        let resolved = config.resolve(true, false);
        assert_eq!(resolved.progress_display, ProgressDisplay::None);

        let config = make_config(ShowProgress::None, false, None, None);
        let resolved = config.resolve(false, false);
        assert_eq!(resolved.progress_display, ProgressDisplay::None);
    }
}