Debian Codegraph @

lib/App/Yath/Options/Finder.pm

raw
1 package App::Yath::Options::Finder;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Test2::Harness::Util qw/mod2file/;
8
9 use App::Yath::Options;
10
11 my %RERUN_MODES = (
12 all => "Re-Run all tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.",
13 failed => "Re-Run failed tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.",
14 retried => "Re-Run retried tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.",
15 passed => "Re-Run passed tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.",
16 missed => "Run missed tests from a previously aborted/stopped run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.",
17 );
18
19 option_group {prefix => 'finder', category => "Finder Options", builds => 'Test2::Harness::Finder'} => sub {
20 option finder => (
21 type => 's',
22 default => 'Test2::Harness::Finder',
23 description => 'Specify what Finder subclass to use when searching for files/processing the file list. Use the "+" prefix to specify a fully qualified namespace, otherwise Test2::Harness::Finder::XXX namespace is assumed.',
24 long_examples => [' MyFinder', ' +Test2::Harness::Finder::MyFinder'],
25 pre_command => 1,
26 adds_options => 1,
27 pre_process => \&finder_pre_process,
28 action => \&finder_action,
29
30 builds => undef, # This option is not for the build
31 );
32
33 option extension => (
34 field => 'extensions',
35 type => 'm',
36 alt => ['ext'],
37 description => 'Specify valid test filename extensions, default: t and t2',
38 );
39
40 option search => (
41 type => 'm',
42
43 description => 'List of tests and test directories to use instead of the default search paths. Typically these can simply be listed as command line arguments without the --search prefix.',
44 );
45
46 option no_long => (
47 description => "Do not run tests that have their duration flag set to 'LONG'",
48 );
49
50 option only_long => (
51 description => "Only run tests that have their duration flag set to 'LONG'",
52 );
53
54 option show_changed_files => (
55 description => "Print a list of changed files if any are found",
56 applicable => \&changes_applicable,
57 );
58
59 option changed_only => (
60 description => "Only search for tests for changed files (Requires a coverage data source, also requires a list of changes either from the --changed option, or a plugin that implements changed_files() or changed_diff())",
61 applicable => \&changes_applicable,
62 );
63
64 option rerun => (
65 type => 'd',
66 description => "Re-Run tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.",
67 long_examples => ['', '=path/to/log.jsonl', '=plugin_specific_string'],
68 );
69
70 option rerun_plugin => (
71 type => 'm',
72 description => "What plugin(s) should be used for rerun (will fallback to other plugins if the listed ones decline the value, this is just used to set an order of priority)",
73 long_examples => [' Foo', ' +App::Yath::Plugin::Foo'],
74 );
75
76 option rerun_modes => (
77 alt => ['rerun-mode'],
78 type => 'm',
79 description => "Pick which test categories to run",
80 long_examples => [' failed,missed,...', map {" $_"} sort keys %RERUN_MODES],
81 );
82
83 for my $mode (keys %RERUN_MODES) {
84 option "rerun_$mode" => (
85 type => 'd',
86 description => $RERUN_MODES{$mode},
87 long_examples => ['', '=path/to/log.jsonl', '=plugin_specific_string'],
88 ignore_for_build => 1,
89 );
90 }
91
92 option changed => (
93 type => 'm',
94 description => "Specify one or more files as having been changed.",
95 long_examples => [' path/to/file'],
96 applicable => \&changes_applicable,
97 );
98
99 option changes_exclude_file => (
100 type => 'm',
101 description => 'Specify one or more files to ignore when looking at changes',
102 long_examples => [' path/to/file'],
103 applicable => \&changes_applicable,
104 );
105
106 option changes_exclude_pattern => (
107 type => 'm',
108 description => 'Ignore files matching this pattern when looking for changes. Your pattern will be inserted unmodified into a `$file =~ m/$pattern/` check.',
109 long_examples => [" '(apple|pear|orange)'"],
110 applicable => \&changes_applicable,
111 );
112
113 option changes_filter_file => (
114 type => 'm',
115 description => 'Specify one or more files to check for changes. Changes to other files will be ignored',
116 long_examples => [' path/to/file'],
117 applicable => \&changes_applicable,
118 );
119
120 option changes_filter_pattern => (
121 type => 'm',
122 description => 'Specify a pattern for change checking. When only running tests for changed files this will limit which files are checked for changes. Only files that match this pattern will be checked. Your pattern will be inserted unmodified into a `$file =~ m/$pattern/` check.',
123 long_examples => [" '(apple|pear|orange)'"],
124 applicable => \&changes_applicable,
125 );
126
127 option changes_diff => (
128 type => 's',
129 description => "Path to a diff file that should be used to find changed files for use with --changed-only. This must be in the same format as `git diff -W --minimal -U1000000`",
130 long_examples => [' path/to/diff.diff'],
131 applicable => \&changes_applicable,
132 );
133
134 option changes_plugin => (
135 type => 's',
136 description => "What plugin should be used to detect changed files.",
137 long_examples => [' Git', ' +App::Yath::Plugin::Git'],
138 applicable => \&changes_applicable,
139 );
140
141 option changes_include_whitespace => (
142 type => 'b',
143 description => "Include changed lines that are whitespace only (default: off)",
144 applicable => \&changes_applicable,
145 default => 0,
146 );
147
148 option changes_exclude_nonsub => (
149 type => 'b',
150 description => "Exclude changes outside of subroutines (perl files only) (default: off)",
151 applicable => \&changes_applicable,
152 default => 0,
153 );
154
155 option changes_exclude_loads => (
156 type => 'b',
157 description => "Exclude coverage tests which only load changed files, but never call code from them. (default: off)",
158 applicable => \&changes_applicable,
159 default => 0,
160 );
161
162 option changes_exclude_opens => (
163 type => 'b',
164 description => "Exclude coverage tests which only open() changed files, but never call code from them. (default: off)",
165 applicable => \&changes_applicable,
166 default => 0,
167 );
168
169 option durations => (
170 type => 's',
171
172 long_examples => [' file.json', ' http://example.com/durations.json'],
173 short_examples => [' file.json', ' http://example.com/durations.json'],
174
175 description => "Point at a json file or url which has a hash of relative test filenames as keys, and 'SHORT', 'MEDIUM', or 'LONG' as values. This will override durations listed in the file headers. An exception will be thrown if the durations file or url does not work.",
176 );
177
178 option maybe_durations => (
179 type => 's',
180
181 long_examples => [' file.json', ' http://example.com/durations.json'],
182 short_examples => [' file.json', ' http://example.com/durations.json'],
183
184 description => "Point at a json file or url which has a hash of relative test filenames as keys, and 'SHORT', 'MEDIUM', or 'LONG' as values. This will override durations listed in the file headers. An exception will be thrown if the durations file or url does not work.",
185 );
186
187 option durations_threshold => (
188 alt => ['Dt'],
189 type => 's',
190 default => undef,
191 description => "Only fetch duration data if running at least this number of tests. Default (-j value + 1)"
192 );
193
194 option exclude_file => (
195 field => 'exclude_files',
196 type => 'm',
197
198 long_examples => [' t/nope.t'],
199 short_examples => [' t/nope.t'],
200
201 description => "Exclude a file from testing",
202 );
203
204 option exclude_pattern => (
205 field => 'exclude_patterns',
206 type => 'm',
207
208 long_examples => [' t/nope.t'],
209 short_examples => [' t/nope.t'],
210
211 description => "Exclude a pattern from testing, matched using m/\$PATTERN/",
212 );
213
214 option exclude_list => (
215 field => 'exclude_lists',
216 type => 'm',
217
218 long_examples => [' file.txt', ' http://example.com/exclusions.txt'],
219 short_examples => [' file.txt', ' http://example.com/exclusions.txt'],
220
221 description => "Point at a file or url which has a new line separated list of test file names to exclude from testing. Starting a line with a '#' will comment it out (for compatibility with Test2::Aggregate list files).",
222 );
223
224 option default_search => (
225 type => 'm',
226
227 description => "Specify the default file/dir search. defaults to './t', './t2', and 'test.pl'. The default search is only used if no files were specified at the command line",
228 );
229
230 option default_at_search => (
231 type => 'm',
232
233 description => "Specify the default file/dir search when 'AUTHOR_TESTING' is set. Defaults to './xt'. The default AT search is only used if no files were specified at the command line",
234 );
235
236 post \&_post_process;
237 };
238
239 sub _post_process {
240 my %params = @_;
241 my $settings = $params{settings};
242 my $options = $params{options};
243
244 my $finder = $settings->finder;
245
246 my $rerun = $finder->rerun;
247
248 for my $mode (sort keys %RERUN_MODES) {
249 my $val = $finder->remove_field("rerun_$mode") or next;
250
251 push @{$finder->rerun_modes} => $mode;
252
253 next if $val eq '1';
254
255 $rerun //= $val;
256 $rerun = $val if $rerun eq '1';
257
258 die "Multiple runs specified for rerun ($val and $rerun). Please pick one.\n" if $val ne $rerun;
259 }
260
261 $finder->field(rerun => $rerun);
262
263 my (%seen, @keep);
264 for my $mode (sort map { split /,/ } @{$finder->rerun_modes}) {
265 next if $seen{$mode}++;
266 die "Invalid rerun-mode '$mode'.\n" unless $RERUN_MODES{$mode};
267 push @keep => $mode;
268 }
269 push @keep => 'all' unless @keep;
270
271 @{$finder->rerun_modes} = @keep;
272
273 if (!defined($settings->finder->durations_threshold)) {
274 if ($settings->check_prefix('runner')) {
275 my $jc = $settings->runner->job_count // 1;
276 $settings->finder->field(durations_threshold => $jc + 1);
277 }
278
279 $settings->finder->field(durations_threshold => 1);
280 }
281
282 $settings->finder->field(default_search => ['./t', './t2', 'test.pl'])
283 unless $settings->finder->default_search && @{$settings->finder->default_search};
284
285 $settings->finder->field(default_at_search => ['./xt'])
286 unless $settings->finder->default_at_search && @{$settings->finder->default_at_search};
287
288 @{$settings->finder->extensions} = ('t', 't2')
289 unless @{$settings->finder->extensions};
290
291 s/^\.//g for @{$settings->finder->extensions};
292 }
293
294 sub normalize_class {
295 my ($class) = @_;
296
297 $class = "Test2::Harness::Finder::$class"
298 unless $class =~ s/^\+//;
299
300 my $file = mod2file($class);
301 require $file;
302
303 return $class;
304 }
305
306 sub finder_pre_process {
307 my %params = @_;
308
309 my $class = $params{val} or return;
310
311 $class = normalize_class($class);
312
313 return unless $class->can('options');
314
315 $params{options}->include_from($class);
316 }
317
318 sub finder_action {
319 my ($prefix, $field, $raw, $norm, $slot, $settings, $handler, $options) = @_;
320
321 my $class = $norm;
322
323 $class = normalize_class($class);
324
325 if ($class->can('options')) {
326 $options->populate_pre_defaults();
327 $options->populate_cmd_defaults();
328 }
329
330 $class->munge_settings($settings, $options) if $class->can('munge_settings');
331
332 $handler->($slot, $class);
333 }
334
335 sub changes_applicable {
336 my ($option, $options) = @_;
337
338 # Cannot use this options with projects
339 return 0 if $options->command_class && $options->command_class->isa('App::Yath::Command::projects');
340 return 1;
341 }
342
343 1;
344
345 __END__
346
347 =pod
348
349 =encoding UTF-8
350
351 =head1 NAME
352
353 App::Yath::Options::Finder - Finder options for Yath.
354
355 =head1 DESCRIPTION
356
357 This is where the command line options for discovering test files are defined.
358
359 =head1 PROVIDED OPTIONS
360
361 =head2 YATH OPTIONS (PRE-COMMAND)
362
363 =head3 Finder Options
364
365 =over 4
366
367 =item --finder MyFinder
368
369 =item --finder +Test2::Harness::Finder::MyFinder
370
371 =item --no-finder
372
373 Specify what Finder subclass to use when searching for files/processing the file list. Use the "+" prefix to specify a fully qualified namespace, otherwise Test2::Harness::Finder::XXX namespace is assumed.
374
375
376 =back
377
378 =head2 COMMAND OPTIONS
379
380 =head3 Finder Options
381
382 =over 4
383
384 =item --changed path/to/file
385
386 =item --no-changed
387
388 Specify one or more files as having been changed.
389
390 Can be specified multiple times
391
392
393 =item --changed-only
394
395 =item --no-changed-only
396
397 Only search for tests for changed files (Requires a coverage data source, also requires a list of changes either from the --changed option, or a plugin that implements changed_files() or changed_diff())
398
399
400 =item --changes-diff path/to/diff.diff
401
402 =item --no-changes-diff
403
404 Path to a diff file that should be used to find changed files for use with --changed-only. This must be in the same format as `git diff -W --minimal -U1000000`
405
406
407 =item --changes-exclude-file path/to/file
408
409 =item --no-changes-exclude-file
410
411 Specify one or more files to ignore when looking at changes
412
413 Can be specified multiple times
414
415
416 =item --changes-exclude-loads
417
418 =item --no-changes-exclude-loads
419
420 Exclude coverage tests which only load changed files, but never call code from them. (default: off)
421
422
423 =item --changes-exclude-nonsub
424
425 =item --no-changes-exclude-nonsub
426
427 Exclude changes outside of subroutines (perl files only) (default: off)
428
429
430 =item --changes-exclude-opens
431
432 =item --no-changes-exclude-opens
433
434 Exclude coverage tests which only open() changed files, but never call code from them. (default: off)
435
436
437 =item --changes-exclude-pattern '(apple|pear|orange)'
438
439 =item --no-changes-exclude-pattern
440
441 Ignore files matching this pattern when looking for changes. Your pattern will be inserted unmodified into a `$file =~ m/$pattern/` check.
442
443 Can be specified multiple times
444
445
446 =item --changes-filter-file path/to/file
447
448 =item --no-changes-filter-file
449
450 Specify one or more files to check for changes. Changes to other files will be ignored
451
452 Can be specified multiple times
453
454
455 =item --changes-filter-pattern '(apple|pear|orange)'
456
457 =item --no-changes-filter-pattern
458
459 Specify a pattern for change checking. When only running tests for changed files this will limit which files are checked for changes. Only files that match this pattern will be checked. Your pattern will be inserted unmodified into a `$file =~ m/$pattern/` check.
460
461 Can be specified multiple times
462
463
464 =item --changes-include-whitespace
465
466 =item --no-changes-include-whitespace
467
468 Include changed lines that are whitespace only (default: off)
469
470
471 =item --changes-plugin Git
472
473 =item --changes-plugin +App::Yath::Plugin::Git
474
475 =item --no-changes-plugin
476
477 What plugin should be used to detect changed files.
478
479
480 =item --default-at-search ARG
481
482 =item --default-at-search=ARG
483
484 =item --no-default-at-search
485
486 Specify the default file/dir search when 'AUTHOR_TESTING' is set. Defaults to './xt'. The default AT search is only used if no files were specified at the command line
487
488 Can be specified multiple times
489
490
491 =item --default-search ARG
492
493 =item --default-search=ARG
494
495 =item --no-default-search
496
497 Specify the default file/dir search. defaults to './t', './t2', and 'test.pl'. The default search is only used if no files were specified at the command line
498
499 Can be specified multiple times
500
501
502 =item --durations file.json
503
504 =item --durations http://example.com/durations.json
505
506 =item --no-durations
507
508 Point at a json file or url which has a hash of relative test filenames as keys, and 'SHORT', 'MEDIUM', or 'LONG' as values. This will override durations listed in the file headers. An exception will be thrown if the durations file or url does not work.
509
510
511 =item --durations-threshold ARG
512
513 =item --durations-threshold=ARG
514
515 =item --Dt ARG
516
517 =item --Dt=ARG
518
519 =item --no-durations-threshold
520
521 Only fetch duration data if running at least this number of tests. Default (-j value + 1)
522
523
524 =item --exclude-file t/nope.t
525
526 =item --no-exclude-file
527
528 Exclude a file from testing
529
530 Can be specified multiple times
531
532
533 =item --exclude-list file.txt
534
535 =item --exclude-list http://example.com/exclusions.txt
536
537 =item --no-exclude-list
538
539 Point at a file or url which has a new line separated list of test file names to exclude from testing. Starting a line with a '#' will comment it out (for compatibility with Test2::Aggregate list files).
540
541 Can be specified multiple times
542
543
544 =item --exclude-pattern t/nope.t
545
546 =item --no-exclude-pattern
547
548 Exclude a pattern from testing, matched using m/$PATTERN/
549
550 Can be specified multiple times
551
552
553 =item --extension ARG
554
555 =item --extension=ARG
556
557 =item --ext ARG
558
559 =item --ext=ARG
560
561 =item --no-extension
562
563 Specify valid test filename extensions, default: t and t2
564
565 Can be specified multiple times
566
567
568 =item --maybe-durations file.json
569
570 =item --maybe-durations http://example.com/durations.json
571
572 =item --no-maybe-durations
573
574 Point at a json file or url which has a hash of relative test filenames as keys, and 'SHORT', 'MEDIUM', or 'LONG' as values. This will override durations listed in the file headers. An exception will be thrown if the durations file or url does not work.
575
576
577 =item --no-long
578
579 =item --no-no-long
580
581 Do not run tests that have their duration flag set to 'LONG'
582
583
584 =item --only-long
585
586 =item --no-only-long
587
588 Only run tests that have their duration flag set to 'LONG'
589
590
591 =item --rerun
592
593 =item --rerun=path/to/log.jsonl
594
595 =item --rerun=plugin_specific_string
596
597 =item --no-rerun
598
599 Re-Run tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.
600
601
602 =item --rerun-all
603
604 =item --rerun-all=path/to/log.jsonl
605
606 =item --rerun-all=plugin_specific_string
607
608 =item --no-rerun-all
609
610 Re-Run all tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.
611
612
613 =item --rerun-failed
614
615 =item --rerun-failed=path/to/log.jsonl
616
617 =item --rerun-failed=plugin_specific_string
618
619 =item --no-rerun-failed
620
621 Re-Run failed tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.
622
623
624 =item --rerun-missed
625
626 =item --rerun-missed=path/to/log.jsonl
627
628 =item --rerun-missed=plugin_specific_string
629
630 =item --no-rerun-missed
631
632 Run missed tests from a previously aborted/stopped run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.
633
634
635 =item --rerun-modes failed,missed,...
636
637 =item --rerun-modes all
638
639 =item --rerun-modes failed
640
641 =item --rerun-modes missed
642
643 =item --rerun-modes passed
644
645 =item --rerun-modes retried
646
647 =item --rerun-mode failed,missed,...
648
649 =item --rerun-mode all
650
651 =item --rerun-mode failed
652
653 =item --rerun-mode missed
654
655 =item --rerun-mode passed
656
657 =item --rerun-mode retried
658
659 =item --no-rerun-modes
660
661 Pick which test categories to run
662
663 Can be specified multiple times
664
665
666 =item --rerun-passed
667
668 =item --rerun-passed=path/to/log.jsonl
669
670 =item --rerun-passed=plugin_specific_string
671
672 =item --no-rerun-passed
673
674 Re-Run passed tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.
675
676
677 =item --rerun-plugin Foo
678
679 =item --rerun-plugin +App::Yath::Plugin::Foo
680
681 =item --no-rerun-plugin
682
683 What plugin(s) should be used for rerun (will fallback to other plugins if the listed ones decline the value, this is just used to set an order of priority)
684
685 Can be specified multiple times
686
687
688 =item --rerun-retried
689
690 =item --rerun-retried=path/to/log.jsonl
691
692 =item --rerun-retried=plugin_specific_string
693
694 =item --no-rerun-retried
695
696 Re-Run retried tests from a previous run from a log file (or last log file). Plugins can intercept this, such as YathUIDB which will grab a run UUID and derive tests to re-run from that.
697
698
699 =item --search ARG
700
701 =item --search=ARG
702
703 =item --no-search
704
705 List of tests and test directories to use instead of the default search paths. Typically these can simply be listed as command line arguments without the --search prefix.
706
707 Can be specified multiple times
708
709
710 =item --show-changed-files
711
712 =item --no-show-changed-files
713
714 Print a list of changed files if any are found
715
716
717 =back
718
719 =head1 SOURCE
720
721 The source code repository for Test2-Harness can be found at
722 F<http://github.com/Test-More/Test2-Harness/>.
723
724 =head1 MAINTAINERS
725
726 =over 4
727
728 =item Chad Granum E<lt>[email protected]<gt>
729
730 =back
731
732 =head1 AUTHORS
733
734 =over 4
735
736 =item Chad Granum E<lt>[email protected]<gt>
737
738 =back
739
740 =head1 COPYRIGHT
741
742 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
743
744 This program is free software; you can redistribute it and/or
745 modify it under the same terms as Perl itself.
746
747 See F<http://dev.perl.org/licenses/>
748
749 =cut