Debian Codegraph @

lib/Test2/Harness/Plugin.pm

raw
1 package Test2::Harness::Plugin;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 # Document, but do not implement
8 #sub changed_files {}
9 #sub changed_diff {}
10
11 sub munge_search {}
12
13 sub claim_file {}
14
15 sub munge_files {}
16
17 sub inject_run_data {}
18
19 sub setup {}
20
21 sub teardown {}
22
23 sub TO_JSON { ref($_[0]) || "$_[0]" }
24
25 sub redirect_io {
26 my $this = shift;
27 my ($settings, $name) = @_;
28
29 my @caller = caller();
30 my $at = "at $caller[1] line $caller[2].\n";
31 die "Invalid settings ($settings) $at" unless $settings && ref($settings) eq 'Test2::Harness::Settings';
32 die "No name provided $at" unless $name;
33 die "This cannot be used without a workspace $at" unless $settings->check_prefix('workspace');
34
35 require File::Spec;
36 require Test2::Harness::Util::IPC;
37
38 my $dir = $settings->workspace->workdir;
39 my $aux = File::Spec->catdir($dir, 'aux_logs');
40 mkdir($aux) unless -d $aux;
41
42 Test2::Harness::Util::IPC::swap_io(\*STDOUT, File::Spec->catfile($aux, "${name}-STDOUT.log"));
43 Test2::Harness::Util::IPC::swap_io(\*STDERR, File::Spec->catfile($aux, "${name}-STDERR.log"));
44
45 return;
46 }
47
48 sub shellcall {
49 my $this = shift;
50 my ($settings, $name, @cmd) = @_;
51
52 require POSIX;
53
54 my @caller = caller();
55 my $at = "at $caller[1] line $caller[2].\n";
56 die "Invalid settings ($settings) $at" unless $settings && ref($settings) eq 'Test2::Harness::Settings';
57 die "No name provided $at" unless $name;
58 die "No command provided $at" unless @cmd && length($cmd[0]);
59
60 my $pid = fork // die "Could not fork: $!";
61 if ($pid) {
62 local $?;
63 waitpid($pid, 0);
64 return $?;
65 }
66 else {
67 local $@;
68
69 eval {
70 if ($settings->check_prefix('workspace')) {
71 $this->redirect_io($settings, $name);
72 }
73 exec(@cmd) if @cmd > 1;
74 exec($cmd[0]);
75 };
76
77 chomp(my $err = $@ // "unknown error");
78
79 warn "Could not run command ($@) $at";
80 POSIX::_exit(1);
81 }
82 }
83
84 1;
85
86 __END__
87
88
89 =pod
90
91 =encoding UTF-8
92
93 =head1 NAME
94
95 Test2::Harness::Plugin - Base class for Test2::Harness plugins.
96
97 =head1 DESCRIPTION
98
99 This class holds the methods specific to L<Test2::Harness> which
100 is the backend. Most of the time you actually want to subclass
101 L<App::Yath::Plugin> which subclasses this class, and holds additional methods
102 that apply to yath (the UI layer).
103
104 =head1 SYNOPSIS
105
106 You probably want to subclass L<App::Yath::Plugin> instead. This class here
107 mainly exists to separate concerns, but is not something you should use
108 directly.
109
110 package Test2::Harness::Plugin::MyPlugin;
111
112 use parent 'Test2::Harness::Plugin';
113
114 # ... Define methods
115
116 1;
117
118 =head1 METHODS
119
120 =over 4
121
122 =item $plugin->munge_search($input, $default_search, $settings)
123
124 C<$input> is an arrayref of files and/or directories provided at the command
125 line.
126
127 C<$default_search> is an arrayref with the default files/directories pulled in
128 when nothing is specified at the command line.
129
130 C<$settings> is an instance of L<Test2::Harness::Settings>
131
132 =item $undef_or_inst = $plugin->claim_file($path, $settings)
133
134 This is a chance for a plugin to claim a test file early, before Test2::Harness
135 takes care of it. If your plugin does not want to claim the file just return
136 undef. To claim the file return an instance of L<Test2::Harness::TestFile>
137 created with C<$path>.
138
139 =item $plugin->munge_files(\@tests, $settings)
140
141 This is an opportunity for your plugin to modify the data for any test file
142 that will be run. The first argument is an arrayref of
143 L<Test2::Harness::TestFile> objects.
144
145 =item $hashref = $plugin->duration_data($settings, $test_names)
146
147 If defined, this can return a hashref of duration data. This should return
148 undef if no duration data is provided. The first plugin listed that provides
149 duration data wins, no other plugins will be checked once duration data is
150 obtained.
151
152 Example duration data:
153
154 {
155 't/foo.t' => 'medium',
156 't/bar.t' => 'short',
157 't/baz.t' => 'long',
158 }
159
160 =item $hashref_or_arrayref = $plugin->coverage_data(\@changed)
161
162 =item $hashref_or_arrayref = $plugin->coverage_data()
163
164 If defined, this can return a hashref of all coverage data, or an arrayref of
165 tests that cover the tests listed in @changed. This should return undef if no
166 coverage data is available. The first plugin to provide coverage data wins, no
167 other plugins will be checked once coverage data has been obtained.
168
169 Examples:
170
171 [
172 'foo.t',
173 'bar.t',
174 'baz.t',
175 ]
176
177 {
178 'lib/Foo.pm' => [
179 't/foo.t',
180 't/integration.t',
181 ],
182 'lib/Bar.pm' => [
183 't/bar.t',
184 't/integration.t',
185 ],
186 }
187
188 =item $plugin->post_process_coverage_tests($settings, \@tests)
189
190 This is an opportunity for a plugin to do post-processing on the list of
191 coverage tests to run. This is mainly useful to remove duplicates if multiple
192 plugins add coverage data, or merging entries where applicable. This will be
193 called after all plugins have generated their coverage test list.
194
195 Plugins may implement this without implementing coverage_data(), making this
196 useful if you want to use a pre-existing coverage module and want to do
197 post-processing on what it provides.
198
199 =item $plugin->inject_run_data(meta => $meta, fields => $fields, run => $run)
200
201 This is a callback that lets your plugin add meta-data or custom fields to the
202 run event. The meta-data and fields are available in the event log, and are
203 particularly useful to L<App::Yath::UI>.
204
205 sub inject_run_data {
206 my $class = shift;
207 my %params = @_;
208
209 my $meta = $params{meta};
210 my $fields = $params{fields};
211
212 # Meta-data is a hash, each plugin should define its own key, and put
213 # data under that key
214 $meta->{MyPlugin}->{stuff} = "Stuff!";
215
216 # Fields is an array of fields that a UI might want to display when showing the run.
217 push @$fields => {name => 'MyPlugin', details => "Human Friendly Stuff", raw => "Less human friendly stuff", data => $all_the_stuff};
218
219 return;
220 }
221
222 =item $plugin->setup($settings)
223
224 This is a callback that lets you run setup logic when the runner starts. Note
225 that in a persistent runner this is run once on startup, it is not run for each
226 C<run> command against the persistent runner.
227
228 =item $plugin->teardown($settings)
229
230 This is a callback that lets you run teardown logic when the runner stops. Note
231 that in a persistent runner this is run once on termination, it is not run for
232 each C<run> command against the persistent runner.
233
234 =item @files = $plugin->changed_files($settings)
235
236 Get a list of files that have changed. Plugins are free to define what
237 "changed" means. This may be used by the finder to determine what tests to run
238 based on coverage data collected in previous runs.
239
240 Note that data from all changed_files() calls from all plugins will be merged.
241
242 =item ($type, $value) = $plugin->changed_diff($settings)
243
244 Generate a diff that can be used to calculate changed files/subs for which to
245 run tests. Unlike changed_files(), only 1 diff will be used, first plugin
246 listed that returns one wins. This is not run at all if a diff is provided via
247 --changed-diff.
248
249 Diffs must be in the same format as this git command:
250
251 git diff -U1000000 -W --minimal BASE_BRANCH_OR_COMMIT
252
253 Some other diff formats may work by chance, but they are not directly
254 supported. In the future other diff formats may be directly supported, but not
255 yet.
256
257 The following return sets are allowed:
258
259 =over 4
260
261 =item file => string
262
263 Path to a diff file
264
265 =item diff => string
266
267 In memory diff as a single string
268
269 =item lines => \@lines
270
271 Diff where each line is a separate string in an arrayref.
272
273 =item line_sub => sub { ... }
274
275 Sub that returns one line per call and undef when there are no more lines
276
277 =item handle => $FH
278
279 A filehandle to the diff
280
281 =back
282
283 =item $exit = $plugin->shellcall($settings, $name, $cmd)
284
285 =item $exit = $plugin->shellcall($settings, $name, @cmd)
286
287 This is essentially the same as C<system()> except that STDERR and STDOUT are
288 redirected to files that the yath collector will pick up so that any output
289 from the command will be seen as events and will be part of the yath log. If no
290 workspace is available this will not redirect IO and it will be identical to
291 calling C<system()>.
292
293 This is particularly useful in C<setup()> and C<teardown()> when running
294 external commands, specially any that daemonize and continue to produce output
295 after the setup/teardown method has completed.
296
297 $name is required because it will be used for filenames, and will be used as
298 the output tag (best to limit it to 8 characters).
299
300 =item $plugin->redirect_io($settings, $name)
301
302 B<WARNING:> This must NEVER be called in a primary yath process. Only use this
303 in forked processes that you control. If this is used in a main process it
304 could hide ALL output.
305
306 This will redirect STDERR and STDOUT to files that will be picked up by the
307 yath collector so that any output appears as proper yath events and will be
308 included in the yath log.
309
310 $name is required because it will be used for filenames, and will be used as
311 the output tag (best to limit it to 8 characters).
312
313 =item $plugin->TO_JSON
314
315 This is here as a bare minimum serialization method. It returns the plugin
316 class name.
317
318 =back
319
320 =head1 SOURCE
321
322 The source code repository for Test2-Harness can be found at
323 F<http://github.com/Test-More/Test2-Harness/>.
324
325 =head1 MAINTAINERS
326
327 =over 4
328
329 =item Chad Granum E<lt>[email protected]<gt>
330
331 =back
332
333 =head1 AUTHORS
334
335 =over 4
336
337 =item Chad Granum E<lt>[email protected]<gt>
338
339 =back
340
341 =head1 COPYRIGHT
342
343 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
344
345 This program is free software; you can redistribute it and/or
346 modify it under the same terms as Perl itself.
347
348 See F<http://dev.perl.org/licenses/>
349
350 =cut