Skip to content

Commit 77c8659

Browse files
committed
test improvements removing race conditions
1 parent 0711579 commit 77c8659

25 files changed

+115
-324
lines changed

.cvsignore

-5
This file was deleted.

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ pm_to_blib
55
MYMETA.json
66
MYMETA.yml
77
*.log
8-
t/tmp/

Changes

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
##################################################
2-
Revision history for Log::Log4perl
3-
##################################################
1+
- test improvements removing race conditions
42

53
1.50 2020-07-22
64
* (ms) Fix for slow unlinking on Windows test - thanks @zhiyuan-lin

MANIFEST.SKIP

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ blib
66
^modules
77
^Log4perl.pm
88
CVS
9-
^t/bak
10-
^t/tmp/
11-
.cvsignore
129
docs
1310
lib/Log/Dispatch
1411
MANIFEST.bak

t/.cvsignore

-2
This file was deleted.

t/010JConsole.t

+4-19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use Log::Log4perl::Appender::TestBuffer;
1010
use Log::Log4perl::Appender::File;
1111
use File::Spec;
1212
use Test::More;
13+
use lib File::Spec->catdir(qw(t lib));
14+
use Log4perlInternalTest qw(tmpdir);
1315

1416
our $LOG_DISPATCH_PRESENT = 0;
1517

@@ -23,20 +25,8 @@ BEGIN {
2325
}
2426
};
2527

26-
my $WORK_DIR = "tmp";
27-
if(-d "t") {
28-
$WORK_DIR = File::Spec->catfile(qw(t tmp));
29-
}
30-
unless (-e "$WORK_DIR"){
31-
mkdir("$WORK_DIR", 0755) || die "can't create $WORK_DIR ($!)";
32-
}
33-
34-
use vars qw(@outfiles $test_logfile);
35-
$test_logfile = File::Spec->catfile($WORK_DIR,'test1.log');
36-
@outfiles = ($test_logfile,);
37-
foreach my $f (@outfiles){
38-
unlink $f if (-e $f);
39-
}
28+
my $WORK_DIR = tmpdir();
29+
my $test_logfile = File::Spec->catfile($WORK_DIR,'test1.log');
4030

4131
my $conf = <<CONF;
4232
log4j.category.cat1 = INFO, myAppender
@@ -52,7 +42,6 @@ Log::Log4perl->init(\$conf);
5242
my $logger = Log::Log4perl->get_logger('cat1');
5343

5444
#hmm, I wonder how portable this is, maybe check $^O first?
55-
use vars qw($OLDOUT); #for -w
5645
open(OLDOUT, ">&STDOUT");
5746
open (TOUCH, ">>$test_logfile");# `touch $test_logfile`;
5847
close TOUCH;
@@ -85,7 +74,3 @@ my $rc = is ($result, $expected);
8574
if( !$rc ) {
8675
warn "Failed with Log::Dispatch $Log::Dispatch::VERSION";
8776
}
88-
89-
foreach my $f (@outfiles){
90-
unlink $f if (-e $f);
91-
}

t/011JFile.t

+4-20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ BEGIN {
88
use Log::Log4perl;
99
use Test::More;
1010
use File::Spec;
11+
use lib File::Spec->catdir(qw(t lib));
12+
use Log4perlInternalTest qw(tmpdir);
1113

1214
our $LOG_DISPATCH_PRESENT = 0;
1315

@@ -21,21 +23,8 @@ BEGIN {
2123
}
2224
};
2325

24-
my $WORK_DIR = "tmp";
25-
if(-d "t") {
26-
$WORK_DIR = File::Spec->catfile(qw(t tmp));
27-
}
28-
unless (-e "$WORK_DIR"){
29-
mkdir("$WORK_DIR", 0755) || die "can't create $WORK_DIR ($!)";
30-
}
31-
32-
use vars qw(@outfiles $test_logfile);
33-
$test_logfile = File::Spec->catfile($WORK_DIR, 'test2.log');
34-
@outfiles = ($test_logfile);
35-
foreach my $f (@outfiles){
36-
unlink $f if (-e $f);
37-
}
38-
26+
my $WORK_DIR = tmpdir();
27+
my $test_logfile = File::Spec->catfile($WORK_DIR, 'test2.log');
3928

4029
my $conf = <<CONF;
4130
log4j.category.cat1 = INFO, myAppender
@@ -70,8 +59,3 @@ EOL
7059
close F;
7160
}
7261
is ($result, $expected);
73-
74-
foreach my $f (@outfiles){
75-
unlink $f if (-e $f);
76-
}
77-

t/012Deeper.t

+11-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# `make test'. After `make install' it should work as `perl test.pl'
33

44

