File Coverage

blib/lib/Test/Tail/Multi.pm
Criterion Covered Total %
statement 67 68 98.5
branch 16 18 88.8
condition 1 3 33.3
subroutine 16 16 100.0
pod 0 4 0.0
total 100 109 91.7


line stmt bran cond sub pod time code
1             package Test::Tail::Multi;
2              
3 8     8   690168 use 5.006001;
  8         37  
4 8     8   76 use strict;
  8         52  
  8         353  
5 8     8   43 use warnings;
  8         26  
  8         676  
6              
7             our $VERSION = '0.07';
8              
9 8     8   4728 use File::Tail;
  8         216555  
  8         477  
10 8     8   1386 use Test::Builder;
  8         167121  
  8         230  
11 8     8   46 use Carp;
  8         14  
  8         970  
12              
13             my $Test = Test::Builder->new();
14             our @monitored;
15             our $cached_output;
16             our $tail_delay = 5;
17              
18             sub import {
19 8     8   107 my $self = shift;
20 8         21 my $caller = caller;
21              
22 8     8   49 no strict 'refs';
  8         14  
  8         822  
23 8         20 *{$caller."::delay"} = \&delay;
  8         60  
24 8         17 *{$caller."::add_file"} = \&add_file;
  8         32  
25 8         17 *{$caller."::contents_like"} = \&contents_like;
  8         26  
26 8         15 *{$caller."::contents_unlike"} = \&contents_unlike;
  8         28  
27              
28 8         34 my %params;
29             {
30 8     8   50 no warnings;
  8         19  
  8         5539  
  8         14  
31 8         59 %params = @_;
32             }
33 8 100       51 if (exists($params{'files'})) {
34 4         11 my $contents = $params{'files'};
35 4 100 33     24 if (ref($contents) eq 'ARRAY') {
    50          
36             # list of files provided
37 2         6 @monitored = map {File::Tail->new(name=>$_)} @$contents;
  2         344  
38             }
39             elsif (defined $contents and ($contents ne "")) {
40 2         17 @monitored = File::Tail->new(name=>$contents);
41             }
42             else {
43 0         0 croak "You must specify at least one file to monitor";
44             }
45             }
46 8         1438 delete $params{'files'};
47              
48 8 100       9399 $Test->plan(%params) if int keys %params;
49             }
50              
51             sub delay($;$) {
52 3     3 0 225366 my ($delay, $comment) = @_;
53 3 50       11 $tail_delay = $delay if defined $delay;
54 3 100       15 $Test->diag($comment) if defined $comment;
55             }
56              
57              
58             sub add_file($;$) {
59 4     4 0 319857 my($file, $comment) = @_;
60 4         35 push @monitored, File::Tail->new($file);
61 3 100       1456 $Test->diag($comment) if defined $comment;
62             }
63              
64             sub contents_like(&$;$) {
65 2     2 0 585 my ($coderef, $pattern, $comment) = @_;
66 2     2   11 _execute($coderef, $pattern, sub { $Test->like(@_) }, $comment);
  2         24  
67             }
68              
69             sub contents_unlike(&$;$) {
70 2     2 0 5028 my ($coderef, $pattern, $comment) = @_;
71 2     2   24 _execute($coderef, $pattern, sub { $Test->unlike(@_) }, $comment);
  2         43  
72             }
73              
74             sub _execute {
75 4     4   11 my ($coderef, $pattern, $testsub, $comment) = @_;
76 4 100       40 if (defined $coderef) {
77             # call code and capture output
78 2         5 $coderef->();
79 2         11200 my ($nfound, $timeleft, @pending);
80 2         21 $nfound = 1;
81 2         35 while ($nfound) {
82 3         109 ($nfound, $timeleft, @pending) =
83             File::Tail::select(undef, undef, undef, $tail_delay, @monitored);
84 3 100       5165951 last unless ($nfound);
85 1         4 foreach (@pending) {
86 1         76 $cached_output .= $_->{"input"}." (".localtime(time).") ".$_->read;
87             }
88             }
89             }
90             # test vs. last output
91             # (fall into here if coderef is not defined)
92 4         29 $testsub->($cached_output, $pattern, $comment);
93             }
94              
95             1;
96             __END__