2 Test::Perl::Critic - Use Perl::Critic in test programs
7 use Test::Perl::Critic;
8 use Test::More tests => 1;
11 Or test all files in one or more directories:
13 use Test::Perl::Critic;
14 all_critic_ok($dir_1, $dir_2, $dir_N );
16 Or test all files in a distribution:
18 use Test::Perl::Critic;
21 Recommended usage for CPAN distributions:
27 use English qw(-no_match_vars);
29 if ( not $ENV{TEST_AUTHOR} ) {
30 my $msg = 'Author test. Set $ENV{TEST_AUTHOR} to a true value to run.';
31 plan( skip_all => $msg );
34 eval { require Test::Perl::Critic; };
37 my $msg = 'Test::Perl::Critic required to criticise code';
38 plan( skip_all => $msg );
41 my $rcfile = File::Spec->catfile( 't', 'perlcriticrc' );
42 Test::Perl::Critic->import( -profile => $rcfile );
46 Test::Perl::Critic wraps the Perl::Critic engine in a convenient
47 subroutine suitable for test programs written using the Test::More
48 framework. This makes it easy to integrate coding-standards enforcement
49 into the build process. For ultimate convenience (at the expense of some
50 flexibility), see the criticism pragma.
52 If you'd like to try Perl::Critic without installing anything, there is
53 a web-service available at <http://perlcritic.com>. The web-service does
54 not yet support all the configuration features that are available in the
55 native Perl::Critic API, but it should give you a good idea of what it
56 does. You can also invoke the perlcritic web-service from the command
57 line by doing an HTTP-post, such as one of these:
59 $> POST http://perlcritic.com/perl/critic.pl < MyModule.pm
60 $> lwp-request -m POST http://perlcritic.com/perl/critic.pl < MyModule.pm
61 $> wget -q -O - --post-file=MyModule.pm http://perlcritic.com/perl/critic.pl
63 Please note that the perlcritic web-service is still alpha code. The URL
64 and interface to the service are subject to change.
67 critic_ok( $FILE [, $TEST_NAME ] )
68 Okays the test if Perl::Critic does not find any violations in
69 $FILE. If it does, the violations will be reported in the test
70 diagnostics. The optional second argument is the name of test,
71 which defaults to "Perl::Critic test for $FILE".
73 If you use this form, you should emit your own Test::More plan
76 all_critic_ok( [ @DIRECTORIES ] )
77 Runs "critic_ok()" for all Perl files beneath the given list of
78 @DIRECTORIES. If @DIRECTORIES is empty or not given, this
79 function tries to find all Perl files in the blib/ directory. If
80 the blib/ directory does not exist, then it tries the lib/
81 directory. Returns true if all files are okay, or false if any
84 This subroutine emits its own Test::More plan, so you do not
85 need to specify an expected number of tests yourself.
87 all_code_files ( [@DIRECTORIES] )
88 DEPRECATED: Use the "all_perl_files" subroutine that is exported
89 by Perl::Critic::Utils instead.
91 Returns a list of all the Perl files found beneath each
92 DIRECTORY, If @DIRECTORIES is an empty list, defaults to blib/.
93 If blib/ does not exist, it tries lib/. Skips any files in CVS
94 or Subversion directories.
98 * Any file that ends in .PL, .pl, .pm, or .t
99 * Any file that has a first line with a shebang containing
103 Perl::Critic is highly configurable. By default, Test::Perl::Critic
104 invokes Perl::Critic with it's default configuration. But if you have
105 developed your code against a custom Perl::Critic configuration, you
106 will want to configure Test::Perl::Critic to do the same.
108 Any arguments given to the "use" pragma will be passed into the
109 Perl::Critic constructor. So if you have developed your code using a
110 custom ~/.perlcriticrc file, you can direct Test::Perl::Critic to use a
113 use Test::Perl::Critic (-profile => 't/perlcriticrc');
116 Now place a copy of your own ~/.perlcriticrc file in the distribution as
117 t/perlcriticrc. Then, "critic_ok()" will be run on all Perl files in
118 this distribution using this same Perl::Critic configuration. See the
119 Perl::Critic documentation for details on the .perlcriticrc file format.
121 Any argument that is supported by the Perl::Critic constructor can be
122 passed through this interface. For example, you can also set the minimum
123 severity level, or include & exclude specific policies like this:
125 use Test::Perl::Critic (-severity => 2, -exclude => ['RequireRcsKeywords']);
128 See the Perl::Critic documentation for complete details on it's options
132 By default, Test::Perl::Critic displays basic information about each
133 Policy violation in the diagnostic output of the test. You can customize
134 the format and content of this information by using the "-verbose"
135 option. This behaves exactly like the "-verbose" switch on the
136 perlcritic program. For example:
138 use Test::Perl::Critic (-verbose => 6);
142 use Test::Perl::Critic (-verbose => '%f: %m at %l');
144 If given a number, Test::Perl::Critic reports violations using one of
145 the predefined formats described below. If given a string, it is
146 interpreted to be an actual format specification. If the "-verbose"
147 option is not specified, it defaults to 3.
149 Verbosity Format Specification
150 ----------- -------------------------------------------------------------
152 2 "%f: (%l:%c) %m\n",
153 3 "%m at %f line %l\n",
154 4 "%m at line %l, column %c. %e. (Severity: %s)\n",
155 5 "%f: %m at line %l, column %c. %e. (Severity: %s)\n",
156 6 "%m at line %l, near '%r'. (Severity: %s)\n",
157 7 "%f: %m at line %l near '%r'. (Severity: %s)\n",
158 8 "[%p] %m at line %l, column %c. (Severity: %s)\n",
159 9 "[%p] %m at line %l, near '%r'. (Severity: %s)\n",
160 10 "%m at line %l, column %c.\n %p (Severity: %s)\n%d\n",
161 11 "%m at line %l, near '%r'.\n %p (Severity: %s)\n%d\n"
163 Formats are a combination of literal and escape characters similar to
164 the way "sprintf" works. See String::Format for a full explanation of
165 the formatting capabilities. Valid escape characters are:
168 ------- ----------------------------------------------------------------
169 %c Column number where the violation occurred
170 %d Full diagnostic discussion of the violation
171 %e Explanation of violation or page numbers in PBP
172 %f Name of the file where the violation occurred.
173 %l Line number where the violation occurred
174 %m Brief description of the violation
175 %P Name of the Policy module that created the violation
176 %p Name of the Policy without the Perl::Critic::Policy:: prefix
177 %r The string of source code that caused the violation
178 %s The severity level of the violation
181 Despite the convenience of using a test script to enforce your coding
182 standards, there are some inherent risks when distributing those tests
183 to others. Since you don't know which version of Perl::Critic the
184 end-user has and whether they have installed any additional Policy
185 modules, you can't really be sure that your code will pass the
186 Test::Perl::Critic tests on another machine.
188 For these reasons, we strongly advise you to make your perlcritic tests
189 optional, or exclude them from the distribution entirely.
191 The recommended usage in the "SYNOPSIS" section illustrates one way to
192 make your perlcritic.t test optional. Also, you should not list
193 Test::Perl::Critic as a requirement in your build script. These tests
194 are only relevant to the author and should not be a prerequisite for
198 <http://www.chrisdolan.net/talk/index.php/2005/11/14/private-regression-
199 tests/> for an interesting discussion about Test::Perl::Critic and other
200 types of author-only regression tests.
207 If you want a small performance boost, you can tell PPI to cache results
208 from previous parsing runs. Most of the processing time is in
209 Perl::Critic, not PPI, so the speedup is not huge (only about 20%).
210 Nonetheless, if your distribution is large, it's worth the effort.
212 Add a block of code like the following to your test program, probably
213 just before the call to "all_critic_ok()". Be sure to adjust the path to
214 the temp directory appropriately for your system.
217 my $cache_path = File::Spec->catdir(File::Spec->tmpdir,
218 "test-perl-critic-cache-$ENV{USER}");
219 if (!-d $cache_path) {
220 mkdir $cache_path, oct 700;
223 PPI::Cache->import(path => $cache_path);
225 We recommend that you do NOT use this technique for tests that will go
226 out to end-users. They're probably going to only run the tests once, so
227 they will not see the benefit of the caching but will still have files
228 stored in their temp directory.
231 If you find any bugs, please submit them to
232 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perl-Critic>. Thanks.
242 Andy Lester, whose Test::Pod module provided most of the code and
243 documentation for Test::Perl::Critic. Thanks, Andy.
246 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
249 Copyright (c) 2005-2006 Jeffrey Ryan Thalhammer. All rights reserved.
251 This program is free software; you can redistribute it and/or modify it
252 under the same terms as Perl itself. The full text of this license can
253 be found in the LICENSE file included with this module.