1Test::Perl::Critic(3) User Contributed Perl DocumentationTest::Perl::Critic(3)
2
3
4

NAME

6       Test::Perl::Critic - Use Perl::Critic in test programs
7

SYNOPSIS

9       Test one file:
10
11         use Test::Perl::Critic;
12         use Test::More tests => 1;
13         critic_ok($file);
14
15       Or test all files in one or more directories:
16
17         use Test::Perl::Critic;
18         all_critic_ok($dir_1, $dir_2, $dir_N );
19
20       Or test all files in a distribution:
21
22         use Test::Perl::Critic;
23         all_critic_ok();
24
25       Recommended usage for CPAN distributions:
26
27         use strict;
28         use warnings;
29         use File::Spec;
30         use Test::More;
31         use English qw(-no_match_vars);
32
33         if ( not $ENV{TEST_AUTHOR} ) {
34             my $msg = 'Author test.  Set $ENV{TEST_AUTHOR} to a true value to run.';
35             plan( skip_all => $msg );
36         }
37
38         eval { require Test::Perl::Critic; };
39
40         if ( $EVAL_ERROR ) {
41            my $msg = 'Test::Perl::Critic required to criticise code';
42            plan( skip_all => $msg );
43         }
44
45         my $rcfile = File::Spec->catfile( 't', 'perlcriticrc' );
46         Test::Perl::Critic->import( -profile => $rcfile );
47         all_critic_ok();
48

DESCRIPTION

50       Test::Perl::Critic wraps the Perl::Critic engine in a convenient
51       subroutine suitable for test programs written using the Test::More
52       framework.  This makes it easy to integrate coding-standards
53       enforcement into the build process.  For ultimate convenience (at the
54       expense of some flexibility), see the criticism pragma.
55
56       If you'd like to try Perl::Critic without installing anything, there is
57       a web-service available at <http://perlcritic.com>.  The web-service
58       does not yet support all the configuration features that are available
59       in the native Perl::Critic API, but it should give you a good idea of
60       what it does.  You can also invoke the perlcritic web-service from the
61       command line by doing an HTTP-post, such as one of these:
62
63         $> POST http://perlcritic.com/perl/critic.pl < MyModule.pm
64         $> lwp-request -m POST http://perlcritic.com/perl/critic.pl < MyModule.pm
65         $> wget -q -O - --post-file=MyModule.pm http://perlcritic.com/perl/critic.pl
66
67       Please note that the perlcritic web-service is still alpha code.  The
68       URL and interface to the service are subject to change.
69

SUBROUTINES

71       critic_ok( $FILE [, $TEST_NAME ] )
72               Okays the test if Perl::Critic does not find any violations in
73               $FILE.  If it does, the violations will be reported in the test
74               diagnostics.  The optional second argument is the name of test,
75               which defaults to "Perl::Critic test for $FILE".
76
77               If you use this form, you should emit your own Test::More plan
78               first.
79
80       all_critic_ok( [ @DIRECTORIES ] )
81               Runs "critic_ok()" for all Perl files beneath the given list of
82               @DIRECTORIES.  If @DIRECTORIES is empty or not given, this
83               function tries to find all Perl files in the blib/ directory.
84               If the blib/ directory does not exist, then it tries the lib/
85               directory.  Returns true if all files are okay, or false if any
86               file fails.
87
88               This subroutine emits its own Test::More plan, so you do not
89               need to specify an expected number of tests yourself.
90
91       all_code_files ( [@DIRECTORIES] )
92               DEPRECATED: Use the "all_perl_files" subroutine that is
93               exported by Perl::Critic::Utils instead.
94
95               Returns a list of all the Perl files found beneath each
96               DIRECTORY, If @DIRECTORIES is an empty list, defaults to blib/.
97               If blib/ does not exist, it tries lib/.  Skips any files in CVS
98               or Subversion directories.
99
100               A Perl file is:
101
102               ·   Any file that ends in .PL, .pl, .pm, or .t
103
104               ·   Any file that has a first line with a shebang containing
105                   'perl'
106

CONFIGURATION

108       Perl::Critic is highly configurable.  By default, Test::Perl::Critic
109       invokes Perl::Critic with it's default configuration.  But if you have
110       developed your code against a custom Perl::Critic configuration, you
111       will want to configure Test::Perl::Critic to do the same.
112
113       Any arguments given to the "use" pragma will be passed into the
114       Perl::Critic constructor.  So if you have developed your code using a
115       custom ~/.perlcriticrc file, you can direct Test::Perl::Critic to use a
116       custom file too.
117
118         use Test::Perl::Critic (-profile => 't/perlcriticrc');
119         all_critic_ok();
120
121       Now place a copy of your own ~/.perlcriticrc file in the distribution
122       as t/perlcriticrc.  Then, "critic_ok()" will be run on all Perl files
123       in this distribution using this same Perl::Critic configuration.  See
124       the Perl::Critic documentation for details on the .perlcriticrc file
125       format.
126
127       Any argument that is supported by the Perl::Critic constructor can be
128       passed through this interface.  For example, you can also set the
129       minimum severity level, or include & exclude specific policies like
130       this:
131
132         use Test::Perl::Critic (-severity => 2, -exclude => ['RequireRcsKeywords']);
133         all_critic_ok();
134
135       See the Perl::Critic documentation for complete details on it's options
136       and arguments.
137

