File Coverage

blib/lib/Test/File/Find/Rule.pm
Criterion Covered Total %
statement 18 39 46.1
branch 0 4 0.0
condition 0 4 0.0
subroutine 6 9 66.6
pod 2 3 66.6
total 26 59 44.0


line stmt bran cond sub pod time code
1             package Test::File::Find::Rule;
2            
3 1     1   44570 use strict;
  1         2  
  1         46  
4 1     1   5 use base qw(Exporter);
  1         2  
  1         336  
5 1     1   6 use vars qw(@EXPORT);
  1         8  
  1         58  
6            
7 1     1   5 use Test::Builder;
  1         1  
  1         36  
8 1     1   4 use File::Spec;
  1         2  
  1         27  
9 1     1   2628 use Number::Compare;
  1         1386  
  1         657  
10            
11             our $VERSION = '1.00';
12            
13             @EXPORT = qw(
14             match_rule_nb_results
15             match_rule_array
16             match_rule_no_result
17             );
18            
19             my $Test = Test::Builder->new();
20            
21             =head1 NAME
22            
23             Test::File::Find::Rule - Test files and directories with File::Find::Rule
24            
25             =head1 SYNOPSIS
26            
27             use Test::File::Find::Rule;
28            
29             # Check that all files in $dir have sensible names
30             my $rule = File::Find::Rule
31             ->file
32             ->relative
33             ->not_name(qr/^[\w]{1,8}\.[a-z]{3,4}$/);
34             match_rule_no_result($rule, $dir, 'File names ok');
35            
36             # Check that all our perl scripts have use strict !
37             my $rule = File::Find::Rule
38             ->file
39             ->relative
40             ->name(@perl_ext)
41             ->not_grep(qr/^\s*use\s+strict;/m, sub { 1 });
42             match_rule_no_result($rule, $dir, 'use strict usage');
43            
44             # With some help of File::Find::Rule::MMagic
45             # Check that there is less than 10 images in $dir
46             # with a size > 1Mo
47             my $rule = File::Find::Rule
48             ->file
49             ->relative
50             ->magic('image/*')
51             ->size('>1Mo');
52             match_rule_nb_result($rule, $dir, '<10', 'Few big images');
53             # We can reuse our F:F:R object
54             match_rule_nb_result($rule, $another_dir, '>100', 'A lot of big images');
55            
56             # Check the exact result from a rule
57             my $dirs = [qw(web lib data tmp)];
58             my $rule = File::Find::Rule
59             ->directory
60             ->mindepth(1)
61             ->maxdepth(1)
62             ->relative;
63             match_rule_array($rule, $dir, $dirs, 'Directory structure ok'));
64            
65             =head1 DESCRIPTION
66            
67             This module provides some functions to test files and directories
68             with all the power of the wonderful File::Find::Rule module.
69            
70             The test functionnality is based on Test::Builder.
71            
72             =head2 EXPORT
73            
74             match_rule_nb_results
75             match_rule_array
76             match_rule_no_result
77            
78             =head2 FUNCTIONS
79            
80             =over 4
81            
82             =item match_rule_nb_result(RULE, DIR, COMPARE [, NAME])
83            
84             RULE is a File::Find::Rule object without a query method. The
85             C method will be called automatically.
86            
87             DIR is a directory. To be safe, I recommend to give an absolute directory
88             and use the C function for your rule so that error messages
89             are shorter.
90            
91             COMPARE is a Number::Compare object. You have to follow
92             L semantics.
93            
94             NAME is the optional name of the test.
95            
96             =cut
97            
98             # $compare is a Number::Compare string (>3 <10Ki 4 ...)
99             sub match_rule_nb_results {
100 0     0 0   my ($rule, $dir, $compare, $name) = @_;
101 0   0       $name ||= "Match the rule";
102            
103 0           my @files = $rule->in($dir);
104 0 0         if (Number::Compare->new($compare)->test(scalar(@files))) {
105 0           $Test->ok(1, $name);
106             } else {
107 0           $Test->ok(0, $name);
108 0           $Test->diag("Expected [$compare]");
109 0           $Test->diag("Got [".scalar(@files)."]");
110 0           $Test->diag("Matched [".join(', ', @files)."]");
111             }
112             }
113            
114             =item match_rule_no_result(RULE, DIR [, NAME])
115            
116             Just a convenient shortcut for
117            
118             match_rule_nb_result(RULE, DIR, 0 [, NAME])
119            
120             =cut
121            
122             sub match_rule_no_result {
123 0     0 1   my ($rule, $dir, $name) = @_;
124            
125 0           match_rule_nb_results($rule, $dir, 0, $name);
126             }
127            
128             =item match_rule_array(RULE, DIR, RESULTS [, NAME])
129            
130             The only difference with the C
131             is the RESULTS param wich is an array ref with
132             the expected results (order does not matter).
133            
134             =cut
135            
136             sub match_rule_array {
137 0     0 1   my ($rule, $dir, $results, $name) = @_;
138 0   0       $name ||= "Match the rule";
139            
140 0           my @files = $rule->in($dir);
141 0           my $files_stringy = join '¨^¨', sort @files;
142 0           my $results_stringy = join '¨^¨', sort @$results;
143 0 0         if ($results_stringy eq $files_stringy) {
144 0           $Test->ok(1, $name);
145             } else {
146 0           $Test->ok(0, $name);
147 0           $Test->diag("Expected [".join(', ', sort @files)."]");
148 0           $Test->diag("Got [".join(', ', sort @$results)."]");
149             }
150             }
151            
152             1;
153            
154             =back
155            
156             =head1 SEE ALSO
157            
158             L, L
159             L, L
160             L, L
161            
162             =head1 AUTHOR
163            
164             Fabien POTENCIER, Efabpot@cpan.orgE
165            
166             =head1 COPYRIGHT
167            
168             Copyright 2003-2004, Fabien POTENCIER, All Rights Reserved
169            
170             =head1 LICENSE
171            
172             You may use, modify, and distribute this under the same terms
173             as Perl itself.
174            
175             =cut