ChangeNotify/Watcher.pm

filter: Statementstmt 95.2% 139 / 146 Branchbran 79.6% 43 / 54 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 87.8% 29 / 33 Podpod 50.0% 1 / 2 totaltotal 90.4% 217 / 240 CC 31 SCAR 13.9
CC
31
Cov
91%
_path_matches
9.0
_entry_for_map
8.0
_maybe_content
5.1
CRAP 4.0
↑ Summary ChangeNotify/Watcher/Default.pm »
1 package File::ChangeNotify::Watcher;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.31';
8  
9 3 ▶use Fcntl qw( S_IMODE );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::ChangeNotify::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Find qw( find );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 use Types::Standard
15 3 ▶ qw( ArrayRef Bool ClassName CodeRef HashRef Num RegexpRef Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Type::Utils -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Trying to import this just blows up on Win32, and checking
19 # Time::HiRes::d_hires_stat() _also_ blows up on Win32.
20 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
22 3 eval {
23 3 require Time::HiRes;
24 3 Time::HiRes->import('stat');
25 };
26 }
27  
28 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 has filter => (
31 is => 'ro',
32 isa => RegexpRef,
33 21 ▶ default => sub {qr/.*/},
Subroutine __ANON__: called CC=1 SCAR=0.0
34 3 );
35  
36 #<<<
37 my $dir_t = subtype as Str,
38 25 ▶ where { -d $_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 0 ▶ message { "$_ is not a valid directory" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
40  
41 my $array_of_dirs_t = subtype as ArrayRef[Str],
42 29 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 29 map {-d} @{$_};
44 },
45 0 ▶ message {"@{$_} is not a list of valid directories"};
Subroutine __ANON__: not called CC=1 SCAR=2.6
46  
47 coerce $array_of_dirs_t,
48 from $dir_t,
49 25 ▶ via { [$_] };
Subroutine __ANON__: called CC=1 SCAR=0.0
50 #>>>
51  
52 3 has directories => (
53 is => 'ro',
54 writer => '_set_directories',
55 isa => $array_of_dirs_t,
56 required => 1,
57 coerce => 1,
58 );
59  
60 3 has follow_symlinks => (
61 is => 'ro',
62 isa => Bool,
63 default => 0,
64 );
65  
66 3 has event_class => (
67 is => 'ro',
68 isa => ClassName,
69 default => 'File::ChangeNotify::Event',
70 );
71  
72 3 has sleep_interval => (
73 is => 'ro',
74 isa => Num,
75 default => 2,
76 );
77  
78 3 my $files_or_regexps_or_code_t
79 = subtype as ArrayRef [ Str | RegexpRef | CodeRef ];
80  
81 has exclude => (
82 is => 'ro',
83 isa => $files_or_regexps_or_code_t,
84 23 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 3 );
86  
87 3 has modify_includes_file_attributes => (
88 is => 'ro',
89 isa => Bool | $files_or_regexps_or_code_t,
90 default => 0,
91 );
92  
93 3 has modify_includes_content => (
94 is => 'ro',
95 isa => Bool | $files_or_regexps_or_code_t,
96 default => 0,
97 );
98  
99 3 has _map => (
100 is => 'ro',
101 writer => '_set_map',
102 isa => HashRef,
103 predicate => '_has_map',
104 );
105  
106 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
107 0 my $self = shift;
108  
109 0 use_module( $self->event_class );
110 }
111  
112 ## no critic ( Subroutines::ProhibitUnusedPrivateSubroutines)
113 54 ▶sub _current_map {
Subroutine _current_map: called CC=2 SCAR=6.9
114 54 my $self = shift;
115  
116 54 my %map;
117  
118 find(
119 {
120 144 ▶ wanted => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
121  
122 # File::Find seems to use '/' as the path separator on Windows
123 # for some odd reason. It really should be using File::Spec
124 # internally everywhere but it doesn't.
125 144 ▶ my $path
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 144 144
126 = $^O eq 'MSWin32'
127 ? File::Spec->canonpath($File::Find::name)
128 : $File::Find::name;
129  
130 144 ▶ if ( $self->_path_is_excluded($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_path_is_excluded($path)) 4 140 144
131 4 $File::Find::prune = 1;
132 4 return;
133 }
134  
135 140 ▶ my $entry = $self->_entry_for_map($path) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->_entry_for_map($path) or return 138 2 140
136 138 $map{$path} = $entry;
137 },
138 follow_fast => ( $self->follow_symlinks ? 1 : 0 ),
139 no_chdir => 1,
140 follow_skip => 2,
141 },
142 54 ▶ @{ $self->directories },
Branch: 1 branch
BranchTrueFalseTotal
$self->follow_symlinks ? : 3 51 54
143 );
144  
145 54 return \%map;
146 }
147 ## use critic
148  
149 150 ▶sub _path_is_excluded {
Subroutine _path_is_excluded: called CC=1 SCAR=0.0
150 150 my $self = shift;
151 150 my $path = shift;
152  
153 150 return $self->_path_matches( $self->exclude, $path );
154 }
155  
156 140 ▶sub _entry_for_map {
Subroutine _entry_for_map: called CC=8 SCAR=20.8
157 140 my $self = shift;
158 140 my $path = shift;
159  
160 140 ▶ my $is_dir = -d $path ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-d $path ? : 82 58 140
161  
162 # This should be free since the stat call was already done when checking
163 # -d.
164 140 my @stat = stat;
165  
166 140 ▶ return if -l $path && !$is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if -l $path and not $is_dir 2 138 140
Condition100%3/3
-l $path and not $is_dir
!ll&&!rl&&r
13352
MC/DC100%2/2
-l $path and not $is_dir
-l $path not $is_dir
Truth table100%3/3
A and not(B)
A= -l $pathB= not $is_dir
ABresult
0X0
100
111
167  
168 138 ▶ unless ($is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_dir) 56 82 138
169 56 my $filter = $self->filter;
170 56 ▶ return unless ( File::Spec->splitpath($path) )[2] =~ /$filter/;
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->splitpath($path))[2] =~ /$filter/ 0 56 56
171 }
172  
173 return {
174 138 ▶ is_dir => $is_dir,
Branch: 2 branches
BranchTrueFalseTotal
$is_dir ? : 82 56 138
$is_dir ? : 82 56 138
175 size => ( $is_dir ? 0 : $stat[7] ),
176 $self->_maybe_file_attributes( $path, \@stat ),
177 ( $is_dir ? () : $self->_maybe_content($path) ),
178 };
179 }
180  
181 138 ▶sub _maybe_file_attributes {
Subroutine _maybe_file_attributes: called CC=3 SCAR=12.7
182 138 my $self = shift;
183 138 my $path = shift;
184 138 my $stat = shift;
185  
186 # The Default watcher always requires the mtime, regardless of whether or
187 # not we're including stat info in the modify events.
188 138 ▶ unless ( $self->_always_requires_mtime ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_always_requires_mtime) 0 138 138
189 return
190 0 ▶ unless $self->_path_matches(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_path_matches($self->modify_includes_file_attributes, $path) 0 0 0
191 $self->modify_includes_file_attributes,
192 $path,
193 );
194 }
195  
196 138 return ( stat => $self->_stat( $path, $stat ) );
197 }
198  
199 138 ▶sub _stat {
Subroutine _stat: called CC=2 SCAR=7.0
200 138 my $self = shift;
201 138 my $path = shift;
202 138 my $stat = shift;
203  
204 138 ▶ my @stat = $stat ? @{$stat} : stat $path;
Branch: 1 branch
BranchTrueFalseTotal
$stat ? : 138 0 138
205 return {
206 138 attributes => {
207 permissions => S_IMODE( $stat[2] ),
208 uid => $stat[4],
209 gid => $stat[5],
210 },
211 mtime => $stat[9],
212 };
213 }
214  
215 0 ▶sub _always_requires_mtime {0}
Subroutine _always_requires_mtime: not called CC=1 SCAR=6.9
216  
217 56 ▶sub _maybe_content {
Subroutine _maybe_content: called CC=5 SCAR=16.3
218 56 my $self = shift;
219 56 my $path = shift;
220  
221 return
222 56 ▶ unless $self->_path_matches( $self->modify_includes_content, $path );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_path_matches($self->modify_includes_content, $path) 48 8 56
223  
224 8 ▶ open my $fh, '<', $path or die "Cannot open $path for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Cannot open $path for reading: $!" 8 0 8
225 8 ▶ binmode $fh, ':bytes' or die qq{Cannot binmode $path as ':bytes': $!};
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, ":bytes" or die "Cannot binmode $path as ':bytes': $!" 8 0 8
226 8 my $content = do {
227 8 local $/ = undef;
228 8 <$fh>;
229 };
230 8 ▶ close $fh or die "Cannot close $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${path}: $!" 8 0 8
231  
232 8 return ( content => $content );
233 }
234  
235 17 ▶sub new_events {
Subroutine new_events: called CC=1 SCAR=0.0
Pod documented
236 17 my $self = shift;
237  
238 17 return $self->_interesting_events;
239 }
240  
241 ## no critic ( Subroutines::ProhibitUnusedPrivateSubroutines)
242 47 ▶sub _modify_event_maybe_file_attribute_changes {
Subroutine _modify_event_maybe_file_attribute_changes: called CC=4 SCAR=13.9
243 47 my $self = shift;
244 47 my $path = shift;
245 47 my $old_map = shift;
246 47 my $new_map = shift;
247  
248 return
249 47 ▶ unless $self->_path_matches(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_path_matches($self->modify_includes_file_attributes, $path) 39 8 47
250 $self->modify_includes_file_attributes,
251 $path,
252 );
253  
254 8 my $old_attr = $old_map->{$path}{stat}{attributes};
255 8 my $new_attr = $new_map->{$path}{stat}{attributes};
256  
257 8 for my $k ( keys %{$new_attr} ) {
258  
259 # Any possible info retrieved from stat will be numeric, so we can
260 # always use numeric comparison safely.
261 return ( attributes => [ $old_attr, $new_attr ] )
262 20 ▶ if $old_attr->{$k} != $new_attr->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $old_attr->{$k} != $new_attr->{$k} 4 16 20
263 }
264  
265 4 return;
266 }
267  
268 14 ▶sub _modify_event_maybe_content_changes {
Subroutine _modify_event_maybe_content_changes: called CC=2 SCAR=6.9
269 14 my $self = shift;
270 14 my $path = shift;
271 14 my $old_map = shift;
272 14 my $new_map = shift;
273  
274 return
275 14 ▶ unless $self->_path_matches( $self->modify_includes_content, $path );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_path_matches($self->modify_includes_content, $path) 10 4 14
276 return (
277 4 content => [ $old_map->{$path}{content}, $new_map->{$path}{content} ]
278 );
279 }
280  
281 267 ▶sub _path_matches {
Subroutine _path_matches: called CC=9 SCAR=22.0
282 267 my $self = shift;
283 267 my $matches = shift;
284 267 my $path = shift;
285  
286 267 ▶ return $matches if !ref $matches;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $matches 95 172 267
287  
288 172 foreach my $matcher ( @{$matches} ) {
289 73 ▶ if ( my $ref = ref $matcher ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $matcher) { } 54 19 73
290 54 ▶ if ( $ref eq 'Regexp' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'Regexp') { } 51 3 54
elsif ($ref eq 'CODE') { } 3 0 3
291 51 ▶ return 1 if $path =~ /$matcher/;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$matcher/ 16 35 51
292 }
293 elsif ( $ref eq 'CODE' ) {
294 3 local $_ = $path;
295 3 ▶ return 1 if $matcher->($path);
Branch: 1 branch
BranchTrueFalseTotal
if &$matcher($path) 0 3 3
296 }
297 }
298 else {
299 19 ▶ return 1 if $path eq $matcher;
Branch: 1 branch
BranchTrueFalseTotal
if $path eq $matcher 1 18 19
300 }
301 }
302  
303 155 return;
304 }
305  
306 2 ▶sub _remove_directory {
Subroutine _remove_directory: called CC=1 SCAR=0.0
307 2 my $self = shift;
308 2 my $dir = shift;
309  
310 $self->_set_directories(
311 2 [ grep { $_ ne $dir } @{ $self->directories } ] );
312 }
313 ## use critic
314  
315 3 1;
316  
317 # ABSTRACT: Role consumed by all watchers
318  
319 __END__
↑ Summary ChangeNotify/Watcher/Default.pm »