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   22948 use strict;
  16         41  
  16         476  
3             # Contains test subroutines for distribution with ExtUtils::ModuleMaker
4 16     16   83 use warnings;
  16         34  
  16         1597  
5             our ( $VERSION, @ISA, @EXPORT_OK );
6             $VERSION = "0.62";
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   108 use Carp;
  16         33  
  16         949  
26 16     16   104 use Cwd;
  16         50  
  16         906  
27 16     16   101 use File::Path;
  16         32  
  16         787  
28 16     16   103 use File::Spec;
  16         35  
  16         776  
29 16     16   1731 use File::Temp qw| tempdir |;
  16         38229  
  16         901  
30 16     16   106 no warnings 'once';
  16         33  
  16         1222  
31             *ok = *Test::More::ok;
32             *is = *Test::More::is;
33             *isnt = *Test::More::isnt;
34             *like = *Test::More::like;
35 16     16   101 use warnings;
  16         53  
  16         486  
36 16     16   6333 use lib ( qw| ./t/testlib | );
  16         8771  
  16         90  
37 16     16   6404 use MockHomeDir;
  16         39346  
  16         27573  
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 27961 my $file = shift;
64 124 50       4959 open my $fh, $file or die "Unable to open filehandle: $!";
65 124         510 my $filetext = do { local $/; <$fh> };
  124         579  
  124         3897  
66 124 50       1512 close $fh or die "Unable to close filehandle: $!";
67 124         944 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 104 my $file = shift;
83 12 50       563 open my $fh, $file or die "Unable to open filehandle: $!";
84 12         524 my @filetext = <$fh>;
85 12 50       160 close $fh or die "Unable to close filehandle: $!";
86 12         196 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 14 my ($manifest_entries, $components) = @_;
112 4         10 my $module_name = join('::' => @{$components});
  4         66  
113 4         46 my $dist_name = join('-' => @{$components});
  4         15  
114 4         10 my $path_str = File::Spec->catdir('lib', @{$components});
  4         72  
