Debian Codegraph @

lib/App/Yath/Options/Debug.pm

raw
1 package App::Yath::Options::Debug;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Test2::Harness::Util::JSON qw/encode_pretty_json/;
8 use Test2::Util::Table qw/table/;
9 use Test2::Harness::Util qw/find_libraries mod2file clean_path/;
10
11 use Errno qw/EINTR/;
12
13 use App::Yath::Options;
14
15 option_group {prefix => 'debug', category => 'Help and Debugging'} => sub {
16 post 99999 => \&_post_process_show_opts;
17 post 99998 => \&_post_process_interactive;
18 post \&_post_process_version;
19 post \&_post_process_help;
20
21 option dummy => (
22 short => 'd',
23 description => 'Dummy run, do not actually execute anything',
24 env_vars => [qw/T2_HARNESS_DUMMY/],
25 clear_env_vars => 1,
26 default => 0,
27 );
28
29 option procname_prefix => (
30 type => 's',
31 default => '',
32 description => 'Add a prefix to all proc names (as seen by ps).',
33 );
34
35 option keep_dirs => (
36 short => 'k',
37 alt => ['keep_dir'],
38 description => 'Do not delete directories when done. This is useful if you want to inspect the directories used for various commands.',
39 default => 0,
40 );
41
42 option 'show-opts' => (
43 description => 'Exit after showing what yath thinks your options mean',
44 pre_command => 1,
45 );
46
47 option version => (
48 short => 'V',
49 description => "Exit after showing a helpful usage message",
50 pre_command => 1,
51 );
52
53 option help => (
54 short => 'h',
55 description => "exit after showing help information",
56 );
57
58 option interactive => (
59 short => 'i',
60 description => 'Use interactive mode, 1 test at a time, stdin forwarded to it',
61 );
62
63 option summary => (
64 type => 'd',
65 description => "Write out a summary json file, if no path is provided 'summary.json' will be used. The .json extension is added automatically if omitted.",
66
67 long_examples => ['', '=/path/to/summary.json'],
68
69 normalize => \&normalize_summary,
70 action => \&summary_action,
71 applicable => sub {
72 my ($option, $options) = @_;
73
74 return 1 if $options->included->{'App::Yath::Options::Run'};
75 return 0;
76 },
77 );
78 };
79
80 sub normalize_summary {
81 my $val = shift;
82
83 return $val if $val eq '1';
84
85 $val =~ s/\.json$//g;
86 $val .= '.json';
87
88 return clean_path($val);
89 }
90
91 sub summary_action {
92 my ($prefix, $field, $raw, $norm, $slot, $settings) = @_;
93
94 return $$slot = clean_path($norm)
95 unless $norm eq '1';
96
97 return if $$slot;
98 return $$slot = clean_path('summary.json');
99 }
100
101 sub _post_process_help {
102 my %params = @_;
103
104 return unless $params{settings}->debug->help;
105
106 my $help;
107 if (my $cmd = $params{command}) {
108 $help = $cmd->cli_help(%params);
109 }
110 else {
111 $help = __PACKAGE__->cli_help(%params);
112 }
113
114 if (eval { require IO::Pager; 1 }) {
115 local $SIG{PIPE} = sub {};
116 my $pager = IO::Pager->new(*STDOUT);
117 $pager->print($help);
118 }
119 else {
120 print $help;
121 }
122
123 exit 0;
124 }
125
126 sub _post_process_show_opts {
127 my %params = @_;
128
129 return unless $params{settings}->debug->show_opts;
130
131 my $settings = $params{settings};
132
133 print "\nCommand selected: " . $params{command}->name . " (" . ref($params{command}) . ")\n" if $params{command};
134
135 my $args = $params{args};
136 print "\nCommand args: " . join(', ' => @$args) . "\n" if @$args;
137
138 my $out = encode_pretty_json($settings);
139
140 print "\nCurrent command line and config options result in these settings:\n";
141 print "$out\n";
142
143 exit 0;
144 }
145
146 my $RAN = 0;
147 sub _post_process_interactive {
148 return if $RAN++;
149 my %params = @_;
150
151 return unless $params{settings}->debug->interactive;
152
153 my $settings = $params{settings};
154
155 my ($fifo);
156 if ($settings->check_prefix('workspace')) {
157 my $dir = $settings->workspace->workdir;
158 $fifo = "$dir/fifo-$$";
159 }
160 else {
161 require File::Temp;
162 my $fh;
163 ($fh, $fifo) = File::Temp::tempfile("YATH-FIFO-$$-XXXXXX", TMPDIR => 1);
164 close($fh);
165 unlink($fifo);
166 }
167
168 ${$settings->debug->vivify_field('fifo')} = $fifo;
169
170 if ($settings->check_prefix('display')) {
171 my $display = $settings->display;
172 $display->field(quiet => 0) if $display->check_field('quiet');
173 $display->field(verbose => 1) if $display->check_field('verbose') && !$display->verbose;
174 }
175
176 if ($settings->check_prefix('formatter')) {
177 my $formatter = $settings->formatter;
178 $formatter->field(qvf => 0) if $formatter->check_field('qvf');
179 }
180
181 if ($settings->check_prefix('run')) {
182 $settings->run->env_vars->{YATH_INTERACTIVE} = $fifo;
183 $ENV{YATH_INTERACTIVE} = $fifo;
184 }
185
186 my $pid = fork() // die "Could not fork: $!";
187 if ($pid) {
188 require Scope::Guard;
189 require POSIX;
190 POSIX::mkfifo($fifo, 0700) or die "Failed to make fifo ($fifo): $!";
191 my $fh;
192
193 my $cleanup = sub {
194 close($fh) if $fh;
195 unlink($fifo) if -e $fifo;
196 };
197
198 my $old_int_handler = $SIG{INT};
199 my $old_term_handler = $SIG{TERM};
200
201 $SIG{INT} = sub { $cleanup->('INT'); $old_int_handler->() if ref $old_int_handler; exit 1; };
202 $SIG{TERM} = sub { $cleanup->('TERM'); $old_term_handler->() if ref $old_term_handler; exit 1; };
203 $SIG{PIPE} = sub { exit 1 };
204
205 $SIG{CHLD} = sub {
206 local $?;
207 my $res = waitpid($pid, 0);
208 my $exit = ($? >> 8);
209
210 close($fh) if $fh;
211 unlink($fifo) if -e $fifo;
212
213 # Forward the exit code from our child
214 exit($exit);
215 };
216
217 for (1 .. 10) {
218 last if open($fh, '>', $fifo);
219 die "Could not open fifo ($fifo): $!" unless $! == EINTR;
220 sleep 0.2;
221 }
222 die "Could not open fifo ($fifo): $!" unless $fh;
223
224 $fh->autoflush(1);
225 my $guard = Scope::Guard->new($cleanup);
226
227 while(1) {
228 my $data = <STDIN>;
229 if (defined($data) && length($data)) {
230 print $fh $data;
231 next;
232 }
233
234 next if defined($data);
235
236 next if kill(0, $pid);
237 print STDERR "Lost child process $pid\n";
238 $cleanup->();
239 exit 255;
240 }
241 }
242
243 close(STDIN);
244 open(STDIN, '<', '/dev/null');
245
246 require Time::HiRes;
247 while (! -e $fifo) { Time::HiRes::sleep(0.02) };
248 }
249
250 sub _post_process_version {
251 my %params = @_;
252
253 return unless $params{settings}->debug->version;
254
255 require App::Yath;
256 my $out = <<" EOT";
257
258 Yath version: $App::Yath::VERSION
259
260 Extended Version Info
261 EOT
262
263 my $plugin_libs = find_libraries('App::Yath::Plugin::*');
264
265 my @vers = (
266 [perl => $^V],
267 ['App::Yath' => App::Yath->VERSION],
268 (
269 map {
270 eval { require(mod2file($_)); 1 }
271 ? [$_ => $_->VERSION // 'N/A']
272 : [$_ => 'N/A']
273 } qw/Test2::API Test2::Suite Test::Builder/
274 ),
275 (
276 map {
277 eval { require($plugin_libs->{$_}); 1 }
278 && [$_ => $_->VERSION // 'N/A']
279 } sort keys %$plugin_libs
280 ),
281 );
282
283 $out .= join "\n" => table(
284 header => [qw/COMPONENT VERSION/],
285 rows => \@vers,
286 );
287
288 print "$out\n\n";
289
290 exit 0;
291 }
292
293 1;
294
295 __END__
296
297 =pod
298
299 =encoding UTF-8
300
301 =head1 NAME
302
303 App::Yath::Options::Debug - Debug options for Yath
304
305 =head1 DESCRIPTION
306
307 This is where debug related command line options live.
308
309 =head1 PROVIDED OPTIONS
310
311 =head2 YATH OPTIONS (PRE-COMMAND)
312
313 =head3 Help and Debugging
314
315 =over 4
316
317 =item --show-opts
318
319 =item --no-show-opts
320
321 Exit after showing what yath thinks your options mean
322
323
324 =item --version
325
326 =item -V
327
328 =item --no-version
329
330 Exit after showing a helpful usage message
331
332
333 =back
334
335 =head2 COMMAND OPTIONS
336
337 =head3 Help and Debugging
338
339 =over 4
340
341 =item --dummy
342
343 =item -d
344
345 =item --no-dummy
346
347 Dummy run, do not actually execute anything
348
349 Can also be set with the following environment variables: C<T2_HARNESS_DUMMY>
350
351
352 =item --help
353
354 =item -h
355
356 =item --no-help
357
358 exit after showing help information
359
360
361 =item --interactive
362
363 =item -i
364
365 =item --no-interactive
366
367 Use interactive mode, 1 test at a time, stdin forwarded to it
368
369
370 =item --keep-dirs
371
372 =item --keep_dir
373
374 =item -k
375
376 =item --no-keep-dirs
377
378 Do not delete directories when done. This is useful if you want to inspect the directories used for various commands.
379
380
381 =item --procname-prefix ARG
382
383 =item --procname-prefix=ARG
384
385 =item --no-procname-prefix
386
387 Add a prefix to all proc names (as seen by ps).
388
389
390 =item --summary
391
392 =item --summary=/path/to/summary.json
393
394 =item --no-summary
395
396 Write out a summary json file, if no path is provided 'summary.json' will be used. The .json extension is added automatically if omitted.
397
398
399 =back
400
401 =head1 SOURCE
402
403 The source code repository for Test2-Harness can be found at
404 F<http://github.com/Test-More/Test2-Harness/>.
405
406 =head1 MAINTAINERS
407
408 =over 4
409
410 =item Chad Granum E<lt>[email protected]<gt>
411
412 =back
413
414 =head1 AUTHORS
415
416 =over 4
417
418 =item Chad Granum E<lt>[email protected]<gt>
419
420 =back
421
422 =head1 COPYRIGHT
423
424 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
425
426 This program is free software; you can redistribute it and/or
427 modify it under the same terms as Perl itself.
428
429 See F<http://dev.perl.org/licenses/>
430
431 =cut