Debian Codegraph @

lib/App/Yath/Command/resources.pm

raw
1 package App::Yath::Command::resources;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Term::Table();
8 use File::Spec();
9 use Time::HiRes qw/sleep/;
10
11 use App::Yath::Util qw/find_pfile/;
12
13 use App::Yath::Options;
14 use Test2::Harness::Runner::State;
15 use Test2::Harness::Util::File::JSON();
16 use Test2::Harness::Util::Queue();
17
18 use parent 'App::Yath::Command';
19 use Test2::Harness::Util::HashBase qw/+state/;
20
21 include_options(
22 'App::Yath::Options::Debug',
23 'App::Yath::Options::Runner',
24 );
25
26 sub group { 'state' }
27
28 sub summary { "View the state info for a test runner" }
29 sub cli_args { "" }
30
31 sub description {
32 return <<" EOT";
33 A look at the state and resources used by a runner.
34 EOT
35 }
36
37 sub pfile_params { (no_fatal => 1) }
38
39 sub newest {
40 my ($a, $b) = @_;
41 return $a unless $b;
42 return $b unless $a;
43
44 my @as = stat($a);
45 my @bs = stat($b);
46 return $a if $as[9] > $bs[9];
47 return $b;
48 }
49
50 sub state {
51 my $self = shift;
52
53 return $self->{+STATE} if $self->{+STATE};
54
55 my $info_file;
56 opendir(my $dh, "./") or die "Could not open current dir: $!";
57 for my $file (readdir($dh)) {
58 next unless $file =~ m{^\.test_info\.\S+\.json$};
59 $info_file = newest($info_file, "./$file");
60 }
61
62 my $pfile = find_pfile($self->settings, no_fatal => 1);
63
64 if (my $use = newest($info_file, $pfile)) {
65 if ($info_file) {
66 my $data = Test2::Harness::Util::File::JSON->new(name => $info_file)->read;
67 return $self->{+STATE} = Test2::Harness::Runner::State->new(%$data, observe => 1);
68 }
69
70 if (my $pfile = find_pfile($self->settings, no_fatal => 1)) {
71 my $data = Test2::Harness::Util::File::JSON->new(name => $pfile)->read();
72 my $workdir = $data->{dir};
73 my $settings = Test2::Harness::Util::File::JSON->new(name => "$workdir/settings.json")->read();
74
75 return $self->{+STATE} = Test2::Harness::Runner::State->new(
76 observe => 1,
77 job_count => $settings->{runner}->{job_count} // 1,
78 workdir => $data->{dir},
79 );
80 }
81 }
82
83 return;
84 }
85
86 sub shared {
87 my $self = shift;
88
89 my $shared;
90 eval {
91 require Test2::Harness::Runner::Resource::SharedJobSlots;
92 $shared = Test2::Harness::Runner::Resource::SharedJobSlots->new(
93 settings => $self->settings,
94 );
95 1;
96 };
97
98 return $shared;
99 }
100
101 sub run {
102 my $self = shift;
103
104 my $res;
105
106 if(my $state = $self->state) {
107 my @list;
108 $res = sub {
109 unless (@list) {
110 $state->poll;
111 @list = (@{$state->resources}, undef);
112 }
113
114 return shift @list;
115 };
116 }
117 elsif (my $shared = $self->shared) {
118 my $alt = 0;
119 $res = sub {
120 if ($alt) {
121 $alt = 0;
122 return undef;
123 }
124
125 $alt = 1;
126 return $shared;
127 };
128 }
129
130 die "No persistent runner, no running test, and no shared resources found\n"
131 unless $res;
132
133 while (1) {
134 my @out = (
135 "\r\e[2J\r\e[1;1H",
136 "\n==== Resource state ====\n",
137 );
138 while (my $resource = $res->()) {
139 my @lines = $resource->status_lines;
140 next unless @lines;
141 push @out => (
142 "\nResource: " . ref($resource) . "\n",
143 join "\n" => @lines,
144 );
145 }
146 push @out => "\n\n";
147 print @out;
148 sleep 0.2;
149 }
150
151 return 0;
152 }
153
154 1;
155
156 __END__
157
158 =pod
159
160 =encoding UTF-8
161
162 =head1 NAME
163
164 App::Yath::Command::resources - View the state info for a test runner
165
166 =head1 DESCRIPTION
167
168 A look at the state and resources used by a runner.
169
170
171 =head1 USAGE
172
173 $ yath [YATH OPTIONS] resources [COMMAND OPTIONS]
174
175 =head2 YATH OPTIONS
176
177 =head3 Developer
178
179 =over 4
180
181 =item --dev-lib
182
183 =item --dev-lib=lib
184
185 =item -D
186
187 =item -D=lib
188
189 =item -Dlib
190
191 =item --no-dev-lib
192
193 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.
194
195 Can be specified multiple times
196
197
198 =back
199
200 =head3 Environment
201
202 =over 4
203
204 =item --persist-dir ARG
205
206 =item --persist-dir=ARG
207
208 =item --no-persist-dir
209
210 Where to find persistence files.
211
212
213 =item --persist-file ARG
214
215 =item --persist-file=ARG
216
217 =item --pfile ARG
218
219 =item --pfile=ARG
220
221 =item --no-persist-file
222
223 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.
224
225
226 =item --project ARG
227
228 =item --project=ARG
229
230 =item --project-name ARG
231
232 =item --project-name=ARG
233
234 =item --no-project
235
236 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.
237
238
239 =back
240
241 =head3 Help and Debugging
242
243 =over 4
244
245 =item --show-opts
246
247 =item --no-show-opts
248
249 Exit after showing what yath thinks your options mean
250
251
252 =item --version
253
254 =item -V
255
256 =item --no-version
257
258 Exit after showing a helpful usage message
259
260
261 =back
262
263 =head3 Plugins
264
265 =over 4
266
267 =item --no-scan-plugins
268
269 =item --no-no-scan-plugins
270
271 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.
272
273
274 =item --plugins PLUGIN
275
276 =item --plugins +App::Yath::Plugin::PLUGIN
277
278 =item --plugins PLUGIN=arg1,arg2,...
279
280 =item --plugin PLUGIN
281
282 =item --plugin +App::Yath::Plugin::PLUGIN
283
284 =item --plugin PLUGIN=arg1,arg2,...
285
286 =item -pPLUGIN
287
288 =item --no-plugins
289
290 Load a yath plugin.
291
292 Can be specified multiple times
293
294
295 =back
296
297 =head2 COMMAND OPTIONS
298
299 =head3 Cover Options
300
301 =over 4
302
303 =item --cover-aggregator ByTest
304
305 =item --cover-aggregator ByRun
306
307 =item --cover-aggregator +Custom::Aggregator
308
309 =item --cover-agg ByTest
310
311 =item --cover-agg ByRun
312
313 =item --cover-agg +Custom::Aggregator
314
315 =item --no-cover-aggregator
316
317 Choose a custom aggregator subclass
318
319
320 =item --cover-class ARG
321
322 =item --cover-class=ARG
323
324 =item --no-cover-class
325
326 Choose a Test2::Plugin::Cover subclass
327
328
329 =item --cover-dirs ARG
330
331 =item --cover-dirs=ARG
332
333 =item --cover-dir ARG
334
335 =item --cover-dir=ARG
336
337 =item --no-cover-dirs
338
339 NO DESCRIPTION - FIX ME
340
341 Can be specified multiple times
342
343
344 =item --cover-exclude-dirs ARG
345
346 =item --cover-exclude-dirs=ARG
347
348 =item --cover-exclude-dir ARG
349
350 =item --cover-exclude-dir=ARG
351
352 =item --no-cover-exclude-dirs
353
354 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.
355
356 Can be specified multiple times
357
358
359 =item --cover-exclude-private
360
361 =item --no-cover-exclude-private
362
363
364
365
366 =item --cover-files
367
368 =item --no-cover-files
369
370 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)
371
372
373 =item --cover-from path/to/log.jsonl
374
375 =item --cover-from http://example.com/coverage
376
377 =item --cover-from path/to/coverage.jsonl
378
379 =item --no-cover-from
380
381 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.
382
383
384 =item --cover-from-type json
385
386 =item --cover-from-type jsonl
387
388 =item --cover-from-type log
389
390 =item --no-cover-from-type
391
392 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.
393
394
395 =item --cover-manager My::Coverage::Manager
396
397 =item --no-cover-manager
398
399 Coverage 'from' manager to use when coverage data does not provide one
400
401
402 =item --cover-maybe-from path/to/log.jsonl
403
404 =item --cover-maybe-from http://example.com/coverage
405
406 =item --cover-maybe-from path/to/coverage.jsonl
407
408 =item --no-cover-maybe-from
409
410 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.
411
412
413 =item --cover-maybe-from-type json
414
415 =item --cover-maybe-from-type jsonl
416
417 =item --cover-maybe-from-type log
418
419 =item --no-cover-maybe-from-type
420
421 Same as "from_type" but for "maybe_from". Defaults to "from_type" if that is specified, otherwise auto-detect
422
423
424 =item --cover-metrics
425
426 =item --no-cover-metrics
427
428
429
430
431 =item --cover-types ARG
432
433 =item --cover-types=ARG
434
435 =item --cover-type ARG
436
437 =item --cover-type=ARG
438
439 =item --no-cover-types
440
441 NO DESCRIPTION - FIX ME
442
443 Can be specified multiple times
444
445
446 =item --cover-write
447
448 =item --cover-write=coverage.jsonl
449
450 =item --cover-write=coverage.json
451
452 =item --no-cover-write
453
454 Create a json or jsonl file of all coverage data seen during the run (This implies --cover-files).
455
456
457 =back
458
459 =head3 Git Options
460
461 =over 4
462
463 =item --git-change-base master
464
465 =item --git-change-base HEAD^
466
467 =item --git-change-base df22abe4
468
469 =item --no-git-change-base
470
471 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.
472
473
474 =back
475
476 =head3 Help and Debugging
477
478 =over 4
479
480 =item --dummy
481
482 =item -d
483
484 =item --no-dummy
485
486 Dummy run, do not actually execute anything
487
488 Can also be set with the following environment variables: C<T2_HARNESS_DUMMY>
489
490
491 =item --help
492
493 =item -h
494
495 =item --no-help
496
497 exit after showing help information
498
499
500 =item --interactive
501
502 =item -i
503
504 =item --no-interactive
505
506 Use interactive mode, 1 test at a time, stdin forwarded to it
507
508
509 =item --keep-dirs
510
511 =item --keep_dir
512
513 =item -k
514
515 =item --no-keep-dirs
516
517 Do not delete directories when done. This is useful if you want to inspect the directories used for various commands.
518
519
520 =item --procname-prefix ARG
521
522 =item --procname-prefix=ARG
523
524 =item --no-procname-prefix
525
526 Add a prefix to all proc names (as seen by ps).
527
528
529 =back
530
531 =head3 Runner Options
532
533 =over 4
534
535 =item --abort-on-bail
536
537 =item --no-abort-on-bail
538
539 Abort all testing if a bail-out is encountered (default: on)
540
541
542 =item --blib
543
544 =item -b
545
546 =item --no-blib
547
548 (Default: include if it exists) Include 'blib/lib' and 'blib/arch' in your module path
549
550
551 =item --cover
552
553 =item --cover=-silent,1,+ignore,^t/,+ignore,^t2/,+ignore,^xt,+ignore,^test.pl
554
555 =item --no-cover
556
557 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
558
559
560 =item --dump-depmap
561
562 =item --no-dump-depmap
563
564 When using staged preload, dump the depmap for each stage as json files
565
566
567 =item --event-timeout SECONDS
568
569 =item --et SECONDS
570
571 =item --no-event-timeout
572
573 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.
574
575
576 =item --fail-on-resource-skip
577
578 =item --no-fail-on-resource-skip
579
580 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.
581
582
583 =item --include ARG
584
585 =item --include=ARG
586
587 =item -I ARG
588
589 =item -I=ARG
590
591 =item --no-include
592
593 Add a directory to your include paths
594
595 Can be specified multiple times
596
597
598 =item --job-count 4
599
600 =item --job-count 8:2
601
602 =item --jobs 4
603
604 =item --jobs 8:2
605
606 =item -j4
607
608 =item -j8:2
609
610 =item --no-job-count
611
612 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.
613
614 Can also be set with the following environment variables: C<YATH_JOB_COUNT>, C<T2_HARNESS_JOB_COUNT>, C<HARNESS_JOB_COUNT>
615
616
617 =item --lib
618
619 =item -l
620
621 =item --no-lib
622
623 (Default: include if it exists) Include 'lib' in your module path
624
625
626 =item --nytprof
627
628 =item --no-nytprof
629
630 Use Devel::NYTProf on tests. This will set addpid=1 for you. This works with or without fork.
631
632
633 =item --post-exit-timeout SECONDS
634
635 =item --pet SECONDS
636
637 =item --no-post-exit-timeout
638
639 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.
640
641
642 =item --preload-threshold ARG
643
644 =item --preload-threshold=ARG
645
646 =item --Pt ARG
647
648 =item --Pt=ARG
649
650 =item -W ARG
651
652 =item -W=ARG
653
654 =item --no-preload-threshold
655
656 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.
657
658
659 =item --preloads ARG
660
661 =item --preloads=ARG
662
663 =item --preload ARG
664
665 =item --preload=ARG
666
667 =item -P ARG
668
669 =item -P=ARG
670
671 =item --no-preloads
672
673 Preload a module before running tests
674
675 Can be specified multiple times
676
677
678 =item --resource-timeout SECONDS
679
680 =item --rt SECONDS
681
682 =item --no-resource-timeout
683
684 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)
685
686
687 =item --resource Port
688
689 =item --resource +Test2::Harness::Runner::Resource::Port
690
691 =item -R Port
692
693 =item --no-resource
694
695 Use a resource module to assign resource assignments to individual tests
696
697 Can be specified multiple times
698
699
700 =item --runner-id ARG
701
702 =item --runner-id=ARG
703
704 =item --no-runner-id
705
706 Runner ID (usually a generated uuid)
707
708
709 =item --shared-jobs-config .sharedjobslots.yml
710
711 =item --shared-jobs-config relative/path/.sharedjobslots.yml
712
713 =item --shared-jobs-config /absolute/path/.sharedjobslots.yml
714
715 =item --no-shared-jobs-config
716
717 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.
718
719
720 =item --slots-per-job 2
721
722 =item -x2
723
724 =item --no-slots-per-job
725
726 This sets the number of slots each job will use (default 1). This is normally set by the ':#' in '-j#:#'.
727
728 Can also be set with the following environment variables: C<T2_HARNESS_JOB_CONCURRENCY>
729
730
731 =item --stall-report
732
733 =item --stall-report=STRONG:LOOSE
734
735 =item --stall-report=SECONDS
736
737 =item --no-stall-report
738
739 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)
740
741
742 =item --stall-report-dir path/to/dir
743
744 =item --no-stall-report-dir
745
746 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.
747
748
749 =item --switch ARG
750
751 =item --switch=ARG
752
753 =item -S ARG
754
755 =item -S=ARG
756
757 =item --no-switch
758
759 Pass the specified switch to perl for each test. This is not compatible with preload.
760
761 Can be specified multiple times
762
763
764 =item --tlib
765
766 =item --no-tlib
767
768 (Default: off) Include 't/lib' in your module path
769
770
771 =item --unsafe-inc
772
773 =item --no-unsafe-inc
774
775 perl is removing '.' from @INC as a security concern. This option keeps things from breaking for now.
776
777 Can also be set with the following environment variables: C<PERL_USE_UNSAFE_INC>
778
779
780 =item --use-fork
781
782 =item --fork
783
784 =item --no-use-fork
785
786 (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.
787
788 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>
789
790
791 =item --use-timeout
792
793 =item --timeout
794
795 =item --no-use-timeout
796
797 (default: on) Enable/disable timeouts
798
799
800 =back
801
802 =head3 YathUI Options
803
804 =over 4
805
806 =item --yathui-api-key ARG
807
808 =item --yathui-api-key=ARG
809
810 =item --no-yathui-api-key
811
812 Yath-UI API key. This is not necessary if your Yath-UI instance is set to single-user
813
814
815 =item --yathui-grace
816
817 =item --no-yathui-grace
818
819 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.
820
821
822 =item --yathui-long-duration 10
823
824 =item --no-yathui-long-duration
825
826 Minimum duration length (seconds) before a test goes from MEDIUM to LONG
827
828
829 =item --yathui-medium-duration 5
830
831 =item --no-yathui-medium-duration
832
833 Minimum duration length (seconds) before a test goes from SHORT to MEDIUM
834
835
836 =item --yathui-mode summary
837
838 =item --yathui-mode qvf
839
840 =item --yathui-mode qvfd
841
842 =item --yathui-mode complete
843
844 =item --no-yathui-mode
845
846 Set the upload mode (default 'qvfd')
847
848
849 =item --yathui-project ARG
850
851 =item --yathui-project=ARG
852
853 =item --no-yathui-project
854
855 The Yath-UI project for your test results
856
857
858 =item --yathui-retry
859
860 =item --no-yathui-retry
861
862 How many times to try an operation before giving up
863
864 Can be specified multiple times
865
866
867 =item --yathui-url http://my-yath-ui.com/...
868
869 =item --uri http://my-yath-ui.com/...
870
871 =item --no-yathui-url
872
873 Yath-UI url
874
875
876 =back
877
878 =head1 SOURCE
879
880 The source code repository for Test2-Harness can be found at
881 F<http://github.com/Test-More/Test2-Harness/>.
882
883 =head1 MAINTAINERS
884
885 =over 4
886
887 =item Chad Granum E<lt>[email protected]<gt>
888
889 =back
890
891 =head1 AUTHORS
892
893 =over 4
894
895 =item Chad Granum E<lt>[email protected]<gt>
896
897 =back
898
899 =head1 COPYRIGHT
900
901 Copyright 2026 Chad Granum E<lt>[email protected]<gt>.
902
903 This program is free software; you can redistribute it and/or
904 modify it under the same terms as Perl itself.
905
906 See F<http://dev.perl.org/licenses/>
907
908 =cut
909