blib/lib/App/Prove/Watch.pm

filter: Statementstmt 87.1% 81 / 93 Branchbran 70.5% 24 / 34 Conditioncond 64.2% 9 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 84.2% 16 / 19 Podpod 0.0% 0 / 4 totaltotal 76.7% 132 / 172 CC 24 SCAR 14.8
CC
24
Cov
81%
_split_args
8.8
__ANON__
6.0
__ANON__
6.0
CRAP 4.4
↑ Summary
1 package App::Prove::Watch;
2 2 $App::Prove::Watch::VERSION = '0.3';
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use App::Prove;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Filesys::Notify::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 =head1 NAME
14  
15 App::Prove::Watch - Run tests whenever changes occur.
16  
17 =head1 VERSION
18  
19 version 0.3
20  
21 =head1 SYNOPSIS
22  
23 $ provewatcher
24  
25 =head1 DESCRIPTION
26  
27 Watches for changes in the current directroy tree and runs prove when there are
28 changes.
29  
30 =head1 ARGUMENTS
31  
32 C<provwatcher> takes all the arguments that C<prove> takes with two additions:
33  
34 =head2 --watch
35  
36 Specifies what directories should be watched:
37  
38 # just watch lib
39 $ provewatcher --watch lib
40
41 # watch lib and t
42 $ provewatcher --watch lib --watch t
43
44 This defaults to C<.> if not given.
45  
46 =head2 --run
47  
48 Allows you to run something other than prove when changes happen. For example if
49 you where using L<Dist::Zilla>
50  
51 $ provewatcher --run 'dzil test'
52
53 =head1 NOTIFICATIONS
54  
55 If you install L<Log::Dispatch::DesktopNotification>, desktop notifications will
56 be sent whenever the overall state of the tests change (failing to passing or
57 passing to failing).
58  
59 L<Log::Dispatch::DesktopNotification> is not listed as a prereq for this module,
60 it will not be installed by default when you install this module.
61  
62 =cut
63  
64 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
65 3 my $class = shift;
66 3 my ($args, $prove_args) = $class->_split_args(@_);
67
68  
69 3 my $watcher = Filesys::Notify::Simple->new($args->{watch});
70 3 my $prove = $class->_get_prove_sub($args, $prove_args);
71  
72 3 return bless {
73 watcher => $watcher,
74 prove => $prove,
75 args => $args,
76 }, $class;
77 }
78  
79 4 ▶sub prove { return $_[0]->{prove}->() }
Subroutine prove: called CC=1 SCAR=0.0
Pod undocumented
80 5 ▶sub watcher {
Subroutine watcher: called CC=2 SCAR=6.9
Pod undocumented
81 5 my $self = shift;
82
83 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 3 5
84 2 $self->{watcher} = shift;
85 }
86
87 5 return $self->{watcher};
88 }
89  
90  
91 2 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod undocumented
92 2 my ($self, $count) = @_;
93
94 2 $self->prove;
95  
96 2 ▶ $count ||= -1;
Condition50%1/2
$count ||= -1
l!l
20
MC/DC0%0/1
$count ||= -1
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
97 2 ▶ while ($count != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count != 0) 3 2 5
98 3 ▶ $self->watcher->wait(sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
99 3 my $doit;
100 3 FILE: foreach my $event (@_) {
101 3 my $file = basename($event->{path});
102 3 ▶ next FILE if $file =~ m/^(?:\.[~#])/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^(?:\.[~#])/ 0 3 3
103
104 3 ▶ if ($self->{args}{ignore}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"args"}{"ignore"}) 2 1 3
105 2 ▶ next FILE if $file =~ $self->{args}{ignore};
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /$self->{"args"}{"ignore"}/ 1 1 2
106 }
107
108 2 $doit++;
109
110 }
111
112 3 ▶ if ($doit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doit) 2 1 3
113 2 $self->prove();
114 2 $count--;
115 }
116 3 });
117 }
118 }
119  
120  
121 3 ▶sub _split_args {
Subroutine _split_args: called CC=8 SCAR=21.7
122 3 my ($class, @args) = @_;
123
124 3 my (@ours, @theirs);
125
126 3 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 3 3 6
127 3 local $_ = shift @args;
128 3 ▶ if ($_ eq '--watch' || $_ eq '--run' || $_ eq '--ignore') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq '--watch' or $_ eq '--run' or $_ eq '--ignore') { } 3 0 3
Condition66%4/6
$_ eq "--watch" or $_ eq "--run" or $_ eq "--ignore"
l!l&&r!l&&!r
210
$_ eq "--watch" or $_ eq "--run"
l!l&&r!l&&!r
021
MC/DC0%0/3
$_ eq "--watch" or $_ eq "--run" or $_ eq "--ignore"
$_ eq "--watch" $_ eq "--run" $_ eq "--ignore"
Truth table50%2/4
A or B or C
A= $_ eq "--watch"B= $_ eq "--run"C= $_ eq "--ignore"
ABCresult
1XX1
01X1
0011
0000
129 3 push(@ours, $_, shift @args);
130 }
131 else {
132 0 push(@theirs, $_);
133 }
134 }
135
136 3 my %ours;
137 3 GetOptionsFromArray(\@ours, \%ours,
138 'watch=s@',
139 'run=s',
140 'ignore=s@',
141 );
142
143 0 ▶ if (!$ours{watch} || !@{$ours{watch}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ours{"watch"} and @{$ours{"watch"};}) 3 0 3
Condition33%1/3
$ours{"watch"} and @{$ours{"watch"};}
!ll&&!rl&&r
300
MC/DC0%0/2
$ours{"watch"} and @{$ours{"watch"};}
$ours{"watch"} @{$ours{"watch"};}
Truth table33%1/3
A and B
A= $ours{"watch"}B= @{$ours{"watch"};}
ABresult
0X0
100
111
144 3 $ours{watch} = ['.']
145 }
146
147 3 ▶ if ($ours{ignore}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ours{"ignore"}) 1 2 3
148 1 my $merged = join('|', map { qr/$_/ } @{$ours{ignore}});
149 1 $ours{ignore} = qr/$merged/;
150 }
151
152 3 return (\%ours, \@theirs);
153 }
154  
155 3 ▶sub _get_prove_sub {
Subroutine _get_prove_sub: called CC=3 SCAR=12.1
156 3 my ($class, $args, $prove_args) = @_;
157
158 3 my $handle_alert = $class->_get_notification_sub;
159
160 3 my $last;
161 my $prove;
162
163 3 ▶ if ($args->{run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'run'}) { } 2 1 3
164 2 ▶ if (ref $args->{run}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'run'}) { } 2 0 2
165 2 $prove = $args->{run};
166 }
167 else {
168 0 ▶ $prove = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
169 0 my $ret = system($args->{run});
170
171 0 ▶ return $ret == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ret == 0 ? : 0 0 0
172 0 };
173 }
174 }
175 else {
176 0 ▶ $prove = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
177 0 my $app = App::Prove->new;
178
179 0 $app->process_args(@$prove_args);
180
181 0 ▶ return $app->run ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$app->run ? : 0 0 0
182 1 };
183 }
184
185 4 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
186 4 my $ret = $prove->();
187
188 4 ▶ if (defined $last && $ret != $last) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last and $ret != $last) 1 3 4
Condition100%3/3
defined $last and $ret != $last
!ll&&!rl&&r
211
MC/DC100%2/2
defined $last and $ret != $last
defined $last $ret != $last
Truth table100%3/3
A and B
A= defined $lastB= $ret != $last
ABresult
0X0
100
111
189 1 my $msg;
190 1 ▶ if ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) { } 1 0 1
191 1 $msg = "Tests are now passing.";
192 }
193 else {
194 0 $msg = "Tests are now failing.";
195 }
196
197 1 $handle_alert->($msg);
198 }
199 4 $last = $ret;
200
201 4 return $ret;
202 3 };
203 }
204  
205  
206 3 ▶sub _get_notification_sub {
Subroutine _get_notification_sub: called CC=2 SCAR=8.5
207 3 my $has_desk_note = eval {
208 3 require Log::Dispatch::DesktopNotification;
209 };
210
211 3 ▶ if ($has_desk_note) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_desk_note) { } 0 3 3
212 0 my $notify = Log::Dispatch::DesktopNotification->new(
213 name => 'notify',
214 min_level => 'notice',
215 app_name => 'provewatcher',
216 );
217
218 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 $notify->log(
220 level => 'notice',
221 message => shift,
222 );
223 }
224 0 }
225 else {
226 3 ▶ return sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
227 }
228 }
229  
230 =head1 TODO
231  
232 =over 2
233  
234 =item *
235  
236 Ironically, for a TDD tool, there's not enough tests.
237  
238 =back
239  
240 =head1 AUTHORS
241  
242 Chris Reinhardt
243 crein@cpan.org
244
245 =head1 COPYRIGHT
246  
247 This program is free software; you can redistribute
248 it and/or modify it under the same terms as Perl itself.
249  
250 The full text of the license can be found in the
251 LICENSE file included with this module.
252  
253 =head1 SEE ALSO
254  
255 L<Test::Continuous>, L<App::Prove>, perl(1)
256  
257 =cut
258
259  
260 2 1;
↑ Summary