5-
BEGIN {
5+
BEGIN {
66
if($ENV{INTERNAL_DEBUG}) {
77
require Log::Log4perl::InternalDebug;
88
Log::Log4perl::InternalDebug->enable();
@@ -12,10 +12,12 @@ BEGIN {
1212
use Log::Log4perl;
1313
use Test::More;
1414
use File::Spec;
15+
use lib File::Spec->catdir(qw(t lib));
16+
use Log4perlInternalTest qw(tmpdir);
1517

1618
our $LOG_DISPATCH_PRESENT = 0;
1719

18-
BEGIN {
20+
BEGIN {
1921
eval { require Log::Dispatch; };
2022
if($@) {
2123
plan skip_all => "only with Log::Dispatch";
@@ -25,26 +27,13 @@ BEGIN {
2527
}
2628
};
2729

28-
my $WORK_DIR = "tmp";
29-
if(-d "t") {
30-
$WORK_DIR = File::Spec->catfile(qw(t tmp));
31-
}
32-
unless (-e "$WORK_DIR"){
33-
mkdir("$WORK_DIR", 0755) || die "can't create $WORK_DIR ($!)";
34-
}
30+
my $WORK_DIR = tmpdir();
3531

3632
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
3733
my $today = sprintf("%4.4d%2.2d%2.2d",$year+1900, $mon+1, $mday);
38-
use vars qw($logfile1 $logfile6 $logfile7);
39-
$logfile1 = File::Spec->catfile(qw(t tmp deeper1.log));
40-
$logfile6 = File::Spec->catfile(qw(t tmp deeper6.log));
41-
$logfile7 = File::Spec->catfile(qw(t tmp deeper7.log));
42-
our @outfiles = ($logfile1, $logfile6, $logfile7);
43-
44-
foreach my $f (@outfiles){
45-
unlink $f if (-e $f);
46-
}
47-
34+
my $logfile1 = File::Spec->catfile($WORK_DIR, qw(deeper1.log));
35+
my $logfile6 = File::Spec->catfile($WORK_DIR, qw(deeper6.log));
36+
my $logfile7 = File::Spec->catfile($WORK_DIR, qw(deeper7.log));
4837

4938
my $config = <<EOL;
5039
#specify LOGLEVEL, appender1, appender2, ...
@@ -99,9 +88,9 @@ my $logger = Log::Log4perl->get_logger('plant');
9988

10089
#set to INFO
10190
$logger->debug("debugging message 1 ");
102-
$logger->info("info message 1 ");
103-
$logger->warn("warning message 1 ");
104-
$logger->fatal("fatal message 1 ");
91+
$logger->info("info message 1 ");
92+
$logger->warn("warning message 1 ");
93+
$logger->fatal("fatal message 1 ");
10594

10695
#set to DEBUG
10796
my $doglogger = Log::Log4perl->get_logger('animal.dog');
@@ -202,11 +191,3 @@ foreach my $l ($xla, $xlab, $xlabc, $xlabcd, $xlabcde){
202191
}
203192

204193
is($result, $expected);
205-
206-
207-
208-
END{
209-
foreach my $f (@outfiles){
210-
unlink $f if (-e $f);
211-
}
212-
}

t/014ConfErrs.t

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ BEGIN {
77

88
use Log::Log4perl;
99
use Test::More;
10+
use File::Spec;
11+
use lib File::Spec->catdir(qw(t lib));
12+
use Log4perlInternalTest qw(tmpdir);
1013

11-
$testfile = 't/tmp/test12.log';
12-
13-
unlink $testfile if (-e $testfile);
14+
my $dir = tmpdir();
15+
my $testfile = File::Spec->catfile($dir, 'test12.log');
1416

1517
# *****************************************************
1618
# nonexistent appender class
@@ -245,8 +247,3 @@ eval {
245247
like($@, qr/log4perl.logger.Foo.Bar redefined/);
246248

247249
BEGIN { plan tests => 14, }
248-
249-
END{
250-
unlink $testfile if (-e $testfile);
251-
}
252-

t/017Watch.t

+2-21
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use strict;
1212

1313
use FindBin qw($Bin);
1414
use lib "$Bin/lib";
15-
use Log4perlInternalTest qw( is_like_windows );
15+
use Log4perlInternalTest qw( is_like_windows tmpdir );
1616
use Test::More;
17-
1817
use Log::Log4perl;
1918
use File::Spec;
2019

@@ -31,29 +30,11 @@ BEGIN {
3130
}
3231
}
3332

34-
my $WORK_DIR = "tmp";
35-
if(-d "t") {
36-
$WORK_DIR = File::Spec->catfile(qw(t tmp));
37-
}
38-
39-
unless (-e "$WORK_DIR"){
40-
mkdir("$WORK_DIR", 0755) || die "can't create $WORK_DIR ($!)";
41-
}
42-
33+
my $WORK_DIR = tmpdir();
4334
my $testfile = File::Spec->catfile($WORK_DIR, "test17.log");
4435
my $testfile2 = File::Spec->catfile($WORK_DIR, "test17b.log");
4536
my $testconf = File::Spec->catfile($WORK_DIR, "test17.conf");
4637

47-
END {
48-
unlink $testfile if (-e $testfile);
49-
unlink $testfile2 if (-e $testfile2);
50-
unlink $testconf if (-e $testconf);
51-
rmdir $WORK_DIR;
52-
}
53-
54-
trunc($testfile);
55-
trunc($testconf);
56-
5738
my $conf1 = <<EOL;
5839
log4j.category.animal.dog = INFO, myAppender
5940

t/018Init.t

+5-16
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,14 @@ use strict;
1414

1515
use Log::Log4perl;
1616
use File::Spec;
17+
use lib File::Spec->catdir(qw(t lib));
18+
use Log4perlInternalTest qw(tmpdir);
1719

18-
my $WORK_DIR = "tmp";
19-
if(-d "t") {
20-
$WORK_DIR = File::Spec->catfile(qw(t tmp));
21-
}
22-
unless (-e "$WORK_DIR"){
23-
mkdir("$WORK_DIR", 0755) || die "can't create $WORK_DIR ($!)";
24-
}
25-
26-
my $testfilea = File::Spec->catfile(qw(t tmp test18a.log));
27-
unlink $testfilea if (-e $testfilea);
28-
29-
my $testfileb = File::Spec->catfile(qw(t tmp test18b.log));
30-
unlink $testfileb if (-e $testfileb);
20+
my $WORK_DIR = tmpdir();
21+
my $testfilea = File::Spec->catfile($WORK_DIR, qw(test18a.log));
22+
my $testfileb = File::Spec->catfile($WORK_DIR, qw(test18b.log));
3123

3224
BEGIN {plan tests => 2}
33-
END { unlink $testfilea;
34-
unlink $testfileb;
35-
}
3625

3726
####################################################
3827
# Double-Init, 2nd time with different log file name

t/019Warn.t

+4-13
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,13 @@ use strict;
1313
use Test;
1414
use Log::Log4perl;
1515
use File::Spec;
16+
use lib File::Spec->catdir(qw(t lib));
17+
use Log4perlInternalTest qw(tmpdir);
1618

17-
my $WORK_DIR = "tmp";
18-
if(-d "t") {
19-
$WORK_DIR = File::Spec->catfile(qw(t tmp));
20-
}
21-
unless (-e "$WORK_DIR"){
22-
mkdir("$WORK_DIR", 0755) || die "can't create $WORK_DIR ($!)";
23-
}
24-
25-
my $TMP_FILE = File::Spec->catfile(qw(t tmp warnings));
26-
$TMP_FILE = "tmp/warnings" if ! -d "t";
19+
my $WORK_DIR = tmpdir();
20+
my $TMP_FILE = File::Spec->catfile($WORK_DIR, qw(warnings));
2721

2822
BEGIN { plan tests => 2 }
29-
END { close IN;
30-
unlink $TMP_FILE;
31-
}
3223

3324
ok(1); # Initialized ok
3425

t/026FileApp.t

+5-35
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
#Testing if the file-appender appends in default mode
22

3-
END {
4-
# Must be before enabling the Log4Perl stuff, or file will still
5-
# be open and locked (under Win32) on program close.
6-
7-
unlink_testfiles();
8-
}
9-
103
BEGIN {
114
if($ENV{INTERNAL_DEBUG}) {
125
require Log::Log4perl::InternalDebug;
136
Log::Log4perl::InternalDebug->enable();
147
}
158
}
169

17-
BEGIN {
18-
use FindBin qw($Bin);
19-
use lib "$Bin/lib";
20-
use Log4perlInternalTest qw( is_like_windows );
21-
}
22-
2310
use Test::More;
2411

2512
use warnings;
2613
use strict;
2714

2815
use Log::Log4perl;
2916
use File::Spec;
30-
use File::Path qw(remove_tree);
17+
use FindBin qw($Bin);
18+
use lib "$Bin/lib";
19+
use Log4perlInternalTest qw( is_like_windows tmpdir );
3120

3221
our $LOG_DISPATCH_PRESENT;
3322

@@ -38,31 +27,12 @@ BEGIN {
3827
}
3928
};
4029

41-
my $WORK_DIR = "tmp";
42-
if(-d "t") {
43-
$WORK_DIR = File::Spec->catfile(qw(t tmp));
44-
}
45-
unless (-e "$WORK_DIR"){
46-
mkdir("$WORK_DIR", 0755) || die "can't create $WORK_DIR ($!)";
47-
}
48-
30+
my $WORK_DIR = tmpdir();
4931
my $testfile = File::Spec->catfile($WORK_DIR, "test26.log");
50-
my $testpath = File::Spec->catfile($WORK_DIR, "test26");
32+
my $testpath = File::Spec->catdir($WORK_DIR, "test26");
5133

5234
BEGIN {plan tests => 27}
5335

54-
sub unlink_testfiles {
55-
unlink $testfile;
56-
unlink "${testfile}_1";
57-
unlink "${testfile}_2";
58-
unlink "${testfile}_3";
59-
unlink "${testfile}_4";
60-
unlink "${testfile}_5";
61-
remove_tree ($testpath, "${testpath}_1");
62-
}
63-
64-
unlink_testfiles();
65-
6636
####################################################
6737
# First, preset the log file with some content
6838
####################################################

0 commit comments

Comments
 (0)