The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Perl::Critic::Progressive - Gradually enforce coding standards

SYNOPSIS

To test one or more files, and/or all files in one or more directories:

  use Test::Perl::Critic::Progressive;
  Test::Perl::Critic::Progressive::progressive_critic_ok($file1, $file2, $dir1, $dir2);

To test all files in a distribution:

  use Test::Perl::Critic::Progressive;
  Test::Perl::Critic::Progressive::progressive_critic_ok();

Recommended usage for public CPAN distributions:

  use strict;
  use warnings;
  use English qw(-no_match_vars);
  use Test::More;

  eval { require Test::Perl::Critic::Progressive };
  plan skip_all => 'T::P::C::Progressive required for this test' if $EVAL_ERROR;

  Test::Perl::Critic::Progressive::progressive_critic_ok();

DESCRIPTION

Applying coding standards to large amounts of legacy code is a daunting task. Often times, legacy code is so non-compliant that it seems downright impossible. But, if you consistently chip away at the problem, you will eventually succeed! Test::Perl::Critic::Progressive uses the Perl::Critic engine to gradually to prevent further deterioration of your code and gradually steer it towards conforming with your chosen coding standards.

The most effective way to use Test::Perl::Critic::Progressive is as a unit test that is run under a continuous-integration system like CruiseControl or AntHill. Each time a developer commits changes to the code, this test will fail and the build will break unless it has the same (or fewer) Perl::Critic violations than the last successful test.

SUBROUTINES

Test::Perl::Critic::Progressive::progressive_critic_ok(@FILES [, @DIRECTORIES ])

Uses Perl::Critic to analyze each of the given @FILES, and/or all Perl files beneath the given list of @DIRECTORIES. If no arguments are given, it analyzes all the Perl files in the blib/ directory. If the blib/ directory does not exist, then it tries the lib/ directory. The results of the analysis will be stored as .perlcritic-history in the same directory where your test script is located.

The first time you run this test, it will always pass. But on each subsequent run, the test will pass only if the number of violations found is less than or equal to the number of violations found during the last passing test. If it does pass, then the history file will be updated with the new analysis results. Once all the violations are removed from the code, this test will always pass, a new violation is introduced.

This subroutine emits its own Test::More plan, so you do not need to specify an expected number of tests yourself.

CONFIGURATION

You can use the following functions to configure the behavior of Test::Perl::Critic::Progressive. You should use these before calling progressive_critic_ok. Note that they are never exported.

Test::Perl::Critic::Progressive::get_history_file();
Test::Perl::Critic::Progressive::set_history_file($FILE);

These functions get or set the full path to the history file. This is where Test::Perl::Critic::Progressive will store the results of each passing analysis. If the $FILE does not exist, it will be created anew. The default is $Bin/.perlcritic-history where $Bin is the directory that the calling test script is located in.

Test::Perl::Critic::Progressive::get_step_size();
Test::Perl::Critic::Progressive::set_step_size($INTEGER);

These functions get or set the minimum acceptable decrease in the total number of violations between each test. The default value is zero, which means that you are not required to remove any violations, but you are also not allowed to add any. If you set the step size to a positive number, the test will require you to remove $INTEGER violations each time the test is run. Thus, increasing the step size forces you to correct the code faster.

Test::Perl::Critic::Progressive::get_critic_args();
Test::Perl::Critic::Progressive::set_critic_args(%ARGS);

These functions get or set the arguments given to Perl::Critic. By default, Test::Perl::Critic::Progressive invokes Perl::Critic with its default configuration. But if you have developed your code against a custom Perl::Critic configuration, you will want to configure this test to do the same.

Any %ARGS given to the set_critic_args will be passed directly into the Perl::Critic constructor. So if you have developed your code using a custom .perlcriticrc file, you can direct Test::Perl::Critic::Progressive to use a custom file too.

  use Test::Perl::Critic::Progressive;

  Test::Perl::Critic::Progressive::set_critic_args(-profile => 't/perlcriticrc);
  Test::Perl::Critic::Progressive::progressive_critic_ok();

Now place a copy of your own .perlcriticrc file in the distribution as t/perlcriticrc. Now, progressive_critic_ok will use this same Perl::Critic configuration. See the Perl::Critic documentation for details on the .perlcriticrc file format.

Any argument that is supported by the Perl::Critic constructor can be passed through this interface. For example, you can also set the minimum severity level, or include & exclude specific policies like this:

  use Test::Perl::Critic::Progressive;

  Test::Perl::Critic::Progressive::set_critic_args(-severity => 2);
  Test::Perl::Critic::Progressive::progressive_critic_ok();

See the Perl::Critic documentation for complete details on its options and arguments.

NOTES

The test is evaluated in two ways. First, the number of violations for each Policy must be less than or equal to the number of the violations found during the last passing test. Second, the total number of violations must be less than or equal the total number of violations found during the last passing test (minus the step size). This prevents you from simply substituting one kind of violation for another.

Over time, you'll probably add new Policies to your Perl::Critic setup. When Test::Perl::Critic::Progressive uses a Policy for the first time, any newly discovered violations of that Policy will not be considered in the test. However, they will be considered in subsequent tests.

BUGS

If you find any bugs, please submit them to http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Perl-Critic-Progressive. Thanks.

SEE ALSO

criticism

Perl::Critic

Test::Perl::Critic

http://www.perlcritic.com

AUTHOR

Jeffrey Ryan Thalhammer <thaljef@cpan.org>

COPYRIGHT

Copyright (c) 2007 Jeffrey Ryan Thalhammer. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.