File Coverage

blib/lib/ExtUtils/ModuleMaker/Auxiliary.pm
Criterion Covered Total %
statement 148 150 98.6
branch 14 24 58.3
condition n/a
subroutine 25 25 100.0
pod 4 14 28.5
total 191 213 89.6


line stmt bran cond sub pod time code
1             package ExtUtils::ModuleMaker::Auxiliary;
2 16     16   17720 use strict;
  16         31  
  16         378  
3             # Contains test subroutines for distribution with ExtUtils::ModuleMaker
4 16     16   59 use warnings;
  16         26  
  16         1225  
5             our ( $VERSION, @ISA, @EXPORT_OK );
6             $VERSION = "0.63";
7             require Exporter;
8             @ISA = qw(Exporter);
9             @EXPORT_OK = qw(
10             read_file_string
11             read_file_array
12             five_file_tests
13             check_MakefilePL
14             failsafe
15             licensetest
16             prepare_mockdirs
17             prepare_mock_homedir
18             basic_file_and_directory_tests
19             license_text_test
20             compact_build_tests
21             check_pm_file
22             pod_present
23             constructor_present
24             );
25 16     16   84 use Carp;
  16         24  
  16         736  
26 16     16   77 use Cwd;
  16         23  
  16         698  
27 16     16   150 use File::Path;
  16         27  
  16         607  
28 16     16   74 use File::Spec;
  16         30  
  16         322  
29 16     16   1180 use File::Temp qw| tempdir |;
  16         26690  
  16         650  
30 16     16   84 no warnings 'once';
  16         26  
  16         957  
31             *ok = *Test::More::ok;
32             *is = *Test::More::is;
33             *isnt = *Test::More::isnt;
34             *like = *Test::More::like;
35 16     16   87 use warnings;
  16         30  
  16         362  
36 16     16   4845 use lib ( qw| ./t/testlib | );
  16         6920  
  16         67  
37 16     16   7900 use ExtUtils::ModuleMaker::MockHomeDir;
  16         41  
  16         20985  
