Debian Codegraph @

t/integration/reload.t

raw
1 use Test2::V0;
2 use Test2::Require::AuthorTesting;
3
4 use File::Temp qw/tempdir/;
5 use File::Spec;
6 use Time::HiRes qw/sleep/;
7
8 use App::Yath::Tester qw/yath/;
9 use Test2::Harness::Util::File::JSONL;
10 use Test2::Harness::Util qw/clean_path/;
11
12 use Test2::Harness::Util::JSON qw/decode_json/;
13
14 my $dir = __FILE__;
15 $dir =~ s{\.t$}{}g;
16 $dir =~ s{^\./}{};
17 my $fqdir = clean_path($dir);
18 my $pdir = $fqdir;
19 $pdir =~ s{\W{0,2}t\W{1,2}integration\W{1,2}reload$}{}g;
20
21 my $tmpdir = tempdir(CLEANUP => 1);
22 mkdir("$tmpdir/Preload") or die "($tmpdir/Preload) $!";
23
24 # The runner has to see each touch as its own change, so the gap between them
25 # cannot be closed entirely. What sets it:
26 #
27 # 1. The preloader does not check for changes more than once a second.
28 # 2. The stage has to finish reloading before the next file changes, or two
29 # changes collapse into one reload.
30 #
31 # Whole-second mtimes used to be a third constraint: stat reports seconds, so
32 # two touches landing in the same second looked identical and the gap had to
33 # clear a second boundary with margin. Giving each touch an mtime past what
34 # the file already had removes that, so the gap only has to cover 1 and 2.
35 sub bump_mtime {
36 my ($path) = @_;
37 my $stamp = (stat($path))[9] + 1;
38 utime($stamp, $stamp, $path) or die "Could not set mtime on '$path': $!";
39 }
40
41 sub touch_files {
42 note "About to touch files with a delay between each, this will take a while";
43
44 for my $file (qw/A B A B ExceptionA ExceptionB WarningA WarningB ExporterA ExporterB IncChange Churn nonperl1 nonperl2/) {
45 my $path = "$dir/lib/Preload/${file}";
46 $path .= '.pm' unless $file =~ m/nonperl/;
47 note "Touching $file...";
48 sleep 1.25;
49
50 if ($file eq 'IncChange') {
51 open(my $fh, '>', "$tmpdir/Preload/IncChange.pm") or die $!;
52
53 print $fh <<' EOT';
54 package Preload::IncChange;
55 use strict;
56 use warnings;
57
58 BEGIN {
59 print "$$ $0 Loaded (DIFFERENT) ${ \__PACKAGE__ }\n";
60 }
61
62 1;
63 EOT
64
65 close($fh);
66 }
67
68 bump_mtime($path);
69 }
70
71 sleep 1.25;
72 }
73
74 sub parse_output {
75 my ($output) = @_;
76
77 # On macOS, these days, /var is actually a symlink to /private/var.
78 # Somewhere along the lines, something is turning /var into /private/var in
79 # the runner, which makes the "strip out the tmpdir" code (marked below)
80 # leave behind a /private. This fix is inelegant, but probably fixes the
81 # overwhelming majority of macOS test failures without introducing any
82 # further problems. A better fix might be to track down and eliminate the
83 # rewriting of the path, or to uniformly make this check match the behavior
84 # under the hood. For now: let's just let macOS users install
85 # TAP2::Harness! -- rjbs, 2022-02-20
86 my $safe_tmpdir = $tmpdir;
87 if ($safe_tmpdir =~ m{\A/var/} && -l '/var') {
88 my $target = File::Spec->rel2abs(readlink('/var'), '/');
89 $target =~ s{/\z}{};
90 $safe_tmpdir =~ s{\A/var}{$target};
91 }
92
93 my %by_proc;
94 for my $line (split /\n/, $output) {
95 next unless $line =~ m/^\s*(\d+) yath-nested-runner(?:-(\S+))? - (.+)$/;
96 my ($pid, $proc, $text) = ($1, $2, $3);
97 $proc //= '';
98 $text =~ s/$pid yath-nested-runner-$proc(\s*-\s*)//g;
99 $text =~ s{(\Q$fqdir\E|\Q$dir\E|\Q$pdir\E)/*}{}g;
100
101 $text =~ s{\Q$safe_tmpdir\E(/)?}{TEMP$1}g; # <-- strip out the tmpdir
102
103 $text =~ s{ line \d+.*$}{}g;
104 push @{$by_proc{$proc || 'default'}} => $text;
105 }
106
107 return \%by_proc;
108 }
109
110 subtest no_in_place => sub {
111 unlink("$tmpdir/Preload/IncChange.pm") if -e "$tmpdir/Preload/IncChange.pm";
112
113 yath(
114 command => 'start',
115 args => ['-PPreload'],
116 pre => ["-D$tmpdir"],
117 exit => 0,
118 );
119
120 touch_files();
121
122 yath(
123 command => 'watch',
124 args => ['STOP'],
125 exit => 0,
126 test => sub {
127 my $out = shift;
128
129 my $parsed = parse_output($out->{output});
130 is(
131 $parsed,
132 {
133 'default' => [
134 'Loaded Preload',
135 ],
136 'A' => [
137 'Loaded Preload::A',
138 'Loaded Preload::WarningA',
139 'Loaded Preload::ExceptionA',
140 'Loaded Preload::ExporterA',
141 'Churn 1',
142 'FOO: foo 1',
143 'Churn 2',
144 'Churn 3',
145 'Runner detected a change in one or more preloaded modules...',
146 'Runner detected changes in file \'lib/Preload/A.pm\'...',
147 'blacklisting changed files and reloading stage...',
148 'Blacklisting Preload::A...',
149 'Loaded Preload::WarningA',
150 'Loaded Preload::ExceptionA',
151 'Loaded Preload::ExporterA',
152 'Churn 1',
153 'FOO: foo 1',
154 'Churn 2',
155 'Churn 3',
156 'Runner detected a change in one or more preloaded modules...',
157 'Runner detected changes in file \'lib/Preload/ExceptionA.pm\'...',
158 'blacklisting changed files and reloading stage...',
159 'Blacklisting Preload::ExceptionA...',
160 'Loaded Preload::WarningA',
161 'Loaded Preload::ExporterA',
162 'Churn 1',
163 'FOO: foo 1',
164 'Churn 2',
165 'Churn 3',
166 'Runner detected a change in one or more preloaded modules...',
167 'Runner detected changes in file \'lib/Preload/WarningA.pm\'...',
168 'blacklisting changed files and reloading stage...',
169 'Blacklisting Preload::WarningA...',
170 'Loaded Preload::ExporterA',
171 'Churn 1',
172 'FOO: foo 1',
173 'Churn 2',
174 'Churn 3',
175 'Runner detected a change in one or more preloaded modules...',
176 'Runner detected changes in file \'lib/Preload/ExporterA.pm\'...',
177 'blacklisting changed files and reloading stage...',
178 'Blacklisting Preload::ExporterA...',
179 'Churn 1',
180 'FOO: foo 1',
181 'Churn 2',
182 'Churn 3',
183 'Runner detected a change in one or more preloaded modules...',
184 'Runner detected changes in file \'lib/Preload/Churn.pm\'...',
185 'Changed file \'Preload/Churn.pm\' contains churn sections, running them instead of a full reload...',
186 'Churn 1',
187 'FOO: foo 2',
188 'Success reloading churn block (lib/Preload/Churn.pm lines 8 -> 16)',
189 'Churn 2',
190 'Success reloading churn block (lib/Preload/Churn.pm lines 18 -> 20)',
191 'Error reloading churn block (lib/Preload/Churn.pm lines 22 -> 28): Died on count 3',
192 'Runner detected a change in one or more preloaded modules...',
193 'Runner detected changes in file \'lib/Preload/nonperl1\'...',
194 'Changed file \'lib/Preload/nonperl1\' has a reload callback, executing it instead of regular reloading...',
195 'RELOAD CALLBACK nonperl1',
196 'Runner detected a change in one or more preloaded modules...',
197 'Runner detected changes in file \'lib/Preload/nonperl2\'...',
198 'Changed file \'lib/Preload/nonperl2\' has a reload callback, executing it instead of regular reloading...',
199 'RELOAD CALLBACK nonperl2',
200 ],
201 'B' => [
202 'Loaded Preload::A',
203 'Loaded Preload::WarningA',
204 'Loaded Preload::ExceptionA',
205 'Loaded Preload::ExporterA',
206 'Loaded Preload::B',
207 'Loaded Preload::WarningB',
208 'Loaded Preload::ExceptionB',
209 'Loaded Preload::ExporterB',
210 'Loaded Preload::IncChange',
211 'Runner detected a change in one or more preloaded modules...',
212 'Runner detected changes in file \'lib/Preload/A.pm\'...',
213 'blacklisting changed files and reloading stage...',
214 'Blacklisting Preload::A...',
215 'Loaded Preload::WarningA',
216 'Loaded Preload::ExceptionA',
217 'Loaded Preload::ExporterA',
218 'Loaded Preload::B',
219 'Loaded Preload::WarningB',
220 'Loaded Preload::ExceptionB',
221 'Loaded Preload::ExporterB',
222 'Loaded Preload::IncChange',
223 'Runner detected a change in one or more preloaded modules...',
224 'Runner detected changes in file \'lib/Preload/B.pm\'...',
225 'blacklisting changed files and reloading stage...',
226 'Blacklisting Preload::B...',
227 'Loaded Preload::WarningA',
228 'Loaded Preload::ExceptionA',
229 'Loaded Preload::ExporterA',
230 'Loaded Preload::WarningB',
231 'Loaded Preload::ExceptionB',
232 'Loaded Preload::ExporterB',
233 'Loaded Preload::IncChange',
234 'Runner detected a change in one or more preloaded modules...',
235 'Runner detected changes in file \'lib/Preload/ExceptionA.pm\'...',
236 'blacklisting changed files and reloading stage...',
237 'Blacklisting Preload::ExceptionA...',
238 'Loaded Preload::WarningA',
239 'Loaded Preload::ExporterA',
240 'Loaded Preload::WarningB',
241 'Loaded Preload::ExceptionB',
242 'Loaded Preload::ExporterB',
243 'Loaded Preload::IncChange',
244 'Runner detected a change in one or more preloaded modules...',
245 'Runner detected changes in file \'lib/Preload/ExceptionB.pm\'...',
246 'blacklisting changed files and reloading stage...',
247 'Blacklisting Preload::ExceptionB...',
248 'Loaded Preload::WarningA',
249 'Loaded Preload::ExporterA',
250 'Loaded Preload::WarningB',
251 'Loaded Preload::ExporterB',
252 'Loaded Preload::IncChange',
253 'Runner detected a change in one or more preloaded modules...',
254 'Runner detected changes in file \'lib/Preload/WarningA.pm\'...',
255 'blacklisting changed files and reloading stage...',
256 'Blacklisting Preload::WarningA...',
257 'Loaded Preload::ExporterA',
258 'Loaded Preload::WarningB',
259 'Loaded Preload::ExporterB',
260 'Loaded Preload::IncChange',
261 'Runner detected a change in one or more preloaded modules...',
262 'Runner detected changes in file \'lib/Preload/WarningB.pm\'...',
263 'blacklisting changed files and reloading stage...',
264 'Blacklisting Preload::WarningB...',
265 'Loaded Preload::ExporterA',
266 'Loaded Preload::ExporterB',
267 'Loaded Preload::IncChange',
268 'Runner detected a change in one or more preloaded modules...',
269 'Runner detected changes in file \'lib/Preload/ExporterA.pm\'...',
270 'blacklisting changed files and reloading stage...',
271 'Blacklisting Preload::ExporterA...',
272 'Loaded Preload::ExporterB',
273 'Loaded Preload::IncChange',
274 'Runner detected a change in one or more preloaded modules...',
275 'Runner detected changes in file \'lib/Preload/ExporterB.pm\'...',
276 'blacklisting changed files and reloading stage...',
277 'Blacklisting Preload::ExporterB...',
278 'Loaded Preload::IncChange',
279 'Runner detected a change in one or more preloaded modules...',
280 'Runner detected changes in file \'lib/Preload/IncChange.pm\'...',
281 'blacklisting changed files and reloading stage...',
282 'Blacklisting Preload::IncChange...',
283 ],
284 },
285 "Reload happened as expected",
286 );
287 },
288 );
289
290 yath(command => 'stop', exit => 0);
291 };
292
293 subtest in_place => sub {
294 unlink("$tmpdir/Preload/IncChange.pm") if -e "$tmpdir/Preload/IncChange.pm";
295
296 yath(
297 command => 'start',
298 args => ['-PPreload', '-r'],
299 pre => ["-D$tmpdir"],
300 exit => 0,
301 );
302
303 touch_files();
304
305 yath(
306 command => 'watch',
307 args => ['STOP'],
308 exit => 0,
309 test => sub {
310 my $out = shift;
311
312 my $parsed = parse_output($out->{output});
313 is(
314 $parsed,
315 {
316 'default' => [
317 'Loaded Preload',
318 ],
319 'A' => [
320 'Loaded Preload::A',
321 'Loaded Preload::WarningA',
322 'Loaded Preload::ExceptionA',
323 'Loaded Preload::ExporterA',
324 'Churn 1',
325 'FOO: foo 1',
326 'Churn 2',
327 'Churn 3',
328 'Runner detected a change in one or more preloaded modules...',
329 'Runner detected changes in file \'lib/Preload/A.pm\'...',
330 'Runner attempting to reload \'lib/Preload/A.pm\' in place...',
331 'Loaded Preload::A',
332 'Runner detected a change in one or more preloaded modules...',
333 'Runner detected changes in file \'lib/Preload/A.pm\'...',
334 'Runner attempting to reload \'lib/Preload/A.pm\' in place...',
335 'Loaded Preload::A',
336 'Runner detected a change in one or more preloaded modules...',
337 'Runner detected changes in file \'lib/Preload/ExceptionA.pm\'...',
338 'Runner attempting to reload \'lib/Preload/ExceptionA.pm\' in place...',
339 'Loaded Preload::ExceptionA',
340 'Runner failed to reload \'lib/Preload/ExceptionA.pm\' in place...',
341 'Loaded Preload::ExceptionA again.',
342 'BEGIN failed--compilation aborted at lib/Preload/ExceptionA.pm',
343 'Compilation failed in require at lib/Test2/Harness/Runner/Reloader.pm',
344 'Runner detected a change in one or more preloaded modules...',
345 'Runner detected changes in file \'lib/Preload/WarningA.pm\'...',
346 'Runner attempting to reload \'lib/Preload/WarningA.pm\' in place...',
347 'Loaded Preload::WarningA',
348 'Runner failed to reload \'lib/Preload/WarningA.pm\' in place...',
349 'Loaded Preload::WarningA again.',
350 'Runner detected a change in one or more preloaded modules...',
351 'Runner detected changes in file \'lib/Preload/ExporterA.pm\'...',
352 'blacklisting changed files and reloading stage...',
353 'Blacklisting Preload::ExporterA...',
354 'Loaded Preload::A',
355 'Loaded Preload::WarningA',
356 'Loaded Preload::ExceptionA',
357 'Churn 1',
358 'FOO: foo 1',
359 'Churn 2',
360 'Churn 3',
361 'Runner detected a change in one or more preloaded modules...',
362 'Runner detected changes in file \'lib/Preload/Churn.pm\'...',
363 'Changed file \'Preload/Churn.pm\' contains churn sections, running them instead of a full reload...',
364 'Churn 1',
365 'FOO: foo 2',
366 'Success reloading churn block (lib/Preload/Churn.pm lines 8 -> 16)',
367 'Churn 2',
368 'Success reloading churn block (lib/Preload/Churn.pm lines 18 -> 20)',
369 'Error reloading churn block (lib/Preload/Churn.pm lines 22 -> 28): Died on count 3',
370 'Runner detected a change in one or more preloaded modules...',
371 'Runner detected changes in file \'lib/Preload/nonperl1\'...',
372 'Changed file \'lib/Preload/nonperl1\' has a reload callback, executing it instead of regular reloading...',
373 'RELOAD CALLBACK nonperl1',
374 'Runner detected a change in one or more preloaded modules...',
375 'Runner detected changes in file \'lib/Preload/nonperl2\'...',
376 'Changed file \'lib/Preload/nonperl2\' has a reload callback, executing it instead of regular reloading...',
377 'RELOAD CALLBACK nonperl2'
378 ],
379 'B' => [
380 'Loaded Preload::A',
381 'Loaded Preload::WarningA',
382 'Loaded Preload::ExceptionA',
383 'Loaded Preload::ExporterA',
384 'Loaded Preload::B',
385 'Loaded Preload::WarningB',
386 'Loaded Preload::ExceptionB',
387 'Loaded Preload::ExporterB',
388 'Loaded Preload::IncChange',
389 'Runner detected a change in one or more preloaded modules...',
390 'Runner detected changes in file \'lib/Preload/A.pm\'...',
391 'Runner attempting to reload \'lib/Preload/A.pm\' in place...',
392 'Loaded Preload::A',
393 'Runner detected a change in one or more preloaded modules...',
394 'Runner detected changes in file \'lib/Preload/B.pm\'...',
395 'Runner attempting to reload \'lib/Preload/B.pm\' in place...',
396 'Loaded Preload::B',
397 'Runner detected a change in one or more preloaded modules...',
398 'Runner detected changes in file \'lib/Preload/A.pm\'...',
399 'Runner attempting to reload \'lib/Preload/A.pm\' in place...',
400 'Loaded Preload::A',
401 'Runner detected a change in one or more preloaded modules...',
402 'Runner detected changes in file \'lib/Preload/B.pm\'...',
403 'Runner attempting to reload \'lib/Preload/B.pm\' in place...',
404 'Loaded Preload::B',
405 'Runner detected a change in one or more preloaded modules...',
406 'Runner detected changes in file \'lib/Preload/ExceptionA.pm\'...',
407 'Runner attempting to reload \'lib/Preload/ExceptionA.pm\' in place...',
408 'Loaded Preload::ExceptionA',
409 'Runner failed to reload \'lib/Preload/ExceptionA.pm\' in place...',
410 'Loaded Preload::ExceptionA again.',
411 'BEGIN failed--compilation aborted at lib/Preload/ExceptionA.pm',
412 'Compilation failed in require at lib/Test2/Harness/Runner/Reloader.pm',
413 'Runner detected a change in one or more preloaded modules...',
414 'Runner detected changes in file \'lib/Preload/ExceptionB.pm\'...',
415 'Runner attempting to reload \'lib/Preload/ExceptionB.pm\' in place...',
416 'Loaded Preload::ExceptionB',
417 'Runner failed to reload \'lib/Preload/ExceptionB.pm\' in place...',
418 'Loaded Preload::ExceptionB again.',
419 'BEGIN failed--compilation aborted at lib/Preload/ExceptionB.pm',
420 'Compilation failed in require at lib/Test2/Harness/Runner/Reloader.pm',
421 'Runner detected a change in one or more preloaded modules...',
422 'Runner detected changes in file \'lib/Preload/WarningA.pm\'...',
423 'Runner attempting to reload \'lib/Preload/WarningA.pm\' in place...',
424 'Loaded Preload::WarningA',
425 'Runner failed to reload \'lib/Preload/WarningA.pm\' in place...',
426 'Loaded Preload::WarningA again.',
427 'Runner detected a change in one or more preloaded modules...',
428 'Runner detected changes in file \'lib/Preload/WarningB.pm\'...',
429 'Runner attempting to reload \'lib/Preload/WarningB.pm\' in place...',
430 'Loaded Preload::WarningB',
431 'Runner failed to reload \'lib/Preload/WarningB.pm\' in place...',
432 'Loaded Preload::WarningB again.',
433 'Runner detected a change in one or more preloaded modules...',
434 'Runner detected changes in file \'lib/Preload/ExporterA.pm\'...',
435 'blacklisting changed files and reloading stage...',
436 'Blacklisting Preload::ExporterA...',
437 'Loaded Preload::A',
438 'Loaded Preload::WarningA',
439 'Loaded Preload::ExceptionA',
440 'Loaded Preload::B',
441 'Loaded Preload::WarningB',
442 'Loaded Preload::ExceptionB',
443 'Loaded Preload::ExporterB',
444 'Loaded Preload::IncChange',
445 'Runner detected a change in one or more preloaded modules...',
446 'Runner detected changes in file \'lib/Preload/ExporterB.pm\'...',
447 'blacklisting changed files and reloading stage...',
448 'Blacklisting Preload::ExporterB...',
449 'Loaded Preload::A',
450 'Loaded Preload::WarningA',
451 'Loaded Preload::ExceptionA',
452 'Loaded Preload::B',
453 'Loaded Preload::WarningB',
454 'Loaded Preload::ExceptionB',
455 'Loaded Preload::IncChange',
456 'Runner detected a change in one or more preloaded modules...',
457 'Runner detected changes in file \'lib/Preload/IncChange.pm\'...',
458 'Runner attempting to reload \'lib/Preload/IncChange.pm\' in place...',
459 'Runner failed to reload \'lib/Preload/IncChange.pm\' in place...',
460 'Reloading \'Preload/IncChange.pm\' loaded \'TEMP/Preload/IncChange.pm\' instead of \'lib/Preload/IncChange.pm\', @INC must have been altered at lib/Test2/Harness/Runner/Reloader.pm'
461 ],
462 },
463 "Reload happened as expected",
464 );
465 },
466 );
467
468 yath(command => 'stop', exit => 0);
469 };
470
471 done_testing;