Skip to content

Commit 26ae9a6

Browse files
authored
Merge pull request #41 from atoomic/test
use File::Temp for unit tests
2 parents bd74fa3 + 05e6a7d commit 26ae9a6

20 files changed

Lines changed: 94 additions & 248 deletions

t/statefile_bad_call_unlocked.t

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
#!/usr/bin/perl
22

3+
use strict;
4+
use warnings;
5+
36
use FindBin;
47
use lib "$FindBin::Bin/mocks";
58

69
use Test::More tests => 5;
710
use File::Path ();
8-
9-
use strict;
10-
use warnings;
11+
use File::Temp;
1112

1213
use cPanel::StateFile;
1314
use MockCacheable;
1415

15-
my $tmpdir = './tmp';
16+
my $tmpdir = File::Temp->newdir();
1617
my $dir = "$tmpdir/state_test";
1718
my $file = "$dir/state_dir/state_file";
1819
my $lockname = "$file.lock";
1920

20-
cleanup();
21-
File::Path::mkpath($tmpdir) or die "Unable to create temporary directory: $!";
22-
2321
my $mock_obj = MockCacheable->new;
24-
my $state = cPanel::StateFile->new( { state_file => $file, data_obj => $mock_obj } );
22+
my $state = cPanel::StateFile->new( { state_file => $file, data_obj => $mock_obj } );
2523

2624
{
2725
my $guard = $state->synch();
@@ -56,10 +54,3 @@ my $state = cPanel::StateFile->new( { state_file => $file, data_obj => $mock_obj
5654
};
5755
like( $@, qr/test exception/, 'Exceptions are passed out of call correctly.' );
5856
}
59-
60-
cleanup();
61-
62-
# Discard temporary files that we don't need any more.
63-
sub cleanup {
64-
File::Path::rmtree($tmpdir) if -d $tmpdir;
65-
}

t/statefile_load_save.t

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@
66
use FindBin;
77
use lib "$FindBin::Bin/mocks";
88
use File::Path ();
9+
use File::Temp ();
910

1011
use Test::More tests => 24;
1112
use cPanel::StateFile;
1213
use MockCacheable;
1314

14-
my $tmpdir = './tmp';
15+
my $tmpdir = File::Temp->newdir();
1516
my $dir = "$tmpdir/state_test";
1617
my $file = "$dir/state_dir/state_file";
1718
my $lockname = "$file.lock";
1819

1920
# TODO: Need to testing for timeout logic, but it would slow down the tests.
2021
# Decide how I would like to turn it on provisionally: cmdline, env, etc.
2122

22-
# clean up if last run failed.
23-
cleanup();
24-
File::Path::mkpath($tmpdir) or die "Unable to create tmpdir: $!";
25-
2623
# test valid creation
2724
my $mock_obj = MockCacheable->new;
2825

@@ -92,19 +89,11 @@ ok( !-e $lockname, "File is unlocked." );
9289
ok( $state->synch(), 'Synch occured.' );
9390
ok( !-e $lockname, "File is not locked." );
9491

95-
is( $mock_obj->{load_called}, 1, "file changed, load." );
96-
is( $mock_obj->{data}, 'This is the updated state file.', 'Correct data is loaded.' );
92+
is( $mock_obj->{load_called}, 1, "file changed, load." );
93+
is( $mock_obj->{data}, 'This is the updated state file.', 'Correct data is loaded.' );
9794

9895
# Test that we don't reload after the last synch
9996
ok( $state->synch(), 'Synch occured.' );
100-
is( $mock_obj->{load_called}, 1, "don't load again." );
101-
is( $mock_obj->{data}, 'This is the updated state file.', 'Correct data is loaded.' );
102-
103-
cleanup();
97+
is( $mock_obj->{load_called}, 1, "don't load again." );
98+
is( $mock_obj->{data}, 'This is the updated state file.', 'Correct data is loaded.' );
10499

105-
# Discard temporary files that we don't need any more.
106-
sub cleanup {
107-
unlink $file if -e $file;
108-
unlink $lockname if -e $lockname;
109-
File::Path::rmtree($tmpdir) if -d $tmpdir;
110-
}

t/taskqueue_bad_statefile.t

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ use strict;
66
use warnings;
77
use FindBin;
88
use File::Path ();
9+
use File::Temp ();
910
use lib "$FindBin::Bin/mocks";
1011

1112
use cPanel::TaskQueue ( -logger => 'cPanel::FakeLogger' );
1213

13-
my $tmpdir = './tmp';
14-
my $statedir = "$tmpdir/state_test";
15-
16-
# In case the last test did not succeed.
17-
cleanup();
18-
File::Path::mkpath($statedir);
14+
my ( $tmpdir, $statedir );
15+
setup();
1916

2017
{
2118
open( my $fh, '>', "$statedir/tasks_queue.stor" );
@@ -34,8 +31,7 @@ File::Path::mkpath($statedir);
3431
);
3532
}
3633

