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

//! Display helpers for durations.

use crate::{
    config::overrides::CompiledDefaultFilter,
    helpers::plural,
    list::SkipCounts,
    reporter::{
        events::{CancelReason, FinalRunStats, RunStatsFailureKind, UnitKind},
        helpers::Styles,
    },
    run_mode::NextestRunMode,
    write_str::WriteStr,
};
use owo_colors::OwoColorize;
use std::{fmt, io, time::Duration};

pub(super) struct DisplayBracketedHhMmSs(pub(super) Duration);

impl fmt::Display for DisplayBracketedHhMmSs {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // This matches indicatif's elapsed_precise display.
        let total_secs = self.0.as_secs();
        let secs = total_secs % 60;
        let total_mins = total_secs / 60;
        let mins = total_mins % 60;
        let hours = total_mins / 60;

        // Buffer the output internally to provide padding.
        let out = format!("{hours:02}:{mins:02}:{secs:02}");
        write!(f, "[{out:>9}] ")
    }
}

pub(super) struct DisplayBracketedDuration(pub(super) Duration);

impl fmt::Display for DisplayBracketedDuration {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // * > means right-align.
        // * 8 is the number of characters to pad to.
        // * .3 means print three digits after the decimal point.
        write!(f, "[{:>8.3?}s] ", self.0.as_secs_f64())
    }
}

pub(super) struct DisplayDurationBy(pub(super) Duration);

impl fmt::Display for DisplayDurationBy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // * > means right-align.
        // * 7 is the number of characters to pad to.
        // * .3 means print three digits after the decimal point.
        write!(f, "by {:>7.3?}s ", self.0.as_secs_f64())
    }
}

pub(super) struct DisplaySlowDuration(pub(super) Duration);

impl fmt::Display for DisplaySlowDuration {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Inside the curly braces:
        // * > means right-align.
        // * 7 is the number of characters to pad to.
        // * .3 means print three digits after the decimal point.
        //
        // The > outside the curly braces is printed literally.
        write!(f, "[>{:>7.3?}s] ", self.0.as_secs_f64())
    }
}

pub(super) fn write_skip_counts(
    mode: NextestRunMode,
    skip_counts: &SkipCounts,
    default_filter: &CompiledDefaultFilter,
    styles: &Styles,
    writer: &mut dyn WriteStr,
) -> io::Result<()> {
    // When running in benchmark mode, we don't display skip counts for
    // tests that were skipped but not benchmarks.
    let real_skipped_tests = if mode == NextestRunMode::Benchmark {
        skip_counts
            .skipped_tests
            .saturating_sub(skip_counts.skipped_tests_non_benchmark)
    } else {
        skip_counts.skipped_tests
    };

    if real_skipped_tests > 0 || skip_counts.skipped_binaries > 0 {
        write!(writer, " (")?;
        write_skip_counts_impl(
            mode,
            real_skipped_tests,
            skip_counts.skipped_binaries,
            styles,
            writer,
        )?;

        // Check if all skipped items are accounted for by a single category.
        let all_rerun = real_skipped_tests == skip_counts.skipped_tests_rerun
            && skip_counts.skipped_binaries == 0;
        let all_default_filter = real_skipped_tests == skip_counts.skipped_tests_default_filter
            && skip_counts.skipped_binaries == skip_counts.skipped_binaries_default_filter;

        if all_rerun {
            // All tests skipped because they already passed in a previous run.
            write!(
                writer,
                " {} that already passed",
                "skipped".style(styles.skip)
            )?;
        } else if all_default_filter {
            // All tests and binaries skipped due to default filter.
            write!(
                writer,
                " {} via {}",
                "skipped".style(styles.skip),
                default_filter.display_config(styles.count)
            )?;
        } else {
            write!(writer, " {}", "skipped".style(styles.skip))?;

            // Show "including" clause for rerun and/or default filter.
            let has_rerun = skip_counts.skipped_tests_rerun > 0;
            let has_default_filter = skip_counts.skipped_binaries_default_filter > 0
                || skip_counts.skipped_tests_default_filter > 0;

            if has_rerun || has_default_filter {
                write!(writer, ", including ")?;

                if has_rerun {
                    write!(
                        writer,
                        "{} {} that already passed",
                        skip_counts.skipped_tests_rerun.style(styles.count),
                        plural::tests_str(mode, skip_counts.skipped_tests_rerun),
                    )?;

                    if has_default_filter {
                        write!(writer, ", and ")?;
                    }
                }

                if has_default_filter {
                    write_skip_counts_impl(
                        mode,
                        skip_counts.skipped_tests_default_filter,
                        skip_counts.skipped_binaries_default_filter,
                        styles,
                        writer,
                    )?;
                    write!(
                        writer,
                        " via {}",
                        default_filter.display_config(styles.count)
                    )?;
                }
            }
        }
        write!(writer, ")")?;
    }

    Ok(())
}

