Debian Codegraph @

lib/App/Yath/Command/failed.pm

raw
1 package App::Yath::Command::failed;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Test2::Util::Table qw/table/;
8 use Test2::Harness::Util::File::JSONL;
9
10 use parent 'App::Yath::Command';
11 use Test2::Harness::Util::HashBase qw{<log_file};
12
13 use App::Yath::Options;
14
15 option brief => (
16 prefix => 'display',
17 category => 'Display Options',
18 description => 'Show only the files that failed, newline separated, no other output. If a file failed once but passed on a retry it will NOT be shown.',
19 );
20
21 sub summary { "Show the failed tests from an event log" }
22
23 sub group { 'log' }
24
25 sub cli_args { "[--] event_log.jsonl[.gz|.bz2] [job1, job2, ...]" }
26
27 sub description {
28 return <<" EOT";
29 This yath command will list the test scripts from an event log that have failed.
30 The only required argument is the path to the log file, which may be compressed.
31 Any extra arguments are assumed to be job id's. If you list any jobs,
32 only the listed jobs will be processed.
33
34 This command accepts all the same renderer/formatter options that the 'test'
35 command accepts.
36 EOT
37 }
38
39 sub run {
40 my $self = shift;
41
42 my $settings = $self->settings;
43 my $args = $self->args;
44
45 shift @$args if @$args && $args->[0] eq '--';
46
47 $self->{+LOG_FILE} = shift @$args or die "You must specify a log file";
48 die "'$self->{+LOG_FILE}' is not a valid log file" unless -f $self->{+LOG_FILE};
49 die "'$self->{+LOG_FILE}' does not look like a log file" unless $self->{+LOG_FILE} =~ m/\.jsonl(\.(gz|bz2))?$/;
50
51 my $stream = Test2::Harness::Util::File::JSONL->new(name => $self->{+LOG_FILE});
52
53 my %failed;
54
55 while(1) {
56 my @events = $stream->poll(max => 1000) or last;
57
58 for my $event (@events) {
59 my $stamp = $event->{stamp} or next;
60 my $job_id = $event->{job_id} or next;
61 my $f = $event->{facet_data} or next;
62
63 push @{$failed{$job_id}->{subtests}} => $self->subtests($f)
64 if $f->{parent} && !$f->{trace}->{nested} && $self->include_subtest($f);
65
66 next unless $f->{harness_job_end};
67 next unless $f->{harness_job_end}->{fail} || $failed{$job_id};
68
69 push @{$failed{$job_id}->{ends}} => $f->{harness_job_end};
70 }
71 }
72
73 my $rows = [];
74 while (my ($job_id, $data) = each %failed) {
75 my $ends = $data->{ends} // [];
76
77 my %seen;
78 my $subtests = join "\n" => grep { !$seen{$_}++ } sort @{$data->{subtests} // []};
79
80 if ($settings->display->brief) {
81 print $ends->[-1]->{rel_file}, "\n" if $ends->[-1]->{fail};
82 }
83 else {
84 push @$rows => [$job_id, scalar(@$ends), $ends->[-1]->{rel_file}, $subtests, $ends->[-1]->{fail} ? "NO" : "YES"];
85 }
86 }
87
88 return 0 if $settings->display->brief;
89
90 unless (@$rows) {
91 print "\nNo jobs failed!\n";
92 return 0;
93 }
94
95 print "\nThe following jobs failed at least once:\n";
96 print join "\n" => table(
97 collapse => 1,
98 header => ['Job ID', 'Times Run', 'Test File', "Subtests", "Succeeded Eventually?"],
99 rows => $rows,
100 );
101 print "\n";
102
103 return 0;
104 }
105
106 sub include_subtest {
107 my $self = shift;
108 my ($f) = @_;
109
110 return 0 unless $f->{parent} && keys %{$f->{parent}};
111 return 0 if $f->{assert}->{pass} || !keys %{$f->{assert}};
112 return 0 if $f->{amnesty} && @{$f->{amnesty}};
113 return 1;
114 }
115
116 sub subtests {
117 my $self = shift;
118 my ($f, $prefix) = @_;
119
120 return unless $self->include_subtest($f);
121
122 my $name = $f->{assert}->{details};
123 unless ($name) {
124 my $frame = $f->{trace}->{frame};
125 $name = "Unnamed Subtest";
126 $name .= " ($frame->[1] line $frame->[2])" if $frame->[1] && $frame->[2];
127 }
128
129 $name = "$prefix -> $name" if $prefix;
130
131 my @out;
132 push @out => $name;
133 for my $child (@{$f->{parent}->{children}}) {
134 next unless $child->{parent};
135 push @out => $self->subtests($child, $name);
136 }
137
138 return @out;
139 }
140
141
142 1;
143
144 __END__
145
146 =pod
147
148 =encoding UTF-8
149
150 =head1 NAME
151
152 App::Yath::Command::failed - Show the failed tests from an event log
153
154 =head1 DESCRIPTION
155
156 This yath command will list the test scripts from an event log that have failed.
157 The only required argument is the path to the log file, which may be compressed.
158 Any extra arguments are assumed to be job id's. If you list any jobs,
159 only the listed jobs will be processed.
160
161 This command accepts all the same renderer/formatter options that the 'test'
162 command accepts.
163
164
165 =head1 USAGE
166
167 $ yath [YATH OPTIONS] failed [COMMAND OPTIONS]
168
169 =head2 YATH OPTIONS
170
171 =head3 Developer
172
173 =over 4
174
175 =item --dev-lib
176
177 =item --dev-lib=lib
178
179 =item -D
180
181 =item -D=lib
182
183 =item -Dlib
184
185 =item --no-dev-lib
186
187 Add paths to @INC before loading ANYTHING. This is what you use if you are developing yath or yath plugins to make sure the yath script finds the local code instead of the installed versions of the same code. You can provide an argument (-Dfoo) to provide a custom path, or you can just use -D without and arg to add lib, blib/lib and blib/arch.
188
189 Can be specified multiple times
190
191
192 =back
193
194 =head3 Environment
195
196 =over 4
197
198 =item --persist-dir ARG
199
200 =item --persist-dir=ARG
201
202 =item --no-persist-dir
203
204 Where to find persistence files.
205
206
207 =item --persist-file ARG
208
209 =item --persist-file=ARG
210
211 =item --pfile ARG
212
213 =item --pfile=ARG
214
215 =item --no-persist-file
216
217 Where to find the persistence file. The default is /{system-tempdir}/project-yath-persist.json. If no project is specified then it will fall back to the current directory. If the current directory is not writable it will default to /tmp/yath-persist.json which limits you to one persistent runner on your system.
218
219
220 =item --project ARG
221
222 =item --project=ARG
223
224 =item --project-name ARG
225
226 =item --project-name=ARG
227
228 =item --no-project
229
230 This lets you provide a label for your current project/codebase. This is best used in a .yath.rc file. This is necessary for a persistent runner.
231
232
233 =back
234
235 =head3 Help and Debugging
236
237 =over 4
238
239 =item --show-opts
240
241 =item --no-show-opts
242
243 Exit after showing what yath thinks your options mean
244
245
246 =item --version
247
248 =item -V
249
250 =item --no-version
251
252 Exit after showing a helpful usage message
253
254
255 =back
256
257 =head3 Plugins
258
259 =over 4
260
261 =item --no-scan-plugins
262
263 =item --no-no-scan-plugins
264
265 Normally yath scans for and loads all App::Yath::Plugin::* modules in order to bring in command-line options they may provide. This flag will disable that. This is useful if you have a naughty plugin that is loading other modules when it should not.
266
267
268 =item --plugins PLUGIN
269
270 =item --plugins +App::Yath::Plugin::PLUGIN
271
272 =item --plugins PLUGIN=arg1,arg2,...
273
274 =item --plugin PLUGIN
275
276 =item --plugin +App::Yath::Plugin::PLUGIN
277
278 =item --plugin PLUGIN=arg1,arg2,...
279
280 =item -pPLUGIN
281
282 =item --no-plugins
283
284 Load a yath plugin.
285
286 Can be specified multiple times
287
288
289 =back
290
291 =head2 COMMAND OPTIONS
292
293 =head3 Cover Options
294
295 =over 4
296
297 =item --cover-aggregator ByTest
298
299 =item --cover-aggregator ByRun
300
301 =item --cover-aggregator +Custom::Aggregator
302
303 =item --cover-agg ByTest
304
305 =item --cover-agg ByRun
306
307 =item --cover-agg +Custom::Aggregator
308
309 =item --no-cover-aggregator
310
311 Choose a custom aggregator subclass
312
313
314 =item --cover-class ARG
315
316 =item --cover-class=ARG
317
318 =item --no-cover-class
319
320 Choose a Test2::Plugin::Cover subclass
321
322
323 =item --cover-dirs ARG
324
325 =item --cover-dirs=ARG
326
327 =item --cover-dir ARG
328
329 =item --cover-dir=ARG
330
331 =item --no-cover-dirs
332
333 NO DESCRIPTION - FIX ME
334
335 Can be specified multiple times
336
337
338 =item --cover-exclude-dirs ARG
339
340 =item --cover-exclude-dirs=ARG
341
342 =item --cover-exclude-dir ARG
343
344 =item --cover-exclude-dir=ARG
345
346 =item --no-cover-exclude-dirs
347
348 Directory to leave out of file coverage and coverage metrics, along with everything under it at any depth. May be specified multiple times. Wildcards are expanded when the option is parsed, so a pattern matching nothing excludes nothing. Values are word-split first, so a path containing whitespace must be quoted or backslash-escaped. A resolved path may not contain a comma.
349
350 Can be specified multiple times
351
352
353 =item --cover-exclude-private
354
355 =item --no-cover-exclude-private
356
357
358
359
360 =item --cover-files
361
362 =item --no-cover-files
363
364 Use Test2::Plugin::Cover to collect coverage data for what files are touched by what tests. Unlike Devel::Cover this has very little performance impact (About 4% difference)
365
366
367 =item --cover-from path/to/log.jsonl
368
369 =item --cover-from http://example.com/coverage
370
371 =item --cover-from path/to/coverage.jsonl
372
373 =item --no-cover-from
374
375 This can be a test log, a coverage dump (old style json or new jsonl format), or a url to any of the previous. Tests will not be run if the file/url is invalid.
376
377
378 =item --cover-from-type json
379
380 =item --cover-from-type jsonl
381
382 =item --cover-from-type log
383
384 =item --no-cover-from-type
385
386 File type for coverage source. Usually it can be detected, but when it cannot be you should specify. "json" is old style single-blob coverage data, "jsonl" is the new by-test style, "log" is a logfile from a previous run.
387
388
389 =item --cover-manager My::Coverage::Manager
390
391 =item --no-cover-manager
392
393 Coverage 'from' manager to use when coverage data does not provide one
394
395
396 =item --cover-maybe-from path/to/log.jsonl
397
398 =item --cover-maybe-from http://example.com/coverage
399
400 =item --cover-maybe-from path/to/coverage.jsonl
401
402 =item --no-cover-maybe-from
403
404 This can be a test log, a coverage dump (old style json or new jsonl format), or a url to any of the previous. Tests will continue even if the coverage file/url is invalid.
405
406
407 =item --cover-maybe-from-type json
408
409 =item --cover-maybe-from-type jsonl
410
411 =item --cover-maybe-from-type log
412
413 =item --no-cover-maybe-from-type
414
415 Same as "from_type" but for "maybe_from". Defaults to "from_type" if that is specified, otherwise auto-detect
416
417
418 =item --cover-metrics
419
420 =item --no-cover-metrics
421
422
423
424
425 =item --cover-types ARG
426
427 =item --cover-types=ARG
428
429 =item --cover-type ARG
430
431 =item --cover-type=ARG
432
433 =item --no-cover-types
434
435 NO DESCRIPTION - FIX ME
436
437 Can be specified multiple times
438
439
440 =item --cover-write
441
442 =item --cover-write=coverage.jsonl
443
444 =item --cover-write=coverage.json
445
446 =item --no-cover-write
447
448 Create a json or jsonl file of all coverage data seen during the run (This implies --cover-files).
449
450
451 =back
452
453 =head3 Display Options
454
455 =over 4
456
457 =item --brief
458
459 =item --no-brief
460
461 Show only the files that failed, newline separated, no other output. If a file failed once but passed on a retry it will NOT be shown.
462
463
464 =back
465
466 =head3 Git Options
467
468 =over 4
469
470 =item --git-change-base master
471
472 =item --git-change-base HEAD^
473
474 =item --git-change-base df22abe4
475
476 =item --no-git-change-base
477
478 Find files changed by all commits in the current branch from most recent stopping when a commit is found that is also present in the history of the branch/commit specified as the change base.
479
480
481 =back
482
483 =head3 Help and Debugging
484
485 =over 4
486
487 =item --dummy
488
489 =item -d
490
491 =item --no-dummy
492
493 Dummy run, do not actually execute anything
494
495 Can also be set with the following environment variables: C<T2_HARNESS_DUMMY>
496
497
498 =item --help
499
500 =item -h
501
502 =item --no-help
503
504 exit after showing help information
505
506
507 =item --interactive
508
509 =item -i
510
511 =item --no-interactive
512
513 Use interactive mode, 1 test at a time, stdin forwarded to it
514
515
516 =item --keep-dirs
517
518 =item --keep_dir
519
520 =item -k
521
522 =item --no-keep-dirs
523
524 Do not delete directories when done. This is useful if you want to inspect the directories used for various commands.
525
526
527 =item --procname-prefix ARG
528
529 =item --procname-prefix=ARG
530
531 =item --no-procname-prefix
532
533 Add a prefix to all proc names (as seen by ps).
534
535
536 =back
537
538 =head3 YathUI Options
539
540 =over 4
541
542 =item --yathui-api-key ARG
543
544 =item --yathui-api-key=ARG
545
546 =item --no-yathui-api-key
547
548 Yath-UI API key. This is not necessary if your Yath-UI instance is set to single-user
549
550
551 =item --yathui-grace
552
553 =item --no-yathui-grace
554
555 If yath cannot connect to yath-ui it normally throws an error, use this to make it fail gracefully. You get a warning, but things keep going.
556
557
558 =item --yathui-long-duration 10
559
560 =item --no-yathui-long-duration
561
562 Minimum duration length (seconds) before a test goes from MEDIUM to LONG
563
564
565 =item --yathui-medium-duration 5
566
567 =item --no-yathui-medium-duration
568
569 Minimum duration length (seconds) before a test goes from SHORT to MEDIUM
570
571
572 =item --yathui-mode summary
573
574 =item --yathui-mode qvf
575
576 =item --yathui-mode qvfd
577
578 =item --yathui-mode complete
579
580 =item --no-yathui-mode
581
582 Set the upload mode (default 'qvfd')
583
584
585 =item --yathui-project ARG
586
587 =item --yathui-project=ARG
588
589 =item --no-yathui-project
590
591 The Yath-UI project for your test results
592
593
594 =item --yathui-retry
595
596 =item --no-yathui-retry
597
598 How many times to try an operation before giving up
599
600 Can be specified multiple times
601
602
603 =item --yathui-url http://my-yath-ui.com/...
604
605 =item --uri http://my-yath-ui.com/...
606
607 =item --no-yathui-url
608
609 Yath-UI url
610
611
612 =back
613
614 =head1 SOURCE
615
616 The source code repository for Test2-Harness can be found at
617 F<http://github.com/Test-More/Test2-Harness/>.
618
619 =head1 MAINTAINERS
620
621 =over 4
622
623 =item Chad Granum E<lt>[email protected]<gt>
624
625 =back
626
627 =head1 AUTHORS
628
629 =over 4
630
631 =item Chad Granum E<lt>[email protected]<gt>
632
633 =back
634
635 =head1 COPYRIGHT
636
637 Copyright 2026 Chad Granum E<lt>[email protected]<gt>.
638
639 This program is free software; you can redistribute it and/or
640 modify it under the same terms as Perl itself.
641
642 See F<http://dev.perl.org/licenses/>
643
644 =cut
645