115              
116 4         60 my @filetext = read_file_array(File::Spec->catfile($dist_name, 'MANIFEST'));
117 4         25 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         1126 @{$components}[0 .. ($#$components - 1)],
  4         57  
124             "$components->[-1].pm",
125             );
126 4         12 my $str;
127 4         13 ok($str = read_file_string($module),
128             "Able to read $module");
129 4         1129 ok($str =~ m|$module_name\s-\sTest\sof\sthe\scapacities\sof\sEU::MM|,
130             'POD contains module name and abstract');
131 4         1172 ok($str =~ m|=head1\sHISTORY|,
132             'POD contains history head');
133 4         1251 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 1211 my ($topdir, $predictref) = @_;
160 11         48 my @pred = @$predictref;
161              
162 11         146 my $mkfl = File::Spec->catfile( $topdir, q{Makefile.PL} );
163 11         67 my $bigstr = read_file_string($mkfl);
164 11         610 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 6221 my ($caller, $argslistref, $pattern, $message) = @_;
175 11         29 my ($tdir, $obj);
176 11         47 $tdir = tempdir( CLEANUP => 1);
177 11         5123 ok(chdir $tdir, 'changed to temp directory for testing');
178 11         5580 local $@ = undef;
179 11         32 eval { $obj = $caller->new (@$argslistref); };
  11         63  
180 11         938 like($@, qr/$pattern/, $message);
181             }
182              
183             sub licensetest {
184 41     41 0 12953 my ($caller, $license, $pattern) = @_;
185 41         103 my ($tdir, $mod);
186 41         166 $tdir = tempdir( CLEANUP => 1);
187 41         15566 ok(chdir $tdir, "changed to temp directory for testing $license");
188              
189 41         11642 ok($mod = $caller->new(
190             NAME => "Alpha::$license",
191             LICENSE => $license,
192             COMPACT => 1,
193             ), "object for module Alpha::$license created");
194 41         11488 ok( $mod->complete_build(), 'call complete_build()' );
195 41         12679 ok(chdir "Alpha-$license", "changed to Alpha-$license directory");
196 41         12427 my $licensetext = read_file_string('LICENSE');
197 41         234 like($licensetext, $pattern, "$license license has predicted content");
198 41         11757 ok(chdir $tdir, "CLEANUP tempdir");
199             }
200              
201             sub prepare_mockdirs {
202 54     54 0 446638 my $home_dir = prepare_mock_homedir();
203 54         280 my $personal_defaults_dir = MockHomeDir::personal_defaults_dir();
204 54 50       1311 croak "Unable to locate '$personal_defaults_dir'" unless (-d $personal_defaults_dir);
205 54         978 ok(-d $personal_defaults_dir, "Directory $personal_defaults_dir created to mock home directory");
206 54         20725 return ($home_dir, $personal_defaults_dir);
207             }
208              
209             sub prepare_mock_homedir {
210 55     55 0 4732 my $home_dir = MockHomeDir::home_dir();
211 55 50       1421 croak "Unable to locate '$home_dir'" unless (-d $home_dir);
212 55         1140 ok(-d $home_dir, "Directory $home_dir created to mock home directory");
213 55         23748 return $home_dir;
214             }
215              
216             sub basic_file_and_directory_tests {
217 48     48 0 244185 my $dist_name = shift;
218 48         260 for my $f ( qw| Changes MANIFEST Makefile.PL LICENSE README | ) {
219 240         77231 my $ff = File::Spec->catfile($dist_name, $f);
220 240         4602 ok (-e $ff, "$ff exists");
221             }
222 48         18549 for my $d ( qw| lib t | ) {
223 96         19337 my $dd = File::Spec->catdir($dist_name, $d);
224 96         1826 ok(-d $dd, "Directory '$dd' exists");
225             }
226             }
227              
228             sub license_text_test {
229 49     49 0 212984 my ($dist_name, $regex) = @_;
230 49         140 my $filetext;
231             {
232 49 50       187 open my $FILE, '<', File::Spec->catfile($dist_name, 'LICENSE')
  49         2726  
233             or croak "Unable to open LICENSE for reading";
234 49         243 $filetext = do {local $/; <$FILE>};
  49         288  
  49         3006  
235 49 50       984 close $FILE or croak "Unable to close LICENSE after reading";
236             }
237 49         714 ok($filetext =~ m/$regex/, "correct LICENSE generated");
238             }
239              
240             sub compact_build_tests {
241             # Assumes COMPACT => 1
242 37     37 0 1205310 my ($components) = @_;
243 37         117 my $dist_name = join('-' => @{$components});
  37         260  
244 37         761 ok( -d $dist_name, "compact top-level directory exists" );
245 37         15548 basic_file_and_directory_tests($dist_name);
246 37         15461 license_text_test($dist_name, qr/Terms of Perl itself/);
247              
248 37         15501 my ($filetext);
249 37         680 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         15252 @{$components}[0 .. ($#$components - 1)],
  37         598  
256             "$components->[-1].pm",
257             );
258 37         325 my $test_file = File::Spec->catfile(
259             $dist_name,
260             't',
261             '001_load.t',
262             );
263 37         176 for my $ff ($module_file, $test_file) {
264 74         16443 ok( -f $ff, "$ff exists");
265             }
266 37         14907 return ($module_file, $test_file);
267             }
268              
269             sub check_pm_file {
270 4     4 0 73 my ($pmfile, $predictref) = @_;
271 4         31 my %pred = %$predictref;
272 4         15 my @pmlines;
273 4         49 @pmlines = read_file_array($pmfile);
274 4         32 ok( scalar(@pmlines), ".pm file has content");
275 4 100       1819 if (defined $pred{'pod_present'}) {
276 2         20 pod_present(\@pmlines, \%pred);
277             }
278 4 100       1160 if (defined $pred{'constructor_present'}) {
279 2         40 constructor_present(\@pmlines, \%pred);
280             }
281             }
282              
283             sub pod_present {
284 2     2 0 12 my $linesref = shift;
285 2         15 my $predictref = shift;
286 2         12 my $podcount = grep {/^=(head|cut)/} @{$linesref};
  56         125  
  2         8  
287 2 50       12 if (${$predictref}{'pod_present'} == 0) {
  2         17  
288 2         14 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 15 my $linesref = shift;
296 2         14 my $predictref = shift;
297 2         10 my $constructorcount = grep {/^=sub new/} @{$linesref};
  160         353  
  2         15  
298 2 50       9 if (${$predictref}{'constructor_present'} == 0) {
  2         37  
299 2         25 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