fn write_skip_counts_impl(
    mode: NextestRunMode,
    skipped_tests: usize,
    skipped_binaries: usize,
    styles: &Styles,
    writer: &mut dyn WriteStr,
) -> io::Result<()> {
    // X tests and Y binaries skipped, or X tests skipped, or Y binaries skipped.
    if skipped_tests > 0 && skipped_binaries > 0 {
        write!(
            writer,
            "{} {} and {} {}",
            skipped_tests.style(styles.count),
            plural::tests_str(mode, skipped_tests),
            skipped_binaries.style(styles.count),
            plural::binaries_str(skipped_binaries),
        )?;
    } else if skipped_tests > 0 {
        write!(
            writer,
            "{} {}",
            skipped_tests.style(styles.count),
            plural::tests_str(mode, skipped_tests),
        )?;
    } else if skipped_binaries > 0 {
        write!(
            writer,
            "{} {}",
            skipped_binaries.style(styles.count),
            plural::binaries_str(skipped_binaries),
        )?;
    }

    Ok(())
}

pub(super) fn write_final_warnings(
    mode: NextestRunMode,
    final_stats: FinalRunStats,
    styles: &Styles,
    writer: &mut dyn WriteStr,
) -> io::Result<()> {
    match final_stats {
        FinalRunStats::Failed {
            kind:
                RunStatsFailureKind::Test {
                    initial_run_count,
                    not_run,
                },
        } if not_run > 0 => {
            write_final_warnings_for_failure(
                mode,
                initial_run_count,
                not_run,
                Some(CancelReason::TestFailure),
                styles,
                writer,
            )?;
        }
        FinalRunStats::Cancelled {
            reason,
            kind:
                RunStatsFailureKind::Test {
                    initial_run_count,
                    not_run,
                },
        } if not_run > 0 => {
            write_final_warnings_for_failure(
                mode,
                initial_run_count,
                not_run,
                reason,
                styles,
                writer,
            )?;
        }
        _ => {}
    }

    Ok(())
}

fn write_final_warnings_for_failure(
    mode: NextestRunMode,
    initial_run_count: usize,
    not_run: usize,
    reason: Option<CancelReason>,
    styles: &Styles,
    writer: &mut dyn WriteStr,
) -> io::Result<()> {
    match reason {
        Some(reason @ CancelReason::TestFailure | reason @ CancelReason::TestFailureImmediate) => {
            writeln!(
                writer,
                "{}: {}/{} {} {} not run due to {} (run with {} to run all tests, or run with {})",
                "warning".style(styles.skip),
                not_run.style(styles.count),
                initial_run_count.style(styles.count),
                plural::tests_plural_if(mode, initial_run_count != 1 || not_run != 1),
                plural::were_plural_if(initial_run_count != 1 || not_run != 1),
                reason.to_static_str().style(styles.skip),
                "--no-fail-fast".style(styles.count),
                "--max-fail".style(styles.count),
            )?;
        }
        _ => {
            let due_to_reason = match reason {
                Some(reason) => {
                    format!(" due to {}", reason.to_static_str().style(styles.skip))
                }
                None => String::new(),
            };
            writeln!(
                writer,
                "{}: {}/{} {} {} not run{}",
                "warning".style(styles.skip),
                not_run.style(styles.count),
                initial_run_count.style(styles.count),
                plural::tests_plural_if(mode, initial_run_count != 1 || not_run != 1),
                plural::were_plural_if(initial_run_count != 1 || not_run != 1),
                due_to_reason,
            )?;
        }
    }

    Ok(())
}

pub(crate) struct DisplayUnitKind {
    mode: NextestRunMode,
    kind: UnitKind,
}

impl DisplayUnitKind {
    pub(crate) fn new(mode: NextestRunMode, kind: UnitKind) -> Self {
        Self { mode, kind }
    }
}

