Mnet/Test.pm

filter: Statementstmt 95.8% 116 / 121 Branchbran 71.2% 57 / 80 Conditioncond 78.5% 22 / 28 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 94.4% 17 / 18 Podpod 66.6% 2 / 3 totaltotal 83.8% 223 / 266 CC 54 SCAR 20.0
CC
54
Cov
85%
_record
11.4
_diff
11.2
_replay
8.5
CRAP 7.4
« Mnet/Expect.pm ↑ Summary Mnet/Opts/Cli/Cache.pm »
1 package Mnet::Test;
2  
3 =head1 NAME
4  
5 Mnet::Test - Record, replay, and test script inputs and outputs
6  
7 =head1 SYNOPSIS
8  
9 # sample test script
10 #
11 # first create an Mnet::Test file with --record <file>
12 # next replay the test with --test --replay <file>
13 # add a print statement to cause test replay to fail
14  
15 # required for this sample
16 # refer to TESTING perldoc in other Mnet modules
17 use Mnet::Expect::Cli;
18 use Mnet::Opts::Cli;
19 use Mnet::Test;
20  
21 # other cli options could be defined and recorded in tests
22 my $cli = Mnet::Opts::Cli->new;
23  
24 # the Mnet::Expect::Cli modules can record session command outputs
25 my $expect = Mnet::Expect::Cli({ spawn => "ssh 1.2.3.4" });
26 my $output = $expect->command("whoami");
27  
28 # stdout and stderr are captures for tests
29 print "$output\n";
30  
31 =head1 DESCRIPTION
32  
33 Mnet::Test can be used to allow script inputs and output to be recorded to a
34 file, which can be replayed later to show any changes.
35  
36 Other L<Mnet> modules are designed to detect and make use of Mnet::Test, if it
37 is being used by a script. Refer to the perldoc TESTING sections of the other
38 modules for explanations of how each module supports Mnet::Test usage. Also
39 refer to the --test, --record, --replay and --test-reset options for more
40 information.
41  
42 Scripts or modules that need to save additional data to --record test data
43 files can call the Mnet::Test::data function to get a referenced hash key that
44 can be used to store data for the current script or module. The --record option
45 will save this data to a file at the end of script execution, and the --replay
46 option can be used to load that data back from the file into the
47 Mnet::Test::data hash.
48  
49 Also note that the Mnet::Test::time function can be used to return repeatable
50 sequences of outputs from the perl time command during --test execution. This
51 helps to avoid changing timestamps causing test failures. The L<Mnet::Log>
52 module automatically normalizes timestamps when running tests.
53  
54 Scripts that do not use L<Mnet::Opts::Cli> to parse command line options can
55 pass the replay file as an argument to the Mnet::Test::data function and call
56 the Mnet::Test::done function at the end of script execution.
57  
58 Note that the optional environment variable that can be specified when creating
59 a new L<Mnet::Opts::Cli> object is not parsed if the --test option is set on
60 the command line, since the value of this envrionment variable may change over
61 time, between users, systems, etc.
62  
63 =head1 FUNCTIONS
64  
65 Mnet::Test implements the functions listed below.
66  
67 =cut
68  
69 # required modules
70 84 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 84 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 84 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 84 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 84 ▶use Mnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 84 ▶use Mnet::Log::Conditional qw( DEBUG INFO WARN FATAL NOTICE );
Subroutine BEGIN: called CC=1 SCAR=0.0
76 84 ▶use Mnet::Opts::Cli::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 84 ▶use Mnet::Tee;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 # modules required for diff output
80 84 ▶BEGIN { push @INC, "$1/Depends" if $INC{"Mnet/Test.pm"} =~ /(.+)Test\.pm$/; }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Test.pm"} =~ /(.+)Test\.pm$/ 84 0 84
81  
82  
83  
84 # init global variables and cli options used by this module
85 84 ▶INIT {
Subroutine INIT: called CC=7 SCAR=19.9
86  
87 # init global variables
88 # Mnet::Test::data hash holds test/record/replay data, undef until used
89 # Mnet::Test::time used for test unixtime, refer to Mnet::Test::time sub
90 84 our ($data, $time) = (undef, 0);
91  
92 # autoflush standard output
93 # so that multi-process syswrite lines are not split
94 84 $| = 1;
95  
96 # init stdout file handle to bypass Mnet::Tee for diff output
97 84 our $stdout = undef;
98 84 ▶ if ($INC{"Mnet/Tee.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Mnet/Tee.pm'}) { } 84 0 84
99 84 $stdout = $Mnet::Tee::stdout;
100 } else {
101 0 ▶ open($stdout, ">&STDOUT") or die "error duping stdout, $!";
Branch: 1 branch
BranchTrueFalseTotal
open $Mnet::Test::stdout, ">&STDOUT" or die "error duping stdout, $!" 0 0 0
102 }
103  
104 # defined --record option
105 Mnet::Opts::Cli::define({
106 getopt => 'record:s',
107 help_tip => 'save test data to file for replay',
108 help_text => '
109 this option works from the command line only
110 files recorded can be replayed using the --replay option
111 data is saved with a .new suffix, then renamed after writing
112 set null to update --replay filename with new --test diff outputs
113 use --test-reset option to remove opts/args from --record files
114 refer to perldoc Mnet::Test for more info
115 ',
116 84 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 74 10 84
117  
118 # define --replay option
119 Mnet::Opts::Cli::define({
120 getopt => 'replay=s',
121 help_tip => 'run with test data from record file',
122 help_text => '
123 this option works from the command line only
124 execute script using replay file created with --record option
125 use --test-reset option to remove opts/args from --replay file
126 refer to perldoc Mnet::Test for more info
127 ',
128 84 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 74 10 84
129  
130 # define --test option
131 Mnet::Opts::Cli::define({
132 getopt => 'test',
133 help_tip => 'diff output with test replay output',
134 help_text => '
135 this option works from the command line only
136 use to compare current script output to --replay output
137 refer to perldoc Mnet::Test for more info
138 ',
139 84 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 74 10 84
140  
141 # define --test-reset option
142 Mnet::Opts::Cli::define({
143 getopt => 'test-reset:s',
144 help_hide => '1',
145 help_tip => 'reset options/args to their defaults',
146 help_text => '
147 this option works from the command line only
148 use to reset options in specified test record/replay file
149 use --test-reset with an option name to reset option to default
150 use --test-reset with no option to reset extra args to default
151 use --help for individual options to check if they are recordable
152 error if --test-reset is specified for non-recordable option
153 refer also to norecord option in Mnet::Opts::Cli::define function
154 refer to perldoc Mnet::Test for more info
155 ',
156 84 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 74 10 84
157  
158 # finished init of options and global variables
159 }
160  
161  
162  
163 118 ▶sub data {
Subroutine data: called CC=8 SCAR=20.9
Pod undocumented
164  
165 =head1 Mnet::Test::data
166  
167 \%data = Mnet::Test::data(\%opts);
168  
169 This function returns a hash reference containing test/record/replay data for
170 the calling module or the main script. It is up to the calling module or main
171 script to manage its own test/record/replay data.
172  
173 The opts hash ref argument is optional, and may be used if desired to specify
174 a replay file. Otherwise the --replay cli option will be checked if the
175 L<Mnet::Opts::Cli> module is used to parse command line options.
176  
177 Note that care must be taken to use the hash reference returned from this
178 function properly. You want to save data in the returned hash reference, not
179 accidently create a new hash reference. For example:
180  
181 ok: my $data = Mnet::Test::data();
182 $data->{sub_hash}->{key} = $value;
183  
184 ok: my $data = Mnet::Test::data();
185 my $sub_hash = \%{$data->{sub_hash}};
186 $sub_hash->{key} = $value;
187  
188 ok: my $sub_hash = \%{Mnet::Test::data()->{sub_hash}};
189 $sub_hash->{key} = $value;
190  
191 bad: Mnet::Test::data()->{sub_hash}->{key} = $value;
192  
193 bad: my $data = Mnet::Test::data();
194 my $sub_hash = $data->{sub_hash};
195 $sub_hash->{key} = $value;
196  
197 Refer to the DESCRIPTION section of this document for more information on how
198 modules or scripts should use this function for test/record/replay data.
199  
200 =cut
201  
202 # read opts hash ref arg, or set via cached cli opts
203 118 ▶ my $opts = Mnet::Opts::Cli::Cache::get(shift // {});
Condition100%2/2
shift() // {}
l!l
2593
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
204  
205 # check for force flag, allowed from Mnet::Opts::Cli only
206 118 my $force = shift;
207 118 ▶ croak("invalid force option") if $force and caller ne "Mnet::Opts::Cli";
Branch: 1 branch
BranchTrueFalseTotal
if $force and caller ne "Mnet::Opts::Cli" 0 118 118
Condition66%2/3
$force and caller ne "Mnet::Opts::Cli"
!ll&&!rl&&r
104140
MC/DC0%0/2
$force and caller ne "Mnet::Opts::Cli"
$force caller ne "Mnet::Opts::Cli"
Truth table66%2/3
A and B
A= $forceB= caller ne "Mnet::Opts::Cli"
ABresult
0X0
100
111
208  
209 # note the calling module name
210 118 my $caller = caller;
211 118 ▶ $caller = "main" if $caller eq "-e";
Branch: 1 branch
BranchTrueFalseTotal
if $caller eq "-e" 0 118 118
212  
213 # init global test data var, if not yet defined
214 # init to an empty hash ref, or from file if --replay cli opt is set
215 # force replay data reload if called with force flag from Mnet::Opts::Cli
216 118 ▶ if (not defined $Mnet::Test::data or $force) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $Mnet::Test::data or $force) 82 36 118
Condition100%3/3
not defined $Mnet::Test::data or $force
l!l&&r!l&&!r
77536
MC/DC100%2/2
not defined $Mnet::Test::data or $force
not defined $Mnet::Test::data $force
Truth table100%3/3
not(A) or B
A= not defined $Mnet::Test::dataB= $force
ABresult
1X1
011
000
217 82 $Mnet::Test::data = {};
218 82 _replay($opts);
219 }
220  
221 # init hash ref for caller if it doesn't yet exist
222 $Mnet::Test::data->{$caller} = {}
223 118 ▶ if not exists $Mnet::Test::data->{$caller};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Mnet::Test::data->{$caller} 70 48 118
224  
225 # finished Mnet::Test::data function, return hash ref for calling module
226 118 return $Mnet::Test::data->{$caller};
227 }
228  
229  
230  
231 89 ▶sub _diff {
Subroutine _diff: called CC=11 SCAR=24.1
232  
233 # $diff = _diff(\%opts)
234 # purpose: returns current output and --replay diff text
235 # \%opts: returns undef if --test or --replay are not defined
236 # $diff: diff output in string format, or undef if no replay data to diff
237  
238 # read opts hash ref arg, or set via cached cli opts
239 89 ▶ my $opts = Mnet::Opts::Cli::Cache::get(shift // {});
Condition50%1/2
shift() // {}
l!l
890
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
240  
241 # return if test or replay options are not set
242 89 ▶ return undef if not defined $opts->{test};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"test"} 71 18 89
243 18 ▶ return undef if not defined $opts->{replay};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"replay"} 9 9 18
244  
245 # abort if we can't get a copy of the current outputs
246 9 my $outputs = Mnet::Tee::test_outputs();
247 9 ▶ FATAL("undefined current test data") if not defined $outputs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outputs 0 9 9
248  
249 # abort if there's no replay test data outputs
250 9 my $test_data = data($opts);
251 FATAL("undefined --replay test data")
252 9 ▶ if not defined $test_data->{outputs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test_data->{"outputs"} 0 9 9
253  
254 # init diff output, use Text::Diffs if available
255 9 my $diff = "";
256 9 ▶ if ($outputs ne $test_data->{outputs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outputs ne $test_data->{"outputs"}) 5 4 9
257 5 eval("require Text::Diff; 1");
258 5 $diff = "Test output is different, need Text::Diff to show more.\n";
259 $diff = Text::Diff::diff(\$test_data->{outputs}, \$outputs)
260 5 ▶ if $INC{"Text/Diff.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Text/Diff.pm"} 5 0 5
261 }
262  
263 # output detected differences, unless running in --batch mode
264 # Mnet::Test::test_pause used to keep test diff output out of test data
265 9 ▶ if (not $opts->{batch}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"batch"}) 4 5 9
266 4 my $was_paused = Mnet::Tee::test_paused();
267 4 Mnet::Tee::test_pause();
268 4 syswrite $Mnet::Test::stdout, "\n" . "-" x 79 . "\n";
269 4 syswrite $Mnet::Test::stdout, "diff --test --replay $opts->{replay}";
270 4 syswrite $Mnet::Test::stdout, "\n" . "-" x 79 . "\n\n";
271 4 ▶ if ($diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff) { } 3 1 4
272 3 syswrite $Mnet::Test::stdout, $diff;
273 } else {
274 1 syswrite $Mnet::Test::stdout, "Test output is identical.\n";
275 }
276 4 syswrite $Mnet::Test::stdout, "\n";
277 4 ▶ Mnet::Tee::test_unpause() if not $was_paused;
Branch: 1 branch
BranchTrueFalseTotal
unless $was_paused 4 0 4
278 }
279  
280 # finished _diff function
281 9 return $diff;
282 }
283  
284  
285  
286 89 ▶sub done {
Subroutine done: called CC=7 SCAR=19.9
Pod documented
287  
288 =head2 Mnet::Test::done
289  
290 $diff = Mnet::Test::done(\%opts)
291  
292 This function does one or two things, depending on the how the record, replay,
293 and test options are set.
294  
295 If the --replay and --test options are set a diff of test output will be
296 returned, or a value of undefined if there was no replay data.
297  
298 If the --record option is set then the Mnet::Test data captured from the
299 current script execution will be saved to the specified file.
300  
301 This function is called automatically at script exit using the --record,
302 --replay, and --test options parsed from a prior Mnet::Opts::Cli->new
303 call. You do not need to call this function unless you are not using
304 L<Mnet::Opts::Cli> to parse command line options or if you want to examine
305 your own test diff data.
306  
307 If the test output has changed a diff will be presented using the L<Text::Diff>
308 module.
309  
310 Refer to the DESCRIPTION section of this document for more information.
311  
312 =cut
313  
314 # read opts hash ref arg, or set via cached cli opts
315 89 ▶ my $opts = Mnet::Opts::Cli::Cache::get(shift // {});
Condition100%2/2
shift() // {}
l!l
584
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
316  
317 # diff replay output if replay and test options are set true
318 89 my $diff = _diff($opts);
319  
320 # record to file, if ncessary
321 # record if set to a specified file
322 # record if there's a test diff
323 89 ▶ if (defined $opts->{record}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"record"}) 11 78 89
324 11 ▶ if ($opts->{record} ne "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'record'} ne '') { } 11 0 11
elsif ($diff) { } 0 0 0
325 11 _record($opts);
326 } elsif ($diff) {
327 0 _record($opts);
328 }
329 }
330  
331 # output warning for test diff in --batch mode
332 # batch child output gets silent pragma set by parent
333 # this will cause children to throw an error if they have a test diff
334 # batch parent will output a warning when it reaps child with an error
335 89 ▶ FATAL("batch mode test output is different") if $opts->{batch} and $diff;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"batch"} and $diff 2 87 89
Condition100%3/3
$opts->{"batch"} and $diff
!ll&&!rl&&r
8162
MC/DC100%2/2
$opts->{"batch"} and $diff
$opts->{"batch"} $diff
Truth table100%3/3
A and B
A= $opts->{"batch"}B= $diff
ABresult
0X0
100
111
336  
337 # finished done function, return diff output
338 87 return $diff;
339 }
340  
341  
342  
343 11 ▶sub _record {
Subroutine _record: called CC=9 SCAR=24.4
344  
345 # _record(\%opts)
346 # purpose: dump test data to a file, returns if record option is undef
347 # \%opts: looks for record hash ref key, uses replay filename if set null
348  
349 # read opts hash ref arg, or set via cached cli opts
350 11 ▶ my $opts = Mnet::Opts::Cli::Cache::get(shift // {});
Condition50%1/2
shift() // {}
l!l
110
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
351  
352 # return if --record option is not set
353 11 ▶ return if not defined $opts->{record};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"record"} 0 11 11
354  
355 # add current captured input to data before recording to file
356 11 $Mnet::Test::data->{"Mnet::Test"}->{outputs} = Mnet::Tee::test_outputs();
357  
358 # prepare dump of test data, with keys in consitant sort order
359 11 my $dump = Data::Dumper->new([$Mnet::Test::data])->Sortkeys(1)->Dump;
360  
361 # replace default Data::Dumper var name with something more descriptive
362 # this will help discourage bypassing this module to access these files
363 11 $dump =~ s/^\$VAR1/\$Mnet::Test::data/g;
364  
365 # log dump of test data that we are going to save
366 11 ▶ if ($opts->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"debug"}) 0 11 11
367 0 DEBUG("_record: $_") foreach split(/\n/, $dump);
368 }
369  
370 # update --replay file if --record is set null
371 11 my $record_file = $opts->{record};
372 11 ▶ $record_file = $opts->{replay} if $record_file eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $record_file eq "" 0 11 11
373 11 ▶ FATAL("null --record set without --replay file")
Branch: 1 branch
BranchTrueFalseTotal
if not defined $record_file or $record_file eq "" 0 11 11
Condition33%1/3
not defined $record_file or $record_file eq ""
l!l&&r!l&&!r
0011
MC/DC0%0/2
not defined $record_file or $record_file eq ""
not defined $record_file $record_file eq ""
Truth table33%1/3
not(A) or B
A= not defined $record_fileB= $record_file eq ""
ABresult
1X1
011
000
374 if not defined $record_file or $record_file eq "";
375  
376 # read dump of test data from replay file, abort on errors
377 11 ▶ open(my $fh, ">", $record_file)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $record_file or FATAL("error opening --record $record_file, $!") 11 0 11
378 or FATAL("error opening --record $record_file, $!");
379 11 print $fh $dump;
380 11 close $fh;
381  
382 # finished _record function
383 11 return;
384 }
385  
386  
387  
388 82 ▶sub _replay {
Subroutine _replay: called CC=8 SCAR=21.4
389  
390 # _replay(\%opts)
391 # purpose: read Mnet::Test::data global hash ref data from replay file
392 # \%opts: hash ref w/replay file, or undef to use Mnet::Opts::Cli::Cache
393 # note: this is called from Mnet::Test::data()
394  
395 # read opts hash ref arg, or set via cached cli opts
396 82 ▶ my $opts = Mnet::Opts::Cli::Cache::get(shift // {});
Condition50%1/2
shift() // {}
l!l
820
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
397  
398 # return if replay option is not set
399 82 ▶ return if not defined $opts->{replay};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"replay"} 66 16 82
400  
401 # create log object using current options
402 16 my $log = Mnet::Log::Conditional->new($opts);
403  
404 # read dump of test data from replay file, abort on errors
405 16 my $dump = "";
406 open(my $fh, "<", $opts->{replay})
407 16 ▶ or $log->fatal("error opening --replay $opts->{replay}, $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $opts->{"replay"} or $log->fatal(qq[error opening --replay $opts->{"replay"}, $!]) 16 0 16
408 16 ▶ $dump .= $_ while <$fh>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $fh) and do { $dump .= $_ } 216 16 232
409 16 close $fh;
410  
411 # log dump of test data that we just read
412 16 ▶ if ($opts->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"debug"}) 0 16 16
413 0 $log->debug("replay: $_") foreach split(/\n/, $dump);
414 }
415  
416 # restore variable name before eval of Data::Dumper file data
417 16 $dump =~ s/^\$Mnet::Test::data/\$data/;
418  
419 # eval replay dump data, warn on eval syntax problems
420 16 my $data = undef;
421 16 eval {
422 0 ▶ local $SIG{__WARN__} = sub { $log->warn("@_") };
Subroutine __ANON__: not called CC=1 SCAR=1.2
423 16 $data = eval $dump;
424 };
425 16 $Mnet::Test::data = $data;
426  
427 # abort if replay test data hash ref eval failed
428 16 ▶ $log->fatal("replay eval failed for --replay $opts->{replay}")
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mnet::Test::data ne "HASH" 0 16 16
429 if ref $Mnet::Test::data ne "HASH";
430  
431 # finished replay function
432 16 return;
433 }
434  
435  
436  
437 21 ▶sub time {
Subroutine time: called CC=6 SCAR=17.9
Pod documented
438  
439 =head2 Mnet::Test::time
440  
441 $unixtime = Mnet::Test::time($incrememt)
442 or $unixtime = Mnet::Test::time(\%opts, $increment)
443  
444 This function can be used by project scripts to get repeatable unixtime output
445 during executions with the --test, --record, or --replay cli options set, and
446 real time from the perl time command otherwise.
447  
448 An optional incrememnt value can be specified in seconds, and defauls to the
449 returned time being incremented by one second for each call to this function.
450  
451 This function can be called with an opts hash ref, which can have record and
452 replay keys set to indicate test output is needed. Otherwise these options are
453 expected to be set via the L<Mnet::Opts::Cli> module.
454  
455 =cut
456  
457 # read input args, increment might be only arg, opts defaults to cached cli
458 21 my ($opts, $increment) = (shift, shift);
459 21 ▶ ($increment, $opts) = ($opts, Mnet::Opts::Cli::Cache::get({}))
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts 12 9 21
460 if not ref $opts;
461  
462 # default to a one second increment for each call
463 21 ▶ $increment = 1 if not $increment;
Branch: 1 branch
BranchTrueFalseTotal
unless $increment 12 9 21
464  
465 # init output unixtime to real time
466 21 my $unixtime = time;
467  
468 # set output incremented test time if test/record/replay options are set
469 21 ▶ if (defined $opts->{record} or $opts->{replay} or $opts->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"record"} or $opts->{"replay"} or $opts->{"test"}) 12 9 21
Condition100%6/6
defined $opts->{"record"} or $opts->{"replay"} or $opts->{"test"}
l!l&&r!l&&!r
759
defined $opts->{"record"} or $opts->{"replay"}
l!l&&r!l&&!r
4314
MC/DC100%3/3
defined $opts->{"record"} or $opts->{"replay"} or $opts->{"test"}
defined $opts->{"record"} $opts->{"replay"} $opts->{"test"}
Truth table100%4/4
A or B or C
A= defined $opts->{"record"}B= $opts->{"replay"}C= $opts->{"test"}
ABCresult
1XX1
01X1
0011
0000
470 12 $unixtime = $Mnet::Test::time += $increment;
471 }
472  
473 # finished Mnet::Test::time function, return unixtime
474 21 return $unixtime;
475 }
476  
477  
478  
479 # process --record and --test cli options, unless Mnet::Log is loaded
480 # called from Mnet::Log end block if loaded, after last log line output
481 # --test diff undef if --replay --test diff was not attempted
482 # --test diff is null for no diff, exit clean even if output had errors
483 # --test diff is non-null for failed diff, exit with a failed status
484 84 ▶END {
Subroutine END: called CC=4 SCAR=14.2
485 84 ▶ if (not $INC{"Mnet/Log.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $INC{"Mnet/Log.pm"}) 10 74 84
486 10 my $diff = Mnet::Test::done();
487 10 ▶ exit 0 if defined $diff;
Branch: 1 branch
BranchTrueFalseTotal
if defined $diff 0 10 10
488 10 ▶ exit 1 if $diff;
Branch: 1 branch
BranchTrueFalseTotal
if $diff 0 10 10
489 }
490 }
491  
492  
493  
494 =head1 SEE ALSO
495  
496 L<Mnet>
497  
498 L<Mnet::Expect::Cli>
499  
500 L<Mnet::Log>
501  
502 L<Mnet::Opts::Cli>
503  
504 L<Mnet::Report::Table>
505  
506 L<Text::Diff>
507  
508 =cut
509  
510 # normal end of package
511 84 1;
512  
« Mnet/Expect.pm ↑ Summary Mnet/Opts/Cli/Cache.pm »