37-
cleanup();
38-
File::Path::mkpath($statedir);
34+
setup();
3935

4036
{
4137
use Storable ();
@@ -51,9 +47,11 @@ File::Path::mkpath($statedir);
5147
ok( -e "$statedir/tasks_queue.stor.broken", 'Bad file moved out of the way.' );
5248
}
5349

54-
cleanup();
55-
5650
# Clean up after myself
57-
sub cleanup {
58-
File::Path::rmtree($tmpdir);
51+
sub setup {
52+
$tmpdir = File::Temp->newdir();
53+
$statedir = "$tmpdir/state_test";
54+
File::Path::mkpath($statedir);
55+
56+
return;
5957
}

t/taskqueue_bad_yaml_statefile.t

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ use strict;
66
use warnings;
77
use FindBin;
88
use File::Path ();
9+
use File::Temp ();
910
use lib "$FindBin::Bin/mocks";
1011

1112
use cPanel::TaskQueue ( -logger => 'cPanel::FakeLogger', -serializer => 'cPanel::TQSerializer::YAML' );
1213

13-
my $tmpdir = './tmp';
14-
my $statedir = "$tmpdir/state_test";
15-
16-
# In case the last test did not succeed.
17-
cleanup();
18-
File::Path::mkpath($statedir);
14+
my ( $tmpdir, $statedir );
15+
setup();
1916

2017
{
2118
open( my $fh, '>', "$statedir/tasks_queue.yaml" );
@@ -34,8 +31,7 @@ File::Path::mkpath($statedir);
3431
);
3532
}
3633

37-
cleanup();
38-
File::Path::mkpath($statedir);
34+
setup();
3935

4036
{
4137
use YAML::Syck ();
@@ -51,9 +47,12 @@ File::Path::mkpath($statedir);
5147
ok( -e "$statedir/tasks_queue.yaml.broken", 'Bad file moved out of the way.' );
5248
}
5349