impl fmt::Display for DisplayUnitKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind {
            UnitKind::Test => {
                if self.mode == NextestRunMode::Benchmark {
                    write!(f, "benchmark")
                } else {
                    write!(f, "test")
                }
            }
            UnitKind::Script => {
                write!(f, "script")
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::overrides::CompiledDefaultFilterSection;
    use nextest_filtering::CompiledExpr;

    #[test]
    fn test_write_skip_counts() {
        // All tests skipped via default filter.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 1,
            skipped_tests_default_filter: 1,
            skipped_binaries: 0,
            skipped_binaries_default_filter: 0,
        }, false), @" (1 test skipped via profile.my-profile.default-filter)");

        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 2,
            skipped_tests_default_filter: 2,
            skipped_binaries: 0,
            skipped_binaries_default_filter: 0,
        }, false), @" (2 tests skipped via profile.my-profile.default-filter)");

        // Tests skipped for other reasons (not default filter or rerun).
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 1,
            skipped_tests_default_filter: 0,
            skipped_binaries: 0,
            skipped_binaries_default_filter: 0,
        }, false), @" (1 test skipped)");

        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 2,
            skipped_tests_default_filter: 0,
            skipped_binaries: 0,
            skipped_binaries_default_filter: 0,
        }, false), @" (2 tests skipped)");

        // Binaries skipped via default filter.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 0,
            skipped_tests_default_filter: 0,
            skipped_binaries: 1,
            skipped_binaries_default_filter: 1,
        }, false), @" (1 binary skipped via profile.my-profile.default-filter)");

        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 0,
            skipped_tests_default_filter: 0,
            skipped_binaries: 2,
            skipped_binaries_default_filter: 2,
        }, true), @" (2 binaries skipped via default-filter in profile.my-profile.overrides)");

        // Binaries skipped for other reasons.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 0,
            skipped_tests_default_filter: 0,
            skipped_binaries: 1,
            skipped_binaries_default_filter: 0,
        }, false), @" (1 binary skipped)");

        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 0,
            skipped_tests_default_filter: 0,
            skipped_binaries: 2,
            skipped_binaries_default_filter: 0,
        }, false), @" (2 binaries skipped)");

        // Tests and binaries skipped via default filter.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 1,
            skipped_tests_default_filter: 1,
            skipped_binaries: 1,
            skipped_binaries_default_filter: 1,
        }, true), @" (1 test and 1 binary skipped via default-filter in profile.my-profile.overrides)");

        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 2,
            skipped_tests_default_filter: 2,
            skipped_binaries: 3,
            skipped_binaries_default_filter: 3,
        }, false), @" (2 tests and 3 binaries skipped via profile.my-profile.default-filter)");

        // Tests and binaries skipped for other reasons.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 1,
            skipped_tests_default_filter: 0,
            skipped_binaries: 1,
            skipped_binaries_default_filter: 0,
        }, false), @" (1 test and 1 binary skipped)");

        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 2,
            skipped_tests_default_filter: 0,
            skipped_binaries: 3,
            skipped_binaries_default_filter: 0,
        }, false), @" (2 tests and 3 binaries skipped)");

        // Mixed: tests skipped for other reasons, binaries skipped via default filter.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 1,
            skipped_tests_default_filter: 0,
            skipped_binaries: 1,
            skipped_binaries_default_filter: 1,
        }, true), @" (1 test and 1 binary skipped, including 1 binary via default-filter in profile.my-profile.overrides)");

        // Mixed: some tests via default filter, others not.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 3,
            skipped_tests_default_filter: 2,
            skipped_binaries: 1,
            skipped_binaries_default_filter: 0,
        }, false), @" (3 tests and 1 binary skipped, including 2 tests via profile.my-profile.default-filter)");

        // No tests or binaries skipped.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 0,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 0,
            skipped_tests_default_filter: 0,
            skipped_binaries: 0,
            skipped_binaries_default_filter: 0,
        }, false), @"");

        // --- Rerun tests ---

        // All tests skipped due to rerun (already passed).
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 1,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 1,
            skipped_tests_default_filter: 0,
            skipped_binaries: 0,
            skipped_binaries_default_filter: 0,
        }, false), @" (1 test skipped that already passed)");

        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 5,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 5,
            skipped_tests_default_filter: 0,
            skipped_binaries: 0,
            skipped_binaries_default_filter: 0,
        }, false), @" (5 tests skipped that already passed)");

        // Some tests skipped due to rerun, some for other reasons.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 3,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 5,
            skipped_tests_default_filter: 0,
            skipped_binaries: 0,
            skipped_binaries_default_filter: 0,
        }, false), @" (5 tests skipped, including 3 tests that already passed)");

        // Tests skipped due to rerun with binaries skipped.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 2,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 2,
            skipped_tests_default_filter: 0,
            skipped_binaries: 1,
            skipped_binaries_default_filter: 0,
        }, false), @" (2 tests and 1 binary skipped, including 2 tests that already passed)");

        // Tests skipped due to rerun with binaries skipped via default filter.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 2,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 2,
            skipped_tests_default_filter: 0,
            skipped_binaries: 1,
            skipped_binaries_default_filter: 1,
        }, false), @" (2 tests and 1 binary skipped, including 2 tests that already passed, and 1 binary via profile.my-profile.default-filter)");

        // Mixed: some tests rerun, some tests via default filter.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 2,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 5,
            skipped_tests_default_filter: 3,
            skipped_binaries: 0,
            skipped_binaries_default_filter: 0,
        }, false), @" (5 tests skipped, including 2 tests that already passed, and 3 tests via profile.my-profile.default-filter)");

        // Mixed: rerun, default filter, and binaries.
        insta::assert_snapshot!(skip_counts_str(&SkipCounts {
            skipped_tests_rerun: 2,
            skipped_tests_non_benchmark: 0,
            skipped_tests: 6,
            skipped_tests_default_filter: 3,
            skipped_binaries: 2,
            skipped_binaries_default_filter: 1,
        }, true), @" (6 tests and 2 binaries skipped, including 2 tests that already passed, and 3 tests and 1 binary via default-filter in profile.my-profile.overrides)");
    }

    fn skip_counts_str(skip_counts: &SkipCounts, override_section: bool) -> String {
        skip_counts_str_impl(NextestRunMode::Test, skip_counts, override_section)
    }

    fn skip_counts_str_impl(
        mode: NextestRunMode,
        skip_counts: &SkipCounts,
        override_section: bool,
    ) -> String {
        let mut buf = String::new();
        write_skip_counts(
            mode,
            skip_counts,
            &CompiledDefaultFilter {
                expr: CompiledExpr::ALL,
                profile: "my-profile".to_owned(),
                section: if override_section {
                    CompiledDefaultFilterSection::Override(0)
                } else {
                    CompiledDefaultFilterSection::Profile
                },
            },
            &Styles::default(),
            &mut buf,
        )
        .unwrap();
        buf
    }

    #[test]
    fn test_final_warnings() {
        let warnings = final_warnings_for(FinalRunStats::Failed {
            kind: RunStatsFailureKind::Test {
                initial_run_count: 3,
                not_run: 1,
            },
        });
        assert_eq!(
            warnings,
            "warning: 1/3 tests were not run due to test failure \
             (run with --no-fail-fast to run all tests, or run with --max-fail)\n"
        );

        let warnings = final_warnings_for(FinalRunStats::Cancelled {
            reason: Some(CancelReason::Signal),
            kind: RunStatsFailureKind::Test {
                initial_run_count: 8,
                not_run: 5,
            },
        });
        assert_eq!(warnings, "warning: 5/8 tests were not run due to signal\n");

        let warnings = final_warnings_for(FinalRunStats::Cancelled {
            reason: Some(CancelReason::Interrupt),
            kind: RunStatsFailureKind::Test {
                initial_run_count: 1,
                not_run: 1,
            },
        });
        assert_eq!(warnings, "warning: 1/1 test was not run due to interrupt\n");

        // This warning is taken care of by cargo-nextest.
        let warnings = final_warnings_for(FinalRunStats::NoTestsRun);
        assert_eq!(warnings, "");

        // No warnings for success.
        let warnings = final_warnings_for(FinalRunStats::Success);
        assert_eq!(warnings, "");

        // No warnings for setup script failure.
        let warnings = final_warnings_for(FinalRunStats::Failed {
            kind: RunStatsFailureKind::SetupScript,
        });
        assert_eq!(warnings, "");

        // No warnings for setup script cancellation.
        let warnings = final_warnings_for(FinalRunStats::Cancelled {
            reason: Some(CancelReason::Interrupt),
            kind: RunStatsFailureKind::SetupScript,
        });
        assert_eq!(warnings, "");
    }

    fn final_warnings_for(stats: FinalRunStats) -> String {
        let mut buf = String::new();
        let styles = Styles::default();
        write_final_warnings(NextestRunMode::Test, stats, &styles, &mut buf).unwrap();
        buf
    }
}