Debian Codegraph @

lib/App/Yath/Command/spawn.pm

raw
1 package App::Yath::Command::spawn;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use App::Yath::Options;
8
9 use Time::HiRes qw/sleep time/;
10 use File::Temp qw/tempfile/;
11
12 use Test2::Harness::Util qw/parse_exit/;
13
14 use parent 'App::Yath::Command::run';
15 use Test2::Harness::Util::HashBase;
16
17 sub group { 'persist' }
18
19 sub summary { "Launch a perl script from the preloaded environment" }
20 sub cli_args { "[--] path/to/script.pl [options and args]" }
21
22 sub description {
23 return <<" EOT";
24 This will launch the specified script from the preloaded yath process.
25
26 NOTE: environment variables are not automatically passed to the spawned
27 process. You must use -e or -E (see help) to specify what environment variables
28 you care about.
29 EOT
30 }
31
32 option_group {prefix => 'spawn', category => 'spawn options'} => sub {
33 option stage => (
34 short => 's',
35 type => 's',
36 description => 'Specify the stage to be used for launching the script',
37 long_examples => [ ' foo'],
38 short_examples => [ ' foo'],
39 default => 'default',
40 );
41
42 option copy_env => (
43 short => 'e',
44 type => 'm',
45 description => "Specify environment variables to pass along with their current values, can also use a regex",
46 long_examples => [ ' HOME', ' SHELL', ' /PERL_.*/i' ],
47 short_examples => [ ' HOME', ' SHELL', ' /PERL_.*/i' ],
48 );
49
50 option env_var => (
51 field => 'env_vars',
52 short => 'E',
53 type => 'h',
54 long_examples => [' VAR=VAL'],
55 short_examples => ['VAR=VAL', ' VAR=VAL'],
56 description => 'Set environment variables for the spawn',
57 );
58 };
59
60 sub read_line {
61 my ($fh, $timeout) = @_;
62
63 $timeout //= 300;
64
65 my $start = time;
66 while (1) {
67 if ($timeout < (time - $start)) {
68 my @caller = caller;
69 die "Timed out at $caller[1] line $caller[2].\n";
70 }
71 seek($fh, 0,1) if eof($fh);
72 my $out = <$fh>;
73 unless (defined $out) {
74 sleep 0.02;
75 next;
76 }
77 chomp($out);
78 return $out;
79 }
80 }
81
82 # This is here for subclasses
83 sub queue_spawn {
84 my $self = shift;
85 my ($args) = @_;
86
87 $self->state->queue_spawn($args);
88 }
89
90 sub run_script { shift @ARGV // die "No script specified" }
91
92 sub stage { $_[0]->settings->spawn->stage }
93
94 sub env_vars {
95 my $self = shift;
96
97 my $settings = $self->settings;
98
99 my $env = {};
100
101 for my $var (@{$settings->spawn->copy_env}) {
102 if ($var =~ m{^/(.*)/(\w*)$}s) {
103 my ($re, $opts) = ($1, $2);
104 my $pattern = length($opts) ? "(?$opts)$re" : $re;
105 $env->{$_} = $ENV{$_} for grep { m/$pattern/ } keys %ENV;
106 }
107 else {
108 $env->{$var} = $ENV{$var};
109 }
110 }
111
112 my $set = $settings->spawn->env_vars;
113 $env->{$_} = $set->{$_} for keys %$set;
114
115 return $env;
116 }
117
118 sub set_pname {
119 my $self = shift;
120 my ($run) = @_;
121
122 $0 = "yath-" . $self->name . " $run " . join (' ', @ARGV);
123 }
124
125 sub pre_process_argv {
126 shift @ARGV if @ARGV && $ARGV[0] eq '--';
127 }
128
129 sub sig_handlers { qw/INT TERM HUP QUIT USR1 USR2 STOP WINCH/ }
130
131 sub set_sig_handlers {
132 my $self = shift;
133 my ($wpid) = @_;
134
135 local $@;
136 eval { my $s = $_; $SIG{$s} = sub { kill($s, $wpid) } } for $self->sig_handlers;
137 }
138
139 sub clear_sig_handlers {
140 my $self = shift;
141
142 local $@;
143 eval { my $s = $_; $SIG{$s} = 'DEFAULT' } for $self->sig_handlers;
144 }
145
146 sub pre_exit_hook {}
147
148 sub run {
149 my $self = shift;
150
151 $self->pre_process_argv;
152
153 my $run = $self->run_script;
154 $self->set_pname($run);
155
156 my ($fh, $name) = tempfile(UNLINK => 1);
157 close($fh);
158
159 $self->queue_spawn({
160 stage => $self->stage // 'default',
161 file => $run,
162 owner => $$,
163 ipcfile => $name,
164 args => [@ARGV],
165 env_vars => $self->env_vars,
166 });
167
168 open($fh, '<', $name) or die "Could not open ipcfile: $!";
169 my $mpid = read_line($fh);
170 my $wpid = read_line($fh);
171 my $win = read_line($fh);
172
173 $self->set_sig_handlers($wpid);
174
175 open(my $wfh, '>>', "/proc/$mpid/fd/$win") or die "Could not open /proc/$mpid/fd/$win: $!";
176 $wfh->autoflush(1);
177 STDIN->blocking(0);
178 while (0 < kill(0, $mpid)) {
179 my $line = <STDIN>;
180 if (defined $line) {
181 print $wfh $line;
182 }
183 else {
184 sleep 0.02;
185 }
186 }
187
188 $self->clear_sig_handlers();
189
190 my $exit = read_line($fh) // die "Could not get exit code";
191 $exit = parse_exit($exit);
192 if ($exit->{sig}) {
193 print STDERR "Terminated with signal: $exit->{sig}.\n";
194 kill($exit->{sig}, $$);
195 }
196
197 print STDERR "Exited with code: $exit->{err}.\n" if $exit->{err};
198
199 $self->pre_exit_hook($exit);
200
201 exit($exit->{err});
202 }
203
204 1;
205
206 __END__
207
208 =pod
209
210 =encoding UTF-8
211
212 =head1 NAME
213
214 App::Yath::Command::spawn - Launch a perl script from the preloaded environment
215
216 =head1 DESCRIPTION
217
218 This will launch the specified script from the preloaded yath process.
219
220 NOTE: environment variables are not automatically passed to the spawned
221 process. You must use -e or -E (see help) to specify what environment variables
222 you care about.
223
224
225 =head1 USAGE
226
227 $ yath [YATH OPTIONS] spawn [COMMAND OPTIONS]
228
229 =head2 YATH OPTIONS
230
231 =head3 Developer
232
233 =over 4
234
235 =item --dev-lib
236
237 =item --dev-lib=lib
238
239 =item -D
240
241 =item -D=lib
242
243 =item -Dlib
244
245 =item --no-dev-lib
246
247 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.
248
249 Can be specified multiple times
250
251
252 =back
253
254 =head3 Environment
255
256 =over 4
257
258 =item --persist-dir ARG
259
260 =item --persist-dir=ARG
261
262 =item --no-persist-dir
263
264 Where to find persistence files.
265
266
267 =item --persist-file ARG
268
269 =item --persist-file=ARG
270
271 =item --pfile ARG
272
273 =item --pfile=ARG
274
275 =item --no-persist-file
276
277 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.
278
279
280 =item --project ARG
281
282 =item --project=ARG
283
284 =item --project-name ARG
285
286 =item --project-name=ARG
287
288 =item --no-project
289
290 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.
291
292
293 =back
294
295 =head3 Help and Debugging
296
297 =over 4
298
299 =item --show-opts
300
301 =item --no-show-opts
302
303 Exit after showing what yath thinks your options mean
304
305
306 =item --version
307
308 =item -V
309
310 =item --no-version
311
312 Exit after showing a helpful usage message
313
314
315 =back
316
317 =head3 Plugins
318
319 =over 4
320
321 =item --no-scan-plugins
322
323 =item --no-no-scan-plugins
324
325 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.
326
327
328 =item --plugins PLUGIN
329
330 =item --plugins +App::Yath::Plugin::PLUGIN
331
332 =item --plugins PLUGIN=arg1,arg2,...
333
334 =item --plugin PLUGIN
335
336 =item --plugin +App::Yath::Plugin::PLUGIN
337
338 =item --plugin PLUGIN=arg1,arg2,...
339
340 =item -pPLUGIN
341
342 =item --no-plugins
343
344 Load a yath plugin.
345
346 Can be specified multiple times
347
348
349 =back
350
351 =head2 COMMAND OPTIONS
352
353 =head3 Cover Options
354
355 =over 4
356
357 =item --cover-aggregator ByTest
358
359 =item --cover-aggregator ByRun
360
361 =item --cover-aggregator +Custom::Aggregator
362
363 =item --cover-agg ByTest
364
365 =item --cover-agg ByRun
366
367 =item --cover-agg +Custom::Aggregator
368
369 =item --no-cover-aggregator
370
371 Choose a custom aggregator subclass
372
373
374 =item --cover-class ARG
375
376 =item --cover-class=ARG
377
378 =item --no-cover-class
379
380 Choose a Test2::Plugin::Cover subclass
381
382
383 =item --cover-dirs ARG
384
385 =item --cover-dirs=ARG
386
387 =item --cover-dir ARG
388
389 =item --cover-dir=ARG
390
391 =item --no-cover-dirs
392
393 NO DESCRIPTION - FIX ME
394
395 Can be specified multiple times
396
397
398 =item --cover-exclude-dirs ARG
399
400 =item --cover-exclude-dirs=ARG
401
402 =item --cover-exclude-dir ARG
403
404 =item --cover-exclude-dir=ARG
405
406 =item --no-cover-exclude-dirs
407
408 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.
409
410 Can be specified multiple times
411
412
413 =item --cover-exclude-private
414
415 =item --no-cover-exclude-private
416
417
418
419
420 =item --cover-files
421
422 =item --no-cover-files
423
424 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)
425
426
427 =item --cover-from path/to/log.jsonl
428
429 =item --cover-from http://example.com/coverage
430
431 =item --cover-from path/to/coverage.jsonl
432
433 =item --no-cover-from
434
435 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.
436
437
438 =item --cover-from-type json
439
440 =item --cover-from-type jsonl
441
442 =item --cover-from-type log
443
444 =item --no-cover-from-type
445
446 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.
447
448
449 =item --cover-manager My::Coverage::Manager
450
451 =item --no-cover-manager
452
453 Coverage 'from' manager to use when coverage data does not provide one
454
455
456 =item --cover-maybe-from path/to/log.jsonl
457
458 =item --cover-maybe-from http://example.com/coverage
459
460 =item --cover-maybe-from path/to/coverage.jsonl
461
462 =item --no-cover-maybe-from
463
464 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.
465
466
467 =item --cover-maybe-from-type json
468
469 =item --cover-maybe-from-type jsonl
470
471 =item --cover-maybe-from-type log
472
473 =item --no-cover-maybe-from-type
474
475 Same as "from_type" but for "maybe_from". Defaults to "from_type" if that is specified, otherwise auto-detect
476
477
478 =item --cover-metrics
479
480 =item --no-cover-metrics
481
482
483
484
485 =item --cover-types ARG
486
487 =item --cover-types=ARG
488
489 =item --cover-type ARG
490
491 =item --cover-type=ARG
492
493 =item --no-cover-types
494
495 NO DESCRIPTION - FIX ME
496
497 Can be specified multiple times
498
499
500 =item --cover-write
501
502 =item --cover-write=coverage.jsonl
503
504 =item --cover-write=coverage.json
505
506 =item --no-cover-write
507
508 Create a json or jsonl file of all coverage data seen during the run (This implies --cover-files).
509
510
511 =back
512
513 =head3 Git Options
514
515 =over 4
516
517 =item --git-change-base master
518
519 =item --git-change-base HEAD^
520
521 =item --git-change-base df22abe4
522
523 =item --no-git-change-base
524
525 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.
526
527
528 =back
529
530 =head3 Help and Debugging
531
532 =over 4
533
534 =item --dummy
535
536 =item -d
537
538 =item --no-dummy
539
540 Dummy run, do not actually execute anything
541
542 Can also be set with the following environment variables: C<T2_HARNESS_DUMMY>
543
544
545 =item --help
546
547 =item -h
548
549 =item --no-help
550
551 exit after showing help information
552
553
554 =item --interactive
555
556 =item -i
557
558 =item --no-interactive
559
560 Use interactive mode, 1 test at a time, stdin forwarded to it
561
562
563 =item --keep-dirs
564
565 =item --keep_dir
566
567 =item -k
568
569 =item --no-keep-dirs
570
571 Do not delete directories when done. This is useful if you want to inspect the directories used for various commands.
572
573
574 =item --procname-prefix ARG
575
576 =item --procname-prefix=ARG
577
578 =item --no-procname-prefix
579
580 Add a prefix to all proc names (as seen by ps).
581
582
583 =back
584
585 =head3 YathUI Options
586
587 =over 4
588
589 =item --yathui-api-key ARG
590
591 =item --yathui-api-key=ARG
592
593 =item --no-yathui-api-key
594
595 Yath-UI API key. This is not necessary if your Yath-UI instance is set to single-user
596
597
598 =item --yathui-grace
599
600 =item --no-yathui-grace
601
602 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.
603
604
605 =item --yathui-long-duration 10
606
607 =item --no-yathui-long-duration
608
609 Minimum duration length (seconds) before a test goes from MEDIUM to LONG
610
611
612 =item --yathui-medium-duration 5
613
614 =item --no-yathui-medium-duration
615
616 Minimum duration length (seconds) before a test goes from SHORT to MEDIUM
617
618
619 =item --yathui-mode summary
620
621 =item --yathui-mode qvf
622
623 =item --yathui-mode qvfd
624
625 =item --yathui-mode complete
626
627 =item --no-yathui-mode
628
629 Set the upload mode (default 'qvfd')
630
631
632 =item --yathui-project ARG
633
634 =item --yathui-project=ARG
635
636 =item --no-yathui-project
637
638 The Yath-UI project for your test results
639
640
641 =item --yathui-retry
642
643 =item --no-yathui-retry
644
645 How many times to try an operation before giving up
646
647 Can be specified multiple times
648
649
650 =item --yathui-url http://my-yath-ui.com/...
651
652 =item --uri http://my-yath-ui.com/...
653
654 =item --no-yathui-url
655
656 Yath-UI url
657
658
659 =back
660
661 =head3 spawn options
662
663 =over 4
664
665 =item --copy-env HOME
666
667 =item --copy-env SHELL
668
669 =item --copy-env /PERL_.*/i
670
671 =item -e HOME
672
673 =item -e SHELL
674
675 =item -e /PERL_.*/i
676
677 =item --no-copy-env
678
679 Specify environment variables to pass along with their current values, can also use a regex
680
681 Can be specified multiple times
682
683
684 =item --env-var VAR=VAL
685
686 =item -EVAR=VAL
687
688 =item -E VAR=VAL
689
690 =item --no-env-var
691
692 Set environment variables for the spawn
693
694 Can be specified multiple times
695
696
697 =item --stage foo
698
699 =item -s foo
700
701 =item --no-stage
702
703 Specify the stage to be used for launching the script
704
705
706 =back
707
708 =head1 SOURCE
709
710 The source code repository for Test2-Harness can be found at
711 F<http://github.com/Test-More/Test2-Harness/>.
712
713 =head1 MAINTAINERS
714
715 =over 4
716
717 =item Chad Granum E<lt>[email protected]<gt>
718
719 =back
720
721 =head1 AUTHORS
722
723 =over 4
724
725 =item Chad Granum E<lt>[email protected]<gt>
726
727 =back
728
729 =head1 COPYRIGHT
730
731 Copyright 2026 Chad Granum E<lt>[email protected]<gt>.
732
733 This program is free software; you can redistribute it and/or
734 modify it under the same terms as Perl itself.
735
736 See F<http://dev.perl.org/licenses/>
737
738 =cut
739