blib/lib/App/TestOnTap/WorkDirManager.pm

filter: Statementstmt 98.0% 153 / 156 Branchbran 58.3% 21 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 34 / 34 Podpod 0.0% 0 / 17 totaltotal 85.6% 208 / 243 SCAR 30.6
CC
21
Cov
91%
new
8.8
endTestRun
5.0
__ANON__
4.1
CRAP 21.4
« blib/lib/App/TestOnTap/Config.pm ↑ Summary blib/lib/App/TestOnTap/Harness.pm »
1 package App::TestOnTap::WorkDirManager;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use App::TestOnTap::Util qw(slashify stringifyTime $IS_WINDOWS);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 ▶use Archive::Zip qw(:ERROR_CODES);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use File::Copy::Recursive qw(dircopy);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use File::Slurp qw(write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 19 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use Net::Domain qw(hostfqdn);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use POSIX qw(uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # CTOR
20 #
21 sub new
22 {
23 27 ▶ my $class = shift;
Subroutine new: called CC=6 SCAR=21.8
Pod undocumented
24 27 my $args = shift;
25 27 my $workdir = shift;
26 27 my $suiteRoot = shift;
27
28 27 ▶ if ($workdir)
Branch: 1 branch
BranchTrueFalseTotal
if ($workdir) { } 0 27 27
29 {
30 # if user specifies a workdir this implies that it should be kept
31 # just make sure there is no such directory beforehand, and create it here
32 # (similar to below; tempdir() will also create one)
33 #
34 0 $workdir = slashify(File::Spec->rel2abs($workdir));
35 0 ▶ die("The workdir '$workdir' already exists\n") if -e $workdir;
Branch: 1 branch
BranchTrueFalseTotal
if -e $workdir 0 0 0
36 0 ▶ mkpath($workdir) or die("Failed to create workdir '$workdir': $!\n");
Branch: 1 branch
BranchTrueFalseTotal
mkpath($workdir) or die "Failed to create workdir '${workdir}': $!\n" 0 0 0
37 }
38 else
39 {
40 # create a temp dir; use automatic cleanup
41 #
42 27 $workdir = slashify(tempdir("testontap-workdir-XXXX", TMPDIR => 1, CLEANUP => 1));
43 }
44  
45 27 my $self = bless
46 (
47 {
48 args => $args,
49 suiteroot => $suiteRoot,
50 root => $workdir,
51 tmp => slashify("$workdir/tmp"),
52 save => slashify("$workdir/save"),
53 save_suite => slashify("$workdir/save/suite"),
54 save_testontap => slashify("$workdir/save/testontap"),
55 tap => slashify("$workdir/save/testontap/tap"),
56 result => slashify("$workdir/save/testontap/result"),
57 json => JSON->new()->utf8()->pretty()->canonical(),
58 orderstrategy => undef,
59 dispensedorder => [],
60 foundtests => [],
61 commandlines => {},
62 fullgraph => undef,
63 prunedgraph => undef,
64 preprocess => undef,
65 },
66 $class
67 );
68  
69 27 foreach my $p (qw(tmp save save_suite save_testontap tap result))
70 {
71 162 ▶ mkpath($self->{$p}) || die("Failed to mkdir '$self->{$p}': $!\n");
Branch: 1 branch
BranchTrueFalseTotal
mkpath($self->{$p}) or die "Failed to mkdir '$self->{$p}': $!\n" 0 162 162
72 }
73  
74 27 return $self;
75 }
76  
77 sub beginTestRun
78 {
79 25 ▶ my $self = shift;
Subroutine beginTestRun: called CC=1 SCAR=0.0
Pod undocumented
80
81 25 $self->{begin} = time();
82
83 25 $self->__save("$self->{save_testontap}/env", { %ENV });
84 }
85  
86 sub endTestRun
87 {
88 25 ▶ my $self = shift;
Subroutine endTestRun: called CC=5 SCAR=16.1
Pod undocumented
89 25 my $args = shift;
90 25 my $aggregator = shift;
91
92 25 $self->{end} = time();
93 25 $self->{runid} = $args->getId();
94  
95 25 ▶ my $summary =
Branch: 1 branch
BranchTrueFalseTotal
$aggregator->all_passed ? : 22 3 25
96 {
97 all_passed => $aggregator->all_passed() ? 1 : 0,
98 status => $aggregator->get_status(),
99 failed => [ $aggregator->failed() ],
100 parse_errors => [ $aggregator->parse_errors() ],
101 passed => [ $aggregator->passed() ],
102 planned => [ $aggregator->planned() ],
103 skipped => [ $aggregator->skipped() ],
104 todo => [ $aggregator->todo() ],
105 todo_passed => [ $aggregator->todo_passed() ],
106 };
107 25 $self->__save("$self->{save_testontap}/summary", $summary);
108  
109 my $testinfo =
110 {
111 config => $self->{args}->getConfig()->getRawCfg(),
112 dispensedorder => $self->{dispensedorder},
113 found => $self->{foundtests},
114 commandlines => $self->{commandlines},
115 fullgraph => $self->{fullgraph},
116 prunedgraph => $self->{prunedgraph},
117 25 };
118 25 $self->__save("$self->{save_testontap}/testinfo", $testinfo);
119  
120 25 my $elapsed = $aggregator->elapsed();
121 my $meta =
122 {
123 format => { major => 1, minor => 0 }, # Change when format of result tree is changed in any way.
124 runid => $args->getId(),
125 suiteid => $args->getConfig()->getId(),
126 suitename => basename($args->getSuiteRoot()),
127 begin => stringifyTime($self->{begin}),
128 end => stringifyTime($self->{end}),
129 elapsed =>
130 {
131 str => $aggregator->elapsed_timestr(),
132 real => $elapsed->real(),
133 cpu => $elapsed->cpu_a(),
134 },
135 user => $IS_WINDOWS ? getlogin() : scalar(getpwuid($<)),
136 host => hostfqdn(),
137 jobs => $args->getJobs(),
138 dollar0 => slashify(File::Spec->rel2abs($0)),
139 argv => $args->getFullArgv(),
140 defines => $args->getDefines(),
141 platform => $^O,
142 uname => [ uname() ],
143 25 ▶ order => $self->{orderstrategy} ? $self->{orderstrategy}->getStrategyName() : undef,
Branch: 2 branches
BranchTrueFalseTotal
$App::TestOnTap::WorkDirManager::IS_WINDOWS ? : 0 25 25
$self->{'orderstrategy'} ? : 23 2 25
144 };
145 25 $self->__save("$self->{save_testontap}/meta", $meta);
146  
147 25 ▶ $self->__saveText("$self->{save_testontap}/preprocess", $self->{preprocess}) if $self->{preprocess};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"preprocess"} 1 24 25
148 }
149  
150 # retain the tap handles we issue so we can 'manually' close them
151 # this can be necessary during a bailout on windows, where the
152 # spool handle closing is not called, and the automatic cleanup
153 # of temp stuff spouts errors to delete a file due to it having an
154 # open handle to it.
155 #
156 # note that putting the handle as a key stringifies it, so we
157 # must use the actual value when closing, not the string...
158 #
159 19 my %tapHandles;
160 END
161 {
162 19 ▶ close($tapHandles{$_}) foreach (keys(%tapHandles));
Subroutine END: called CC=2 SCAR=6.9
163 }
164  
165 sub openTAPHandle
166 {
167 47 ▶ my $self = shift;
Subroutine openTAPHandle: called CC=2 SCAR=6.9
Pod undocumented
168 47 my $testPath = slashify(shift, '/');
169
170 47 my $sr = slashify($self->{suiteroot}, '/');
171 47 $testPath =~ s#^\Q$sr\E/(.*)#$1#;
172 47 my $tapPath = slashify("$self->{tap}/$testPath.tap");
173 47 mkpath(dirname($tapPath));
174 47 ▶ open(my $h, '>', $tapPath) or die("Failed to open '$tapPath': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $h, ">", $tapPath or die "Failed to open '${tapPath}': $!" 0 47 47
175  
176 # save the handle in the list, forcibly stringify it as key and
177 # save the actual value
178 #
179 47 $tapHandles{"$h"} = $h;
180
181 47 return $h;
182 }
183  
184 sub closeTAPHandle
185 {
186 47 ▶ my $self = shift;
Subroutine closeTAPHandle: called CC=2 SCAR=7.0
Pod undocumented
187 47 my $parser = shift;
188
189 47 my $spool_handle = $parser->delete_spool;
190 47 ▶ if ($spool_handle)
Branch: 1 branch
BranchTrueFalseTotal
if ($spool_handle) 47 0 47
191 {
192 47 close($spool_handle);
193
194 # don't forget to remove the key/value in the list
195 # using the stringified version of the handle!
196 #
197 47 delete($tapHandles{"$spool_handle"});
198 }
199
200 47 return;
201 }
202  
203 sub getResultCollector
204 {
205 27 ▶ my $self = shift;
Subroutine getResultCollector: called CC=1 SCAR=0.0
Pod undocumented
206
207 return
208 sub
209 {
210 47 ▶ my $pathAndNamePair = shift;
Subroutine __ANON__: called CC=4 SCAR=14.2
211 47 my $parser = shift;
212  
213 47 ▶ my %results =
Branch: 3 branches
BranchTrueFalseTotal
$parser->has_problems ? : 1 46 47
$parser->is_good_plan ? : 47 0 47
$parser->skip_all ? : 0 47 47
214 (
215 # individual test results
216 #
217 passed => [ $parser->passed() ],
218 actual_passed => [ $parser->actual_passed() ],
219 failed => [ $parser->failed() ],
220 actual_failed => [ $parser->actual_failed() ],
221 todo => [ $parser->todo() ],
222 todo_passed => [ $parser->failed() ],
223 skipped => [ $parser->skipped() ],
224
225 # total test results
226 #
227 has_problems => $parser->has_problems() ? 1 : 0,
228 plan => $parser->plan(),
229 is_good_plan => $parser->is_good_plan() ? 1 : 0,
230 tests_planned => $parser->tests_planned(),
231 tests_run => $parser->tests_run(),
232 skip_all => ($parser->skip_all() ? $parser->skip_all() : 0),
233 start_time => stringifyTime($parser->start_time()),
234 end_time => stringifyTime($parser->end_time()),
235 version => $parser->version(),
236 'exit' => $parser->exit(),
237 parse_errors => [ $parser->parse_errors() ],
238 );
239
240 47 $self->__save("$self->{result}/$pathAndNamePair->[1]", \%results);
241 27 };
242 }
243  
244 sub saveResult
245 {
246 2 ▶ my $self = shift;
Subroutine saveResult: called CC=4 SCAR=13.9
Pod undocumented
247 2 my $resultDir = shift;
248 2 my $asArchive = shift;
249  
250 2 my $pfx = basename($self->{suiteroot});
251 2 my $runid = $self->{runid};
252 2 my $ts = stringifyTime($self->{begin});
253 2 my $name = "$pfx.$ts.$runid";
254 2 my $from = slashify($self->{save});
255  
256 2 my $to;
257 2 ▶ if ($asArchive)
Branch: 1 branch
BranchTrueFalseTotal
if ($asArchive) { } 1 1 2
258 {
259 1 $to = slashify("$resultDir/$name.zip");
260 1 my $zip = Archive::Zip->new();
261 1 $zip->addTree($from, $name);
262 1 my $err = $zip->writeToFileNamed($to);
263 1 ▶ die("Failed to write archive '$to': $!\n") if $err != AZ_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $err != 0 0 1 1
264 }
265 else
266 {
267 1 $to = slashify("$resultDir/$name");
268 {
269 1 local $File::Copy::Recursive::KeepMode = 0;
270 1 ▶ die("Failed to copy result '$from' => '$to': $!\n") unless dircopy($from, $to);
Branch: 1 branch
BranchTrueFalseTotal
unless dircopy($from, $to) 0 1 1
271 }
272 }
273
274 2 return $to;
275 }
276  
277 sub getTmp
278 {
279 26 ▶ my $self = shift;
Subroutine getTmp: called CC=1 SCAR=0.0
Pod undocumented
280
281 26 return $self->{tmp};
282 }
283  
284 sub getSaveSuite
285 {
286 26 ▶ my $self = shift;
Subroutine getSaveSuite: called CC=1 SCAR=0.0
Pod undocumented
287
288 26 return $self->{save_suite};
289 }
290  
291 sub recordOrderStrategy
292 {
293 55 ▶ my $self = shift;
Subroutine recordOrderStrategy: called CC=1 SCAR=0.0
Pod undocumented
294 55 my $orderstrategy = shift;
295
296 55 $self->{orderstrategy} = $orderstrategy;
297 }
298  
299 sub recordDispensedOrder
300 {
301 55 ▶ my $self = shift;
Subroutine recordDispensedOrder: called CC=1 SCAR=0.0
Pod undocumented
302 55 my @dispensed = @_;
303
304 55 push(@{$self->{dispensedorder}}, @dispensed);
305 }
306  
307 sub recordFoundTests
308 {
309 27 ▶ my $self = shift;
Subroutine recordFoundTests: called CC=1 SCAR=0.0
Pod undocumented
310 27 my @foundTests = @_;
311
312 27 push(@{$self->{foundtests}}, @foundTests);
313 }
314  
315 sub recordFullGraph
316 {
317 27 ▶ my $self = shift;
Subroutine recordFullGraph: called CC=1 SCAR=0.0
Pod undocumented
318 27 my %fullgraph = @_;
319
320 27 $self->{fullgraph} = \%fullgraph;
321 }
322  
323 sub recordPrunedGraph
324 {
325 2 ▶ my $self = shift;
Subroutine recordPrunedGraph: called CC=1 SCAR=0.0
Pod undocumented
326 2 my %prunedgraph = @_;
327
328 2 $self->{prunedgraph} = \%prunedgraph;
329 }
330  
331 sub recordPreprocess
332 {
333 1 ▶ my $self = shift;
Subroutine recordPreprocess: called CC=1 SCAR=0.0
Pod undocumented
334 1 my $preproc = shift;
335
336 1 $self->{preprocess} = $preproc;
337 }
338  
339 sub recordPostprocess
340 {
341 1 ▶ my $self = shift;
Subroutine recordPostprocess: called CC=1 SCAR=0.0
Pod undocumented
342 1 my $postproc = shift;
343
344 1 $self->__saveText("$self->{save_testontap}/postprocess", $postproc);
345 }
346  
347 sub recordCommandLine
348 {
349 48 ▶ my $self = shift;
Subroutine recordCommandLine: called CC=1 SCAR=0.0
Pod undocumented
350 48 my $test = shift;
351 48 my $cmdline = shift;
352
353 48 $self->{commandlines}->{$test} = $cmdline;
354 }
355  
356 sub __save
357 {
358 147 ▶ my $self = shift;
Subroutine __save: called CC=2 SCAR=7.0
359 147 my $name = shift;
360 147 my $data = shift;
361
362 147 my $file = slashify("$name.json");
363 147 mkpath(dirname($file));
364 147 ▶ write_file($file, $self->{json}->encode($data)) || die("Failed to write '$file': $!\n");
Branch: 1 branch
BranchTrueFalseTotal
write_file($file, $self->{"json"}->encode($data)) or die "Failed to write '${file}': $!\n" 0 147 147
365 }
366  
367 sub __saveText
368 {
369 2 ▶ my $self = shift;
Subroutine __saveText: called CC=2 SCAR=7.0
370 2 my $name = shift;
371 2 my $data = shift;
372
373 2 my $file = slashify("$name.txt");
374 2 mkpath(dirname($file));
375 2 ▶ write_file($file, @$data) || die("Failed to write '$file': $!\n");
Branch: 1 branch
BranchTrueFalseTotal
write_file($file, @$data) or die "Failed to write '${file}': $!\n" 0 2 2
376 }
377  
378 19 1;
« blib/lib/App/TestOnTap/Config.pm ↑ Summary blib/lib/App/TestOnTap/Harness.pm »