ChangeNotify/Watcher/Default.pm

filter: Statementstmt 98.7% 79 / 80 Branchbran 92.3% 24 / 26 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 3 totaltotal 94.3% 133 / 141 SCAR 29.5
CC
19
Cov
97%
_interesting_events
9.0
_entry_for_map
7.0
__ANON__
3.0
CRAP 19.0
↑ Summary ChangeNotify/Watcher.pm »
1 package File::ChangeNotify::Watcher::Default;
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.29';
8  
9 3 ▶use File::Find qw( finddepth );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Time::HiRes qw( sleep );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Types::Standard qw( HashRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Trying to import this just blows up on Win32, and checking
15 # Time::HiRes::d_hires_stat() _also_ blows up on Win32.
16 BEGIN {
17 ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
18 3 ▶ eval { Time::HiRes->import('stat') };
Subroutine BEGIN: called CC=1 SCAR=0.0
19 }
20  
21 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 with 'File::ChangeNotify::Watcher';
24  
25 has _map => (
26 is => 'ro',
27 writer => '_set_map',
28 isa => HashRef,
29 17 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 3 );
31  
32 4 ▶sub sees_all_events {0}
Subroutine sees_all_events: called CC=1 SCAR=0.0
Pod undocumented
33  
34 sub BUILD {
35 17 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
36  
37 17 $self->_set_map( $self->_build_map() );
38 }
39  
40 sub _build_map {
41 38 ▶ my $self = shift;
Subroutine _build_map: called CC=2 SCAR=6.9
42  
43 38 my %map;
44  
45 File::Find::find(
46 {
47 wanted => sub {
48 87 ▶ my $path = $File::Find::name;
Subroutine __ANON__: called CC=3 SCAR=11.0
49  
50 87 ▶ if ( $self->_path_is_excluded($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_path_is_excluded($path)) 4 83 87
51 4 $File::Find::prune = 1;
52 4 return;
53 }
54  
55 83 ▶ my $entry = $self->_entry_for_map($path) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->_entry_for_map($path) or return 4 79 83
56 79 $map{$path} = $entry;
57 },
58 follow_fast => ( $self->follow_symlinks() ? 1 : 0 ),
59 no_chdir => 1,
60 follow_skip => 2,
61 },
62 38 ▶ @{ $self->directories() },
Branch: 1 branch
BranchTrueFalseTotal
$self->follow_symlinks ? : 3 35 38
63 );
64  
65 38 return \%map;
66 }
67  
68 sub _entry_for_map {
69 83 ▶ my $self = shift;
Subroutine _entry_for_map: called CC=7 SCAR=19.5
70 83 my $path = shift;
71  
72 83 ▶ my $is_dir = -d $path ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-d $path ? : 51 32 83
73  
74 83 ▶ return if -l $path && !$is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if -l $path and not $is_dir 4 79 83
Condition100%3/3
-l $path and not $is_dir
!ll&&!rl&&r
7364
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
75  
76 79 ▶ unless ($is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_dir) 28 51 79
77 28 my $filter = $self->filter();
78 28 ▶ return unless ( File::Spec->splitpath($path) )[2] =~ /$filter/;
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->splitpath($path))[2] =~ /$filter/ 0 28 28
79 }
80  
81 return {
82 79 ▶ is_dir => $is_dir,
Branch: 1 branch
BranchTrueFalseTotal
$is_dir ? : 51 28 79
83 mtime => _mtime(*_),
84 size => ( $is_dir ? 0 : -s _ ),
85 };
86 }
87  
88 # It seems that Time::HiRes's stat does not act exactly like the
89 # built-in, so if I do ( stat _ )[9] it will not work (grr).
90 sub _mtime {
91 79 ▶ my @stat = stat;
Subroutine _mtime: called CC=1 SCAR=0.0
92  
93 79 return $stat[9];
94 }
95  
96 sub wait_for_events {
97 8 ▶ my $self = shift;
Subroutine wait_for_events: called CC=2 SCAR=7.4
Pod undocumented
98  
99 8 while (1) {
100 8 my @events = $self->_interesting_events();
101 8 ▶ return @events if @events;
Branch: 1 branch
BranchTrueFalseTotal
if @events 8 0 8
102  
103 0 sleep $self->sleep_interval();
104 }
105 }
106  
107 sub _interesting_events {
108 21 ▶ my $self = shift;
Subroutine _interesting_events: called CC=9 SCAR=22.0
109  
110 21 my @interesting;
111  
112 21 my $old_map = $self->_map();
113 21 my $new_map = $self->_build_map();
114  
115 21 for my $path ( sort keys %{$old_map} ) {
116 33 ▶ if ( !exists $new_map->{$path} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $new_map->{$path}) { } 6 27 33
elsif (not $old_map->{$path}{'is_dir'} and $old_map->{$path}{'mtime'} != $new_map->{$path}{'mtime'} || $old_map->{$path}{'size'} != $new_map->{$path}{'size'}) { } 2 48 50
117 6 ▶ if ( $old_map->{$path}{is_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_map->{$path}{"is_dir"}) 2 4 6
118 2 $self->_remove_directory($path);
119 }
120  
121 6 push @interesting, $self->event_class()->new(
122 path => $path,
123 type => 'delete',
124 );
125 }
126 ▶ elsif (
Condition83%5/6
not $old_map->{$path}{"is_dir"} and $old_map->{$path}{"mtime"} != $new_map->{$path}{"mtime"} || $old_map->{$path}{"size"} != $new_map->{$path}{"size"}
!ll&&!rl&&r
2322
$old_map->{$path}{"mtime"} != $new_map->{$path}{"mtime"} or $old_map->{$path}{"size"} != $new_map->{$path}{"size"}
l!l&&r!l&&!r
202
MC/DC66%2/3
not $old_map->{$path}{"is_dir"} and $old_map->{$path}{"mtime"} != $new_map->{$path}{"mtime"} || $old_map->{$path}{"size"} != $new_map->{$path}{"size"}
not $old_map->{$path}{"is_dir"} $old_map->{$path}{"mtime"} != $new_map->{$path}{"mtime"} $old_map->{$path}{"size"} != $new_map->{$path}{"size"}
Truth table75%3/4
not(A) and B or C
A= not $old_map->{$path}{"is_dir"}B= $old_map->{$path}{"mtime"} != $new_map->{$path}{"mtime"}C= $old_map->{$path}{"size"} != $new_map->{$path}{"size"}
ABCresult
0XX0
1000
11X1
1011
127 !$old_map->{$path}{is_dir}
128 && ( $old_map->{$path}{mtime} != $new_map->{$path}{mtime}
129 || $old_map->{$path}{size} != $new_map->{$path}{size} )
130 ) {
131 2 push @interesting, $self->event_class()->new(
132 path => $path,
133 type => 'modify',
134 );
135 }
136 }
137  
138 21 for my $path ( sort grep { !exists $old_map->{$_} } keys %{$new_map} ) {
139 14 ▶ if ( -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 2 12 14
140 2 push @interesting, $self->event_class()->new(
141 path => $path,
142 type => 'create',
143 ),
144 ;
145 }
146 else {
147 12 push @interesting, $self->event_class()->new(
148 path => $path,
149 type => 'create',
150 );
151 }
152 }
153  
154 21 $self->_set_map($new_map);
155  
156 21 return @interesting;
157 }
158  
159 3 __PACKAGE__->meta()->make_immutable();
160  
161 3 1;
162  
163 # ABSTRACT: Fallback default watcher subclass
164  
165 __END__
↑ Summary ChangeNotify/Watcher.pm »