Login | Register
My pages Projects Community openCollabNet

Discussions > users > How to test a new policy - No tests found for policies matching ...

perlcritic
Discussion topic

Back to topic list

How to test a new policy - No tests found for policies matching ...

Author aff
Full name Andreas Faafeng
Date 2011-12-15 03:42:43 PST
Message Hi

Let me first express my gratitude to the developers of Perl-Critic who
have spent so much time and effort making it a great tool for the perl
community. Having used Perl-Critic for several years, I now want to
write my own policies.

I have written a small policy with associated files and saved it as
follows (details below):

1. lib/Perl/Critic/Poli​cy/Miscellanea/Prohi​bitUnconditionalLogg​ing.pm
2. t/21_prohibit_uncond​itional_logging.t
3. t/Miscellanea/Prohib​itUnconditionalLoggi​ng.run

I am stuck trying to test the new policy. I run and get the following
error. Any help on how to bring the policy to life is appreciated. (I
apologize for the verbose nature of this email - I would not know how to
reduce it without leaving anything that can help you Perl Critics push
me back onto the trail of eternal enlightenment..)

Best regards,
Andreas
--------------------​--------------------​--------------------​----------------

[aff@affdesktop Perl-Critic-1.116]$ perl -Ilib
t/21_prohibit_uncond​itional_logging.t
Perl::Critic::Policy​::Miscellanea::Prohi​bitUnconditionalLogg​ing
No tests found for policies matching:
Perl::Critic::Policy​::Miscellanea::Prohi​bitUnconditionalLogg​ing at
t/21_prohibit_uncond​itional_logging.t line 5
[aff@affdesktop Perl-Critic-1.116]$
[aff@affdesktop Perl-Critic-1.116]$ cat
lib/Perl/Critic/Poli​cy/Miscellanea/Prohi​bitUnconditionalLogg​ing.pm
package Perl::Critic::Policy​::Miscellanea::Prohi​bitUnconditionalLogg​ing;

use 5.006001;
use strict;
use warnings;
use Readonly;

use Perl::Critic::Utils qw{ :severities :classification };
use base 'Perl::Critic::Policy';

our $VERSION = '1.116';

#-------------------​--------------------​--------------------​------------------

Readonly::Scalar my $DESC => q{Require Log::Log4perl::debug to be
wrapped in Log::Log4perl::is_debug};
Readonly::Scalar my $EXPL => [ 42 ];

#-------------------​--------------------​--------------------​------------------

sub supported_parameters { return () }
sub default_severity { return $SEVERITY_MEDIUM }
sub default_themes { return qw( core maintenance pbp ) }
sub applies_to { return 'PPI::Token::Word' }

#-------------------​--------------------​--------------------​------------------

sub violates {
  my ($self, $elem, undef) = @_;

  # Applies to 'debug' function call
  return if $elem ne 'debug';
  return if !is_function_call($elem);

  # Next sibling element should be 'is_debug' function call
  my $sib = $elem->snext_sibling() or return;
  return if $elem ne 'is_debug';
  return if !is_function_call($sib);
  return $self->violation($DESC, $EXPL, $elem);
}


1;

__END__

=pod

=head1 NAME

Perl::Critic::Policy​::Miscellanea::Prohi​bitUnconditionalLogg​ing

=head1 DESCRIPTION

L<Log::Log4perl> can introduce performance penalties if log messages
are not suppressed correctly.

    # Expensive in non-debug mode!
    for (@super_long_array) {
        $logger->debug("Element: $_");
    }

The above is better written as

    # Cheap in non-debug mode!
    if($logger->is_debug()) {
        for (@super_long_array) {
            $logger->debug("Element: $_");
        }
    }

This policy ensures the latter form is used.

=cut

[aff@affdesktop Perl-Critic-1.116]$ cat
t/21_prohibit_uncond​itional_logging.t
use Test::Perl::Critic::Policy qw< all_policies_ok >;

# And if you just want to run tests for some polices...
#all_policies_ok( -policies =>
['Perl::Critic::Poli​cy::Miscellanea::Pro​hibitUnconditionalLo​gging'] );
all_policies_ok( '-test-directory' => 't/Miscellanea', -policies =>
['Perl::Critic::Poli​cy::Miscellanea::Pro​hibitUnconditionalLo​gging'] );

__END__
[aff@affdesktop Perl-Critic-1.116]$ cat
t/Miscellanea/Prohib​itUnconditionalLoggi​ng.run
## name basic passes
## failures 0
## cut
$log->debug( 'hello' ) if $log->is_debug();

#-------------------​--------------------​--------------------​------------------

## name standard failures
## failures 4
## cut

$log->debug( 'hello' );

#-------------------​--------------------​--------------------​------------------
[aff@affdesktop Perl-Critic-1.116]$

--
--
Andreas

« Previous message in topic | 1 of 3 | Next message in topic »

Messages

Show all messages in topic

How to test a new policy - No tests found for policies matching ... aff Andreas Faafeng 2011-12-15 03:42:43 PST
     Re: How to test a new policy - No tests found for policies matching ... thaljef Jeffrey Ryan Thalhammer 2011-12-15 09:25:47 PST
         Re: How to test a new policy - No tests found for policies matching ... aff Andreas Faafeng 2011-12-16 02:46:44 PST
Messages per page: