Skip to content

Commit

Permalink
Item12022: Address perlcriticisms
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk@15239 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
PaulHarvey authored and PaulHarvey committed Aug 1, 2012
1 parent 8831692 commit 9fccd87
Showing 1 changed file with 68 additions and 39 deletions.
107 changes: 68 additions & 39 deletions UnitTestContrib/test/unit/LoggerTests.pm
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# tests for Foswiki::Logger

package LoggerTests;
use FoswikiTestCase;
use strict;
use warnings;
use FoswikiTestCase();
our @ISA = qw( FoswikiTestCase );

use strict;
use File::Temp;
use File::Path;
use Foswiki::Logger::PlainFile;
use File::Temp();
use File::Path();
use Foswiki::Logger::PlainFile();

# NOTE: Test logs are created in the test web so they get torn down when the
# web is torn down in the superclass.
Expand All @@ -22,30 +23,38 @@ sub set_up {
$logDir = "logDir$$";
$Foswiki::cfg{Log}{Dir} = "$logDir";
mkdir $Foswiki::cfg{Log}{Dir};

return;
}

sub tear_down {
my $this = shift;

File::Path::rmtree($logDir);
$this->SUPER::tear_down();

return;
}

sub CompatibilityLogger {
my $this = shift;
require Foswiki::Logger::Compatibility;
$Foswiki::cfg{Log}{Implementation} = 'Foswiki::Logger::Compatibility';
$this->{logger} = new Foswiki::Logger::Compatibility();
$this->{logger} = Foswiki::Logger::Compatibility->new();
$Foswiki::cfg{LogFileName} = "$logDir/logfile%DATE%";
$Foswiki::cfg{DebugFileName} = "$logDir/debug%DATE%";
$Foswiki::cfg{WarningFileName} = "$logDir/warn%DATE%!!";

return;
}

sub PlainFileLogger {
my $this = shift;
require Foswiki::Logger::PlainFile;
$Foswiki::cfg{Log}{Implementation} = 'Foswiki::Logger::PlainFile';
$this->{logger} = new Foswiki::Logger::PlainFile();
$this->{logger} = Foswiki::Logger::PlainFile->new();

return;
}

sub ObfuscatingLogger {
Expand All @@ -54,7 +63,9 @@ sub ObfuscatingLogger {
$Foswiki::cfg{Log}{Implementation} =
'Foswiki::Logger::PlainFile::Obfuscating';
$Foswiki::cfg{Log}{Obfuscating}{MaskIP} = 1;
$this->{logger} = new Foswiki::Logger::PlainFile::Obfuscating();
$this->{logger} = Foswiki::Logger::PlainFile::Obfuscating->new();

return;
}

sub fixture_groups {
Expand Down Expand Up @@ -112,17 +123,18 @@ sub verify_simpleWriteAndReplay {
my $it = $this->{logger}->eachEventSince( $time, $level );
$this->assert( $it->hasNext(), $level );
my $data = $it->next();
my $t = shift(@$data);
my $t = shift( @{$data} );
$this->assert( $t >= $time, "$t $time" );
$ipaddr = '109.104.118.183'
if ( $Foswiki::cfg{Log}{Implementation} eq
'Foswiki::Logger::PlainFile::Obfuscating'
&& $level eq 'warning' );
$this->assert_str_equals( "$level.Green.Eggs.and.$ipaddr",
join( '.', @$data ) );
join( '.', @{$data} ) );
$this->assert( !$it->hasNext() );
}

return;
}

sub verify_eachEventSinceOnEmptyLog {
Expand All @@ -135,6 +147,8 @@ sub verify_eachEventSinceOnEmptyLog {
}
$this->assert( !$it->hasNext() );
}

return;
}

