Debian Codegraph @

lib/App/Yath/Options/Runner.pm

raw
1 package App::Yath::Options::Runner;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use List::Util qw/min/;
8 use Test2::Util qw/IS_WIN32/;
9 use App::Yath::Util qw/find_in_updir/;
10 use Test2::Harness::Util qw/clean_path mod2file/;
11 use Test2::Harness::Util::UUID qw/gen_uuid/;
12 use File::Spec;
13
14 use App::Yath::Options;
15
16 my $DEFAULT_COVER_ARGS = '-silent,1,+ignore,^t/,+ignore,^t2/,+ignore,^xt,+ignore,^test.pl';
17
18 option_group {prefix => 'runner', category => "Runner Options"} => sub {
19 option use_fork => (
20 alt => ['fork'],
21 description => "(default: on, except on windows) Normally tests are run by forking, which allows for features like preloading. This will turn off the behavior globally (which is not compatible with preloading). This is slower, it is better to tag misbehaving tests with the '# HARNESS-NO-PRELOAD' comment in their header to disable forking only for those tests.",
22 env_vars => [qw/!T2_NO_FORK T2_HARNESS_FORK !T2_HARNESS_NO_FORK YATH_FORK !YATH_NO_FORK/],
23 default => sub {
24 return 0 if IS_WIN32;
25 return 1;
26 },
27 );
28
29 option abort_on_bail => (
30 type => 'b',
31 default => 1,
32 description => "Abort all testing if a bail-out is encountered (default: on)",
33 );
34
35 option use_timeout => (
36 alt => ['timeout'],
37 description => "(default: on) Enable/disable timeouts",
38 default => 1,
39 );
40
41 option shared_jobs_config => (
42 type => 's',
43 description => 'Where to look for a shared slot config file. If a filename with no path is provided yath will search the current and all parent directories for the name.',
44 default => '.sharedjobslots.yml',
45 long_examples => [ ' .sharedjobslots.yml', ' relative/path/.sharedjobslots.yml', ' /absolute/path/.sharedjobslots.yml' ],
46 );
47
48 post \&jobs_post_process;
49 option job_count => (
50 type => 's',
51 short => 'j',
52 alt => ['jobs'],
53 description => 'Set the number of concurrent jobs to run. Add a :# if you also wish to designate multiple slots per test. 8:2 means 8 slots, but each test gets 2 slots, so 4 tests run concurrently. Tests can find their concurrency assignment in the "T2_HARNESS_MY_JOB_CONCURRENCY" environment variable.',
54 env_vars => [qw/YATH_JOB_COUNT T2_HARNESS_JOB_COUNT HARNESS_JOB_COUNT/],
55 clear_env_vars => 1,
56 long_examples => [' 4', ' 8:2'],
57 short_examples => ['4', '8:2'],
58
59 action => sub {
60 my ($prefix, $field, $raw, $norm, $slot, $settings, $handler) = @_;
61
62 my ($jobs, $slots) = split /:/, $norm;
63
64 $$slot = $jobs;
65
66 $settings->runner->slots_per_job($slots) if defined $slots;
67
68 fix_job_resources($settings);
69 },
70 );
71
72 option slots_per_job => (
73 type => 's',
74 short => 'x',
75 description => "This sets the number of slots each job will use (default 1). This is normally set by the ':#' in '-j#:#'.",
76 env_vars => ['T2_HARNESS_JOB_CONCURRENCY'],
77 clear_env_vars => 1,
78 long_examples => [' 2'],
79 short_examples => ['2'],
80 );
81
82 option dump_depmap => (
83 type => 'b',
84 description => "When using staged preload, dump the depmap for each stage as json files",
85 default => 0,
86 );
87
88 option includes => (
89 name => 'include',
90 short => 'I',
91 type => 'm',
92 description => "Add a directory to your include paths",
93 );
94
95 option resources => (
96 name => 'resource',
97 short => 'R',
98 type => 'm',
99 description => "Use a resource module to assign resource assignments to individual tests",
100 long_examples => [' Port', ' +Test2::Harness::Runner::Resource::Port'],
101 short_examples => [' Port'],
102
103 normalize => sub {
104 my $val = shift;
105
106 $val = "Test2::Harness::Runner::Resource::$val"
107 unless $val =~ s/^\+//;
108
109 return $val;
110 },
111 );
112
113 option tlib => (
114 description => "(Default: off) Include 't/lib' in your module path",
115 default => 0,
116 action => sub {
117 my ($prefix, $field, $raw, $norm, $slot, $settings, $handler) = @_;
118 push @{$settings->runner->includes} => File::Spec->catdir('t', 'lib');
119 },
120 );
121
122 option lib => (
123 short => 'l',
124 description => "(Default: include if it exists) Include 'lib' in your module path",
125 default => 1,
126 action => sub {
127 my ($prefix, $field, $raw, $norm, $slot, $settings, $handler) = @_;
128 push @{$settings->runner->includes} => 'lib';
129 $settings->runner->lib(0);
130 $settings->runner->blib(0);
131 },
132 );
133
134 option blib => (
135 short => 'b',
136 description => "(Default: include if it exists) Include 'blib/lib' and 'blib/arch' in your module path",
137 default => 1,
138 action => sub {
139 my ($prefix, $field, $raw, $norm, $slot, $settings, $handler) = @_;
140
141 push @{$settings->runner->includes} => (
142 File::Spec->catdir('blib', 'lib'),
143 File::Spec->catdir('blib', 'arch'),
144 );
145
146 $settings->runner->lib(0);
147 $settings->runner->blib(0);
148 },
149 );
150
151 option unsafe_inc => (
152 description => "perl is removing '.' from \@INC as a security concern. This option keeps things from breaking for now.",
153 env_vars => [qw/PERL_USE_UNSAFE_INC/],
154 default => 0,
155 );
156
157 option preloads => (
158 type => 'm',
159 alt => ['preload'],
160 short => 'P',
161 description => 'Preload a module before running tests',
162 );
163
164 option preload_threshold => (
165 short => 'W',
166 alt => ['Pt'],
167 type => 's',
168 default => 0,
169 description => "Only do preload if at least N tests are going to be run. In some cases a full preload takes longer than simply running the tests, this lets you specify a minimum number of test jobs that will be run for preload to happen. This has no effect for a persistent runner. The default is 0, and it means always preload."
170 );
171
172 option nytprof => (
173 type => 'b',
174 description => "Use Devel::NYTProf on tests. This will set addpid=1 for you. This works with or without fork.",
175 long_examples => [''],
176 );
177
178 post \&cover_post_process;
179 option cover => (
180 type => 'd',
181 description => "Use Devel::Cover to calculate test coverage. This disables forking. If no args are specified the following are used: $DEFAULT_COVER_ARGS",
182 long_examples => ['', '=-silent,1,+ignore,^t/,+ignore,^t2/,+ignore,^xt,+ignore,^test.pl'],
183 action => sub {
184 my ($prefix, $field, $raw, $norm, $slot, $settings) = @_;
185
186 return $$slot = $DEFAULT_COVER_ARGS if $norm eq '1';
187 return $$slot = $norm;
188 },
189 );
190
191 option switch => (
192 field => 'switches',
193 short => 'S',
194 type => 'm',
195 description => 'Pass the specified switch to perl for each test. This is not compatible with preload.',
196 );
197
198 option fail_on_resource_skip => (
199 type => 'b',
200 default => 0,
201 description => 'Treat resource-skipped tests as failures instead of skips. When enabled, tests that would be skipped due to unavailable resources will be marked as failing.',
202 long_examples => [''],
203 );
204
205 option resource_timeout => (
206 alt => ['rt'],
207
208 type => 's',
209 default => 0,
210
211 long_examples => [' SECONDS'],
212 short_examples => [' SECONDS'],
213 description => 'Abort the test run if no tests have been able to start for SECONDS seconds while there are pending tests and none running. This is useful when a resource class is broken and always claims a resource will become available, preventing yath from ever finishing. (Default: 0, meaning no timeout)',
214 );
215
216 option stall_report => (
217 type => 'd',
218 default => 0,
219 autofill => '600:1200',
220
221 long_examples => ['', '=STRONG:LOOSE', '=SECONDS'],
222 description => 'Print diagnostics when the scheduler appears to have stopped starting tests. Give it alone for sensible waits, or set them: STRONG is the wait, in seconds, before reporting when tests are pending and none are running; LOOSE is the wait when tests are pending and others are still running. A single number sets both, and a zero in either field turns off that tier alone, so 600:0 reports only when nothing is running. This only prints a report, it never ends the run, and a report may be benign. Enabling it sends SIGUSR1 to yath\'s own processes to collect stack traces, pauses output for a few seconds per report, stops after 5 reports, and writes a yath-stall-report-*.json bundle both into the run log and to a file (see --stall-report-dir). (Default: off; --stall-report alone means 600:1200)',
223 );
224
225 option stall_report_dir => (
226 type => 's',
227
228 long_examples => [' path/to/dir'],
229 description => 'Directory for the JSON bundle --stall-report writes. Defaults to the directory yath was run from. Use this when that directory is not writable, or to collect bundles somewhere durable.',
230 );
231
232 option event_timeout => (
233 alt => ['et'],
234
235 type => 's',
236 default => 60,
237
238 long_examples => [' SECONDS'],
239 short_examples => [' SECONDS'],
240 description => 'Kill test if no output is received within timeout period. (Default: 60 seconds). Add the "# HARNESS-NO-TIMEOUT" comment to the top of a test file to disable timeouts on a per-test basis. This prevents a hung test from running forever.',
241 );
242
243 option post_exit_timeout => (
244 alt => ['pet'],
245
246 type => 's',
247 default => 15,
248
249 long_examples => [' SECONDS'],
250 short_examples => [' SECONDS'],
251 description => 'Stop waiting post-exit after the timeout period. (Default: 15 seconds) Some tests fork and allow the parent to exit before writing all their output. If Test2::Harness detects an incomplete plan after the test exits it will monitor for more events until the timeout period. Add the "# HARNESS-NO-TIMEOUT" comment to the top of a test file to disable timeouts on a per-test basis.'
252 );
253
254 option runner_id => (
255 type => 's',
256 default => sub { gen_uuid() },
257 description => 'Runner ID (usually a generated uuid)',
258 );
259 };
260
261 sub jobs_post_process {
262 my %params = @_;
263 my $settings = $params{settings};
264
265 my $runner = $settings->runner or return;
266
267 fix_job_resources($settings);
268
269 $ENV{T2_HARNESS_MY_JOB_COUNT} = $runner->job_count;
270 $ENV{T2_HARNESS_MY_MAX_JOB_CONCURRENCY} = $runner->slots_per_job;
271 }
272
273 sub fix_job_resources {
274 my ($settings) = @_;
275
276 my $runner = $settings->runner;
277
278 require Test2::Harness::Runner::Resource::SharedJobSlots::Config;
279 my $sconf = Test2::Harness::Runner::Resource::SharedJobSlots::Config->find(settings => $settings);
280
281 my %found;
282 for my $r (@{$runner->resources}) {
283 require(mod2file($r));
284 next unless $r->job_limiter;
285 $found{$r}++;
286 }
287
288 if ($sconf && !$found{'Test2::Harness::Runner::Resource::SharedJobSlots'} && !$sconf->disabled) {
289 if (delete $found{'Test2::Harness::Runner::Resource::JobCount'}) {
290 @{$settings->runner->resources} = grep { $_ ne 'Test2::Harness::Runner::Resource::JobCount' } @{$runner->resources};
291 }
292
293 if (!keys %found) {
294 require Test2::Harness::Runner::Resource::SharedJobSlots;
295 unshift @{$runner->resources} => 'Test2::Harness::Runner::Resource::SharedJobSlots';
296 $found{'Test2::Harness::Runner::Resource::SharedJobSlots'}++;
297 }
298 }
299 elsif (!keys %found) {
300 require Test2::Harness::Runner::Resource::JobCount;
301 unshift @{$runner->resources} => 'Test2::Harness::Runner::Resource::JobCount';
302 }
303
304 if ($found{'Test2::Harness::Runner::Resource::SharedJobSlots'} && $sconf) {
305 $runner->field(job_count => $sconf->default_slots_per_run || $sconf->max_slots_per_run) if $runner && !$runner->job_count;
306 $runner->field(slots_per_job => $sconf->default_slots_per_job || $sconf->max_slots_per_job) if $runner && !$runner->slots_per_job;
307
308 my $run_slots = $runner->job_count;
309 my $job_slots = $runner->slots_per_job;
310
311 die "Requested job count ($run_slots) exceeds the system shared limit (" . $sconf->max_slots_per_run . ").\n"
312 if $run_slots > $sconf->max_slots_per_run;
313
314 die "Requested job concurrency ($job_slots) exceeds the system shared limit (" . $sconf->max_slots_per_job . ").\n"
315 if $job_slots > $sconf->max_slots_per_job;
316 }
317
318 $runner->field(job_count => 1) if $runner && !$runner->job_count;
319 $runner->field(slots_per_job => 1) if $runner && !$runner->slots_per_job;
320
321 my $run_slots = $runner->job_count;
322 my $job_slots = $runner->slots_per_job;
323
324 die "The slots_per_job (set to $job_slots) must not be larger than the job_count (set to $run_slots).\n" if $job_slots > $run_slots;
325 }
326
327 sub cover_post_process {
328 my %params = @_;
329 my $settings = $params{settings};
330
331 if ($ENV{T2_DEVEL_COVER} && !$settings->runner->cover) {
332 $settings->runner->field(cover => $ENV{T2_DEVEL_COVER} eq '1' ? $ENV{T2_DEVEL_COVER} : $DEFAULT_COVER_ARGS);
333 }
334
335 return unless $settings->runner->cover;
336
337 # For nested things
338 $ENV{T2_NO_FORK} = 1;
339 $ENV{T2_DEVEL_COVER} = $settings->runner->cover;
340 $settings->runner->field(use_fork => 0);
341
342 return unless $settings->check_prefix('run');
343 push @{$settings->run->load_import->{'@'}} => 'Devel::Cover';
344 $settings->run->load_import->{'Devel::Cover'} = [split(/,/, $settings->runner->cover)];
345 }
346
347 1;
348
349 __END__
350
351
352 =pod
353
354 =encoding UTF-8
355
356 =head1 NAME
357
358 App::Yath::Options::Runner - Runner options for Yath.
359
360 =head1 DESCRIPTION
361
362 This is where command line options for the runner are defined.
363
364 =head1 PROVIDED OPTIONS
365
366 =head2 COMMAND OPTIONS
367
368 =head3 Runner Options
369
370 =over 4
371
372 =item --abort-on-bail
373
374 =item --no-abort-on-bail
375
376 Abort all testing if a bail-out is encountered (default: on)
377
378
379 =item --blib
380
381 =item -b
382
383 =item --no-blib
384
385 (Default: include if it exists) Include 'blib/lib' and 'blib/arch' in your module path
386
387
388 =item --cover
389
390 =item --cover=-silent,1,+ignore,^t/,+ignore,^t2/,+ignore,^xt,+ignore,^test.pl
391
392 =item --no-cover
393
394 Use Devel::Cover to calculate test coverage. This disables forking. If no args are specified the following are used: -silent,1,+ignore,^t/,+ignore,^t2/,+ignore,^xt,+ignore,^test.pl
395
396
397 =item --dump-depmap
398
399 =item --no-dump-depmap
400
401 When using staged preload, dump the depmap for each stage as json files
402
403
404 =item --event-timeout SECONDS
405
406 =item --et SECONDS
407
408 =item --no-event-timeout
409
410 Kill test if no output is received within timeout period. (Default: 60 seconds). Add the "# HARNESS-NO-TIMEOUT" comment to the top of a test file to disable timeouts on a per-test basis. This prevents a hung test from running forever.
411
412
413 =item --fail-on-resource-skip
414
415 =item --no-fail-on-resource-skip
416
417 Treat resource-skipped tests as failures instead of skips. When enabled, tests that would be skipped due to unavailable resources will be marked as failing.
418
419
420 =item --include ARG
421
422 =item --include=ARG
423
424 =item -I ARG
425
426 =item -I=ARG
427
428 =item --no-include
429
430 Add a directory to your include paths
431
432 Can be specified multiple times
433
434
435 =item --job-count 4
436
437 =item --job-count 8:2
438
439 =item --jobs 4
440
441 =item --jobs 8:2
442
443 =item -j4
444
445 =item -j8:2
446
447 =item --no-job-count
448
449 Set the number of concurrent jobs to run. Add a :# if you also wish to designate multiple slots per test. 8:2 means 8 slots, but each test gets 2 slots, so 4 tests run concurrently. Tests can find their concurrency assignment in the "T2_HARNESS_MY_JOB_CONCURRENCY" environment variable.
450
451 Can also be set with the following environment variables: C<YATH_JOB_COUNT>, C<T2_HARNESS_JOB_COUNT>, C<HARNESS_JOB_COUNT>
452
453
454 =item --lib
455
456 =item -l
457
458 =item --no-lib
459
460 (Default: include if it exists) Include 'lib' in your module path
461
462
463 =item --nytprof
464
465 =item --no-nytprof
466
467 Use Devel::NYTProf on tests. This will set addpid=1 for you. This works with or without fork.
468
469
470 =item --post-exit-timeout SECONDS
471
472 =item --pet SECONDS
473
474 =item --no-post-exit-timeout
475
476 Stop waiting post-exit after the timeout period. (Default: 15 seconds) Some tests fork and allow the parent to exit before writing all their output. If Test2::Harness detects an incomplete plan after the test exits it will monitor for more events until the timeout period. Add the "# HARNESS-NO-TIMEOUT" comment to the top of a test file to disable timeouts on a per-test basis.
477
478
479 =item --preload-threshold ARG
480
481 =item --preload-threshold=ARG
482
483 =item --Pt ARG
484
485 =item --Pt=ARG
486
487 =item -W ARG
488
489 =item -W=ARG
490
491 =item --no-preload-threshold
492
493 Only do preload if at least N tests are going to be run. In some cases a full preload takes longer than simply running the tests, this lets you specify a minimum number of test jobs that will be run for preload to happen. This has no effect for a persistent runner. The default is 0, and it means always preload.
494
495
496 =item --preloads ARG
497
498 =item --preloads=ARG
499
500 =item --preload ARG
501
502 =item --preload=ARG
503
504 =item -P ARG
505
506 =item -P=ARG
507
508 =item --no-preloads
509
510 Preload a module before running tests
511
512 Can be specified multiple times
513
514
515 =item --resource-timeout SECONDS
516
517 =item --rt SECONDS
518
519 =item --no-resource-timeout
520
521 Abort the test run if no tests have been able to start for SECONDS seconds while there are pending tests and none running. This is useful when a resource class is broken and always claims a resource will become available, preventing yath from ever finishing. (Default: 0, meaning no timeout)
522
523
524 =item --resource Port
525
526 =item --resource +Test2::Harness::Runner::Resource::Port
527
528 =item -R Port
529
530 =item --no-resource
531
532 Use a resource module to assign resource assignments to individual tests
533
534 Can be specified multiple times
535
536
537 =item --runner-id ARG
538
539 =item --runner-id=ARG
540
541 =item --no-runner-id
542
543 Runner ID (usually a generated uuid)
544
545
546 =item --shared-jobs-config .sharedjobslots.yml
547
548 =item --shared-jobs-config relative/path/.sharedjobslots.yml
549
550 =item --shared-jobs-config /absolute/path/.sharedjobslots.yml
551
552 =item --no-shared-jobs-config
553
554 Where to look for a shared slot config file. If a filename with no path is provided yath will search the current and all parent directories for the name.
555
556
557 =item --slots-per-job 2
558
559 =item -x2
560
561 =item --no-slots-per-job
562
563 This sets the number of slots each job will use (default 1). This is normally set by the ':#' in '-j#:#'.
564
565 Can also be set with the following environment variables: C<T2_HARNESS_JOB_CONCURRENCY>
566
567
568 =item --stall-report
569
570 =item --stall-report=STRONG:LOOSE
571
572 =item --stall-report=SECONDS
573
574 =item --no-stall-report
575
576 Print diagnostics when the scheduler appears to have stopped starting tests. Give it alone for sensible waits, or set them: STRONG is the wait, in seconds, before reporting when tests are pending and none are running; LOOSE is the wait when tests are pending and others are still running. A single number sets both, and a zero in either field turns off that tier alone, so 600:0 reports only when nothing is running. This only prints a report, it never ends the run, and a report may be benign. Enabling it sends SIGUSR1 to yath's own processes to collect stack traces, pauses output for a few seconds per report, stops after 5 reports, and writes a yath-stall-report-*.json bundle both into the run log and to a file (see --stall-report-dir). (Default: off; --stall-report alone means 600:1200)
577
578
579 =item --stall-report-dir path/to/dir
580
581 =item --no-stall-report-dir
582
583 Directory for the JSON bundle --stall-report writes. Defaults to the directory yath was run from. Use this when that directory is not writable, or to collect bundles somewhere durable.
584
585
586 =item --switch ARG
587
588 =item --switch=ARG
589
590 =item -S ARG
591
592 =item -S=ARG
593
594 =item --no-switch
595
596 Pass the specified switch to perl for each test. This is not compatible with preload.
597
598 Can be specified multiple times
599
600
601 =item --tlib
602
603 =item --no-tlib
604
605 (Default: off) Include 't/lib' in your module path
606
607
608 =item --unsafe-inc
609
610 =item --no-unsafe-inc
611
612 perl is removing '.' from @INC as a security concern. This option keeps things from breaking for now.
613
614 Can also be set with the following environment variables: C<PERL_USE_UNSAFE_INC>
615
616
617 =item --use-fork
618
619 =item --fork
620
621 =item --no-use-fork
622
623 (default: on, except on windows) Normally tests are run by forking, which allows for features like preloading. This will turn off the behavior globally (which is not compatible with preloading). This is slower, it is better to tag misbehaving tests with the '# HARNESS-NO-PRELOAD' comment in their header to disable forking only for those tests.
624
625 Can also be set with the following environment variables: C<!T2_NO_FORK>, C<T2_HARNESS_FORK>, C<!T2_HARNESS_NO_FORK>, C<YATH_FORK>, C<!YATH_NO_FORK>
626
627
628 =item --use-timeout
629
630 =item --timeout
631
632 =item --no-use-timeout
633
634 (default: on) Enable/disable timeouts
635
636
637 =back
638
639 =head1 SOURCE
640
641 The source code repository for Test2-Harness can be found at
642 F<http://github.com/Test-More/Test2-Harness/>.
643
644 =head1 MAINTAINERS
645
646 =over 4
647
648 =item Chad Granum E<lt>[email protected]<gt>
649
650 =back
651
652 =head1 AUTHORS
653
654 =over 4
655
656 =item Chad Granum E<lt>[email protected]<gt>
657
658 =back
659
660 =head1 COPYRIGHT
661
662 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
663
664 This program is free software; you can redistribute it and/or
665 modify it under the same terms as Perl itself.
666
667 See F<http://dev.perl.org/licenses/>
668
669 =cut