| 1 | package Module::Install::StandardTests; |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | use File::Spec; |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | our $VERSION = '0.06'; |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | use base 'Module::Install::Base'; |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | sub use_standard_tests { |
|---|
| 15 | my ($self, %specs) = @_; |
|---|
| 16 | |
|---|
| 17 | my %with = map { $_ => 1 } qw/compile pod pod_coverage perl_critic/; |
|---|
| 18 | if (exists $specs{without}) { |
|---|
| 19 | $specs{without} = [ $specs{without} ] unless ref $specs{without}; |
|---|
| 20 | delete $with{$_} for @{ $specs{without} }; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | $self->test_requires('Test::More'); |
|---|
| 24 | $self->test_requires('UNIVERSAL::require'); |
|---|
| 25 | |
|---|
| 26 | # Unlike other tests, this is mandatory. |
|---|
| 27 | $self->test_requires('Test::Compile'); |
|---|
| 28 | |
|---|
| 29 | $self->write_standard_test_compile; # no if; this is mandatory |
|---|
| 30 | $self->write_standard_test_pod if $with{pod}; |
|---|
| 31 | $self->write_standard_test_pod_coverage if $with{pod_coverage}; |
|---|
| 32 | $self->write_standard_test_perl_critic if $with{perl_critic}; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | sub write_test_file { |
|---|
| 37 | my ($self, $filename, $code) = @_; |
|---|
| 38 | $filename = File::Spec->catfile('t', $filename); |
|---|
| 39 | |
|---|
| 40 | # Outdent the code somewhat. Remove first empty line, if any. Then |
|---|
| 41 | # determine the indent of the first line. Throw that amount of indenting |
|---|
| 42 | # away from any line. This allows you to indent the code so it's visually |
|---|
| 43 | # clearer (see methods below) while creating output that's indented more |
|---|
| 44 | # or less correctly. Smoke result HTML pages link to the .t files, so it |
|---|
| 45 | # looks neater. |
|---|
| 46 | |
|---|
| 47 | $code =~ s/^ *\n//; |
|---|
| 48 | (my $indent = $code) =~ s/^( *).*/$1/s; |
|---|
| 49 | $code =~ s/^$indent//gm; |
|---|
| 50 | |
|---|
| 51 | print "Creating $filename\n"; |
|---|
| 52 | open(my $fh, ">$filename") or die "can't create $filename $!"; |
|---|
| 53 | |
|---|
| 54 | my $perl = $^X; |
|---|
| 55 | print $fh <<TEST; |
|---|
| 56 | #!$perl -w |
|---|
| 57 | |
|---|
| 58 | use strict; |
|---|
| 59 | use warnings; |
|---|
| 60 | |
|---|
| 61 | $code |
|---|
| 62 | TEST |
|---|
| 63 | |
|---|
| 64 | close $fh or die "can't close $filename $!\n"; |
|---|
| 65 | $self->realclean_files($filename); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | sub write_standard_test_compile { |
|---|
| 70 | my $self = shift; |
|---|
| 71 | $self->write_test_file('000_standard__compile.t', q/ |
|---|
| 72 | BEGIN { |
|---|
| 73 | use Test::More; |
|---|
| 74 | eval "use Test::Compile"; |
|---|
| 75 | Test::More->builder->BAIL_OUT( |
|---|
| 76 | "Test::Compile required for testing compilation") if $@; |
|---|
| 77 | all_pm_files_ok(); |
|---|
| 78 | } |
|---|
| 79 | /); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | sub write_standard_test_pod { |
|---|
| 84 | my $self = shift; |
|---|
| 85 | $self->write_test_file('000_standard__pod.t', q/ |
|---|
| 86 | use Test::More; |
|---|
| 87 | eval "use Test::Pod"; |
|---|
| 88 | plan skip_all => "Test::Pod required for testing POD" if $@; |
|---|
| 89 | all_pod_files_ok(); |
|---|
| 90 | /); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | sub write_standard_test_pod_coverage { |
|---|
| 95 | my $self = shift; |
|---|
| 96 | $self->write_test_file('000_standard__pod_coverage.t', q/ |
|---|
| 97 | use Test::More; |
|---|
| 98 | eval "use Test::Pod::Coverage"; |
|---|
| 99 | plan skip_all => |
|---|
| 100 | "Test::Pod::Coverage required for testing POD coverage" if $@; |
|---|
| 101 | all_pod_coverage_ok(); |
|---|
| 102 | /); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | sub write_standard_test_perl_critic { |
|---|
| 107 | my $self = shift; |
|---|
| 108 | $self->write_test_file('000_standard__perl_critic.t', q/ |
|---|
| 109 | use FindBin '$Bin'; |
|---|
| 110 | use File::Spec; |
|---|
| 111 | use UNIVERSAL::require; |
|---|
| 112 | use Test::More; |
|---|
| 113 | |
|---|
| 114 | plan skip_all => |
|---|
| 115 | 'Author test. Set $ENV{TEST_AUTHOR} to a true value to run.' |
|---|
| 116 | unless $ENV{TEST_AUTHOR}; |
|---|
| 117 | |
|---|
| 118 | my %opt; |
|---|
| 119 | my $rc_file = File::Spec->catfile($Bin, 'perlcriticrc'); |
|---|
| 120 | $opt{'-profile'} = $rc_file if -r $rc_file; |
|---|
| 121 | |
|---|
| 122 | if (Perl::Critic->require('1.078') && |
|---|
| 123 | Test::Perl::Critic->require && |
|---|
| 124 | Test::Perl::Critic->import(%opt)) { |
|---|
| 125 | |
|---|
| 126 | all_critic_ok("lib"); |
|---|
| 127 | } else { |
|---|
| 128 | plan skip_all => $@; |
|---|
| 129 | } |
|---|
| 130 | /); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | 1; |
|---|
| 135 | |
|---|
| 136 | __END__ |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | =head1 NAME |
|---|
| 141 | |
|---|
| 142 | Module::Install::StandardTests - generate standard tests for installation |
|---|
| 143 | |
|---|
| 144 | =head1 SYNOPSIS |
|---|
| 145 | |
|---|
| 146 | use inc::Module::Install; |
|---|
| 147 | name 'Class-Null'; |
|---|
| 148 | all_from 'lib/Class/Null.pm'; |
|---|
| 149 | |
|---|
| 150 | use_test_base; |
|---|
| 151 | use_standard_tests; |
|---|
| 152 | auto_include; |
|---|
| 153 | WriteAll; |
|---|
| 154 | |
|---|
| 155 | =head1 DESCRIPTION |
|---|
| 156 | |
|---|
| 157 | Writes a few standard test files to the test directory C<t/>. |
|---|
| 158 | |
|---|
| 159 | =head1 FUNCTIONS |
|---|
| 160 | |
|---|
| 161 | =over 4 |
|---|
| 162 | |
|---|
| 163 | =item use_standard_tests |
|---|
| 164 | |
|---|
| 165 | use_standard_tests; |
|---|
| 166 | use_standard_tests(without => 'pod_coverage'); |
|---|
| 167 | use_standard_tests(without => [ qw/pod_coverage perl_critic/ ]); |
|---|
| 168 | |
|---|
| 169 | Adds a few requirements to the build process, then simply calls the |
|---|
| 170 | C<write_standard_test_*> methods one after the other. |
|---|
| 171 | |
|---|
| 172 | If you pass a named argument called C<without>, the the tests corresponding to |
|---|
| 173 | the value (as a string) or values (as an array reference) are omitted. Possible values are: |
|---|
| 174 | |
|---|
| 175 | =over 4 |
|---|
| 176 | |
|---|
| 177 | =item compile |
|---|
| 178 | |
|---|
| 179 | =item pod |
|---|
| 180 | |
|---|
| 181 | =item pod_coverage |
|---|
| 182 | |
|---|
| 183 | =item perl_critic |
|---|
| 184 | |
|---|
| 185 | =back |
|---|
| 186 | |
|---|
| 187 | =item write_standard_test_compile |
|---|
| 188 | |
|---|
| 189 | Writes the C<t/000_standard__compile.t> file, which uses L<Test::Compile> to |
|---|
| 190 | check that all perl module files compile. If L<Test::Compile> is not |
|---|
| 191 | available, the tests are skipped. |
|---|
| 192 | |
|---|
| 193 | =item write_standard_test_perl_critic |
|---|
| 194 | |
|---|
| 195 | Writes the C<t/000_standard__perl_critic.t> file, which uses |
|---|
| 196 | L<Test::Perl::Critic> to criticise Perl source code for best practices. If |
|---|
| 197 | L<Test::Perl::Critic> is not available, the tests are skipped. |
|---|
| 198 | |
|---|
| 199 | If there is a C<t/perlcriticrc> file, it is used as the Perl::Critic |
|---|
| 200 | configuration. |
|---|
| 201 | |
|---|
| 202 | =item write_standard_test_pod |
|---|
| 203 | |
|---|
| 204 | Writes the C<t/000_standard__pod.t> file, which uses L<Test::Pod> to check for |
|---|
| 205 | POD errors in files. If L<Test::Pod> is not available, the tests are skipped. |
|---|
| 206 | |
|---|
| 207 | =item write_standard_test_pod_coverage |
|---|
| 208 | |
|---|
| 209 | Writes the C<t/000_standard__pod_coverage.t> file, which uses |
|---|
| 210 | L<Test::Pod::Coverage> to check for POD coverage in the distribution. If |
|---|
| 211 | L<Test::Pod::Coverage> is not available, the tests are skipped. |
|---|
| 212 | |
|---|
| 213 | =item write_test_file($filename, $code) |
|---|
| 214 | |
|---|
| 215 | $self->write_test_file('000_standard__perl_critic.t', q/.../); |
|---|
| 216 | |
|---|
| 217 | Writes the code into the specified file inside the C<t/> directory. The |
|---|
| 218 | shebang line, together with C<use warnings;> and C<use strict;> are prepended |
|---|
| 219 | to the code. |
|---|
| 220 | |
|---|
| 221 | =back |
|---|
| 222 | |
|---|
| 223 | =head1 CAVEATS |
|---|
| 224 | |
|---|
| 225 | =over 4 |
|---|
| 226 | |
|---|
| 227 | =item auto_install |
|---|
| 228 | |
|---|
| 229 | If you use C<use_standard_tests()> together with C<auto_install()>, make sure |
|---|
| 230 | that C<auto_install()> is called after C<use_standard_tests()>. This is |
|---|
| 231 | because C<auto_install()> looks at the C<t/> directory to determine which |
|---|
| 232 | tests to run during C<make test>, so to pick up the standard tests they will |
|---|
| 233 | have to have been generated beforehand. |
|---|
| 234 | |
|---|
| 235 | =back |
|---|
| 236 | |
|---|
| 237 | =head1 BUGS AND LIMITATIONS |
|---|
| 238 | |
|---|
| 239 | No bugs have been reported. |
|---|
| 240 | |
|---|
| 241 | Please report any bugs or feature requests through the web interface at |
|---|
| 242 | L<http://rt.cpan.org>. |
|---|
| 243 | |
|---|
| 244 | =head1 INSTALLATION |
|---|
| 245 | |
|---|
| 246 | See perlmodinstall for information and options on installing Perl modules. |
|---|
| 247 | |
|---|
| 248 | =head1 AVAILABILITY |
|---|
| 249 | |
|---|
| 250 | The latest version of this module is available from the Comprehensive Perl |
|---|
| 251 | Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a CPAN |
|---|
| 252 | site near you. Or see <http://www.perl.com/CPAN/authors/id/M/MA/MARCEL/>. |
|---|
| 253 | |
|---|
| 254 | =head1 AUTHORS |
|---|
| 255 | |
|---|
| 256 | Marcel GrE<uuml>nauer, C<< <marcel@cpan.org> >> |
|---|
| 257 | |
|---|
| 258 | =head1 COPYRIGHT AND LICENSE |
|---|
| 259 | |
|---|
| 260 | Copyright 2007-2008 by the authors. |
|---|
| 261 | |
|---|
| 262 | This library is free software; you can redistribute it and/or modify |
|---|
| 263 | it under the same terms as Perl itself. |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | =cut |
|---|
| 267 | |
|---|