38              
39             =head1 NAME
40              
41             ExtUtils::ModuleMaker::Auxiliary - Subroutines for testing ExtUtils::ModuleMaker
42              
43             =head1 DESCRIPTION
44              
45             This package contains subroutines used in one or more F files in
46             ExtUtils::ModuleMaker's test suite. They may prove useful in writing test
47             suites for distributions which subclass ExtUtils::ModuleMaker.
48              
49             =head1 SUBROUTINES
50              
51             =head2 C
52              
53             Function: Read the contents of a file into a string.
54             Argument: String holding name of a file created by complete_build().
55             Returns: String holding text of the file read.
56             Used: To see whether text of files such as README, Makefile.PL,
57             etc. was created correctly by returning a string against which
58             a pattern can be matched.
59              
60             =cut
61              
62             sub read_file_string {
63 124     124 1 17181 my $file = shift;
64 124 50       3791 open my $fh, $file or die "Unable to open filehandle: $!";
65 124         347 my $filetext = do { local $/; <$fh> };
  124         416  
  124         3027  
66 124 50       1110 close $fh or die "Unable to close filehandle: $!";
67 124         708 return $filetext;
68             }
69              
70             =head2 C
71              
72             Function: Read a file line-by-line into an array.
73             Argument: String holding name of a file created by complete_build().
74             Returns: Array holding the lines of the file read.
75             Used: To see whether text of files such as README, Makefile.PL,
76             etc. was created correctly by returning an array against whose
77             elements patterns can be matched.
78              
79             =cut
80              
81             sub read_file_array {
82 12     12 1 36 my $file = shift;
83 12 50       392 open my $fh, $file or die "Unable to open filehandle: $!";
84 12         350 my @filetext = <$fh>;
85 12 50       117 close $fh or die "Unable to close filehandle: $!";
86 12         151 return @filetext;
87             }
88              
89             =head2 C
90              
91             Function: Verify that content of MANIFEST and lib/*.pm were created
92             correctly.
93             Argument: Two arguments:
94             1. A number predicting the number of entries in the MANIFEST.
95             2. A reference to an array holding the components of the module's name, e.g.:
96             [ qw( Alpha Beta Gamma ) ].
97             Returns: n/a.
98             Used: To see whether MANIFEST and lib/*.pm have correct text.
99             Runs 6 Test::More tests:
100             1. Number of entries in MANIFEST.
101             2. Change to directory under lib.
102             3. Applies read_file_string to the stem.pm file.
103             4. Determine whether stem.pm's POD contains module name and
104             abstract.
105             5. Determine whether POD contains a HISTORY head.
106             6. Determine whether POD contains correct author information.
107              
108             =cut
109              
110             sub five_file_tests {
111 4     4 1 8 my ($manifest_entries, $components) = @_;
112 4         5 my $module_name = join('::' => @{$components});
  4         12  
113 4         5 my $dist_name = join('-' => @{$components});
  4         8  
114 4         5 my $path_str = File::Spec->catdir('lib', @{$components});
  4         25  
115              
116 4         22 my @filetext = read_file_array(File::Spec->catfile($dist_name, 'MANIFEST'));
117 4         17 is(scalar(@filetext), $manifest_entries,
118             'Correct number of entries in MANIFEST');
119              
120             my $module = File::Spec->catfile(
121             $dist_name,
122             'lib',
123 4         894 @{$components}[0 .. ($#$components - 1)],
  4         38  
124             "$components->[-1].pm",
125             );
126 4         7 my $str;
127 4         8 ok($str = read_file_string($module),
128             "Able to read $module");
129 4         883 ok($str =~ m|$module_name\s-\sTest\sof\sthe\scapacities\sof\sEU::MM|,
130             'POD contains module name and abstract');
131 4         814 ok($str =~ m|=head1\sHISTORY|,
132             'POD contains history head');
133 4         815 ok($str =~ m|
134             Phineas\sT\.\sBluster\n
135             \s+CPAN\sID:\s+PTBLUSTER\n
136             \s+Peanut\sGallery\n
137             \s+phineas\@anonymous\.com\n
138             \s+http:\/\/www\.anonymous\.com\/~phineas
139             |xs,
140             'POD contains correct author info');
141             }
142              
143             =head2 C
144              
145             Function: Verify that content of Makefile.PL was created correctly.
146             Argument: Two arguments:
147             1. A string holding the directory in which the Makefile.PL
148             should have been created.
149             2. A reference to an array holding strings each of which is a
150             prediction as to content of particular lines in Makefile.PL.
151             Returns: n/a.
152             Used: To see whether Makefile.PL created by complete_build() has
153             correct entries. Runs 1 Test::More test which checks NAME,
154             VERSION_FROM, AUTHOR and ABSTRACT.
155              
156             =cut
157              
158             sub check_MakefilePL {
159 11     11 1 801 my ($topdir, $predictref) = @_;
160 11         28 my @pred = @$predictref;
161              
162 11         106 my $mkfl = File::Spec->catfile( $topdir, q{Makefile.PL} );
163 11         47 my $bigstr = read_file_string($mkfl);
164 11         455 like($bigstr, qr/
165             NAME.+$pred[0].+
166             VERSION_FROM.+$pred[1].+
167             AUTHOR.+$pred[2].+
168             \($pred[3]\).+
169             ABSTRACT.+$pred[4]
170             /sx, "Makefile.PL has predicted values");
171             }
172              
173             sub failsafe {
174 11     11 0 3346 my ($caller, $argslistref, $pattern, $message) = @_;
175 11         15 my ($tdir, $obj);
176 11         20 $tdir = tempdir( CLEANUP => 1);
177 11         2869 ok(chdir $tdir, 'changed to temp directory for testing');
178 11         3303 local $@ = undef;
179 11         26 eval { $obj = $caller->new (@$argslistref); };
  11         35  
180 11         501 like($@, qr/$pattern/, $message);
181             }
182              
183             sub licensetest {
184 41     41 0 11007 my ($caller, $license, $pattern) = @_;
185 41         69 my ($tdir, $mod);
186 41         102 $tdir = tempdir( CLEANUP => 1);
187 41         13196 ok(chdir $tdir, "changed to temp directory for testing $license");
188              
189 41         10656 ok($mod = $caller->new(
190             NAME => "Alpha::$license",
191             LICENSE => $license,
192             COMPACT => 1,
193             ), "object for module Alpha::$license created");
194 41         10444 ok( $mod->complete_build(), 'call complete_build()' );
195 41         11426 ok(chdir "Alpha-$license", "changed to Alpha-$license directory");
196 41         10548 my $licensetext = read_file_string('LICENSE');
197 41         179 like($licensetext, $pattern, "$license license has predicted content");
198 41         10591 ok(chdir $tdir, "CLEANUP tempdir");
199             }
200              
201             sub prepare_mockdirs {
202 54     54 0 302476 my $home_dir = prepare_mock_homedir();
203 54         191 my $personal_defaults_dir = ExtUtils::ModuleMaker::MockHomeDir::personal_defaults_dir();
204 54 50       781 croak "Unable to locate '$personal_defaults_dir'" unless (-d $personal_defaults_dir);
205 54         734 ok(-d $personal_defaults_dir, "Directory $personal_defaults_dir created to mock home directory");
206 54         13079 return ($home_dir, $personal_defaults_dir);
207             }
208              
209             sub prepare_mock_homedir {
210 55     55 0 3308 my $home_dir = ExtUtils::ModuleMaker::MockHomeDir::home_dir();
211 55 50       808 croak "Unable to locate '$home_dir'" unless (-d $home_dir);
212 55         738 ok(-d $home_dir, "Directory $home_dir created to mock home directory");
213 55         14484 return $home_dir;
214             }
215              
216             sub basic_file_and_directory_tests {
217 48     48 0 125305 my $dist_name = shift;
218 48         186 for my $f ( qw| Changes MANIFEST Makefile.PL LICENSE README | ) {
219 240         48869 my $ff = File::Spec->catfile($dist_name, $f);
220 240         3520 ok (-e $ff, "$ff exists");
221             }
222 48         11536 for my $d ( qw| lib t | ) {
223 96         11872 my $dd = File::Spec->catdir($dist_name, $d);
224 96         1287 ok(-d $dd, "Directory '$dd' exists");
225             }
226             }
227              
228             sub license_text_test {
229 49     49 0 127374 my ($dist_name, $regex) = @_;
230 49         92 my $filetext;
231             {
232 49 50       89 open my $FILE, '<', File::Spec->catfile($dist_name, 'LICENSE')
  49         2019  
233             or croak "Unable to open LICENSE for reading";
234 49         160 $filetext = do {local $/; <$FILE>};
  49         159  
  49         2174  
235 49 50       585 close $FILE or croak "Unable to close LICENSE after reading";
236             }
237 49         511 ok($filetext =~ m/$regex/, "correct LICENSE generated");
238             }
239              
240             sub compact_build_tests {
241             # Assumes COMPACT => 1
242 37     37 0 760142 my ($components) = @_;
243 37         81 my $dist_name = join('-' => @{$components});
  37         141  
244 37         500 ok( -d $dist_name, "compact top-level directory exists" );
245 37         9583 basic_file_and_directory_tests($dist_name);
246 37         8967 license_text_test($dist_name, qr/Terms of Perl itself/);
247              
248 37         9133 my ($filetext);
249 37         477 ok($filetext = read_file_string(File::Spec->catfile($dist_name, 'Makefile.PL')),
250             'Able to read Makefile.PL');
251              
252             my $module_file = File::Spec->catfile(
253             $dist_name,
254             'lib',
255 37         9159 @{$components}[0 .. ($#$components - 1)],
  37         383  
256             "$components->[-1].pm",
257             );
258 37         220 my $test_file = File::Spec->catfile(
259             $dist_name,
260             't',
261             '001_load.t',
262             );
263 37         163 for my $ff ($module_file, $test_file) {
264 74         9746 ok( -f $ff, "$ff exists");
265             }
266 37         8907 return ($module_file, $test_file);
267             }
268              
269             sub check_pm_file {
270 4     4 0 63 my ($pmfile, $predictref) = @_;
271 4         17 my %pred = %$predictref;
272 4         16 my @pmlines;
273 4         41 @pmlines = read_file_array($pmfile);
274 4         15 ok( scalar(@pmlines), ".pm file has content");
275 4 100       1293 if (defined $pred{'pod_present'}) {
276 2         11 pod_present(\@pmlines, \%pred);
277             }
278 4 100       742 if (defined $pred{'constructor_present'}) {
279 2         36 constructor_present(\@pmlines, \%pred);
280             }
281             }
282              
283             sub pod_present {
284 2     2 0 6 my $linesref = shift;
285 2         6 my $predictref = shift;
286 2         10 my $podcount = grep {/^=(head|cut)/} @{$linesref};
  56         91  
  2         13  
287 2 50       4 if (${$predictref}{'pod_present'} == 0) {
  2         16  
288 2         17 is( $podcount, 0, "no POD correctly detected in module");
289             } else {
290 0         0 isnt( $podcount, 0, "POD detected in module");
291             }
292             }
293              
294             sub constructor_present {
295 2     2 0 11 my $linesref = shift;
296 2         12 my $predictref = shift;
297 2         8 my $constructorcount = grep {/^=sub new/} @{$linesref};
  160         177  
  2         7  
298 2 50       3 if (${$predictref}{'constructor_present'} == 0) {
  2         21  
299 2         14 is( $constructorcount, 0, "constructor correctly absent from module");
300             } else {
301 0           isnt( $constructorcount, 0, "constructor correctly present in module");
302             }
303             }
304              
305             =head1 SEE ALSO
306              
307             F.
308              
309             =cut
310              
311             1;
312