Infrastructure/File.pm

filter: Statementstmt 33.0% 40 / 121 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 22.2% 6 / 27 Podpod 0.0% 0 / 1 totaltotal 27.8% 46 / 165 CC 10 SCAR 17.1
CC
10
Cov
29%
__ANON__
20.0
__ANON__
12.0
__ANON__
6.0
CRAP 5.6
« Infrastructure/Swap.pm ↑ Summary Infrastructure/DNS.pm »
1 package Test::BDD::Infrastructure::File;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.005'; # VERSION
7 # ABSTRACT: cucumber step definitions for checking files
8
9 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Test::BDD::Cucumber::StepFile qw( Given When Then );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub S { Test::BDD::Cucumber::StepFile::S }
Subroutine S: not called CC=1 SCAR=6.9
Pod undocumented
13  
14 1 ▶use Test::BDD::Infrastructure::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 convert_unit convert_cmp_operator $CMP_OPERATOR_RE convert_interval);
16  
17 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 0 ▶Given qr/the (?:file|directory) (\S+) exists/, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
21 0 my $path = $1;
22  
23 0 ▶ if( ! -e $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path) 0 0 0
24 0 fail("the file $path does not exist");
25 }
26 0 S->{'path'} = $path;
27 1 };
28  
29  
30 # perl file test operators
31 0 ▶Then qr/the file must be empty/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 my $path = S->{'path'};
33 0 ok( -z $path, "file $path must be empty");
34 1 };
35 0 ▶Then qr/the file must be non-zero size/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 0 my $path = S->{'path'};
37 0 ok( -s $path, "file $path must be non-zero size");
38 1 };
39 0 ▶Then qr/the file must be non-zero size/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 my $path = S->{'path'};
41 0 ok( -s $path, "file $path must be non-zero size");
42 1 };
43 0 ▶Then qr/the file type must be plain file/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 my $path = S->{'path'};
45 0 ok( -f $path, "file $path must be a plain file");
46 1 };
47 0 ▶Then qr/the file type must be directory/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 my $path = S->{'path'};
49 0 ok( -d $path, "file $path must be a directory");
50 1 };
51 0 ▶Then qr/the file type must be symbolic link/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 my $path = S->{'path'};
53 0 ok( -l $path, "file $path must be a symbolic link");
54 1 };
55 0 ▶Then qr/the file type must be socket/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 my $path = S->{'path'};
57 0 ok( -S $path, "file $path must be a socket");
58 1 };
59 0 ▶Then qr/the file type must be pipe/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 my $path = S->{'path'};
61 0 ok( -p $path, "file $path must be a pipe");
62 1 };
63 0 ▶Then qr/the file type must be block device/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 my $path = S->{'path'};
65 0 ok( -b $path, "file $path must be a block device");
66 1 };
67 0 ▶Then qr/the file type must be character device/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 my $path = S->{'path'};
69 0 ok( -c $path, "file $path must be a character device");
70 1 };
71  
72  
73 # stat()
74 0 ▶Then qr/the file mode must be (\d+)/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 my $mode = $1;
76 0 my $path = S->{'path'};
77 0 my $file_mode = sprintf("%04o", (stat($path))[2] & oct(7777) );
78 0 cmp_ok( $file_mode, 'eq', $mode, "file mode of $path must be $mode");
79 1 };
80 0 ▶Then qr/the file must be owned by user (\S+)/, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
81 0 my $uid = $1;
82 0 ▶ if( $uid !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uid =~ /^\d+$/) 0 0 0
83 0 $uid = getpwnam( $uid );
84 }
85 0 my $path = S->{'path'};
86 0 cmp_ok( (stat($path))[4], 'eq', $uid, "file owner of $path must be uid $uid");
87 1 };
88 0 ▶Then qr/the file must be owned by group (\S+)/, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
89 0 my $gid = $1;
90 0 ▶ if( $gid !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gid =~ /^\d+$/) 0 0 0
91 0 $gid = getgrnam( $gid );
92 }
93 0 my $path = S->{'path'};
94 0 cmp_ok( (stat($path))[5], 'eq', $gid, "file owner of $path must be gid $gid");
95 1 };
96 0 ▶Then qr/the file size must be $CMP_OPERATOR_RE (\d+) (\S+)/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 my $op = convert_cmp_operator( $1 );
98 0 my $size = convert_unit( $2, $3 );
99 0 my $path = S->{'path'};
100 0 cmp_ok( (stat($path))[7], $op, $size, "size of file $path must be $op $size");
101 1 };
102 0 ▶Then qr/the file ([acm]time) must be $CMP_OPERATOR_RE (\d+) (\S+)/, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
103 0 my $path = S->{'path'};
104 0 my $field = $1;
105 0 my $time;
106 0 ▶ if( $field eq 'atime' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field eq 'atime') { } 0 0 0
elsif ($field eq 'ctime') { } 0 0 0
107 0 $time = (stat($path))[8]; # atime
108 } elsif( $field eq 'ctime' ) {
109 0 $time = (stat($path))[10]; # ctime
110 } else {
111 0 $time = (stat($path))[9]; # mtime
112 }
113 0 my $op = convert_cmp_operator( $2 );
114 0 my $seconds = convert_interval( $3, $4 );
115 0 my $age = time - $time;
116 0 cmp_ok( $age, $op, $seconds, "the $field age of file $path must be $op $seconds");
117 1 };
118  
119  
120 # lines
121 0 ▶Then qr/the file must contain $CMP_OPERATOR_RE (\d+) lines/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 my $path = S->{'path'};
123 0 my $op = convert_cmp_operator( $1 );
124 0 my $count = $2;
125 0 my @content = read_file( $path );
126 0 cmp_ok( scalar(@content), $op, $count, "the file $path must contain $op $count lines");
127 1 };
128  
129 0 ▶Then qr/the files? content must (not match|match) (.*)$/, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
130 0 my $path = S->{'path'};
131 0 my $regex = $2;
132 0 my $content = read_file( $path );
133 0 ▶ if( $1 eq 'not match' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'not match') { } 0 0 0
134 0 unlike( $content, qr/$regex/, "files content must not match $regex");
135 } else {
136 0 like( $content, qr/$regex/, "files content must match $regex");
137 }
138 1 };
139  
140  
141 # directory
142 0 ▶Then qr/the directory must contain $CMP_OPERATOR_RE (\d+) files/, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 my $path = S->{'path'};
144 0 my $op = convert_cmp_operator( $1 );
145 0 my $count = $2;
146 0 my @content = read_dir( $path );
147 0 cmp_ok( scalar(@content), $op, $count, "the directory $path must contain $op $count files");
148 1 };
149 0 ▶Then qr/the directory must contain (a|no) file like (.*)$/, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
150 0 my $path = S->{'path'};
151 0 my $regex = $2;
152 0 my @files = read_dir( $path );
153 0 my $matched = 0;
154 0 foreach my $file ( @files ) {
155 0 ▶ if( $file =~ /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /$regex/) 0 0 0
156 0 $matched++;
157 0 diag('file matched: '.$file);
158 }
159 }
160 0 ▶ if( $1 eq 'no' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'no') { } 0 0 0
161 0 cmp_ok($matched, '==', 0, "no file in the directory must match $regex");
162 } else {
163 0 cmp_ok($matched, '>', 0, "a file in the directory must match $regex");
164 }
165 1 };
166  
167 1 1;
168  
169 __END__
« Infrastructure/Swap.pm ↑ Summary Infrastructure/DNS.pm »