54-
cleanup();
50+
exit;
51+
52+
sub setup {
53+
$tmpdir = File::Temp->newdir();
54+
$statedir = "$tmpdir/state_test";
55+
File::Path::mkpath($statedir);
5556

56-
# Clean up after myself
57-
sub cleanup {
58-
File::Path::rmtree($tmpdir);
57+
return;
5958
}

t/taskqueue_basics.t

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ use strict;
77
use FindBin;
88
use lib "$FindBin::Bin/mocks";
99
use File::Path ();
10+
use File::Temp ();
1011

1112
use Test::More tests => 42;
1213
use cPanel::TaskQueue;
1314

14-
my $tmpdir = './tmp';
15+
my $tmpdir = File::Temp->newdir();
1516
my $statedir = "$tmpdir/state_test";
1617

17-
# In case the last test did not succeed.
18-
cleanup();
19-
File::Path::mkpath($tmpdir) or die "Unable to create tmpdir: $!";
20-
2118
# Create the real TaskQueue
2219
my $queue = cPanel::TaskQueue->new( { name => 'tasks', state_dir => $statedir } );
2320
isa_ok( $queue, 'cPanel::TaskQueue', 'Correct object built.' );
@@ -58,10 +55,10 @@ ok( !$queue->queue_task("noop a b c"), 'cannot queue a duplicate command' );
5855
# Look at first task
5956
my $task = $queue->peek_next_task();
6057
isa_ok( $task, 'cPanel::TaskQueue::Task', 'We have a task' );
61-
is( $task->command(), 'noop', 'Correct command in the queue.' );
62-
is( join( ' ', $task->args() ), 'a b c', 'Correct command arguments.' );
63-
is( $task->argstring(), 'a b c', 'Correct command argument string.' );
64-
is( $task->uuid(), $qid2, 'Correct Task id.' );
58+
is( $task->command(), 'noop', 'Correct command in the queue.' );
59+
is( join( ' ', $task->args() ), 'a b c', 'Correct command arguments.' );
60+
is( $task->argstring(), 'a b c', 'Correct command argument string.' );
61+
is( $task->uuid(), $qid2, 'Correct Task id.' );
6562

6663
# Test a second TaskQueue on same file.
6764
my $q2 = cPanel::TaskQueue->new( { name => 'tasks', state_dir => $statedir } );
@@ -104,10 +101,3 @@ like( $@, qr/No Task uuid/, 'Can not check processing task without a uuid' );
104101

105102
eval { $queue->is_task_processing(1111111); };
106103
like( $@, qr/No Task uuid/, 'Can not check processing task with an invalid uuid' );
107-
108-
cleanup();
109-
110-
# Clean up after myself
111-
sub cleanup {
112-
File::Path::rmtree($tmpdir);
113-
}

t/taskqueue_dupes_and_overrides.t

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ use strict;
77
use FindBin;
88
use lib "$FindBin::Bin/mocks";
99
use File::Path ();
10+
use File::Temp ();
1011

1112
use Test::More tests => 39;
1213
use cPanel::TaskQueue;
1314
use cPanel::TaskQueue::Processor;
1415

15-
my $tmpdir = './tmp';
16+
my $tmpdir = File::Temp->newdir();
1617
my $statedir = "$tmpdir/statedir";
1718
my $missing_dir = "$tmpdir/task_queue_test";
1819

@@ -43,10 +44,6 @@ my $missing_dir = "$tmpdir/task_queue_test";
4344

4445
cPanel::TaskQueue->register_task_processor( 'mock', MockProcessor->new() );
4546

46-
# In case the last test did not succeed.
47-
cleanup();
48-
File::Path::mkpath($tmpdir) or die "Unable to create tmpdir: $!";
49-
5047
# Create the real TaskQueue
5148
my $queue = cPanel::TaskQueue->new( { name => 'tasks', state_dir => $statedir } );
5249
isa_ok( $queue, 'cPanel::TaskQueue', 'Correct object built.' );
@@ -109,9 +106,7 @@ ok( $queue->queue_task('mock all off'), 'queue override request' );
109106
is( $queue->how_many_queued(), 1, 'only one task exists' );
110107
is( $queue->peek_next_task()->get_arg(0), 'all', 'it is the override' );
111108

112-
# perform cleanup.
113-
114-
cleanup();
109+
exit;
115110

116111
sub remove_and_check_tasks {
117112
my $q = shift;
@@ -125,8 +120,3 @@ sub remove_and_check_tasks {
125120
++$i;
126121
}
127122
}
128-
129-
# Clean up after myself
130-
sub cleanup {
131-
File::Path::rmtree($tmpdir) if -d $tmpdir;
132-
}

t/taskqueue_find_commands.t

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ use strict;
44
use FindBin;
55
use lib "$FindBin::Bin/mocks";
66
use File::Path ();
7+
use File::Temp ();
78

89
use Test::More tests => 10;
910
use cPanel::TaskQueue;
1011
use cPanel::TaskQueue::PluginManager;
1112

12-
my $tmpdir = './tmp';
13+
my $tmpdir = File::Temp->newdir();
1314
my $statedir = "$tmpdir/state_test";
1415

15-
# In case the last test did not succeed.
16-
cleanup();
17-
File::Path::mkpath($tmpdir) or die "Unable to create tmpdir: $!";
18-
1916
cPanel::TaskQueue::PluginManager::load_all_plugins(
2017
directories => ["$FindBin::Bin/mocks"],
2118
namespaces => ['cPanel::ExampleTasks'],
@@ -58,9 +55,3 @@ is( $queue->how_many_queued(), scalar(@qids), 'Commands queued for further testi
5855
my @tasks = $queue->find_commands('hello');
5956
is_deeply( [ map { $_->uuid } @tasks ], [ $qids[5] ], 'Found only the substring command.' );
6057
}
61-
62-
cleanup();
63-
64-
sub cleanup {
65-
File::Path::rmtree($tmpdir);
66-
}

t/taskqueue_find_task.t

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ use strict;
77
use FindBin;
88
use lib "$FindBin::Bin/mocks";
99
use File::Path ();
10+
use File::Temp ();
1011

1112
use Test::More tests => 20;
1213
use cPanel::TaskQueue;
1314

14-
my $tmpdir = './tmp';
15+
my $tmpdir = File::Temp->newdir();
1516
my $statedir = "$tmpdir/statedir";
1617

17-
# In case the last test did not succeed.
18-
cleanup();
19-
File::Path::mkpath($tmpdir) or die "Unable to create tmpdir: $!";
20-
2118
{
2219

2320
package SleepTask;
@@ -84,10 +81,3 @@ ok( !$queue->find_command('xyzzy'), 'Did not find non-existant command.' );
8481
ok( !$queue->find_task($tid), 'Can not find task that is not isn queue.' );
8582
}
8683

87-
cleanup();
88-
89-
# Clean up after myself
90-
sub cleanup {
91-
File::Path::rmtree($tmpdir) if -d $tmpdir;
92-
}
93-

t/taskqueue_logging_policy.t

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use FindBin;
77
use lib "$FindBin::Bin/mocks";
88
use File::Path ();
9+
use File::Temp ();
910

1011
use Test::More tests => 10;
1112
my $logger;
@@ -22,15 +23,11 @@ like( $@, qr/Policies already/, 'Cannot reset policies to defaults.' );
2223
eval "use cPanel::TaskQueue ();";
2324
ok( !$@, 'Can reload with import turned off.' );
2425

25-
my $tmpdir = './tmp';
26-
27-
# clean up if last run failed.
28-
cleanup();
29-
File::Path::mkpath($tmpdir) or die "Unable to create tmpdir: $!";
26+
my $tmpdir = File::Temp->newdir();
3027

3128
# test bad new calls.
3229
eval { my $cf = cPanel::TaskQueue->new( {} ); };
33-
like( $@, qr/state directory/, 'Cannot create StateFile without parameters' );
30+
like( $@, qr/state directory/, 'Cannot create StateFile without parameters' );
3431
like( ( $logger->get_msgs() )[0], qr/throw.*?state directory/, 'Logged correctly.' );
3532
$logger->reset_msgs();
3633

@@ -53,9 +50,3 @@ ok( !$queue->queue_task($task), 'Finished trying to queue a task with no retries
5350
like( ( $logger->get_msgs() )[0], qr/info.*?0 retries/, 'Infoed correctly.' );
5451
$logger->reset_msgs();
5552

56-
cleanup();
57-
58-
# Discard temporary files that we don't need any more.
59-
sub cleanup {
60-
File::Path::rmtree($tmpdir) if -d $tmpdir;
61-
}

0 commit comments

Comments
 (0)