Debian Codegraph @

lib/App/Yath/Command/watch.pm

raw
1 package App::Yath::Command::watch;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Time::HiRes qw/sleep/;
8
9 use Test2::Harness::Util::File::JSON;
10
11 use App::Yath::Util qw/find_pfile/;
12 use Test2::Harness::Util qw/open_file/;
13
14 use parent 'App::Yath::Command';
15 use Test2::Harness::Util::HashBase;
16
17 sub group { 'persist' }
18
19 sub summary { "Monitor the persistent test runner" }
20 sub cli_args { "" }
21
22 sub description {
23 return <<" EOT";
24 This command will tail the logs from a persistent instance of yath. STDOUT and
25 STDERR will be printed as seen, so may not be in proper order.
26 EOT
27 }
28
29 sub run {
30 my $self = shift;
31
32 my $args = $self->args;
33 shift @$args if @$args && $args->[0] eq '--';
34 my $stop;
35 $stop = 1 if @$args && $args->[0] eq 'STOP';
36
37 my $pfile = find_pfile($self->settings, no_fatal => 1)
38 or die "No persistent harness was found for the current path.\n";
39
40 print "\nFound: $pfile\n";
41 my $data = Test2::Harness::Util::File::JSON->new(name => $pfile)->read();
42 print " PID: $data->{pid}\n";
43 print " Dir: $data->{dir}\n";
44 print "\n";
45
46 my $err_f = File::Spec->catfile($data->{dir}, 'error.log');
47 my $out_f = File::Spec->catfile($data->{dir}, 'output.log');
48
49 my $err_fh = open_file($err_f, '<');
50 my $out_fh = open_file($out_f, '<');
51
52 my $auxdir = File::Spec->catdir($data->{dir}, 'aux_logs');
53 my %aux;
54
55 while (1) {
56 my $count = 0;
57 seek($out_fh, 0, 1) if eof($out_fh);
58 while (my $line = <$out_fh>) {
59 $count++;
60 print STDOUT $line;
61 }
62 seek($err_fh, 0, 1) if eof($err_fh);
63 while (my $line = <$err_fh>) {
64 $count++;
65 print STDERR $line;
66 }
67
68 if (-d $auxdir) {
69 opendir(my $dh, $auxdir) or die "Could not open auxdir: $!";
70 for my $file (readdir($dh)) {
71 next if $aux{$file};
72 next unless $file =~ m/\.log$/;
73 my $full = File::Spec->catfile($auxdir, $file);
74 next unless -f $full;
75 $aux{$file} = open_file($full, '<');
76 $count++;
77 }
78 }
79
80 for my $file (sort keys %aux) {
81 my $fh = $aux{$file};
82 my $ofh = $file =~ m/STDERR/ ? \*STDERR : \*STDOUT;
83 seek($fh, 0, 1) if eof($fh);
84 while (my $line = <$fh>) {
85 $count++;
86 print $ofh $line;
87 }
88 }
89
90 next if $count;
91 last if $stop;
92 last unless -f $pfile;
93 sleep 0.02;
94 }
95
96 return 0;
97 }
98
99
100 1;
101
102 __END__
103
104 =pod
105
106 =encoding UTF-8
107
108 =head1 NAME
109
110 App::Yath::Command::watch - Monitor the persistent test runner
111
112 =head1 DESCRIPTION
113
114 This command will tail the logs from a persistent instance of yath. STDOUT and
115 STDERR will be printed as seen, so may not be in proper order.
116
117
118 =head1 USAGE
119
120 $ yath [YATH OPTIONS] watch [COMMAND OPTIONS]
121
122 =head2 YATH OPTIONS
123
124 =head3 Developer
125
126 =over 4
127
128 =item --dev-lib
129
130 =item --dev-lib=lib
131
132 =item -D
133
134 =item -D=lib
135
136 =item -Dlib
137
138 =item --no-dev-lib
139
140 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.
141
142 Can be specified multiple times
143
144
145 =back
146
147 =head3 Environment
148
149 =over 4
150
151 =item --persist-dir ARG
152
153 =item --persist-dir=ARG
154
155 =item --no-persist-dir
156
157 Where to find persistence files.
158
159
160 =item --persist-file ARG
161
162 =item --persist-file=ARG
163
164 =item --pfile ARG
165
166 =item --pfile=ARG
167
168 =item --no-persist-file
169
170 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.
171
172
173 =item --project ARG
174
175 =item --project=ARG
176
177 =item --project-name ARG
178
179 =item --project-name=ARG
180
181 =item --no-project
182
183 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.
184
185
186 =back
187
188 =head3 Help and Debugging
189
190 =over 4
191
192 =item --show-opts
193
194 =item --no-show-opts
195
196 Exit after showing what yath thinks your options mean
197
198
199 =item --version
200
201 =item -V
202
203 =item --no-version
204
205 Exit after showing a helpful usage message
206
207
208 =back
209
210 =head3 Plugins
211
212 =over 4
213
214 =item --no-scan-plugins
215
216 =item --no-no-scan-plugins
217
218 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.
219
220
221 =item --plugins PLUGIN
222
223 =item --plugins +App::Yath::Plugin::PLUGIN
224
225 =item --plugins PLUGIN=arg1,arg2,...
226
227 =item --plugin PLUGIN
228
229 =item --plugin +App::Yath::Plugin::PLUGIN
230
231 =item --plugin PLUGIN=arg1,arg2,...
232
233 =item -pPLUGIN
234
235 =item --no-plugins
236
237 Load a yath plugin.
238
239 Can be specified multiple times
240
241
242 =back
243
244 =head2 COMMAND OPTIONS
245
246 =head3 Cover Options
247
248 =over 4
249
250 =item --cover-aggregator ByTest
251
252 =item --cover-aggregator ByRun
253
254 =item --cover-aggregator +Custom::Aggregator
255
256 =item --cover-agg ByTest
257
258 =item --cover-agg ByRun
259
260 =item --cover-agg +Custom::Aggregator
261
262 =item --no-cover-aggregator
263
264 Choose a custom aggregator subclass
265
266
267 =item --cover-class ARG
268
269 =item --cover-class=ARG
270
271 =item --no-cover-class
272
273 Choose a Test2::Plugin::Cover subclass
274
275
276 =item --cover-dirs ARG
277
278 =item --cover-dirs=ARG
279
280 =item --cover-dir ARG
281
282 =item --cover-dir=ARG
283
284 =item --no-cover-dirs
285
286 NO DESCRIPTION - FIX ME
287
288 Can be specified multiple times
289
290
291 =item --cover-exclude-dirs ARG
292
293 =item --cover-exclude-dirs=ARG
294
295 =item --cover-exclude-dir ARG
296
297 =item --cover-exclude-dir=ARG
298
299 =item --no-cover-exclude-dirs
300
301 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.
302
303 Can be specified multiple times
304
305
306 =item --cover-exclude-private
307
308 =item --no-cover-exclude-private
309
310
311
312
313 =item --cover-files
314
315 =item --no-cover-files
316
317 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)
318
319
320 =item --cover-from path/to/log.jsonl
321
322 =item --cover-from http://example.com/coverage
323
324 =item --cover-from path/to/coverage.jsonl
325
326 =item --no-cover-from
327
328 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.
329
330
331 =item --cover-from-type json
332
333 =item --cover-from-type jsonl
334
335 =item --cover-from-type log
336
337 =item --no-cover-from-type
338
339 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.
340
341
342 =item --cover-manager My::Coverage::Manager
343
344 =item --no-cover-manager
345
346 Coverage 'from' manager to use when coverage data does not provide one
347
348
349 =item --cover-maybe-from path/to/log.jsonl
350
351 =item --cover-maybe-from http://example.com/coverage
352
353 =item --cover-maybe-from path/to/coverage.jsonl
354
355 =item --no-cover-maybe-from
356
357 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.
358
359
360 =item --cover-maybe-from-type json
361
362 =item --cover-maybe-from-type jsonl
363
364 =item --cover-maybe-from-type log
365
366 =item --no-cover-maybe-from-type
367
368 Same as "from_type" but for "maybe_from". Defaults to "from_type" if that is specified, otherwise auto-detect
369
370
371 =item --cover-metrics
372
373 =item --no-cover-metrics
374
375
376
377
378 =item --cover-types ARG
379
380 =item --cover-types=ARG
381
382 =item --cover-type ARG
383
384 =item --cover-type=ARG
385
386 =item --no-cover-types
387
388 NO DESCRIPTION - FIX ME
389
390 Can be specified multiple times
391
392
393 =item --cover-write
394
395 =item --cover-write=coverage.jsonl
396
397 =item --cover-write=coverage.json
398
399 =item --no-cover-write
400
401 Create a json or jsonl file of all coverage data seen during the run (This implies --cover-files).
402
403
404 =back
405
406 =head3 Git Options
407
408 =over 4
409
410 =item --git-change-base master
411
412 =item --git-change-base HEAD^
413
414 =item --git-change-base df22abe4
415
416 =item --no-git-change-base
417
418 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.
419
420
421 =back
422
423 =head3 Help and Debugging
424
425 =over 4
426
427 =item --dummy
428
429 =item -d
430
431 =item --no-dummy
432
433 Dummy run, do not actually execute anything
434
435 Can also be set with the following environment variables: C<T2_HARNESS_DUMMY>
436
437
438 =item --help
439
440 =item -h
441
442 =item --no-help
443
444 exit after showing help information
445
446
447 =item --interactive
448
449 =item -i
450
451 =item --no-interactive
452
453 Use interactive mode, 1 test at a time, stdin forwarded to it
454
455
456 =item --keep-dirs
457
458 =item --keep_dir
459
460 =item -k
461
462 =item --no-keep-dirs
463
464 Do not delete directories when done. This is useful if you want to inspect the directories used for various commands.
465
466
467 =item --procname-prefix ARG
468
469 =item --procname-prefix=ARG
470
471 =item --no-procname-prefix
472
473 Add a prefix to all proc names (as seen by ps).
474
475
476 =back
477
478 =head3 YathUI Options
479
480 =over 4
481
482 =item --yathui-api-key ARG
483
484 =item --yathui-api-key=ARG
485
486 =item --no-yathui-api-key
487
488 Yath-UI API key. This is not necessary if your Yath-UI instance is set to single-user
489
490
491 =item --yathui-grace
492
493 =item --no-yathui-grace
494
495 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.
496
497
498 =item --yathui-long-duration 10
499
500 =item --no-yathui-long-duration
501
502 Minimum duration length (seconds) before a test goes from MEDIUM to LONG
503
504
505 =item --yathui-medium-duration 5
506
507 =item --no-yathui-medium-duration
508
509 Minimum duration length (seconds) before a test goes from SHORT to MEDIUM
510
511
512 =item --yathui-mode summary
513
514 =item --yathui-mode qvf
515
516 =item --yathui-mode qvfd
517
518 =item --yathui-mode complete
519
520 =item --no-yathui-mode
521
522 Set the upload mode (default 'qvfd')
523
524
525 =item --yathui-project ARG
526
527 =item --yathui-project=ARG
528
529 =item --no-yathui-project
530
531 The Yath-UI project for your test results
532
533
534 =item --yathui-retry
535
536 =item --no-yathui-retry
537
538 How many times to try an operation before giving up
539
540 Can be specified multiple times
541
542
543 =item --yathui-url http://my-yath-ui.com/...
544
545 =item --uri http://my-yath-ui.com/...
546
547 =item --no-yathui-url
548
549 Yath-UI url
550
551
552 =back
553
554 =head1 SOURCE
555
556 The source code repository for Test2-Harness can be found at
557 F<http://github.com/Test-More/Test2-Harness/>.
558
559 =head1 MAINTAINERS
560
561 =over 4
562
563 =item Chad Granum E<lt>[email protected]<gt>
564
565 =back
566
567 =head1 AUTHORS
568
569 =over 4
570
571 =item Chad Granum E<lt>[email protected]<gt>
572
573 =back
574
575 =head1 COPYRIGHT
576
577 Copyright 2026 Chad Granum E<lt>[email protected]<gt>.
578
579 This program is free software; you can redistribute it and/or
580 modify it under the same terms as Perl itself.
581
582 See F<http://dev.perl.org/licenses/>
583
584 =cut
585