DIAGNOSTIC DETAILS

139       By default, Test::Perl::Critic displays basic information about each
140       Policy violation in the diagnostic output of the test.  You can
141       customize the format and content of this information by using the
142       "-verbose" option.  This behaves exactly like the "-verbose" switch on
143       the perlcritic program.  For example:
144
145         use Test::Perl::Critic (-verbose => 6);
146
147         #or...
148
149         use Test::Perl::Critic (-verbose => '%f: %m at %l');
150
151       If given a number, Test::Perl::Critic reports violations using one of
152       the predefined formats described below. If given a string, it is
153       interpreted to be an actual format specification. If the "-verbose"
154       option is not specified, it defaults to 3.
155
156           Verbosity     Format Specification
157           -----------   -------------------------------------------------------------
158            1            "%f:%l:%c:%m\n",
159            2            "%f: (%l:%c) %m\n",
160            3            "%m at %f line %l\n",
161            4            "%m at line %l, column %c.  %e.  (Severity: %s)\n",
162            5            "%f: %m at line %l, column %c.  %e.  (Severity: %s)\n",
163            6            "%m at line %l, near '%r'.  (Severity: %s)\n",
164            7            "%f: %m at line %l near '%r'.  (Severity: %s)\n",
165            8            "[%p] %m at line %l, column %c.  (Severity: %s)\n",
166            9            "[%p] %m at line %l, near '%r'.  (Severity: %s)\n",
167           10            "%m at line %l, column %c.\n  %p (Severity: %s)\n%d\n",
168           11            "%m at line %l, near '%r'.\n  %p (Severity: %s)\n%d\n"
169
170       Formats are a combination of literal and escape characters similar to
171       the way "sprintf" works. See String::Format for a full explanation of
172       the formatting capabilities. Valid escape characters are:
173
174           Escape    Meaning
175           -------   ----------------------------------------------------------------
176           %c        Column number where the violation occurred
177           %d        Full diagnostic discussion of the violation
178           %e        Explanation of violation or page numbers in PBP
179           %f        Name of the file where the violation occurred.
180           %l        Line number where the violation occurred
181           %m        Brief description of the violation
182           %P        Name of the Policy module that created the violation
183           %p        Name of the Policy without the Perl::Critic::Policy:: prefix
184           %r        The string of source code that caused the violation
185           %s        The severity level of the violation
186

CAVEATS

188       Despite the convenience of using a test script to enforce your coding
189       standards, there are some inherent risks when distributing those tests
190       to others.  Since you don't know which version of Perl::Critic the end-
191       user has and whether they have installed any additional Policy modules,
192       you can't really be sure that your code will pass the
193       Test::Perl::Critic tests on another machine.
194
195       For these reasons, we strongly advise you to make your perlcritic tests
196       optional, or exclude them from the distribution entirely.
197
198       The recommended usage in the "SYNOPSIS" section illustrates one way to
199       make your perlcritic.t test optional.  Also, you should not list
200       Test::Perl::Critic as a requirement in your build script.  These tests
201       are only relevant to the author and should not be a prerequisite for
202       end-use.
203
204       See
205       <http://www.chrisdolan.net/talk/index.php/2005/11/14/private-regression-tests/>
206       for an interesting discussion about Test::Perl::Critic and other types
207       of author-only regression tests.
208

EXPORTS

210         critic_ok()
211         all_critic_ok()
212

PERFORMANCE HACKS

214       If you want a small performance boost, you can tell PPI to cache
215       results from previous parsing runs.  Most of the processing time is in
216       Perl::Critic, not PPI, so the speedup is not huge (only about 20%).
217       Nonetheless, if your distribution is large, it's worth the effort.
218
219       Add a block of code like the following to your test program, probably
220       just before the call to "all_critic_ok()".  Be sure to adjust the path
221       to the temp directory appropriately for your system.
222
223           use File::Spec;
224           my $cache_path = File::Spec->catdir(File::Spec->tmpdir,
225                                               "test-perl-critic-cache-$ENV{USER}");
226           if (!-d $cache_path) {
227              mkdir $cache_path, oct 700;
228           }
229           require PPI::Cache;
230           PPI::Cache->import(path => $cache_path);
231
232       We recommend that you do NOT use this technique for tests that will go
233       out to end-users.  They're probably going to only run the tests once,
234       so they will not see the benefit of the caching but will still have
235       files stored in their temp directory.
236

BUGS

238       If you find any bugs, please submit them to
239       <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perl-Critic>.  Thanks.
240

SEE ALSO

242       Module::Starter::PBP
243
244       Perl::Critic
245
246       Test::More
247

CREDITS

249       Andy Lester, whose Test::Pod module provided most of the code and
250       documentation for Test::Perl::Critic.  Thanks, Andy.
251

AUTHOR

253       Jeffrey Ryan Thalhammer <thaljef@cpan.org>
254
256       Copyright (c) 2005-2006 Jeffrey Ryan Thalhammer.  All rights reserved.
257
258       This program is free software; you can redistribute it and/or modify it
259       under the same terms as Perl itself.  The full text of this license can
260       be found in the LICENSE file included with this module.
261
262
263
264perl v5.10.1                      2010-11-12             Test::Perl::Critic(3)
Impressum