blib/lib/Test/File.pm

filter: Statementstmt 85.9% 549 / 639 Branchbran 77.0% 222 / 288 Conditioncond 64.8% 107 / 165 MC/DCmcdc 46.8% 44 / 94 Subroutinesub 94.5% 69 / 73 Podpod 100% 47 / 47 totaltotal 79.4% 1038 / 1306 CC 215 SCAR 24.8
CC
215
Cov
80%
symlink_target_is_absolute_ok
110.0
_IsSymlinkCreationAllowed
66.7
file_is_not_symlink_ok
30.0
CRAP 11.9
↑ Summary
1 package Test::File;
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 our @EXPORT = qw(
11 file_exists_ok file_not_exists_ok
12 file_empty_ok file_not_empty_ok file_size_ok file_max_size_ok
13 file_min_size_ok file_readable_ok file_not_readable_ok
14 file_writeable_ok file_writable_ok file_not_writeable_ok file_not_writable_ok
15 file_executable_ok file_not_executable_ok
16 file_mode_is file_mode_isnt
17 file_mode_has file_mode_hasnt
18 file_is_symlink_ok file_is_not_symlink_ok
19 symlink_target_exists_ok symlink_target_is
20 symlink_target_dangles_ok
21 dir_exists_ok dir_contains_ok
22 link_count_is_ok link_count_gt_ok link_count_lt_ok
23 owner_is owner_isnt
24 group_is group_isnt
25 file_line_count_is file_line_count_isnt file_line_count_between
26 file_contains_like file_contains_unlike
27 file_contains_utf8_like file_contains_utf8_unlike
28 file_contains_encoded_like file_contains_encoded_unlike
29 file_mtime_gt_ok file_mtime_lt_ok file_mtime_age_ok
30 );
31  
32 18 our $VERSION = '1.995';
33 18 ▶XSLoader::load(__PACKAGE__, $VERSION) if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 18 18
34  
35 18 my $Test = Test::Builder->new();
36  
37 =encoding utf8
38  
39 =head1 NAME
40  
41 Test::File -- test file attributes
42  
43 =head1 SYNOPSIS
44  
45 use Test::File;
46  
47 =head1 DESCRIPTION
48  
49 This modules provides a collection of test utilities for file
50 attributes.
51  
52 Some file attributes depend on the owner of the process testing the
53 file in the same way the file test operators do. For instance, root
54 (or super-user or Administrator) may always be able to read files no
55 matter the permissions.
56  
57 Some attributes don't make sense outside of Unix, either, so some
58 tests automatically skip if they think they won't work on the
59 platform. If you have a way to make these functions work on Windows,
60 for instance, please send me a patch. :) If you want to pretend to be
61 Windows on a non-Windows machine (for instance, to test C<skip()>),
62 you can set the C<PRETEND_TO_BE_WINDOWS> environment variable.
63  
64 The optional NAME parameter for every function allows you to specify a
65 name for the test. If not supplied, a reasonable default will be
66 generated.
67  
68 =head2 Functions
69  
70 =over 4
71  
72 =cut
73  
74 78 ▶sub _is_plain_file {
Subroutine _is_plain_file: called CC=5 SCAR=16.6
75 78 my $filename = _normalize( shift );
76  
77 78 my $message = do {
78 78 ▶ if( ! -e $filename ) { "does not exist" }
Branch: 3 branches
BranchTrueFalseTotal
if (not -e $filename) { } 15 63 78
elsif (not -f _) { } 0 63 63
elsif (-d _) { } 0 63 63
79 0 elsif( ! -f _ ) { "is not a plain file" }
80 0 elsif( -d _ ) { "is a directory" }
81 63 else { () }
82 };
83  
84 78 ▶ if( $message ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message) 15 63 78
85 15 $Test->diag( "file [$filename] $message");
86 15 return 0;
87 }
88  
89 63 return 1;
90 }
91  
92 254 ▶sub _normalize {
Subroutine _normalize: called CC=3 SCAR=11.0
93 254 my $file = shift;
94 254 ▶ return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 4 250 254
95  
96 250 ▶ return $file =~ m|/|
Branch: 1 branch
BranchTrueFalseTotal
$file =~ m[/] ? : 3 247 250
97 ? File::Spec->catfile( split m|/|, $file )
98 : $file;
99 }
100  
101 40 ▶sub _win32 {
Subroutine _win32: called CC=4 SCAR=13.9
102 40 ▶ return 0 if $^O eq 'darwin';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "darwin" 1 39 40
103 39 ▶ return $ENV{PRETEND_TO_BE_WIN32} if defined $ENV{PRETEND_TO_BE_WIN32};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"PRETEND_TO_BE_WIN32"} 1 38 39
104 38 ▶ return $^O =~ m/Win/ || $^O eq 'msys';
Condition66%2/3
$^O =~ /Win/ || $^O eq 'msys'
l!l&&r!l&&!r
5033
MC/DC50%1/2
$^O =~ /Win/ || $^O eq 'msys'
$^O =~ /Win/ $^O eq 'msys'
Truth table66%2/3
A || B
A= $^O =~ /Win/B= $^O eq 'msys'
ABresult
1X1
011
000
105 }
106  
107 # returns true if symlinks can't exist
108 18 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
109 18 my $cannot_symlink;
110  
111 19 ▶ sub _no_symlinks_here {
Subroutine _no_symlinks_here: called CC=2 SCAR=6.9
112 19 ▶ return $cannot_symlink if defined $cannot_symlink;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cannot_symlink 18 1 19
113  
114 1 $cannot_symlink = ! do {
115 1 eval {
116 1 symlink("",""); # symlink exist in perl
117 1 _IsSymlinkCreationAllowed() # symlink is ok in current session
118 }
119 };
120 }
121  
122 1 ▶ sub _IsSymlinkCreationAllowed {
Subroutine _IsSymlinkCreationAllowed: called CC=9 SCAR=42.0
123 1 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 1 1
124 #
125 # Bare copy of Perl's Win32::IsSymlinkCreationAllowed but with Test::File::Win32 namespace instead of Win32
126 #
127 0 my(undef, $major, $minor, $build) = Test::File::Win32::GetOSVersion();
128  
129 # Vista was the first Windows version with symlink support
130 0 ▶ return !!0 if $major < 6;
Branch: 1 branch
BranchTrueFalseTotal
if $major < 6 0 0 0
131  
132 # Since Windows 10 1703, enabling the developer mode allows to create
133 # symlinks regardless of process privileges
134 0 ▶ if ($major > 10 || ($major == 10 && ($minor > 0 || $build > 15063))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major > 10 or $major == 10 and $minor > 0 || $build > 15063) 0 0 0
Condition0%0/9
$major > 10 or $major == 10 and $minor > 0 || $build > 15063
l!l&&r!l&&!r
000
$major == 10 and $minor > 0 || $build > 15063
!ll&&!rl&&r
000
$minor > 0 or $build > 15063
l!l&&r!l&&!r
000
MC/DC0%0/4
$major > 10 or $major == 10 and $minor > 0 || $build > 15063
$major > 10 $major == 10 $minor > 0 $build > 15063
Truth table0%0/5
A or B and C or D
A= $major > 10B= $major == 10C= $minor > 0D= $build > 15063
ABCDresult
1XXX1
011X1
01011
00XX0
01000
135 0 ▶ return !!1 if Test::File::Win32::IsDeveloperModeEnabled();
Branch: 1 branch
BranchTrueFalseTotal
if Test::File::Win32::IsDeveloperModeEnabled() 0 0 0
136 }
137  
138 0 my $privs = Test::File::Win32::GetProcessPrivileges();
139  
140 0 ▶ return !!0 unless $privs;
Branch: 1 branch
BranchTrueFalseTotal
unless $privs 0 0 0
141  
142 # It doesn't matter if the permission is enabled or not, it just has to
143 # exist. CreateSymbolicLink() will automatically enable it when needed.
144 0 return exists $privs->{SeCreateSymbolicLinkPrivilege};
145 }
146  
147 1 1;
148 }
149  
150 =item has_symlinks
151  
152 Returns true is this module thinks that the current system supports
153 symlinks.
154  
155 This is not a test function. It's something that tests can use to
156 determine what it should expect or skip.
157  
158 =cut
159  
160 1 ▶ sub has_symlinks { ! _no_symlinks_here() }
Subroutine has_symlinks: called CC=1 SCAR=0.0
Pod documented
161 }
162  
163 # owner_is and owner_isn't should skip on OS where the question makes no
164 # sense. I really don't know a good way to test for that, so I'm going
165 # to skip on the two OS's that I KNOW aren't multi-user. I'd love to add
166 # more if anyone knows of any
167 # Note: I don't have a dos or mac os < 10 machine to test this on
168 31 ▶sub _obviously_non_multi_user {
Subroutine _obviously_non_multi_user: called CC=6 SCAR=17.9
169 31 ▶ foreach my $os ( qw(dos MacOS) ) { return 1 if $^O eq $os }
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq $os 6 51 57
170  
171 25 ▶ return 0 if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 5 20 25
172  
173 20 eval { my $holder = getpwuid(0) };
174 20 ▶ return 1 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 19 20
175  
176 19 eval { my $holder = getgrgid(0) };
177 19 ▶ return 1 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 18 19
178  
179 18 return 0;
180 }
181  
182 =item file_exists_ok( FILENAME [, NAME ] )
183  
184 Ok if the file exists, and not ok otherwise.
185  
186 =cut
187  
188 11 ▶sub file_exists_ok {
Subroutine file_exists_ok: called CC=3 SCAR=11.0
Pod documented
189 11 my $filename = _normalize( shift );
190 11 ▶ my $name = shift || "$filename exists";
Condition100%2/2
shift() || "$filename exists"
l!l
110
MC/DC100%1/1
shift() || "$filename exists"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
191  
192 11 my $ok = -e $filename;
193  
194 11 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 10 1 11
195 10 $Test->ok(1, $name);
196 }
197 else {
198 1 $Test->diag("file [$filename] does not exist");
199 1 $Test->ok(0, $name);
200 }
201 }
202  
203 =item file_not_exists_ok( FILENAME [, NAME ] )
204  
205 Ok if the file does not exist, and not okay if it does exist.
206  
207 =cut
208  
209 5 ▶sub file_not_exists_ok {
Subroutine file_not_exists_ok: called CC=3 SCAR=11.0
Pod documented
210 5 my $filename = _normalize( shift );
211 5 ▶ my $name = shift || "$filename does not exist";
Condition100%2/2
shift() || "$filename does not exist"
l!l
14
MC/DC100%1/1
shift() || "$filename does not exist"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
212  
213 5 my $ok = not -e $filename;
214  
215 5 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 4 1 5
216 4 $Test->ok(1, $name);
217 }
218 else {
219 1 $Test->diag("file [$filename] exists");
220 1 $Test->ok(0, $name);
221 }
222 }
223  
224 =item file_empty_ok( FILENAME [, NAME ] )
225  
226 Ok if the file exists and has empty size, not ok if the file does not
227 exist or exists with non-zero size.
228  
229 Previously this tried to test any sort of file. Sometime in the future
230 this will fail if the argument is not a plain file or is a directory.
231  
232 =cut
233  
234 4 ▶sub file_empty_ok {
Subroutine file_empty_ok: called CC=4 SCAR=13.9
Pod documented
235 4 my $filename = _normalize( shift );
236 4 ▶ my $name = shift || "$filename is empty";
Condition100%2/2
shift() || "$filename is empty"
l!l
13
MC/DC100%1/1
shift() || "$filename is empty"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
237  
238 4 ▶ return $Test->ok( 0, $name ) unless _is_plain_file( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_file($filename) 1 3 4
239  
240 3 my $ok = -z $filename;
241  
242 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
243 2 $Test->ok(1, $name);
244 }
245 else {
246 1 $Test->diag( "file [$filename] exists with non-zero size" );
247 1 $Test->ok(0, $name);
248 }
249 }
250  
251 =item file_not_empty_ok( FILENAME [, NAME ] )
252  
253 Ok if the file exists and has non-zero size, not ok if the file does
254 not exist or exists with zero size.
255  
256 Previously this tried to test any sort of file. Sometime in the future
257 this will fail if the argument is not a plain file or is a directory.
258  
259 =cut
260  
261 7 ▶sub file_not_empty_ok {
Subroutine file_not_empty_ok: called CC=4 SCAR=13.9
Pod documented
262 7 my $filename = _normalize( shift );
263 7 ▶ my $name = shift || "$filename is not empty";
Condition100%2/2
shift() || "$filename is not empty"
l!l
16
MC/DC100%1/1
shift() || "$filename is not empty"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
264  
265 7 ▶ return $Test->ok( 0, $name ) unless _is_plain_file( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_file($filename) 2 5 7
266  
267 5 my $ok = not -z _;
268  
269 5 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 3 2 5
270 3 $Test->ok(1, $name);
271 }
272 else {
273 2 $Test->diag( "file [$filename] exists with zero size" );
274 2 $Test->ok(0, $name);
275 }
276 }
277  
278 =item file_size_ok( FILENAME, SIZE [, NAME ] )
279  
280 Ok if the file exists and has SIZE size in bytes (exactly), not ok if
281 the file does not exist or exists with size other than SIZE.
282  
283 Previously this tried to test any sort of file. Sometime in the future
284 this will fail if the argument is not a plain file or is a directory.
285  
286 =cut
287  
288 4 ▶sub file_size_ok {
Subroutine file_size_ok: called CC=4 SCAR=13.9
Pod documented
289 4 my $filename = _normalize( shift );
290 4 my $expected = int shift;
291 4 ▶ my $name = shift || "$filename has right size";
Condition100%2/2
shift() || "$filename has right size"
l!l
13
MC/DC100%1/1
shift() || "$filename has right size"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
292  
293 4 ▶ return $Test->ok( 0, $name ) unless _is_plain_file( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_file($filename) 1 3 4
294  
295 3 my $ok = ( -s $filename ) == $expected;
296  
297 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
298 2 $Test->ok(1, $name);
299 }
300 else {
301 1 my $actual = -s $filename;
302 1 $Test->diag(
303 "file [$filename] has actual size [$actual] not [$expected]" );
304  
305 1 $Test->ok(0, $name);
306 }
307 }
308  
309 =item file_max_size_ok( FILENAME, MAX [, NAME ] )
310  
311 Ok if the file exists and has size less than or equal to MAX bytes, not
312 ok if the file does not exist or exists with size greater than MAX
313 bytes.
314  
315 Previously this tried to test any sort of file. Sometime in the future
316 this will fail if the argument is not a plain file or is a directory.
317  
318 =cut
319  
320 4 ▶sub file_max_size_ok {
Subroutine file_max_size_ok: called CC=4 SCAR=13.9
Pod documented
321 4 my $filename = _normalize( shift );
322 4 my $max = int shift;
323 4 ▶ my $name = shift || "$filename is under $max bytes";
Condition100%2/2
shift() || "$filename is under $max bytes"
l!l
13
MC/DC100%1/1
shift() || "$filename is under $max bytes"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
324  
325 4 ▶ return $Test->ok( 0, $name ) unless _is_plain_file( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_file($filename) 1 3 4
326  
327 3 my $ok = ( -s $filename ) <= $max;
328  
329 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
330 2 $Test->ok(1, $name);
331 }
332 else {
333 1 my $actual = -s $filename;
334 1 $Test->diag(
335 "file [$filename] has actual size [$actual] " .
336 "greater than [$max]"
337 );
338  
339 1 $Test->ok(0, $name);
340 }
341 }
342  
343 =item file_min_size_ok( FILENAME, MIN [, NAME ] )
344  
345 Ok if the file exists and has size greater than or equal to MIN bytes,
346 not ok if the file does not exist or exists with size less than MIN
347 bytes.
348  
349 Previously this tried to test any sort of file. Sometime in the future
350 this will fail if the argument is not a plain file or is a directory.
351  
352 =cut
353  
354 4 ▶sub file_min_size_ok {
Subroutine file_min_size_ok: called CC=4 SCAR=13.9
Pod documented
355 4 my $filename = _normalize( shift );
356 4 my $min = int shift;
357 4 ▶ my $name = shift || "$filename is over $min bytes";
Condition100%2/2
shift() || "$filename is over $min bytes"
l!l
13
MC/DC100%1/1
shift() || "$filename is over $min bytes"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
358  
359 4 ▶ return $Test->ok( 0, $name ) unless _is_plain_file( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_file($filename) 1 3 4
360  
361 3 my $ok = ( -s $filename ) >= $min;
362  
363 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
364 2 $Test->ok(1, $name);
365 }
366 else {
367 1 my $actual = -s $filename;
368 1 $Test->diag(
369 "file [$filename] has actual size ".
370 "[$actual] less than [$min]"
371 );
372  
373 1 $Test->ok(0, $name);
374 }
375 }
376  
377 =item file_line_count_is( FILENAME, COUNT [, NAME ] )
378  
379 Ok if the file exists and has COUNT lines (exactly), not ok if the
380 file does not exist or exists with a line count other than COUNT.
381  
382 This function uses the current value of C<$/> as the line ending and
383 counts the lines by reading them and counting how many it read.
384  
385 Previously this tried to test any sort of file. Sometime in the future
386 this will fail if the argument is not a plain file or is a directory.
387  
388 =cut
389  
390 sub _ENOFILE () { -1 }
391 sub _ECANTOPEN () { -2 }
392 sub _ENOTPLAIN () { -3 }
393  
394 9 ▶sub _file_line_counter {
Subroutine _file_line_counter: called CC=5 SCAR=16.4
395 9 my $filename = shift;
396  
397 9 ▶ return _ENOFILE unless -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 0 9 9
398 9 ▶ return _ENOTPLAIN unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 9 9
399 9 ▶ return _ECANTOPEN unless open my( $fh ), "<", $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless open my $fh, "<", $filename 0 9 9
400  
401 9 my $count = 0;
402 9 ▶ while( <$fh> ) { $count++ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 18 9 27
403  
404 9 return $count;
405 }
406  
407 # XXX: lots of cut and pasting here, needs refactoring
408 # looks like the refactoring might be worse than this though
409 4 ▶sub file_line_count_is {
Subroutine file_line_count_is: called CC=9 SCAR=23.5
Pod documented
410 4 my $filename = _normalize( shift );
411 4 my $expected = shift;
412 4 my $name = do {
413 18 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
414 4 ▶ shift || "$filename line count is $expected lines";
Condition50%1/2
shift() || "$filename line count is $expected lines"
l!l
04
MC/DC0%0/1
shift() || "$filename line count is $expected lines"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
415 };
416  
417 4 ▶ return $Test->ok( 0, $name ) unless _is_plain_file( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_file($filename) 1 3 4
418  
419 3 ▶ unless( defined $expected && int( $expected ) == $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $expected and int $expected == $expected) 1 2 3
Condition66%2/3
defined $expected and int $expected == $expected
!ll&&!rl&&r
102
MC/DC50%1/2
defined $expected and int $expected == $expected
defined $expected int $expected == $expected
Truth table66%2/3
A and B
A= defined $expectedB= int $expected == $expected
ABresult
0X0
100
111
420 18 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
421 1 $Test->diag( "file_line_count_is expects a positive whole number for " .
422 "the second argument. Got [$expected]" );
423 1 return $Test->ok( 0, $name );
424 }
425  
426 2 my $got = _file_line_counter( $filename );
427  
428 2 ▶ if( $got eq _ENOFILE ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($got eq -1) { } 0 2 2
elsif ($got eq -3) { } 0 2 2
elsif ($got == -2) { } 0 2 2
elsif ($got == $expected) { } 1 1 2
429 0 $Test->diag( "file [$filename] does not exist" );
430 0 $Test->ok( 0, $name );
431 }
432 elsif( $got eq _ENOTPLAIN ) {
433 0 $Test->diag( "file [$filename] is not a plain file" );
434 0 $Test->ok( 0, $name );
435 }
436 elsif( $got == _ECANTOPEN ) {
437 0 $Test->diag( "file [$filename] could not be opened: \$! is [$!]" );
438 0 $Test->ok( 0, $name );
439 }
440 elsif( $got == $expected ) {
441 1 $Test->ok( 1, $name );
442 }
443 else {
444 1 $Test->diag( "expected [$expected] lines in [$filename], " .
445 "got [$got] lines" );
446 1 $Test->ok( 0, $name );
447 }
448  
449 }
450  
451 =item file_line_count_isnt( FILENAME, COUNT [, NAME ] )
452  
453 Ok if the file exists and doesn't have exactly COUNT lines, not ok if
454 the file does not exist or exists with a line count of COUNT. Read
455 that carefully: the file must exist for this test to pass!
456  
457 This function uses the current value of C<$/> as the line ending and
458 counts the lines by reading them and counting how many it read.
459  
460 Previously this tried to test any sort of file. Sometime in the future
461 this will fail if the argument is not a plain file or is a directory.
462  
463 =cut
464  
465 5 ▶sub file_line_count_isnt {
Subroutine file_line_count_isnt: called CC=9 SCAR=23.5
Pod documented
466 5 my $filename = _normalize( shift );
467 5 my $expected = shift;
468 5 my $name = do {
469 18 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
470 5 ▶ shift || "$filename line count is not $expected lines";
Condition50%1/2
shift() || "$filename line count is not $expected lines"
l!l
05
MC/DC0%0/1
shift() || "$filename line count is not $expected lines"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
471 };
472  
473 5 ▶ return $Test->ok( 0, $name ) unless _is_plain_file( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_file($filename) 1 4 5
474  
475 4 ▶ unless( defined $expected && int( $expected ) == $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $expected and int $expected == $expected) 1 3 4
Condition66%2/3
defined $expected and int $expected == $expected
!ll&&!rl&&r
103
MC/DC50%1/2
defined $expected and int $expected == $expected
defined $expected int $expected == $expected
Truth table66%2/3
A and B
A= defined $expectedB= int $expected == $expected
ABresult
0X0
100
111
476 18 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
477 1 $Test->diag( "file_line_count_is expects a positive whole number for " .
478 "the second argument. Got [$expected]" );
479 1 return $Test->ok( 0, $name );
480 }
481  
482 3 my $got = _file_line_counter( $filename );
483  
484 3 ▶ if( $got eq _ENOFILE ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($got eq -1) { } 0 3 3
elsif ($got eq -3) { } 0 3 3
elsif ($got == -2) { } 0 3 3
elsif ($got != $expected) { } 2 1 3
485 0 $Test->diag( "file [$filename] does not exist" );
486 0 $Test->ok( 0, $name );
487 }
488 elsif( $got eq _ENOTPLAIN ) {
489 0 $Test->diag( "file [$filename] is not a plain file" );
490 0 $Test->ok( 0, $name );
491 }
492 elsif( $got == _ECANTOPEN ) {
493 0 $Test->diag( "file [$filename] could not be opened: \$! is [$!]" );
494 0 $Test->ok( 0, $name );
495 }
496 elsif( $got != $expected ) {
497 2 $Test->ok( 1, $name );
498 }
499 else {
500 1 $Test->diag( "expected something other than [$expected] lines in [$filename], " .
501 "but got [$got] lines" );
502 1 $Test->ok( 0, $name );
503 }
504  
505 }
506  
507 =item file_line_count_between( FILENAME, MIN, MAX, [, NAME ] )
508  
509 Ok if the file exists and has a line count between MIN and MAX,
510 inclusively.
511  
512 This function uses the current value of C<$/> as the line ending and
513 counts the lines by reading them and counting how many it read.
514  
515 Previously this tried to test any sort of file. Sometime in the future
516 this will fail if the argument is not a plain file or is a directory.
517  
518 =cut
519  
520 8 ▶sub file_line_count_between {
Subroutine file_line_count_between: called CC=11 SCAR=25.7
Pod documented
521 8 my $filename = _normalize( shift );
522 8 my $min = shift;
523 8 my $max = shift;
524  
525 8 my $name = do {
526 18 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
527 8 ▶ shift || "$filename line count is between [$min] and [$max] lines";
Condition50%1/2
shift() || "$filename line count is between [$min] and [$max] lines"
l!l
08
MC/DC0%0/1
shift() || "$filename line count is between [$min] and [$max] lines"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
528 };
529 8 ▶ return $Test->ok( 0, $name ) unless _is_plain_file( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_file($filename) 3 5 8
530  
531 5 foreach my $ref ( \$min, \$max ) {
532 9 ▶ unless( defined $$ref && int( $$ref ) == $$ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$ref and int $$ref == $$ref) 1 8 9
Condition66%2/3
defined $$ref and int $$ref == $$ref
!ll&&!rl&&r
108
MC/DC50%1/2
defined $$ref and int $$ref == $$ref
defined $$ref int $$ref == $$ref
Truth table66%2/3
A and B
A= defined $$refB= int $$ref == $$ref
ABresult
0X0
100
111
533 18 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
534 1 $Test->diag( "file_line_count_between expects positive whole numbers for " .
535 "the second and third arguments. Got [$min] and [$max]" );
536 1 return $Test->ok( 0, $name );
537 }
538 }
539  
540 4 my $got = _file_line_counter( $filename );
541  
542 4 ▶ if( $got eq _ENOFILE ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($got eq -1) { } 0 4 4
elsif ($got eq -3) { } 0 4 4
elsif ($got == -2) { } 0 4 4
elsif ($min <= $got and $got <= $max) { } 3 1 4
543 0 $Test->diag( "file [$filename] does not exist" );
544 0 $Test->ok( 0, $name );
545 }
546 elsif( $got eq _ENOTPLAIN ) {
547 0 $Test->diag( "file [$filename] is not a plain file" );
548 0 $Test->ok( 0, $name );
549 }
550 elsif( $got == _ECANTOPEN ) {
551 0 $Test->diag( "file [$filename] could not be opened: \$! is [$!]" );
552 0 $Test->ok( 0, $name );
553 }
554 ▶ elsif( $min <= $got and $got <= $max ) {
Condition66%2/3
$min <= $got and $got <= $max
!ll&&!rl&&r
103
MC/DC50%1/2
$min <= $got and $got <= $max
$min <= $got $got <= $max
Truth table66%2/3
A and B
A= $min <= $gotB= $got <= $max
ABresult
0X0
100
111
555 3 $Test->ok( 1, $name );
556 }
557 else {
558 1 $Test->diag( "expected a line count between [$min] and [$max] " .
559 "in [$filename], but got [$got] lines"
560 );
561 1 $Test->ok( 0, $name );
562 }
563 }
564  
565 =item file_contains_like ( FILENAME, PATTERN [, NAME ] )
566  
567 Ok if the file exists and its contents (as one big string) match
568 PATTERN, not ok if the file does not exist, is not readable, or exists
569 but doesn't match PATTERN.
570  
571 Since the file contents are read into memory, you should not use this
572 for large files. Besides memory consumption, test diagnostics for
573 failing tests might be difficult to decipher. However, for short
574 files this works very well.
575  
576 Because the entire contents are treated as one large string, you can
577 make a pattern that tests multiple lines. Don't forget that you may
578 need to use the /s modifier for such patterns:
579  
580 # make sure file has one or more paragraphs with CSS class X
581 file_contains_like($html_file, qr{<p class="X">.*?</p>}s);
582  
583 Contrariwise, if you need to match at the beginning or end of a line
584 inside the file, use the /m modifier:
585  
586 # make sure file has a setting for foo
587 file_contains_like($config_file, qr/^ foo \s* = \s* \w+ $/mx);
588  
589 If you want to test your file contents against multiple patterns, but
590 don't want to have the file read in repeatedly, you can pass an
591 arrayref of patterns instead of a single pattern, like so:
592  
593 # make sure our template has rendered correctly
594 file_contains_like($template_out,
595 [
596 qr/^ $title_line $/mx,
597 map { qr/^ $_ $/mx } @chapter_headings,
598 qr/^ $footer_line $/mx,
599 ]);
600  
601 Please note that if you do this, and your file does not exist or is
602 not readable, you'll only get one test failure instead of a failure
603 for each pattern. This could cause your test plan to be off, although
604 you may not care at that point because your test failed anyway. If
605 you do care, either skip the test plan altogether by employing
606 L<Test::More>'s C<done_testing()> function, or use
607 L</file_readable_ok> in conjunction with a C<SKIP> block.
608  
609 Contributed by Buddy Burden C<< <barefoot@cpan.org> >>.
610  
611 =item file_contains_unlike ( FILENAME, PATTERN [, NAME ] )
612  
613 Ok if the file exists and its contents (as one big string) do B<not>
614 match PATTERN, not ok if the file does not exist, is not readable, or
615 exists but matches PATTERN.
616  
617 All notes and caveats for L</file_contains_like> apply to this
618 function as well.
619  
620 Contributed by Buddy Burden C<< <barefoot@cpan.org> >>.
621  
622 =item file_contains_utf8_like ( FILENAME, PATTERN [, NAME ] )
623  
624 The same as C<file_contains_like>, except the file is opened as UTF-8.
625  
626 =item file_contains_utf8_unlike ( FILENAME, PATTERN [, NAME ] )
627  
628 The same as C<file_contains_unlike>, except the file is opened as UTF-8.
629  
630 =item file_contains_encoded_like ( FILENAME, ENCODING, PATTERN [, NAME ] )
631  
632 The same as C<file_contains_like>, except the file is opened with ENCODING
633  
634 =item file_contains_encoded_unlike ( FILENAME, ENCODING, PATTERN [, NAME ] )
635  
636 The same as C<file_contains_unlike>, except the file is opened with ENCODING.
637  
638 =cut
639  
640 9 ▶sub file_contains_like {
Subroutine file_contains_like: called CC=1 SCAR=0.0
Pod documented
641 9 local $Test::Builder::Level = $Test::Builder::Level + 1;
642 9 _file_contains(like => "contains", undef, @_);
643 }
644  
645 9 ▶sub file_contains_unlike {
Subroutine file_contains_unlike: called CC=1 SCAR=0.0
Pod documented
646 9 local $Test::Builder::Level = $Test::Builder::Level + 1;
647 9 _file_contains(unlike => "doesn't contain", undef, @_);
648 }
649  
650 5 ▶sub file_contains_utf8_like {
Subroutine file_contains_utf8_like: called CC=1 SCAR=0.0
Pod documented
651 5 local $Test::Builder::Level = $Test::Builder::Level + 1;
652 5 _file_contains(like => "contains", 'UTF-8', @_);
653 }
654  
655 5 ▶sub file_contains_utf8_unlike {
Subroutine file_contains_utf8_unlike: called CC=1 SCAR=0.0
Pod documented
656 5 local $Test::Builder::Level = $Test::Builder::Level + 1;
657 5 _file_contains(unlike => "doesn't contain", 'UTF-8', @_);
658 }
659  
660 5 ▶sub file_contains_encoded_like {
Subroutine file_contains_encoded_like: called CC=1 SCAR=0.0
Pod documented
661 5 local $Test::Builder::Level = $Test::Builder::Level + 1;
662 5 my $filename = shift;
663 5 my $encoding = shift;
664 5 _file_contains(like => "contains", $encoding, $filename, @_);
665 }
666  
667 5 ▶sub file_contains_encoded_unlike {
Subroutine file_contains_encoded_unlike: called CC=1 SCAR=0.0
Pod documented
668 5 local $Test::Builder::Level = $Test::Builder::Level + 1;
669 5 my $filename = shift;
670 5 my $encoding = shift;
671 5 _file_contains(unlike => "doesn't contain", $encoding, $filename, @_);
672 }
673  
674 38 ▶sub _file_contains {
Subroutine _file_contains: called CC=9 SCAR=22.0
675 38 my $method = shift;
676 38 my $verb = shift;
677 38 my $encoding = shift;
678 38 my $filename = _normalize( shift );
679 38 my $patterns = shift;
680 38 my $name = shift;
681  
682 38 my (@patterns, %patterns);
683 38 ▶ if (ref $patterns eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $patterns eq 'ARRAY') { } 22 16 38
684 22 @patterns = @$patterns;
685 22 ▶ %patterns = map { $_ => $name || "$filename $verb $_" } @patterns;
Condition100%2/2
$name || "$filename $verb $_"
l!l
1232
MC/DC100%1/1
$name || "$filename $verb $_"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
686 }
687 else {
688 16 @patterns = ($patterns);
689 16 ▶ %patterns = ( $patterns => $name || "$filename $verb $patterns" );
Condition50%1/2
$name || "$filename $verb $patterns"
l!l
016
MC/DC0%0/1
$name || "$filename $verb $patterns"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
690 }
691  
692 # for purpose of checking the file's existence, just use the first
693 # test name as the name
694 38 $name = $patterns{$patterns[0]};
695  
696 38 ▶ return $Test->ok( 0, $name ) unless _is_plain_file( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_file($filename) 4 34 38
697  
698 34 ▶ unless( -r $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $filename) 4 30 34
699 4 $Test->diag( "file [$filename] is not readable" );
700 4 return $Test->ok(0, $name);
701 }
702  
703 # do the slurp
704 30 my $file_contents;
705 {
706 30 ▶ unless (open(FH, $filename)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FH, $filename) 0 30 30
707 0 $Test->diag( "file [$filename] could not be opened: \$! is [$!]" );
708 0 return $Test->ok( 0, $name );
709 }
710  
711 30 ▶ if (defined $encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding) 20 10 30
712 20 binmode FH, ":encoding($encoding)";
713 }
714  
715 30 local $/ = undef;
716 30 $file_contents = <FH>;
717 30 close FH;
718 }
719  
720 30 foreach my $p (@patterns) {
721 48 $Test->$method($file_contents, $p, $patterns{$p});
722 }
723 }
724  
725 =item file_readable_ok( FILENAME [, NAME ] )
726  
727 Ok if the file exists and is readable, not ok if the file does not
728 exist or is not readable.
729  
730 =cut
731  
732 2 ▶sub file_readable_ok {
Subroutine file_readable_ok: called CC=3 SCAR=11.6
Pod documented
733 2 my $filename = _normalize( shift );
734 2 ▶ my $name = shift || "$filename is readable";
Condition100%2/2
shift() || "$filename is readable"
l!l
11
MC/DC100%1/1
shift() || "$filename is readable"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
735  
736 2 my $ok = -r $filename;
737  
738 2 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 0 2
739 2 $Test->ok(1, $name);
740 }
741 else {
742 0 $Test->diag( "file [$filename] is not readable" );
743 0 $Test->ok(0, $name);
744 }
745 }
746  
747 =item file_not_readable_ok( FILENAME [, NAME ] )
748  
749 Ok if the file exists and is not readable, not ok if the file does not
750 exist or is readable.
751  
752 =cut
753  
754 3 ▶sub file_not_readable_ok {
Subroutine file_not_readable_ok: called CC=3 SCAR=11.0
Pod documented
755 3 my $filename = _normalize( shift );
756 3 ▶ my $name = shift || "$filename is not readable";
Condition100%2/2
shift() || "$filename is not readable"
l!l
12
MC/DC100%1/1
shift() || "$filename is not readable"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
757  
758 3 my $ok = not -r $filename;
759  
760 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
761 2 $Test->ok(1, $name);
762 }
763 else {
764 1 $Test->diag( "file [$filename] is readable" );
765 1 $Test->ok(0, $name);
766 }
767 }
768  
769 =item file_writable_ok( FILENAME [, NAME ] )
770  
771 =item file_writeable_ok( FILENAME [, NAME ] )
772  
773 Ok if the file exists and is writable, not ok if the file does not
774 exist or is not writable.
775  
776 The original name is C<file_writeable_ok> with that extra I<e>. That
777 still works but there's a function with the correct spelling too.
778  
779 =cut
780  
781 0 ▶sub file_writeable_ok {
Subroutine file_writeable_ok: not called CC=1 SCAR=6.9
Pod documented
782 0 carp "file_writeable_ok is now file_writable_ok";
783  
784 0 &file_writable_ok;
785 }
786  
787 3 ▶sub file_writable_ok {
Subroutine file_writable_ok: called CC=3 SCAR=11.0
Pod documented
788 3 my $filename = _normalize( shift );
789 3 ▶ my $name = shift || "$filename is writable";
Condition100%2/2
shift() || "$filename is writable"
l!l
12
MC/DC100%1/1
shift() || "$filename is writable"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
790  
791 3 my $ok = -w $filename;
792  
793 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
794 2 $Test->ok(1, $name);
795 }
796 else {
797 1 $Test->diag( "file [$filename] is not writable" );
798 1 $Test->ok(0, $name);
799 }
800 }
801  
802 =item file_not_writeable_ok( FILENAME [, NAME ] )
803  
804 =item file_not_writable_ok( FILENAME [, NAME ] )
805  
806 Ok if the file exists and is not writable, not ok if the file does not
807 exist or is writable.
808  
809 The original name is C<file_not_writeable_ok> with that extra I<e>.
810 That still works but there's a function with the correct spelling too.
811  
812 =cut
813  
814 0 ▶sub file_not_writeable_ok {
Subroutine file_not_writeable_ok: not called CC=1 SCAR=6.9
Pod documented
815 0 carp "file_not_writeable_ok is now file_not_writable_ok";
816  
817 0 &file_not_writable_ok;
818 }
819  
820 2 ▶sub file_not_writable_ok {
Subroutine file_not_writable_ok: called CC=3 SCAR=11.0
Pod documented
821 2 my $filename = _normalize( shift );
822 2 ▶ my $name = shift || "$filename is not writable";
Condition50%1/2
shift() || "$filename is not writable"
l!l
02
MC/DC0%0/1
shift() || "$filename is not writable"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
823  
824 2 my $ok = not -w $filename;
825  
826 2 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 1 1 2
827 1 $Test->ok(1, $name);
828 }
829 else {
830 1 $Test->diag("file [$filename] is writable");
831 1 $Test->ok(0, $name);
832 }
833 }
834  
835 =item file_executable_ok( FILENAME [, NAME ] )
836  
837 Ok if the file exists and is executable, not ok if the file does not
838 exist or is not executable.
839  
840 This test automatically skips if it thinks it is on a Windows
841 platform.
842  
843 =cut
844  
845 4 ▶sub file_executable_ok {
Subroutine file_executable_ok: called CC=4 SCAR=13.9
Pod documented
846 4 ▶ if( _win32() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_win32()) 1 3 4
847 1 $Test->skip( "file_executable_ok doesn't work on Windows" );
848 1 return;
849 }
850  
851 3 my $filename = _normalize( shift );
852 3 ▶ my $name = shift || "$filename is executable";
Condition100%2/2
shift() || "$filename is executable"
l!l
12
MC/DC100%1/1
shift() || "$filename is executable"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
853  
854 3 my $ok = -x $filename;
855  
856 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
857 2 $Test->ok(1, $name);
858 }
859 else {
860 1 $Test->diag("file [$filename] is not executable");
861 1 $Test->ok(0, $name);
862 }
863 }
864  
865 =item file_not_executable_ok( FILENAME [, NAME ] )
866  
867 Ok if the file exists and is not executable, not ok if the file does
868 not exist or is executable.
869  
870 This test automatically skips if it thinks it is on a Windows
871 platform.
872  
873 =cut
874  
875 4 ▶sub file_not_executable_ok {
Subroutine file_not_executable_ok: called CC=4 SCAR=13.9
Pod documented
876 4 ▶ if( _win32() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_win32()) 1 3 4
877 1 $Test->skip( "file_not_executable_ok doesn't work on Windows" );
878 1 return;
879 }
880  
881 3 my $filename = _normalize( shift );
882 3 ▶ my $name = shift || "$filename is not executable";
Condition100%2/2
shift() || "$filename is not executable"
l!l
12
MC/DC100%1/1
shift() || "$filename is not executable"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
883  
884 3 my $ok = not -x $filename;
885  
886 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
887 2 $Test->ok(1, $name);
888 }
889 else {
890 1 $Test->diag("file [$filename] is executable");
891 1 $Test->ok(0, $name);
892 }
893 }
894  
895 =item file_mode_is( FILENAME, MODE [, NAME ] )
896  
897 Ok if the file exists and the mode matches, not ok if the file does
898 not exist or the mode does not match.
899  
900 This test automatically skips if it thinks it is on a Windows
901 platform.
902  
903 Contributed by Shawn Sorichetti C<< <ssoriche@coloredblocks.net> >>
904  
905 =cut
906  
907 6 ▶sub file_mode_is {
Subroutine file_mode_is: called CC=5 SCAR=16.1
Pod documented
908 6 ▶ if( _win32() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_win32()) 1 5 6
909 1 $Test->skip( "file_mode_is doesn't work on Windows" );
910 1 return;
911 }
912  
913 5 my $filename = _normalize( shift );
914 5 my $mode = shift;
915  
916 5 ▶ my $name = shift || sprintf("%s mode is %04o", $filename, $mode);
Condition66%2/3
shift() || sprintf('%s mode is %04o', $filename, $mode)
l!l&&r!l&&!r
140
MC/DC0%0/2
shift() || sprintf('%s mode is %04o', $filename, $mode)
shift() sprintf('%s mode is %04o', $filename, $mode)
Truth table66%2/3
A || B
A= shift()B= sprintf('%s mode is %04o', $filename, $mode)
ABresult
1X1
011
000
917  
918 5 ▶ my $ok = -e $filename && ((stat($filename))[2] & 07777) == $mode;
Condition66%2/3
-e $filename && ((stat $filename)[2] & 4095) == $mode
!ll&&!rl&&r
014
MC/DC50%1/2
-e $filename && ((stat $filename)[2] & 4095) == $mode
-e $filename ((stat $filename)[2] & 4095) == $mode
Truth table66%2/3
A && B
A= -e $filenameB= ((stat $filename)[2] & 4095) == $mode
ABresult
0X0
100
111
919  
920 5 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 4 1 5
921 4 $Test->ok(1, $name);
922 }
923 else {
924 1 $Test->diag(sprintf("file [%s] mode is not %04o", $filename, $mode) );
925 1 $Test->ok(0, $name);
926 }
927 }
928  
929 =item file_mode_isnt( FILENAME, MODE [, NAME ] )
930  
931 Ok if the file exists and mode does not match, not ok if the file does
932 not exist or mode does match.
933  
934 This test automatically skips if it thinks it is on a Windows
935 platform.
936  
937 Contributed by Shawn Sorichetti C<< <ssoriche@coloredblocks.net> >>
938  
939 =cut
940  
941 6 ▶sub file_mode_isnt {
Subroutine file_mode_isnt: called CC=5 SCAR=16.1
Pod documented
942 6 ▶ if( _win32() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_win32()) 1 5 6
943 1 $Test->skip( "file_mode_isnt doesn't work on Windows" );
944 1 return;
945 }
946  
947 5 my $filename = _normalize( shift );
948 5 my $mode = shift;
949  
950 5 ▶ my $name = shift || sprintf("%s mode is not %04o",$filename,$mode);
Condition66%2/3
shift() || sprintf('%s mode is not %04o', $filename, $mode)
l!l&&r!l&&!r
140
MC/DC0%0/2
shift() || sprintf('%s mode is not %04o', $filename, $mode)
shift() sprintf('%s mode is not %04o', $filename, $mode)
Truth table66%2/3
A || B
A= shift()B= sprintf('%s mode is not %04o', $filename, $mode)
ABresult
1X1
011
000
951  
952 5 ▶ my $ok = not (-e $filename && ((stat($filename))[2] & 07777) == $mode);
Condition66%2/3
-e $filename && ((stat $filename)[2] & 4095) == $mode
!ll&&!rl&&r
041
MC/DC50%1/2
-e $filename && ((stat $filename)[2] & 4095) == $mode
-e $filename ((stat $filename)[2] & 4095) == $mode
Truth table66%2/3
A && B
A= -e $filenameB= ((stat $filename)[2] & 4095) == $mode
ABresult
0X0
100
111
953  
954 5 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 4 1 5
955 4 $Test->ok(1, $name);
956 }
957 else {
958 1 $Test->diag(sprintf("file [%s] mode is %04o",$filename,$mode));
959 1 $Test->ok(0, $name);
960 }
961 }
962  
963 =item file_mode_has( FILENAME, MODE [, NAME ] )
964  
965 Ok if the file exists and has all the bits in mode turned on, not ok
966 if the file does not exist or the mode does not match. That is, C<<
967 FILEMODE & MODE == MODE >> must be true.
968  
969 This test automatically skips if it thinks it is on a Windows
970 platform.
971  
972 Contributed by Ricardo Signes C<< <rjbs@cpan.org> >>
973  
974 =cut
975  
976 4 ▶sub file_mode_has {
Subroutine file_mode_has: called CC=6 SCAR=18.6
Pod documented
977 4 ▶ if( _win32() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_win32()) 0 4 4
978 0 $Test->skip( "file_mode_has doesn't work on Windows" );
979 0 return;
980 }
981  
982 4 my $filename = _normalize( shift );
983 4 my $mode = shift;
984  
985 4 ▶ my $name = shift || sprintf("%s mode has all bits of %04o", $filename, $mode);
Condition66%2/3
shift() || sprintf('%s mode has all bits of %04o', $filename, $mode)
l!l&&r!l&&!r
130
MC/DC0%0/2
shift() || sprintf('%s mode has all bits of %04o', $filename, $mode)
shift() sprintf('%s mode has all bits of %04o', $filename, $mode)
Truth table66%2/3
A || B
A= shift()B= sprintf('%s mode has all bits of %04o', $filename, $mode)
ABresult
1X1
011
000
986  
987 4 my $present = -e $filename;
988 4 ▶ my $gotmode = $present ? (stat($filename))[2] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$present ? : 4 0 4
989 4 ▶ my $ok = $present && ($gotmode & $mode) == $mode;
Condition66%2/3
$present && ($gotmode & $mode) == $mode
!ll&&!rl&&r
022
MC/DC50%1/2
$present && ($gotmode & $mode) == $mode
$present ($gotmode & $mode) == $mode
Truth table66%2/3
A && B
A= $presentB= ($gotmode & $mode) == $mode
ABresult
0X0
100
111
990  
991 4 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 2 4
992 2 $Test->ok(1, $name);
993 }
994 else {
995 2 my $missing = ($gotmode ^ $mode) & $mode;
996 2 $Test->diag(sprintf("file [%s] mode is missing component %04o", $filename, $missing) );
997 2 $Test->ok(0, $name);
998 }
999 }
1000  
1001 =item file_mode_hasnt( FILENAME, MODE [, NAME ] )
1002  
1003 Ok if the file exists and has all the bits in mode turned off, not ok
1004 if the file does not exist or the mode does not match. That is,
1005 C<< FILEMODE & MODE == 0 >> must be true.
1006  
1007 This test automatically skips if it thinks it is on a
1008 Windows platform.
1009  
1010 Contributed by Ricardo Signes C<< <rjbs@cpan.org> >>
1011  
1012 =cut
1013  
1014 3 ▶sub file_mode_hasnt {
Subroutine file_mode_hasnt: called CC=6 SCAR=18.6
Pod documented
1015 3 ▶ if( _win32() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_win32()) 0 3 3
1016 0 $Test->skip( "file_mode_hasnt doesn't work on Windows" );
1017 0 return;
1018 }
1019  
1020 3 my $filename = _normalize( shift );
1021 3 my $mode = shift;
1022  
1023 3 ▶ my $name = shift || sprintf("%s mode has no bits of %04o", $filename, $mode);
Condition66%2/3
shift() || sprintf('%s mode has no bits of %04o', $filename, $mode)
l!l&&r!l&&!r
120
MC/DC0%0/2
shift() || sprintf('%s mode has no bits of %04o', $filename, $mode)
shift() sprintf('%s mode has no bits of %04o', $filename, $mode)
Truth table66%2/3
A || B
A= shift()B= sprintf('%s mode has no bits of %04o', $filename, $mode)
ABresult
1X1
011
000
1024  
1025 3 my $present = -e $filename;
1026 3 ▶ my $gotmode = $present ? (stat($filename))[2] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$present ? : 3 0 3
1027 3 ▶ my $ok = $present && ($gotmode & $mode) == 0;
Condition66%2/3
$present && ($gotmode & $mode) == 0
!ll&&!rl&&r
012
MC/DC50%1/2
$present && ($gotmode & $mode) == 0
$present ($gotmode & $mode) == 0
Truth table66%2/3
A && B
A= $presentB= ($gotmode & $mode) == 0
ABresult
0X0
100
111
1028  
1029 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
1030 2 $Test->ok(1, $name);
1031 }
1032 else {
1033 1 my $bad = $gotmode & $mode;
1034 1 $Test->diag(sprintf("file [%s] mode has forbidden component %04o", $filename, $bad) );
1035 1 $Test->ok(0, $name);
1036 }
1037 }
1038  
1039 =item file_is_symlink_ok( FILENAME [, NAME ] )
1040  
1041 Ok if FILENAME is a symlink, even if it points to a non-existent
1042 file. This test automatically skips if the operating system does
1043 not support symlinks.
1044  
1045 =cut
1046  
1047 7 ▶sub file_is_symlink_ok {
Subroutine file_is_symlink_ok: called CC=4 SCAR=13.9
Pod documented
1048 7 ▶ if( _no_symlinks_here() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_no_symlinks_here()) 1 6 7
1049 1 $Test->skip(
1050 "file_is_symlink_ok doesn't work on systems without symlinks" );
1051 1 return;
1052 }
1053  
1054 6 my $file = shift;
1055 6 ▶ my $name = shift || "$file is a symlink";
Condition100%2/2
shift() || "$file is a symlink"
l!l
33
MC/DC100%1/1
shift() || "$file is a symlink"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1056  
1057 6 ▶ if( -l $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $file) { } 4 2 6
1058 4 $Test->ok(1, $name)
1059 }
1060 else {
1061 2 $Test->diag( "file [$file] is not a symlink" );
1062 2 $Test->ok(0, $name);
1063 }
1064 }
1065  
1066 =item file_is_not_symlink_ok( FILENAME [, NAME ] )
1067  
1068 Ok if FILENAME is a not symlink. This test automatically skips if the
1069 operating system does not support symlinks. If the file does not
1070 exist, the test fails.
1071  
1072 =cut
1073  
1074 0 ▶sub file_is_not_symlink_ok {
Subroutine file_is_not_symlink_ok: not called CC=5 SCAR=34.0
Pod documented
1075 0 ▶ if( _no_symlinks_here() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_no_symlinks_here()) 0 0 0
1076 0 $Test->skip(
1077 "file_is_symlink_ok doesn't work on systems without symlinks" );
1078 0 return;
1079 }
1080  
1081 0 my $file = shift;
1082 0 ▶ my $name = shift || "$file is not a symlink";
Condition0%0/2
shift() || "$file is not a symlink"
l!l
00
MC/DC0%0/1
shift() || "$file is not a symlink"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1083  
1084 0 ▶ unless( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
1085 0 $Test->diag( "file [$file] does not exist" );
1086 0 return $Test->ok(0, $name);
1087 }
1088  
1089 0 ▶ if( ! -l $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -l $file) { } 0 0 0
1090 0 $Test->ok(1, $name)
1091 }
1092 else {
1093 0 $Test->diag( "file [$file] is a symlink" );
1094 0 $Test->ok(0, $name);
1095 }
1096 }
1097  
1098 =item symlink_target_exists_ok( SYMLINK [, TARGET] [, NAME ] )
1099  
1100 Ok if FILENAME is a symlink and it points to a existing file. With the
1101 optional TARGET argument, the test fails if SYMLINK's target is not
1102 TARGET. This test automatically skips if the operating system does not
1103 support symlinks. If the file does not exist, the test fails.
1104  
1105 =cut
1106  
1107 6 ▶sub symlink_target_exists_ok {
Subroutine symlink_target_exists_ok: called CC=7 SCAR=19.5
Pod documented
1108 6 ▶ if( _no_symlinks_here() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_no_symlinks_here()) 1 5 6
1109 1 $Test->skip(
1110 "symlink_target_exists_ok doesn't work on systems without symlinks"
1111 );
1112 1 return;
1113 }
1114  
1115 5 my $file = shift;
1116 5 ▶ my $dest = shift || readlink( $file );
Condition66%2/3
shift() || readlink $file
l!l&&r!l&&!r
401
MC/DC50%1/2
shift() || readlink $file
shift() readlink $file
Truth table66%2/3
A || B
A= shift()B= readlink $file
ABresult
1X1
011
000
1117 5 ▶ my $name = shift || "$file is a symlink";
Condition100%2/2
shift() || "$file is a symlink"
l!l
14
MC/DC100%1/1
shift() || "$file is a symlink"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1118  
1119 5 ▶ unless( -l $file )
Branch: 1 branch
BranchTrueFalseTotal
unless (-l $file) 1 4 5
1120 {
1121 1 $Test->diag( "file [$file] is not a symlink" );
1122 1 return $Test->ok( 0, $name );
1123 }
1124  
1125 4 ▶ unless( -e $dest ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dest) 1 3 4
1126 1 $Test->diag( "symlink [$file] points to non-existent target [$dest]" );
1127 1 return $Test->ok( 0, $name );
1128 }
1129  
1130 3 my $actual = readlink( $file );
1131 3 ▶ unless( $dest eq $actual ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dest eq $actual) 1 2 3
1132 1 $Test->diag(
1133 "symlink [$file] points to\n" .
1134 " got: $actual\n" .
1135 " expected: $dest\n"
1136 );
1137 1 return $Test->ok( 0, $name );
1138 }
1139  
1140 2 $Test->ok( 1, $name );
1141 }
1142  
1143 =item symlink_target_dangles_ok( SYMLINK [, NAME ] )
1144  
1145 Ok if FILENAME is a symlink and if it doesn't point to a existing
1146 file. This test automatically skips if the operating system does not
1147 support symlinks. If the file does not exist, the test fails.
1148  
1149 =cut
1150  
1151 sub symlink_target_dangles_ok
1152 5 ▶ {
Subroutine symlink_target_dangles_ok: called CC=5 SCAR=16.1
Pod documented
1153 5 ▶ if( _no_symlinks_here() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_no_symlinks_here()) 1 4 5
1154 1 $Test->skip(
1155 "symlink_target_dangles_ok doesn't work on systems without symlinks" );
1156 1 return;
1157 }
1158  
1159 4 my $file = shift;
1160 4 my $dest = readlink( $file );
1161 4 ▶ my $name = shift || "$file is a symlink";
Condition100%2/2
shift() || "$file is a symlink"
l!l
31
MC/DC100%1/1
shift() || "$file is a symlink"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1162  
1163 4 ▶ unless( -l $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-l $file) 1 3 4
1164 1 $Test->diag( "file [$file] is not a symlink" );
1165 1 return $Test->ok( 0, $name );
1166 }
1167  
1168 3 ▶ if( -e $dest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dest) 2 1 3
1169 2 $Test->diag(
1170 "symlink [$file] points to existing file [$dest] but shouldn't" );
1171 2 return $Test->ok( 0, $name );
1172 }
1173  
1174 1 $Test->ok( 1, $name );
1175 }
1176  
1177 =item symlink_target_is( SYMLINK, TARGET [, NAME ] )
1178  
1179 Ok if FILENAME is a symlink and if points to TARGET. This test
1180 automatically skips if the operating system does not support symlinks.
1181 If the file does not exist, the test fails.
1182  
1183 =cut
1184  
1185 4 ▶sub symlink_target_is {
Subroutine symlink_target_is: called CC=7 SCAR=19.9
Pod documented
1186 4 ▶ if( _no_symlinks_here() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_no_symlinks_here()) 1 3 4
1187 1 $Test->skip(
1188 "symlink_target_is doesn't work on systems without symlinks" );
1189 1 return;
1190 }
1191  
1192 3 my $file = shift;
1193 3 my $dest = shift;
1194 3 ▶ my $name = shift || "symlink $file points to $dest";
Condition100%2/2
shift() || "symlink $file points to $dest"
l!l
21
MC/DC100%1/1
shift() || "symlink $file points to $dest"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1195  
1196 3 ▶ unless( -l $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-l $file) 1 2 3
1197 1 $Test->diag( "file [$file] is not a symlink" );
1198 1 return $Test->ok( 0, $name );
1199 }
1200  
1201 2 my $actual_dest = readlink( $file );
1202 2 my $link_error = $!;
1203  
1204 2 ▶ unless( defined $actual_dest ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $actual_dest) 0 2 2
1205 0 $Test->diag( "symlink [$file] does not have a defined target" );
1206 0 ▶ $Test->diag( "readlink error: $link_error" ) if defined $link_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $link_error 0 0 0
1207 0 return $Test->ok( 0, $name );
1208 }
1209  
1210 2 ▶ if( $dest eq $actual_dest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest eq $actual_dest) { } 1 1 2
1211 1 $Test->ok( 1, $name );
1212 }
1213 else {
1214 1 $Test->ok( 0, $name );
1215 1 $Test->diag(" got: $actual_dest" );
1216 1 $Test->diag(" expected: $dest" );
1217 }
1218 }
1219  
1220 =item symlink_target_is_absolute_ok( SYMLINK [, NAME ] )
1221  
1222 Ok if FILENAME is a symlink and if its target is an absolute path.
1223 This test automatically skips if the operating system does not support
1224 symlinks. If the file does not exist, the test fails.
1225  
1226 =cut
1227  
1228 0 ▶sub symlink_target_is_absolute_ok {
Subroutine symlink_target_is_absolute_ok: not called CC=10 SCAR=47.0
Pod documented
1229 0 ▶ if( _no_symlinks_here() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_no_symlinks_here()) 0 0 0
1230 0 $Test->skip(
1231 "symlink_target_exists_ok doesn't work on systems without symlinks" );
1232 0 return;
1233 }
1234  
1235 0 my( $from, $from_base, $to, $to_base, $name ) = @_;
1236 0 my $link = readlink( $from );
1237 0 ▶ my $link_err = defined( $link ) ? '' : $!; # $! doesn't always get reset
Branch: 1 branch
BranchTrueFalseTotal
defined $link ? : 0 0 0
1238 0 my $link_abs = abs_path( rel2abs($link, $from_base) );
1239 0 my $to_abs = abs_path( rel2abs($to, $to_base) );
1240  
1241 0 ▶ if (defined( $link_abs ) && defined( $to_abs ) && $link_abs eq $to_abs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $link_abs and defined $to_abs and $link_abs eq $to_abs) { } 0 0 0
Condition0%0/6
defined $link_abs and defined $to_abs and $link_abs eq $to_abs
!ll&&!rl&&r
000
defined $link_abs and defined $to_abs
!ll&&!rl&&r
000
MC/DC0%0/3
defined $link_abs and defined $to_abs and $link_abs eq $to_abs
defined $link_abs defined $to_abs $link_abs eq $to_abs
Truth table0%0/4
A and B and C
A= defined $link_absB= defined $to_absC= $link_abs eq $to_abs
ABCresult
0XX0
10X0
1100
1111
1242 0 $Test->ok( 1, $name );
1243 }
1244 else {
1245 0 $Test->ok( 0, $name );
1246 0 ▶ $link ||= 'undefined';
Condition0%0/2
$link ||= "undefined"
l!l
00
MC/DC0%0/1
$link ||= "undefined"
$link
Truth table0%0/2
A
A= $link
Aresult
11
00
1247 0 ▶ $link_abs ||= 'undefined';
Condition0%0/2
$link_abs ||= "undefined"
l!l
00
MC/DC0%0/1
$link_abs ||= "undefined"
$link_abs
Truth table0%0/2
A
A= $link_abs
Aresult
11
00
1248 0 ▶ $to_abs ||= 'undefined';
Condition0%0/2
$to_abs ||= "undefined"
l!l
00
MC/DC0%0/1
$to_abs ||= "undefined"
$to_abs
Truth table0%0/2
A
A= $to_abs
Aresult
11
00
1249  
1250 0 $Test->diag(" link: $from");
1251 0 $Test->diag(" got: $link");
1252 0 $Test->diag(" (abs): $link_abs");
1253 0 $Test->diag(" expected: $to");
1254 0 $Test->diag(" (abs): $to_abs");
1255 0 ▶ $Test->diag(" readlink() error: $link_err") if ($link_err);
Branch: 1 branch
BranchTrueFalseTotal
if $link_err 0 0 0
1256 }
1257 }
1258  
1259 =item dir_exists_ok( DIRECTORYNAME [, NAME ] )
1260  
1261 Ok if the file exists and is a directory, not ok if the file doesn't exist, or exists but isn't a
1262 directory.
1263  
1264 Contributed by Buddy Burden C<< <barefoot@cpan.org> >>.
1265  
1266 =cut
1267  
1268 4 ▶sub dir_exists_ok {
Subroutine dir_exists_ok: called CC=4 SCAR=13.9
Pod documented
1269 4 my $filename = _normalize( shift );
1270 4 ▶ my $name = shift || "$filename is a directory";
Condition100%2/2
shift() || "$filename is a directory"
l!l
13
MC/DC100%1/1
shift() || "$filename is a directory"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1271  
1272 4 ▶ unless( -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $filename) 1 3 4
1273 1 $Test->diag( "directory [$filename] does not exist" );
1274 1 return $Test->ok(0, $name);
1275 }
1276  
1277 3 my $ok = -d $filename;
1278  
1279 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
1280 2 $Test->ok(1, $name);
1281 }
1282 else {
1283 1 $Test->diag( "file [$filename] exists but is not a directory" );
1284 1 $Test->ok(0, $name);
1285 }
1286 }
1287  
1288 =item dir_contains_ok( DIRECTORYNAME, FILENAME [, NAME ] )
1289  
1290 Ok if the directory exists and contains the file, not ok if the directory doesn't exist, or exists
1291 but doesn't contain the file.
1292  
1293 Contributed by Buddy Burden C<< <barefoot@cpan.org> >>.
1294  
1295 =cut
1296  
1297 4 ▶sub dir_contains_ok {
Subroutine dir_contains_ok: called CC=4 SCAR=13.9
Pod documented
1298 4 my $dirname = _normalize( shift );
1299 4 my $filename = _normalize( shift );
1300 4 ▶ my $name = shift || "directory $dirname contains file $filename";
Condition100%2/2
shift() || "directory $dirname contains file $filename"
l!l
13
MC/DC100%1/1
shift() || "directory $dirname contains file $filename"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1301  
1302 4 ▶ unless( -d $dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dirname) 1 3 4
1303 1 $Test->diag( "directory [$dirname] does not exist" );
1304 1 return $Test->ok(0, $name);
1305 }
1306  
1307 3 my $ok = -e File::Spec->catfile($dirname, $filename);
1308  
1309 3 ▶ if( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 2 1 3
1310 2 $Test->ok(1, $name);
1311 }
1312 else {
1313 1 $Test->diag( "file [$filename] does not exist in directory $dirname" );
1314 1 $Test->ok(0, $name);
1315 }
1316 }
1317  
1318 =item link_count_is_ok( FILE, LINK_COUNT [, NAME ] )
1319  
1320 Ok if the link count to FILE is LINK_COUNT. LINK_COUNT is interpreted
1321 as an integer. A LINK_COUNT that evaluates to 0 returns Ok if the file
1322 does not exist.
1323  
1324  
1325 =cut
1326  
1327 3 ▶sub link_count_is_ok {
Subroutine link_count_is_ok: called CC=3 SCAR=11.0
Pod documented
1328 3 my $file = shift;
1329 3 my $count = int( 0 + shift );
1330  
1331 3 ▶ my $name = shift || "$file has a link count of [$count]";
Condition100%2/2
shift() || "$file has a link count of [$count]"
l!l
21
MC/DC100%1/1
shift() || "$file has a link count of [$count]"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1332  
1333 3 my $actual = ( stat $file )[3];
1334  
1335 3 ▶ unless( $actual == $count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($actual == $count) 1 2 3
1336 1 $Test->diag(
1337 "file [$file] points has [$actual] links: expected [$count]" );
1338 1 return $Test->ok( 0, $name );
1339 }
1340  
1341 2 $Test->ok( 1, $name );
1342 }
1343  
1344 =item link_count_gt_ok( FILE, LINK_COUNT [, NAME ] )
1345  
1346 Ok if the link count to FILE is greater than LINK_COUNT. LINK_COUNT is
1347 interpreted as an integer. A LINK_COUNT that evaluates to 0 returns Ok
1348 if the file has at least one link.
1349  
1350 =cut
1351  
1352 3 ▶sub link_count_gt_ok {
Subroutine link_count_gt_ok: called CC=3 SCAR=11.0
Pod documented
1353 3 my $file = shift;
1354 3 my $count = int( 0 + shift );
1355  
1356 3 ▶ my $name = shift || "$file has a link count of [$count]";
Condition100%2/2
shift() || "$file has a link count of [$count]"
l!l
21
MC/DC100%1/1
shift() || "$file has a link count of [$count]"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1357  
1358 3 my $actual = (stat $file )[3];
1359  
1360 3 ▶ unless( $actual > $count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($actual > $count) 1 2 3
1361 1 $Test->diag(
1362 "file [$file] points has [$actual] links: ".
1363 "expected more than [$count]" );
1364 1 return $Test->ok( 0, $name );
1365 }
1366  
1367 2 $Test->ok( 1, $name );
1368 }
1369  
1370 =item link_count_lt_ok( FILE, LINK_COUNT [, NAME ] )
1371  
1372 Ok if the link count to FILE is less than LINK_COUNT. LINK_COUNT is
1373 interpreted as an integer. A LINK_COUNT that evaluates to 0 returns Ok
1374 if the file has at least one link.
1375  
1376 =cut
1377  
1378 3 ▶sub link_count_lt_ok {
Subroutine link_count_lt_ok: called CC=3 SCAR=11.0
Pod documented
1379 3 my $file = shift;
1380 3 my $count = int( 0 + shift );
1381  
1382 3 ▶ my $name = shift || "$file has a link count of [$count]";
Condition100%2/2
shift() || "$file has a link count of [$count]"
l!l
21
MC/DC100%1/1
shift() || "$file has a link count of [$count]"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1383  
1384 3 my $actual = (stat $file )[3];
1385  
1386 3 ▶ unless( $actual < $count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($actual < $count) 1 2 3
1387 1 $Test->diag(
1388 "file [$file] points has [$actual] links: ".
1389 "expected less than [$count]" );
1390 1 return $Test->ok( 0, $name );
1391 }
1392  
1393 2 $Test->ok( 1, $name );
1394 }
1395  
1396  
1397 # owner_is, owner_isnt, group_is and group_isnt are almost
1398 # identical in the beginning, so I'm writing a skeleton they can all use.
1399 # I can't think of a better name...
1400 22 ▶sub _dm_skeleton {
Subroutine _dm_skeleton: called CC=4 SCAR=13.9
1401 18 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1402  
1403 22 ▶ if( _obviously_non_multi_user() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_obviously_non_multi_user()) 3 19 22
1404 3 my $calling_sub = (caller(1))[3];
1405 3 $Test->skip( $calling_sub . " only works on a multi-user OS" );
1406 3 return 'skip';
1407 }
1408  
1409 19 my $filename = _normalize( shift );
1410 19 my $testing_for = shift;
1411 19 my $name = shift;
1412  
1413 19 ▶ unless( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 1 18 19
1414 1 $Test->diag( "file name not specified" );
1415 1 return $Test->ok( 0, $name );
1416 }
1417  
1418 18 ▶ unless( -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $filename) 1 17 18
1419 1 $Test->diag( "file [$filename] does not exist" );
1420 1 return $Test->ok( 0, $name );
1421 }
1422  
1423 17 return;
1424 }
1425  
1426 =item owner_is( FILE , OWNER [, NAME ] )
1427  
1428 Ok if FILE's owner is the same as OWNER. OWNER may be a text user name
1429 or a numeric userid. Test skips on Dos, and Mac OS <= 9.
1430 If the file does not exist, the test fails.
1431  
1432 Contributed by Dylan Martin
1433  
1434 =cut
1435  
1436 4 ▶sub owner_is {
Subroutine owner_is: called CC=9 SCAR=23.5
Pod documented
1437 4 my $filename = shift;
1438 4 my $owner = shift;
1439 4 ▶ my $name = shift || "$filename belongs to $owner";
Condition100%2/2
shift() || "$filename belongs to $owner"
l!l
22
MC/DC100%1/1
shift() || "$filename belongs to $owner"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1440  
1441 4 my $err = _dm_skeleton( $filename, $owner, $name );
1442 4 ▶ return if( defined( $err ) && $err eq 'skip' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $err and $err eq "skip" 0 4 4
Condition33%1/3
defined $err and $err eq "skip"
!ll&&!rl&&r
400
MC/DC0%0/2
defined $err and $err eq "skip"
defined $err $err eq "skip"
Truth table33%1/3
A and B
A= defined $errB= $err eq "skip"
ABresult
0X0
100
111
1443 4 ▶ return $err if defined($err);
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 4 4
1444  
1445 4 my $owner_uid = _get_uid( $owner );
1446 4 ▶ unless( defined $owner_uid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $owner_uid) 1 3 4
1447 1 $Test->diag("user [$owner] does not exist on this system");
1448 1 return $Test->ok( 0, $name );
1449 }
1450  
1451 3 my $file_uid = ( stat $filename )[4];
1452  
1453 3 ▶ unless( defined $file_uid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file_uid) 0 3 3
1454 0 $Test->skip("stat failed to return owner uid for $filename");
1455 0 return;
1456 }
1457  
1458 3 ▶ return $Test->ok( 1, $name ) if $file_uid == $owner_uid;
Branch: 1 branch
BranchTrueFalseTotal
if $file_uid == $owner_uid 2 1 3
1459  
1460 1 my $real_owner = ( getpwuid $file_uid )[0];
1461 1 ▶ unless( defined $real_owner ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $real_owner) 0 1 1
1462 0 $Test->diag("file does not belong to $owner");
1463 0 return $Test->ok( 0, $name );
1464 }
1465  
1466 1 $Test->diag( "file [$filename] belongs to $real_owner ($file_uid), ".
1467 "not $owner ($owner_uid)" );
1468 1 return $Test->ok( 0, $name );
1469 }
1470  
1471 =item owner_isnt( FILE, OWNER [, NAME ] )
1472  
1473 Ok if FILE's owner is not the same as OWNER. OWNER may be a text user name
1474 or a numeric userid. Test skips on Dos and Mac OS <= 9. If the file
1475 does not exist, the test fails.
1476  
1477 Contributed by Dylan Martin
1478  
1479 =cut
1480  
1481 4 ▶sub owner_isnt {
Subroutine owner_isnt: called CC=7 SCAR=19.7
Pod documented
1482 4 my $filename = shift;
1483 4 my $owner = shift;
1484 4 ▶ my $name = shift || "$filename doesn't belong to $owner";
Condition100%2/2
shift() || "$filename doesn't belong to $owner"
l!l
22
MC/DC100%1/1
shift() || "$filename doesn't belong to $owner"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1485  
1486 4 my $err = _dm_skeleton( $filename, $owner, $name );
1487 4 ▶ return if( defined( $err ) && $err eq 'skip' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $err and $err eq "skip" 0 4 4
Condition33%1/3
defined $err and $err eq "skip"
!ll&&!rl&&r
400
MC/DC0%0/2
defined $err and $err eq "skip"
defined $err $err eq "skip"
Truth table33%1/3
A and B
A= defined $errB= $err eq "skip"
ABresult
0X0
100
111
1488 4 ▶ return $err if defined($err);
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 4 4
1489  
1490 4 my $owner_uid = _get_uid( $owner );
1491 4 ▶ unless( defined $owner_uid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $owner_uid) 1 3 4
1492 1 return $Test->ok( 1, $name );
1493 }
1494  
1495 3 my $file_uid = ( stat $filename )[4];
1496  
1497 #$Test->diag( "owner_isnt: $owner_uid $file_uid" );
1498 3 ▶ return $Test->ok( 1, $name ) if $file_uid != $owner_uid;
Branch: 1 branch
BranchTrueFalseTotal
if $file_uid != $owner_uid 2 1 3
1499  
1500 1 $Test->diag( "file [$filename] belongs to $owner ($owner_uid)" );
1501 1 return $Test->ok( 0, $name );
1502 }
1503  
1504 =item group_is( FILE , GROUP [, NAME ] )
1505  
1506 Ok if FILE's group is the same as GROUP. GROUP may be a text group name or
1507 a numeric group id. Test skips on Dos, Mac OS <= 9 and any other operating
1508 systems that do not support getpwuid() and friends. If the file does not
1509 exist, the test fails.
1510  
1511 Contributed by Dylan Martin
1512  
1513 =cut
1514  
1515 5 ▶sub group_is {
Subroutine group_is: called CC=9 SCAR=23.5
Pod documented
1516 5 my $filename = shift;
1517 5 my $group = shift;
1518 5 ▶ my $name = ( shift || "$filename belongs to group $group" );
Condition100%2/2
shift() || "$filename belongs to group $group"
l!l
41
MC/DC100%1/1
shift() || "$filename belongs to group $group"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1519  
1520 5 my $err = _dm_skeleton( $filename, $group, $name );
1521 5 ▶ return if( defined( $err ) && $err eq 'skip' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $err and $err eq "skip" 0 5 5
Condition33%1/3
defined $err and $err eq "skip"
!ll&&!rl&&r
500
MC/DC0%0/2
defined $err and $err eq "skip"
defined $err $err eq "skip"
Truth table33%1/3
A and B
A= defined $errB= $err eq "skip"
ABresult
0X0
100
111
1522 5 ▶ return $err if defined($err);
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 5 5
1523  
1524 5 my $group_gid = _get_gid( $group );
1525 5 ▶ unless( defined $group_gid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $group_gid) 1 4 5
1526 1 $Test->diag("group [$group] does not exist on this system");
1527 1 return $Test->ok( 0, $name );
1528 }
1529  
1530 4 my $file_gid = ( stat $filename )[5];
1531  
1532 4 ▶ unless( defined $file_gid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file_gid) 0 4 4
1533 0 $Test->skip("stat failed to return group gid for $filename");
1534 0 return;
1535 }
1536  
1537 4 ▶ return $Test->ok( 1, $name ) if $file_gid == $group_gid;
Branch: 1 branch
BranchTrueFalseTotal
if $file_gid == $group_gid 3 1 4
1538  
1539 1 my $real_group = ( getgrgid $file_gid )[0];
1540 1 ▶ unless( defined $real_group ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $real_group) 0 1 1
1541 0 $Test->diag("file does not belong to $group");
1542 0 return $Test->ok( 0, $name );
1543 }
1544  
1545 1 $Test->diag( "file [$filename] belongs to $real_group ($file_gid), ".
1546 "not $group ($group_gid)" );
1547  
1548 1 return $Test->ok( 0, $name );
1549 }
1550  
1551 =item group_isnt( FILE , GROUP [, NAME ] )
1552  
1553 Ok if FILE's group is not the same as GROUP. GROUP may be a text group name or
1554 a numeric group id. Test skips on Dos, Mac OS <= 9 and any other operating
1555 systems that do not support getpwuid() and friends. If the file does not
1556 exist, the test fails.
1557  
1558 Contributed by Dylan Martin
1559  
1560 =cut
1561  
1562 4 ▶sub group_isnt {
Subroutine group_isnt: called CC=7 SCAR=20.6
Pod documented
1563 4 my $filename = shift;
1564 4 my $group = shift;
1565 4 ▶ my $name = shift || "$filename does not belong to group $group";
Condition100%2/2
shift() || "$filename does not belong to group $group"
l!l
31
MC/DC100%1/1
shift() || "$filename does not belong to group $group"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1566  
1567 4 my $err = _dm_skeleton( $filename, $group, $name );
1568 4 ▶ return if( defined( $err ) && $err eq 'skip' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $err and $err eq "skip" 0 4 4
Condition33%1/3
defined $err and $err eq "skip"
!ll&&!rl&&r
400
MC/DC0%0/2
defined $err and $err eq "skip"
defined $err $err eq "skip"
Truth table33%1/3
A and B
A= defined $errB= $err eq "skip"
ABresult
0X0
100
111
1569 4 ▶ return $err if defined($err);
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 4 4
1570  
1571 4 my $group_gid = _get_gid( $group );
1572 4 my $file_gid = ( stat $filename )[5];
1573  
1574 4 ▶ unless( defined $file_gid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file_gid) 0 4 4
1575 0 $Test->skip("stat failed to return group gid for $filename");
1576 0 return;
1577 }
1578  
1579 4 ▶ return $Test->ok( 1, $name ) if $file_gid != $group_gid;
Branch: 1 branch
BranchTrueFalseTotal
if $file_gid != $group_gid 3 1 4
1580  
1581 1 $Test->diag( "file [$filename] belongs to $group ($group_gid)" );
1582 1 return $Test->ok( 0, $name );
1583 }
1584  
1585 8 ▶sub _get_uid {
Subroutine _get_uid: called CC=8 SCAR=22.1
1586 8 my $arg = shift;
1587  
1588 # the name might be numeric (why would you do that?), so we need
1589 # to figure out which of several possibilities we have. And, 0 means
1590 # root, so we have to be very careful with the values.
1591  
1592 # maybe the argument is a UID. First, it has to be numeric. If it's
1593 # a UID, we'll get the same UID back. But, if we get back a value
1594 # that doesn't mean that we are done. There might be a name with
1595 # the same value.
1596 #
1597 # Don't use this value in comparisons! An undef could be turned
1598 # into zero!
1599 8 ▶ my $from_uid = (getpwuid($arg))[2] if $arg =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A[0-9]+\z/ 2 6 8
1600  
1601 # Now try the argument as a name. If it's a name, then we'll get
1602 # back a UID. Maybe we get back nothing.
1603 8 my $from_nam = (getpwnam($arg))[2];
1604  
1605 8 return do {
1606 # first case, we got back nothing from getpwnam but did get
1607 # something from getpwuid. The arg is not a name and is a
1608 # UID.
1609 8 ▶ if( defined $from_uid and not defined $from_nam ) { $arg }
Branch: 3 branches
BranchTrueFalseTotal
if (defined $from_uid and not defined $from_nam) { } 2 6 8
elsif (not defined $from_uid and defined $from_nam) { } 4 2 6
elsif (not defined $from_uid || defined $from_nam) { } 2 0 2
Condition66%2/3
defined $from_uid and not defined $from_nam
!ll&&!rl&&r
602
MC/DC50%1/2
defined $from_uid and not defined $from_nam
defined $from_uid not defined $from_nam
Truth table66%2/3
A and not(B)
A= defined $from_uidB= not defined $from_nam
ABresult
0X0
100
111
1610 # second case, we got back nothing from getpwuid but did get
1611 # something from getpwnam. The arg is a name and is not a
1612 # UID.
1613 4 ▶ elsif( not defined $from_uid and defined $from_nam ) { $from_nam }
Condition66%2/3
not defined $from_uid and defined $from_nam
!ll&&!rl&&r
024
MC/DC50%1/2
not defined $from_uid and defined $from_nam
not defined $from_uid defined $from_nam
Truth table66%2/3
not(A) and B
A= not defined $from_uidB= defined $from_nam
ABresult
0X0
100
111
1614 # Now, what happens if neither are defined? The argument does
1615 # not correspond to a name or GID on the system. Since no such
1616 # user exists, we return undef.
1617 2 ▶ elsif( not defined $from_uid and not defined $from_nam ) { undef }
Condition33%1/3
defined $from_uid || defined $from_nam
l!l&&r!l&&!r
002
MC/DC0%0/2
defined $from_uid || defined $from_nam
defined $from_uid defined $from_nam
Truth table33%1/3
A || B
A= defined $from_uidB= defined $from_nam
ABresult
1X1
011
000
1618 # But what if they are both defined? The argument could represent
1619 # a UID and a name, and those could be different users! In this
1620 # case, we'll choose the original argument. That might be wrong,
1621 # so the best we can do is a warning.
1622 else {
1623 0 carp( "Found both a UID or name for <$arg>. Guessing the UID is <$arg>." );
1624 0 $arg
1625 }
1626 };
1627 }
1628  
1629 9 ▶sub _get_gid {
Subroutine _get_gid: called CC=8 SCAR=22.1
1630 9 my $arg = shift;
1631  
1632 # the name might be numeric (why would you do that?), so we need
1633 # to figure out which of several possibilities we have. And, 0 means
1634 # root, so we have to be very careful with the values.
1635  
1636 # maybe the argument is a GID. First, it has to be numeric. If it's
1637 # a GID, we'll get the same GID back. But, if we get back a value
1638 # that doesn't mean that we are done. There might be a name with
1639 # the same value.
1640 #
1641 # Don't use this value in comparisons! An undef could be turned
1642 # into zero!
1643 9 ▶ my $from_gid = (getgrgid($arg))[2] if $arg =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A[0-9]+\z/ 3 6 9
1644  
1645 # Now try the argument as a name. If it's a name, then we'll get
1646 # back a GID. Maybe we get back nothing.
1647 9 my $from_nam = (getgrnam($arg))[2];
1648  
1649 9 return do {
1650 # first case, we got back nothing from getgrnam but did get
1651 # something from getpwuid. The arg is not a name and is a
1652 # GID.
1653 9 ▶ if( defined $from_gid and not defined $from_nam ) { $arg }
Branch: 3 branches
BranchTrueFalseTotal
if (defined $from_gid and not defined $from_nam) { } 3 6 9
elsif (not defined $from_gid and defined $from_nam) { } 5 1 6
elsif (not defined $from_gid || defined $from_nam) { } 1 0 1
Condition66%2/3
defined $from_gid and not defined $from_nam
!ll&&!rl&&r
603
MC/DC50%1/2
defined $from_gid and not defined $from_nam
defined $from_gid not defined $from_nam
Truth table66%2/3
A and not(B)
A= defined $from_gidB= not defined $from_nam
ABresult
0X0
100
111
1654 # second case, we got back nothing from getgrgid but did get
1655 # something from getgrnam. The arg is a name and is not a
1656 # GID.
1657 5 ▶ elsif( not defined $from_gid and defined $from_nam ) { $from_nam }
Condition66%2/3
not defined $from_gid and defined $from_nam
!ll&&!rl&&r
015
MC/DC50%1/2
not defined $from_gid and defined $from_nam
not defined $from_gid defined $from_nam
Truth table66%2/3
not(A) and B
A= not defined $from_gidB= defined $from_nam
ABresult
0X0
100
111
1658 # Now, what happens if neither are defined? The argument does
1659 # not correspond to a name or GID on the system. Since no such
1660 # user exists, we return undef.
1661 1 ▶ elsif( not defined $from_gid and not defined $from_nam ) { undef }
Condition33%1/3
defined $from_gid || defined $from_nam
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $from_gid || defined $from_nam
defined $from_gid defined $from_nam
Truth table33%1/3
A || B
A= defined $from_gidB= defined $from_nam
ABresult
1X1
011
000
1662 # But what if they are both defined? The argument could represent
1663 # a GID and a name, and those could be different users! In this
1664 # case, we'll choose the original argument. That might be wrong,
1665 # so the best we can do is a warning.
1666 else {
1667 0 carp( "Found both a GID or name for <$arg>. Guessing the GID is <$arg>." );
1668 0 $arg;
1669 }
1670 };
1671 }
1672  
1673 =item file_mtime_age_ok( FILE [, WITHIN_SECONDS ] [, NAME ] )
1674  
1675 Ok if FILE's modified time is WITHIN_SECONDS inclusive of the system's current time.
1676 This test uses stat() to obtain the mtime. If the file does not exist the test
1677 returns failure. If stat() fails, the test is skipped.
1678  
1679 =cut
1680  
1681 4 ▶sub file_mtime_age_ok {
Subroutine file_mtime_age_ok: called CC=5 SCAR=16.1
Pod documented
1682 4 my $filename = shift;
1683 4 ▶ my $within_secs = shift || 0;
Condition100%2/2
shift() || 0
l!l
31
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1684 4 ▶ my $name = shift || "$filename mtime within $within_secs seconds of current time";
Condition100%2/2
shift() || "$filename mtime within $within_secs seconds of current time"
l!l
22
MC/DC100%1/1
shift() || "$filename mtime within $within_secs seconds of current time"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1685  
1686 4 my $time = time();
1687  
1688 4 my $filetime = _stat_file($filename, 9);
1689  
1690 4 ▶ return if ( $filetime == -1 ); #skip
Branch: 1 branch
BranchTrueFalseTotal
if $filetime == -1 0 4 4
1691  
1692 4 ▶ return $Test->ok(1, $name) if ( $filetime + $within_secs > $time-1 );
Branch: 1 branch
BranchTrueFalseTotal
if $filetime + $within_secs > $time - 1 2 2 4
1693  
1694 2 $Test->diag( "file [$filename] mtime [$filetime] is not $within_secs seconds within current system time [$time].");
1695 2 return $Test->ok(0, $name);
1696 }
1697  
1698 =item file_mtime_gt_ok( FILE, UNIXTIME [, NAME ] )
1699  
1700 Ok if FILE's mtime is > UNIXTIME. This test uses stat() to get the mtime. If stat() fails
1701 this test is skipped. If FILE does not exist, this test fails.
1702  
1703 =cut
1704  
1705 3 ▶sub file_mtime_gt_ok {
Subroutine file_mtime_gt_ok: called CC=4 SCAR=13.9
Pod documented
1706 3 my $filename = shift;
1707 3 my $time = int shift;
1708 3 ▶ my $name = shift || "$filename mtime is greater than unix timestamp $time";
Condition100%2/2
shift() || "$filename mtime is greater than unix timestamp $time"
l!l
21
MC/DC100%1/1
shift() || "$filename mtime is greater than unix timestamp $time"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1709  
1710 3 my $filetime = _stat_file($filename, 9);
1711  
1712 3 ▶ return if ( $filetime == -1 ); #skip
Branch: 1 branch
BranchTrueFalseTotal
if $filetime == -1 0 3 3
1713  
1714 3 ▶ return $Test->ok(1, $name) if ( $filetime > $time );
Branch: 1 branch
BranchTrueFalseTotal
if $filetime > $time 2 1 3
1715  
1716 1 $Test->diag( "file [$filename] mtime [$filetime] not greater than $time" );
1717 1 $Test->ok(0, $name);
1718 }
1719  
1720 =item file_mtime_lt_ok( FILE, UNIXTIME, [, NAME ] )
1721  
1722 Ok if FILE's modified time is < UNIXTIME. This test uses stat() to get the mtime. If stat() fails
1723 this test is skipped. If FILE does not exist, this test fails.
1724  
1725 =cut
1726  
1727 3 ▶sub file_mtime_lt_ok {
Subroutine file_mtime_lt_ok: called CC=4 SCAR=13.9
Pod documented
1728 3 my $filename = shift;
1729 3 my $time = int shift;
1730 3 ▶ my $name = shift || "$filename mtime less than unix timestamp $time";
Condition100%2/2
shift() || "$filename mtime less than unix timestamp $time"
l!l
21
MC/DC100%1/1
shift() || "$filename mtime less than unix timestamp $time"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1731  
1732 # gets mtime
1733 3 my $filetime = _stat_file($filename, 9);
1734  
1735 3 ▶ return if ( $filetime == -1 ); #skip
Branch: 1 branch
BranchTrueFalseTotal
if $filetime == -1 0 3 3
1736  
1737 3 ▶ return $Test->ok(1, $name) if ( $filetime < $time );
Branch: 1 branch
BranchTrueFalseTotal
if $filetime < $time 2 1 3
1738  
1739 1 $Test->diag( "file [$filename] mtime [$filetime] not less than $time" );
1740 1 $Test->ok(0, $name);
1741 }
1742  
1743 # private function to safely stat a file
1744 #
1745 # Arugments:
1746 # filename file to perform on
1747 # attr_pos pos of the array returned from stat we want to compare. perldoc -f stat
1748 #
1749 # Returns:
1750 # -1 - stat failed
1751 # 0 - failure (file doesn't exist etc)
1752 # filetime - on success, time requested provided by stat
1753 #
1754 12 ▶sub _stat_file {
Subroutine _stat_file: called CC=4 SCAR=14.0
1755 12 my $filename = _normalize( shift );
1756 12 my $attr_pos = shift;
1757  
1758 12 ▶ unless( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 1 11 12
1759 1 $Test->diag( "file name not specified" );
1760 1 return 0;
1761 }
1762  
1763 11 ▶ unless( -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $filename) 1 10 11
1764 1 $Test->diag( "file [$filename] does not exist" );
1765 1 return 0;
1766 }
1767  
1768 10 my $filetime = ( stat($filename) )[$attr_pos];
1769  
1770 10 ▶ unless( $filetime ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filetime) 0 10 10
1771 0 $Test->diag( "stat of $filename failed" );
1772 0 return -1; #skip on stat failure
1773 }
1774  
1775 10 return $filetime;
1776 }
1777  
1778 =back
1779  
1780 =head1 TO DO
1781  
1782 * check properties for other users (readable_by_root, for instance)
1783  
1784 * check times
1785  
1786 * check number of links to file
1787  
1788 * check path parts (directory, filename, extension)
1789  
1790 =head1 SEE ALSO
1791  
1792 L<Test::Builder>,
1793 L<Test::More>
1794  
1795 If you are using the new C<Test2> stuff, see Test2::Tools::File
1796 (https://github.com/torbjorn/Test2-Tools-File).
1797  
1798 =head1 SOURCE AVAILABILITY
1799  
1800 This module is in Github:
1801  
1802 https://github.com/briandfoy/test-file
1803  
1804 =head1 AUTHOR
1805  
1806 brian d foy, C<< <briandfoy@pobox.com> >>
1807  
1808 =head1 CREDITS
1809  
1810 Shawn Sorichetti C<< <ssoriche@coloredblocks.net> >> provided
1811 some functions.
1812  
1813 Tom Metro helped me figure out some Windows capabilities.
1814  
1815 Dylan Martin added C<owner_is> and C<owner_isnt>.
1816  
1817 David Wheeler added C<file_line_count_is>.
1818  
1819 Buddy Burden C<< <barefoot@cpan.org> >> provided C<dir_exists_ok>,
1820 C<dir_contains_ok>, C<file_contains_like>, and
1821 C<file_contains_unlike>.
1822  
1823 xmikew C<< <https://github.com/xmikew> >> provided the C<mtime_age>
1824 stuff.
1825  
1826 Torbjørn Lindahl is working on L<Test2::Tools::File> and we're
1827 working together to align our interfaces.
1828  
1829 Jean-Damien Durand added bits to use Win32::IsSymlinkCreationAllowed,
1830 new since Win32 0.55.
1831  
1832 =head1 COPYRIGHT AND LICENSE
1833  
1834 Copyright © 2002-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
1835  
1836 This program is free software; you can redistribute it and/or modify
1837 it under the terms of the Artistic License 2.0
1838  
1839 =cut
1840  
1841 18 "The quick brown fox jumped over the lazy dog";
↑ Summary