my $plainFileTestTime;
Expand All @@ -146,10 +160,11 @@ sub PlainFileTestTime {
# Test specific to PlainFile logger
sub verify_eachEventSinceOnSeveralLogs {
my $this = shift;
my $logger = new Foswiki::Logger::PlainFile();
my $logger = Foswiki::Logger::PlainFile->new();
my $cache = \&Foswiki::Logger::PlainFile::_time;
no warnings 'redefine';
*Foswiki::Logger::PlainFile::_time = \&PlainFileTestTime;
use warnings 'redefine';

$plainFileTestTime = 3600;
$logger->log( 'info', "Seal" );
Expand Down Expand Up @@ -195,15 +210,18 @@ sub verify_eachEventSinceOnSeveralLogs {
$this->assert_str_equals( "Porpoise", $data->[1] );
$this->assert( !$it->hasNext() );

no warnings 'redefine';
*Foswiki::Logger::PlainFile::_time = $cache;
use warnings 'redefine';

return;
}

sub verify_filter {

# with PlainFile, warning up are all crammed into one logfile
my $this = shift;
my $logger = new Foswiki::Logger::PlainFile();
my $logger = Foswiki::Logger::PlainFile->new();
$logger->log( 'warning', "Shark" );
$logger->log( 'error', "Dolphin" );
$logger->log( 'critical', "Injury" );
Expand All @@ -224,6 +242,8 @@ sub verify_filter {
$data = $it->next();
$this->assert_str_equals( "Hurts", $data->[1] );
$this->assert( !$it->hasNext() );

return;
}

my $mode; # access mode
Expand All @@ -245,18 +265,19 @@ sub verify_rotate_events {
no warnings 'redefine';
*Foswiki::Logger::PlainFile::_time = \&PlainFileTestTime;
*Foswiki::Logger::PlainFile::_stat = \&PlainFileTestStat;
use warnings 'redefine';

$Foswiki::Logger::PlainFile::dontRotate = 1;

my $then = Foswiki::Time::parseTime("2000-02-01T00:00Z");

$plainFileTestTime = $then;
$mode = 0777;
$mode = oct(777);

# Don't try to rotate a non-existant log
my $lfn = "$Foswiki::cfg{Log}{Dir}/events.log";

my $logger = new Foswiki::Logger::PlainFile();
my $logger = Foswiki::Logger::PlainFile->new();
$this->assert( !-e $lfn );
$logger->_rotate($plainFileTestTime);
$this->assert( !-e $lfn );
Expand Down Expand Up @@ -291,10 +312,10 @@ sub verify_rotate_events {
$logger->log( 'alert', 'Salve nauta' );

local $/ = undef;
$this->assert( open( F, '<', $lfn ) );
my $e = <F>;
$this->assert( open( my $F, '<', $lfn ) );
my $e = <$F>;
$this->assert_equals( "| 2000-02-01T00:00:00Z info | Salve nauta |\n", $e );
close(F);
$this->assert( close($F) );

# We should see the creation of a backup log with
# the last-month entry, and the current log should be cut down to
Expand All @@ -303,15 +324,18 @@ sub verify_rotate_events {
$backup =~ s/log$/200001/;
$this->assert( -e $backup );

$this->assert( open( F, '<', $backup ) );
$e = <F>;
$this->assert( open( $F, '<', $backup ) );
$e = <$F>;
$this->assert_equals(
"| 2000-01-31T23:59:00Z info | Nil carborundum illegitami |\n", $e );
close(F);
$this->assert( close($F) );

no warnings 'redefine';
*Foswiki::Logger::PlainFile::_time = $timecache;
*Foswiki::Logger::PlainFile::_stat = $statcache;
use warnings 'redefine';

return;
}

sub verify_rotate_debug {
Expand All @@ -326,18 +350,19 @@ sub verify_rotate_debug {
no warnings 'redefine';
*Foswiki::Logger::PlainFile::_time = \&PlainFileTestTime;
*Foswiki::Logger::PlainFile::_stat = \&PlainFileTestStat;
use warnings 'redefine';

$Foswiki::Logger::PlainFile::dontRotate = 1;

my $then = Foswiki::Time::parseTime("2000-02-01T00:00Z");

$plainFileTestTime = $then;
$mode = 0777;
$mode = oct(777);

# Don't try to rotate a non-existant log
my $lfn = "$Foswiki::cfg{Log}{Dir}/debug.log";

my $logger = new Foswiki::Logger::PlainFile();
my $logger = Foswiki::Logger::PlainFile->new();
$this->assert( !-e $lfn );
$logger->_rotate($plainFileTestTime);
$this->assert( !-e $lfn );
Expand Down Expand Up @@ -372,11 +397,11 @@ sub verify_rotate_debug {
$logger->log( 'alert', 'Salve nauta' );

local $/ = undef;
$this->assert( open( F, '<', $lfn ) );
my $e = <F>;
$this->assert( open( my $F, '<', $lfn ) );
my $e = <$F>;
$this->assert_equals( "| 2000-02-01T00:00:00Z debug | Salve nauta |\n",
$e );
close(F);
$this->assert( close($F) );

# We should see the creation of a backup log with
# the last-month entry, and the current log should be cut down to
Expand All @@ -385,15 +410,18 @@ sub verify_rotate_debug {
$backup =~ s/log$/200001/;
$this->assert( -e $backup );

$this->assert( open( F, '<', $backup ) );
$e = <F>;
$this->assert( open( $F, '<', $backup ) );
$e = <$F>;
$this->assert_equals(
"| 2000-01-31T23:59:00Z debug | Nil carborundum illegitami |\n", $e );
close(F);
$this->assert( close($F) );

no warnings 'redefine';
*Foswiki::Logger::PlainFile::_time = $timecache;
*Foswiki::Logger::PlainFile::_stat = $statcache;
use warnings 'redefine';

return;
}

sub verify_rotate_error {
Expand All @@ -414,12 +442,12 @@ sub verify_rotate_error {
my $then = Foswiki::Time::parseTime("2000-02-01T00:00Z");

$plainFileTestTime = $then;
$mode = 0777;
$mode = oct(777);

# Don't try to rotate a non-existant log
my $lfn = "$Foswiki::cfg{Log}{Dir}/error.log";

my $logger = new Foswiki::Logger::PlainFile();
my $logger = Foswiki::Logger::PlainFile->new();
$this->assert( !-e $lfn );
$logger->_rotate($plainFileTestTime);
$this->assert( !-e $lfn );
Expand Down Expand Up @@ -454,39 +482,40 @@ sub verify_rotate_error {
$logger->log( 'alert', 'Salve nauta' );

local $/ = undef;
$this->assert( open( F, '<', $lfn ) );
my $e = <F>;
$this->assert_equals( $e, <<FILE );
$this->assert( open( my $F, '<', $lfn ) );
my $e = <$F>;
$this->assert( close($F) );
$this->assert_equals( $e, <<'FILE' );
| 2000-02-01T00:00:00Z warning | Salve nauta |
| 2000-02-01T00:00:00Z critical | Salve nauta |
| 2000-02-01T00:00:00Z emergency | Salve nauta |
| 2000-02-01T00:00:00Z error | Salve nauta |
| 2000-02-01T00:00:00Z alert | Salve nauta |
FILE

close(F);

# We should see the creation of a backup log with
# the last-month entry, and the current log should be cut down to
# this month's entry.
my $backup = $lfn;
$backup =~ s/log$/200001/;
$this->assert( -e $backup );

$this->assert( open( F, '<', $backup ) );
$e = <F>;
$this->assert_equals( $e, <<FILE );
$this->assert( open( $F, '<', $backup ) );
$e = <$F>;
$this->assert_equals( $e, <<'FILE' );
| 2000-01-31T23:59:00Z warning | Nil carborundum illegitami |
| 2000-01-31T23:59:00Z critical | Nil carborundum illegitami |
| 2000-01-31T23:59:00Z emergency | Nil carborundum illegitami |
| 2000-01-31T23:59:00Z error | Nil carborundum illegitami |
| 2000-01-31T23:59:00Z alert | Nil carborundum illegitami |
FILE
close(F);
$this->assert( close($F) );

*Foswiki::Logger::PlainFile::_time = $timecache;
*Foswiki::Logger::PlainFile::_stat = $statcache;
use warnings 'redefine';

return;
}

1;

0 comments on commit 9fccd87

Please sign in to comment.