root/lang/perl/Module-Install-StandardTests/trunk/lib/Module/Install/StandardTests.pm @ 16491

Revision 16491, 6.6 kB (checked in by hanekomu, 6 years ago)

continued to update distribution to standard style

Line 
1package Module::Install::StandardTests;
2
3use warnings;
4use strict;
5use File::Spec;
6
7
8our $VERSION = '0.06';
9
10
11use base 'Module::Install::Base';
12
13
14sub 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
36sub 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
58use strict;
59use warnings;
60
61$code
62TEST
63
64    close $fh or die "can't close $filename $!\n";
65    $self->realclean_files($filename);
66}
67
68
69sub 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
83sub 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
94sub 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
106sub 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
1341;
135
136__END__
137
138
139
140=head1 NAME
141
142Module::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
157Writes 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
169Adds a few requirements to the build process, then simply calls the
170C<write_standard_test_*> methods one after the other.
171
172If you pass a named argument called C<without>, the the tests corresponding to
173the 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
189Writes the C<t/000_standard__compile.t> file, which uses L<Test::Compile> to
190check that all perl module files compile. If L<Test::Compile> is not
191available, the tests are skipped.
192
193=item write_standard_test_perl_critic
194
195Writes the C<t/000_standard__perl_critic.t> file, which uses
196L<Test::Perl::Critic> to criticise Perl source code for best practices. If
197L<Test::Perl::Critic> is not available, the tests are skipped.
198
199If there is a C<t/perlcriticrc> file, it is used as the Perl::Critic
200configuration.
201
202=item write_standard_test_pod
203
204Writes the C<t/000_standard__pod.t> file, which uses L<Test::Pod> to check for
205POD errors in files. If L<Test::Pod> is not available, the tests are skipped.
206
207=item write_standard_test_pod_coverage
208
209Writes the C<t/000_standard__pod_coverage.t> file, which uses
210L<Test::Pod::Coverage> to check for POD coverage in the distribution. If
211L<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
217Writes the code into the specified file inside the C<t/> directory. The
218shebang line, together with C<use warnings;> and C<use strict;> are prepended
219to the code.
220
221=back
222
223=head1 CAVEATS
224
225=over 4
226
227=item auto_install
228
229If you use C<use_standard_tests()> together with C<auto_install()>, make sure
230that C<auto_install()> is called after C<use_standard_tests()>. This is
231because C<auto_install()> looks at the C<t/> directory to determine which
232tests to run during C<make test>, so to pick up the standard tests they will
233have to have been generated beforehand.
234
235=back
236
237=head1 BUGS AND LIMITATIONS
238
239No bugs have been reported.
240
241Please report any bugs or feature requests through the web interface at
242L<http://rt.cpan.org>.
243
244=head1 INSTALLATION
245
246See perlmodinstall for information and options on installing Perl modules.
247
248=head1 AVAILABILITY
249
250The latest version of this module is available from the Comprehensive Perl
251Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a CPAN
252site near you. Or see <http://www.perl.com/CPAN/authors/id/M/MA/MARCEL/>.
253
254=head1 AUTHORS
255
256Marcel GrE<uuml>nauer, C<< <marcel@cpan.org> >>
257
258=head1 COPYRIGHT AND LICENSE
259
260Copyright 2007-2008 by the authors.
261
262This library is free software; you can redistribute it and/or modify
263it under the same terms as Perl itself.
264
265
266=cut
267
Note: See TracBrowser for help on using the browser.