App-Prove-Watch

 view release on metacpan or  search on metacpan

lib/App/Prove/Watch.pm  view on Meta::CPAN

package App::Prove::Watch;
$App::Prove::Watch::VERSION = '0.3';

use strict;
use warnings;

use App::Prove;
use Filesys::Notify::Simple;
use File::Basename;
use Getopt::Long qw(GetOptionsFromArray);


=head1 NAME

App::Prove::Watch - Run tests whenever changes occur.

=head1 VERSION

version 0.3

=head1 SYNOPSIS

	$ provewatcher 

=head1 DESCRIPTION

Watches for changes in the current directroy tree and runs prove when there are
changes.

=head1 ARGUMENTS

C<provwatcher> takes all the arguments that C<prove> takes with two additions:

=head2 --watch

Specifies what directories should be watched:

	# just watch lib
	$ provewatcher --watch lib
	
	# watch lib and t
	$ provewatcher --watch lib --watch t
	
This defaults to C<.> if not given.

=head2 --run

Allows you to run something other than prove when changes happen. For example if
you where using L<Dist::Zilla>

	$ provewatcher --run 'dzil test'
	
=head1 NOTIFICATIONS

If you install L<Log::Dispatch::DesktopNotification>, desktop notifications will
be sent whenever the overall state of the tests change (failing to passing or
passing to failing).

L<Log::Dispatch::DesktopNotification> is not listed as a prereq for this module,
it will not be installed by default when you install this module.

=cut

sub new {
	my $class = shift;
	my ($args, $prove_args) = $class->_split_args(@_);
	

	my $watcher      = Filesys::Notify::Simple->new($args->{watch});
	my $prove        = $class->_get_prove_sub($args, $prove_args);

	return bless {
		watcher => $watcher,
		prove   => $prove,
		args    => $args,
	}, $class;
}

sub prove   { return $_[0]->{prove}->() }
sub watcher { 
	my $self = shift;
	
	if (@_) {
		$self->{watcher} = shift;
	}
	
	return $self->{watcher};
}


sub run {
	my ($self, $count) = @_;
	
	$self->prove;

	$count ||= -1;
	while ($count != 0) {
		$self->watcher->wait(sub {
			my $doit;
			FILE: foreach my $event (@_) {
				my $file = basename($event->{path});
				next FILE if $file =~ m/^(?:\.[~#])/;
				
				if ($self->{args}{ignore}) {
					next FILE if $file =~ $self->{args}{ignore};
				}
				
				$doit++;
				
			}
			
			if ($doit) {
				$self->prove();
				$count--;
			}



( run in 1.096 second using v1.01-cache-2.11-cpan-22024b96cdf )