blib/lib/App/TestOnTap/Util.pm

filter: Statementstmt 68.9% 60 / 87 Branchbran 32.6% 15 / 46 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 6 totaltotal 57.1% 92 / 161 SCAR 43.0
CC
25
Cov
57%
__readLines
110.0
runprocess
6.1
trim
6.0
CRAP 73.8
« blib/lib/App/TestOnTap/Args.pm ↑ Summary blib/lib/App/TestOnTap/Dispenser.pm »
1 package App::TestOnTap::Util;
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 Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 our $IS_WINDOWS = $^O eq 'MSWin32';
12 19 ▶our $IS_PACKED = $ENV{PAR_0} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PAR_0'} ? : 0 19 19
13 19 ▶our $FILE_SEP = $IS_WINDOWS ? '\\' : '/';
Branch: 1 branch
BranchTrueFalseTotal
$App::TestOnTap::Util::IS_WINDOWS ? : 0 19 19
14 19 ▶our $SHELL_ARG_DELIM = $IS_WINDOWS ? '"' : "'";
Branch: 1 branch
BranchTrueFalseTotal
$App::TestOnTap::Util::IS_WINDOWS ? : 0 19 19
15  
16 19 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 our @EXPORT_OK =
18 qw
19 (
20 slashify
21 trim
22 stringifyTime
23 ensureArray
24 expandAts
25 runprocess
26 $IS_WINDOWS
27 $IS_PACKED
28 $FILE_SEP
29 $SHELL_ARG_DELIM
30 );
31  
32 # pass in a path and ensure it contains the native form of slash vs backslash
33 # (or force either one)
34 #
35 sub slashify
36 {
37 878 ▶ my $s = shift;
Subroutine slashify: called CC=3 SCAR=11.2
Pod undocumented
38 878 ▶ my $fsep = shift || $FILE_SEP;
Condition66%2/3
shift() || $App::TestOnTap::Util::FILE_SEP
l!l&&r!l&&!r
3585200
MC/DC0%0/2
shift() || $App::TestOnTap::Util::FILE_SEP
shift() $App::TestOnTap::Util::FILE_SEP
Truth table66%2/3
A || B
A= shift()B= $App::TestOnTap::Util::FILE_SEP
ABresult
1X1
011
000
39  
40 878 my $dblStart = $s =~ s#^[\\/]{2}##;
41 878 $s =~ s#[/\\]+#$fsep#g;
42  
43 878 ▶ return $dblStart ? "$fsep$fsep$s" : $s;
Branch: 1 branch
BranchTrueFalseTotal
$dblStart ? : 0 878 878
44 }
45  
46 # trim off any ws at front/end of a string
47 #
48 sub trim
49 {
50 0 ▶ my $s = shift;
Subroutine trim: not called CC=2 SCAR=17.9
Pod undocumented
51  
52 0 ▶ $s =~ s/^\s+|\s+$//g if defined($s);
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 0 0
53  
54 0 return $s;
55 }
56  
57 # turn an epoch time into a compact ISO8601 UTC string
58 #
59 sub stringifyTime
60 {
61 146 ▶ my $tm = shift;
Subroutine stringifyTime: called CC=2 SCAR=6.9
Pod undocumented
62
63 # deal with possible hires timings and
64 # convert the raw timestamps to strings
65 #
66 146 my $subsecs = '';
67 146 ▶ if ($tm =~ m#\.#)
Branch: 1 branch
BranchTrueFalseTotal
if ($tm =~ /\./) 94 52 146
68 {
69 94 $tm =~ s#^([^\.]+)\.(.*)#$1#;
70 94 $subsecs = ".$2";
71 }
72
73 146 return strftime("%Y%m%dT%H%M%S${subsecs}Z", gmtime($tm));
74 }
75  
76 # ensure we end up with an array
77 # input can be:
78 # undef
79 # an array
80 # a space-separared string
81 # a newline-separated string
82 #
83 sub ensureArray
84 {
85 283 ▶ my $data = shift || [];
Subroutine ensureArray: called CC=4 SCAR=13.9
Pod undocumented
Condition100%2/2
shift() || []
l!l
20677
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
86
87 return
88 283 ▶ (ref($data) eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
ref $data eq 'ARRAY' ? : 152 131 283
$data =~ /\n/ ? : 0 131 131
89 ? $data
90 : ($data =~ m#\n#)
91 ? [ split("\n", $data) ]
92 : [ split(' ', $data) ];
93 }
94  
95 # expand any array elements using '@xyz' as new line elements read from 'xyz'
96 # also, handle recursion where included files itself refers to further files
97 # possibly using relative paths
98 #
99 sub expandAts
100 {
101 29 ▶ my $dirctx = shift;
Subroutine expandAts: called CC=4 SCAR=17.2
Pod undocumented
102
103 29 my @a;
104 29 foreach my $e (@_)
105 {
106 90 ▶ if ($e =~ /^@(.+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /^@(.+)/) { } 0 90 90
107 {
108 # if we find a filename use as-if its absolute, otherwise tack on
109 # the current dir context
110 #
111 0 my $fn = $1;
112 0 ▶ $fn = File::Spec->file_name_is_absolute($fn) ? $fn : "$dirctx/$fn";
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($fn) ? : 0 0 0
113
114 # recursively read file contents into the array
115 # using the current files directory as the new dir context
116 #
117 0 push(@a, expandAts(dirname($fn), __readLines($fn)))
118 }
119 else
120 {
121 # just keep the value as-is
122 #
123 90 push(@a, $e);
124 }
125 }
126 29 return @a;
127 }
128  
129 # read all lines from a file and return as an array
130 # supports line continuation, e.g. a line with '\' at the end causes
131 # appending the line after etc, in order to create a single line.
132 # - a line starting with '#' will be ignored as a comment
133 # - all lines will be trimmed from space at each end
134 # - an empty line will be ignored
135 #
136 sub __readLines
137 {
138 0 ▶ my $fn = slashify(File::Spec->rel2abs(shift()));
Subroutine __readLines: not called CC=10 SCAR=47.0
139
140 0 ▶ die("No such file: '$fn'\n") unless -f $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fn 0 0 0
141  
142 0 my @lines;
143 0 ▶ open (my $fh, '<', $fn) or die("Failed to open '$fn': $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $fn or die "Failed to open '${fn}': $!\n" 0 0 0
144 0 my $line;
145 0 ▶ while (defined($line = <$fh>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
146 {
147 0 chomp($line);
148
149 # handle lines with line continuation
150 # until no more continuation is found
151 #
152 0 ▶ if ($line =~ s#\\\s*$##)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/\\\s*$//) 0 0 0
153 {
154 # append lines...
155 #
156 0 $line .= <$fh>;
157
158 # ...and repeat, unless we hit eof
159 #
160 0 ▶ redo unless eof($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless eof $fh 0 0 0
161 }
162
163 # if the resulting line is a comment line, ignore it
164 #
165 0 ▶ if ($line !~ /^\s*#/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*#/) 0 0 0
166 {
167 # ensure removing any trailing line continuation is removed
168 # (can happen if there is no extra line after a line continuation, just eof)
169 #
170 0 $line =~ s#\\\s*$##;
171
172 # trim the ends, and add it - but only if it's not empty
173 #
174 0 $line = trim($line);
175
176 0 ▶ if ($line)
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 0 0 0
177 {
178 # expand any ${envvar} in the line
179 #
180 0 ▶ while ($line =~ m#\$\{([^}]+)\}#)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\$\{([^}]+)\}/) 0 0 0
181 {
182 0 my $ev = $1;
183 0 ▶ die("No environment variable '$ev' in '$line'\n") unless exists($ENV{$ev});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{$ev} 0 0 0
184 0 $line =~ s#\$\{\Q$ev\E\}#$ENV{$ev}#;
185 }
186
187 # line is ready
188 #
189 0 push(@lines, $line);
190 }
191 }
192 }
193 0 close($fh);
194
195 0 return @lines;
196 }
197  
198 sub runprocess
199 {
200 2 ▶ my $cb = shift;
Subroutine runprocess: called CC=6 SCAR=18.1
Pod undocumented
201 2 my $wd = shift;
202 2 my @argv = @_;
203
204 2 my $cwd = getcwd();
205 2 ▶ chdir($wd) || die("Failed to change directory to '$wd': $!\n");
Branch: 1 branch
BranchTrueFalseTotal
chdir $wd or die "Failed to change directory to '${wd}': $!\n" 0 2 2
206  
207 2 $_ = "$SHELL_ARG_DELIM$_$SHELL_ARG_DELIM" foreach (@argv);
208 2 my $cmdline = join(' ', @argv);
209
210 2 ▶ open(my $fh, '-|', "$cmdline 2>&1") || die("Failed to run '$cmdline': $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "$cmdline 2>&1" or die "Failed to run '${cmdline}': $!\n" 0 2 2
211 2 ▶ while (my $line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 40 2 42
212 {
213 40 $cb->($line);
214 }
215 2 close($fh);
216 2 my $xit = $? >> 8;
217  
218 2 ▶ chdir($cwd) || die("Failed to change directory back to '$cwd': $!\n");
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or die "Failed to change directory back to '${cwd}': $!\n" 0 2 2
219
220 2 return $xit;
221 }
222  
223 19 1;
« blib/lib/App/TestOnTap/Args.pm ↑ Summary blib/lib/App/TestOnTap/Dispenser.pm »