From 13f51c2044fb22f6b3cb8c674a23fe636f7fb33d Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 19 Aug 2018 14:48:47 +0200 Subject: Reorganize tests Signed-off-by: Florian Pritz --- .proverc | 2 ++ t/00_compile.t | 9 -------- t/01_untaint.t | 19 ----------------- t/02_parsing.t | 53 ---------------------------------------------- t/03_timespec.t | 21 ------------------ t/04_find_archives.t | 29 ------------------------- t/05_pod.t | 8 ------- t/06_pod_coverage.t | 9 -------- t/07_cache_contains_data.t | 22 ------------------- t/08_search_path.t | 39 ---------------------------------- t/09_find_archives.t | 51 -------------------------------------------- t/cache_contains_data.t | 22 +++++++++++++++++++ t/compile.t | 9 ++++++++ t/find_archives.t | 51 ++++++++++++++++++++++++++++++++++++++++++++ t/find_archives_mocked.t | 29 +++++++++++++++++++++++++ t/handle_added_archives.t | 53 ++++++++++++++++++++++++++++++++++++++++++++++ t/helper/untaint.t | 19 +++++++++++++++++ t/pod.t | 8 +++++++ t/pod_coverage.t | 9 ++++++++ t/search_path.t | 39 ++++++++++++++++++++++++++++++++++ t/timespec_to_seconds.t | 21 ++++++++++++++++++ 21 files changed, 262 insertions(+), 260 deletions(-) create mode 100644 .proverc delete mode 100644 t/00_compile.t delete mode 100644 t/01_untaint.t delete mode 100644 t/02_parsing.t delete mode 100644 t/03_timespec.t delete mode 100644 t/04_find_archives.t delete mode 100644 t/05_pod.t delete mode 100644 t/06_pod_coverage.t delete mode 100644 t/07_cache_contains_data.t delete mode 100644 t/08_search_path.t delete mode 100644 t/09_find_archives.t create mode 100644 t/cache_contains_data.t create mode 100644 t/compile.t create mode 100644 t/find_archives.t create mode 100644 t/find_archives_mocked.t create mode 100644 t/handle_added_archives.t create mode 100644 t/helper/untaint.t create mode 100644 t/pod.t create mode 100644 t/pod_coverage.t create mode 100644 t/search_path.t create mode 100644 t/timespec_to_seconds.t diff --git a/.proverc b/.proverc new file mode 100644 index 0000000..8cc148d --- /dev/null +++ b/.proverc @@ -0,0 +1,2 @@ +-r +t diff --git a/t/00_compile.t b/t/00_compile.t deleted file mode 100644 index 2c21a88..0000000 --- a/t/00_compile.t +++ /dev/null @@ -1,9 +0,0 @@ -use strict; -use Test::More 0.98; - -use_ok $_ for qw( - App::BorgRestore -); - -done_testing; - diff --git a/t/01_untaint.t b/t/01_untaint.t deleted file mode 100644 index f9d8a9c..0000000 --- a/t/01_untaint.t +++ /dev/null @@ -1,19 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use Test::Exception; - -use App::BorgRestore::Helper; - -ok(App::BorgRestore::Helper::untaint_archive_name('abc-1234:5+1') eq 'abc-1234:5+1'); -ok(App::BorgRestore::Helper::untaint_archive_name('abc') eq 'abc'); -ok(App::BorgRestore::Helper::untaint_archive_name('root-2016-09-30T15+02:00.checkpoint') eq 'root-2016-09-30T15+02:00.checkpoint'); - -dies_ok(sub{App::BorgRestore::Helper::untaint_archive_name('abc`"\'')}, 'special chars not allowed'); -dies_ok(sub{App::BorgRestore::Helper::untaint_archive_name('abc`')}, 'special chars not allowed'); -dies_ok(sub{App::BorgRestore::Helper::untaint_archive_name('abc"')}, 'special chars not allowed'); -dies_ok(sub{App::BorgRestore::Helper::untaint_archive_name('abc\'')}, 'special chars not allowed'); - - -done_testing; diff --git a/t/02_parsing.t b/t/02_parsing.t deleted file mode 100644 index c2b8e78..0000000 --- a/t/02_parsing.t +++ /dev/null @@ -1,53 +0,0 @@ -use strict; -use warnings; - -use POSIX qw(tzset); -use Test::Differences; -use Test::MockObject; -use Test::More; - -use App::BorgRestore; - -# Only log calls to $db->add_path -my $db = Test::MockObject->new(); -$db->set_true(qw(add_path -begin_work -commit -vacuum -add_archive_name -verify_cache_fill_rate_ok)); -$db->mock('-get_archive_id', sub {return 'prefix-archive-1' if $_[1] eq 'archive-1';}); -$db->mock('-get_archive_names', sub {return []}); - -$ENV{TZ} = 'UTC'; -tzset; - -my $borg = Test::MockObject->new(); -$borg->set_list('borg_list', ['archive-1']); -$borg->mock('list_archive', sub { - my ($self, $archive, $cb) = @_; - $cb->("XXX, 1970-01-01 00:00:05 ."); - $cb->("XXX, 1970-01-01 00:00:10 boot"); - $cb->("XXX, 1970-01-01 00:00:20 boot/grub"); - $cb->("XXX, 1970-01-01 00:00:08 boot/grub/grub.cfg"); - $cb->("XXX, 1970-01-01 00:00:13 boot/foo"); - $cb->("XXX, 1970-01-01 00:00:13 boot/foo/blub"); - $cb->("XXX, 1970-01-01 00:00:19 boot/foo/bar"); - } ); - -# Call the actual function we want to test -my $app = App::BorgRestore->new({borg => $borg, db => $db}); -$app->_handle_added_archives(['archive-1']); - -# Check if $db->add_path has been called properly -my (@calls, @a); -push @calls, [@a] while @a = $db->next_call(); - -# sort by path -@calls = sort {$a->[1][2] cmp $b->[1][2];} @calls; - -eq_or_diff(\@calls, [ - ['add_path', [$db, 'prefix-archive-1', 'boot', 20]], - ['add_path', [$db, 'prefix-archive-1', 'boot/foo', 19]], - ['add_path', [$db, 'prefix-archive-1', 'boot/foo/bar', 19]], - ['add_path', [$db, 'prefix-archive-1', 'boot/foo/blub', 13]], - ['add_path', [$db, 'prefix-archive-1', 'boot/grub', 20]], - ['add_path', [$db, 'prefix-archive-1', 'boot/grub/grub.cfg', 8]], - ], "Database is populated with the correct timestamps"); - -done_testing; diff --git a/t/03_timespec.t b/t/03_timespec.t deleted file mode 100644 index 0f367c9..0000000 --- a/t/03_timespec.t +++ /dev/null @@ -1,21 +0,0 @@ -use strict; -use warnings; - -use Test::More; - -use App::BorgRestore; - -my $app = App::BorgRestore->new_no_defaults(undef); - -is($app->_timespec_to_seconds('5s'), 5, '5 seconds'); -is($app->_timespec_to_seconds('5minutes'), 5*60, '5 minutes'); -is($app->_timespec_to_seconds('6d'), 6*60*60*24, '6 days'); -is($app->_timespec_to_seconds('8m'), 8*60*60*24*31, '8 months'); -is($app->_timespec_to_seconds('2y'), 2*60*60*24*365, '2 years'); - -is($app->_timespec_to_seconds('5sec'), undef, 'invalid unit returns undef'); -is($app->_timespec_to_seconds('5'), undef, 'missing unit returns undef'); -is($app->_timespec_to_seconds('blub'), undef, 'string returns undef'); -is($app->_timespec_to_seconds(''), undef, 'empty string returns undef'); - -done_testing; diff --git a/t/04_find_archives.t b/t/04_find_archives.t deleted file mode 100644 index 7f0186a..0000000 --- a/t/04_find_archives.t +++ /dev/null @@ -1,29 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use Test::MockObject; -use Test::Differences; - -use App::BorgRestore; - -my $db = Test::MockObject->new(); -$db->mock('get_archives_for_path', sub {return [ - { modification_time => 5, archive => "test2"}, - { modification_time => 2, archive => "test1"}, - { modification_time => 10, archive => "test3"}, - { modification_time => 10, archive => "test4"}, - { modification_time => 2, archive => "test1-1"}, - { modification_time => 15, archive => "test5"}, - ];}); - -my $app = App::BorgRestore->new_no_defaults({db => $db}); - -eq_or_diff($app->find_archives('test/path'), [ - {archive => 'test1', modification_time => 2}, - {archive => 'test2', modification_time => 5}, - {archive => 'test3', modification_time => 10}, - {archive => 'test5', modification_time => 15}, -]); - -done_testing; diff --git a/t/05_pod.t b/t/05_pod.t deleted file mode 100644 index de15747..0000000 --- a/t/05_pod.t +++ /dev/null @@ -1,8 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use Test::Pod; - -my @poddirs = qw( blib script ); -all_pod_files_ok( all_pod_files( @poddirs ) ); diff --git a/t/06_pod_coverage.t b/t/06_pod_coverage.t deleted file mode 100644 index fc346af..0000000 --- a/t/06_pod_coverage.t +++ /dev/null @@ -1,9 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use Test::Pod::Coverage; - -pod_coverage_ok( "App::BorgRestore"); - -done_testing; diff --git a/t/07_cache_contains_data.t b/t/07_cache_contains_data.t deleted file mode 100644 index 2187ab0..0000000 --- a/t/07_cache_contains_data.t +++ /dev/null @@ -1,22 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use Test::MockObject; -use Test::Differences; - -use App::BorgRestore; - -my $db = Test::MockObject->new(); -my $app = App::BorgRestore->new_no_defaults({db => $db}); - -$db->mock('get_archive_names', sub {return [qw(a b c)];}); -is($app->cache_contains_data(), 1); - -$db->mock('get_archive_names', sub {return [];}); -is($app->cache_contains_data(), 0); - -$db->mock('get_archive_names', sub {return [qw(a)];}); -is($app->cache_contains_data(), 1); - -done_testing; diff --git a/t/08_search_path.t b/t/08_search_path.t deleted file mode 100644 index ef6901c..0000000 --- a/t/08_search_path.t +++ /dev/null @@ -1,39 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use Test::MockObject; -use Test::Differences; - -use App::BorgRestore; -use App::BorgRestore::DB; -use Log::Any::Adapter ('TAP'); - -my $db = App::BorgRestore::DB->new(":memory:", 0); -my $app = App::BorgRestore->new_no_defaults({db => $db}); - -$db->add_archive_name("test1"); -my $archive_id1 = $db->get_archive_id("test1"); -$db->add_archive_name("test2"); -my $archive_id2 = $db->get_archive_id("test2"); -$db->add_archive_name("test3"); -my $archive_id3 = $db->get_archive_id("test3"); -$db->add_archive_name("test4"); -my $archive_id4 = $db->get_archive_id("test4"); - -$db->add_path($archive_id1, "test/path", 5); -$db->add_path($archive_id1, "test/foo", 4); -$db->add_path($archive_id1, "test", 5); - -$db->add_path($archive_id2, "test/path", 5); -$db->add_path($archive_id2, "test", 5); - -$db->add_path($archive_id3, "test/path", 10); -$db->add_path($archive_id3, "test/foo", 4); -$db->add_path($archive_id3, "test", 10); - -eq_or_diff($app->search_path("test"), ['test', 'test/foo', 'test/path']); -eq_or_diff($app->search_path("test%"), ['test', 'test/foo', 'test/path']); -eq_or_diff($app->search_path("%foo"), ['test/foo']); - -done_testing; diff --git a/t/09_find_archives.t b/t/09_find_archives.t deleted file mode 100644 index 7b1fc0f..0000000 --- a/t/09_find_archives.t +++ /dev/null @@ -1,51 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use Test::MockObject; -use Test::Differences; -use Test::Exception; - -use App::BorgRestore; -use App::BorgRestore::DB; -use Log::Any::Adapter ('TAP'); - -my $db = App::BorgRestore::DB->new(":memory:", 0); - -my $app = App::BorgRestore->new_no_defaults({db => $db}); - -$db->add_archive_name("test1"); -my $archive_id1 = $db->get_archive_id("test1"); -$db->add_archive_name("test2"); -my $archive_id2 = $db->get_archive_id("test2"); -$db->add_archive_name("test3"); -my $archive_id3 = $db->get_archive_id("test3"); -$db->add_archive_name("test4"); -my $archive_id4 = $db->get_archive_id("test4"); - -$db->add_path($archive_id1, "test/path", 5); -$db->add_path($archive_id1, "test/foo", 4); -$db->add_path($archive_id1, "test", 5); - -$db->add_path($archive_id2, "test/path", 5); -$db->add_path($archive_id2, "test", 5); - -$db->add_path($archive_id3, "test/path", 10); -$db->add_path($archive_id3, "test/foo", 4); -$db->add_path($archive_id3, "test", 10); - -eq_or_diff($app->find_archives('test/path'), [ - { - archive => 'test1', - modification_time => 5 - }, - { - archive => 'test3', - modification_time => 10 - }, - ]); - -dies_ok {$app->find_archives('test/nope')}; -is ($@, "Path 'test/nope' not found in any archive.\n"); - -done_testing; diff --git a/t/cache_contains_data.t b/t/cache_contains_data.t new file mode 100644 index 0000000..2187ab0 --- /dev/null +++ b/t/cache_contains_data.t @@ -0,0 +1,22 @@ +use strict; +use warnings; + +use Test::More; +use Test::MockObject; +use Test::Differences; + +use App::BorgRestore; + +my $db = Test::MockObject->new(); +my $app = App::BorgRestore->new_no_defaults({db => $db}); + +$db->mock('get_archive_names', sub {return [qw(a b c)];}); +is($app->cache_contains_data(), 1); + +$db->mock('get_archive_names', sub {return [];}); +is($app->cache_contains_data(), 0); + +$db->mock('get_archive_names', sub {return [qw(a)];}); +is($app->cache_contains_data(), 1); + +done_testing; diff --git a/t/compile.t b/t/compile.t new file mode 100644 index 0000000..2c21a88 --- /dev/null +++ b/t/compile.t @@ -0,0 +1,9 @@ +use strict; +use Test::More 0.98; + +use_ok $_ for qw( + App::BorgRestore +); + +done_testing; + diff --git a/t/find_archives.t b/t/find_archives.t new file mode 100644 index 0000000..7b1fc0f --- /dev/null +++ b/t/find_archives.t @@ -0,0 +1,51 @@ +use strict; +use warnings; + +use Test::More; +use Test::MockObject; +use Test::Differences; +use Test::Exception; + +use App::BorgRestore; +use App::BorgRestore::DB; +use Log::Any::Adapter ('TAP'); + +my $db = App::BorgRestore::DB->new(":memory:", 0); + +my $app = App::BorgRestore->new_no_defaults({db => $db}); + +$db->add_archive_name("test1"); +my $archive_id1 = $db->get_archive_id("test1"); +$db->add_archive_name("test2"); +my $archive_id2 = $db->get_archive_id("test2"); +$db->add_archive_name("test3"); +my $archive_id3 = $db->get_archive_id("test3"); +$db->add_archive_name("test4"); +my $archive_id4 = $db->get_archive_id("test4"); + +$db->add_path($archive_id1, "test/path", 5); +$db->add_path($archive_id1, "test/foo", 4); +$db->add_path($archive_id1, "test", 5); + +$db->add_path($archive_id2, "test/path", 5); +$db->add_path($archive_id2, "test", 5); + +$db->add_path($archive_id3, "test/path", 10); +$db->add_path($archive_id3, "test/foo", 4); +$db->add_path($archive_id3, "test", 10); + +eq_or_diff($app->find_archives('test/path'), [ + { + archive => 'test1', + modification_time => 5 + }, + { + archive => 'test3', + modification_time => 10 + }, + ]); + +dies_ok {$app->find_archives('test/nope')}; +is ($@, "Path 'test/nope' not found in any archive.\n"); + +done_testing; diff --git a/t/find_archives_mocked.t b/t/find_archives_mocked.t new file mode 100644 index 0000000..7f0186a --- /dev/null +++ b/t/find_archives_mocked.t @@ -0,0 +1,29 @@ +use strict; +use warnings; + +use Test::More; +use Test::MockObject; +use Test::Differences; + +use App::BorgRestore; + +my $db = Test::MockObject->new(); +$db->mock('get_archives_for_path', sub {return [ + { modification_time => 5, archive => "test2"}, + { modification_time => 2, archive => "test1"}, + { modification_time => 10, archive => "test3"}, + { modification_time => 10, archive => "test4"}, + { modification_time => 2, archive => "test1-1"}, + { modification_time => 15, archive => "test5"}, + ];}); + +my $app = App::BorgRestore->new_no_defaults({db => $db}); + +eq_or_diff($app->find_archives('test/path'), [ + {archive => 'test1', modification_time => 2}, + {archive => 'test2', modification_time => 5}, + {archive => 'test3', modification_time => 10}, + {archive => 'test5', modification_time => 15}, +]); + +done_testing; diff --git a/t/handle_added_archives.t b/t/handle_added_archives.t new file mode 100644 index 0000000..c2b8e78 --- /dev/null +++ b/t/handle_added_archives.t @@ -0,0 +1,53 @@ +use strict; +use warnings; + +use POSIX qw(tzset); +use Test::Differences; +use Test::MockObject; +use Test::More; + +use App::BorgRestore; + +# Only log calls to $db->add_path +my $db = Test::MockObject->new(); +$db->set_true(qw(add_path -begin_work -commit -vacuum -add_archive_name -verify_cache_fill_rate_ok)); +$db->mock('-get_archive_id', sub {return 'prefix-archive-1' if $_[1] eq 'archive-1';}); +$db->mock('-get_archive_names', sub {return []}); + +$ENV{TZ} = 'UTC'; +tzset; + +my $borg = Test::MockObject->new(); +$borg->set_list('borg_list', ['archive-1']); +$borg->mock('list_archive', sub { + my ($self, $archive, $cb) = @_; + $cb->("XXX, 1970-01-01 00:00:05 ."); + $cb->("XXX, 1970-01-01 00:00:10 boot"); + $cb->("XXX, 1970-01-01 00:00:20 boot/grub"); + $cb->("XXX, 1970-01-01 00:00:08 boot/grub/grub.cfg"); + $cb->("XXX, 1970-01-01 00:00:13 boot/foo"); + $cb->("XXX, 1970-01-01 00:00:13 boot/foo/blub"); + $cb->("XXX, 1970-01-01 00:00:19 boot/foo/bar"); + } ); + +# Call the actual function we want to test +my $app = App::BorgRestore->new({borg => $borg, db => $db}); +$app->_handle_added_archives(['archive-1']); + +# Check if $db->add_path has been called properly +my (@calls, @a); +push @calls, [@a] while @a = $db->next_call(); + +# sort by path +@calls = sort {$a->[1][2] cmp $b->[1][2];} @calls; + +eq_or_diff(\@calls, [ + ['add_path', [$db, 'prefix-archive-1', 'boot', 20]], + ['add_path', [$db, 'prefix-archive-1', 'boot/foo', 19]], + ['add_path', [$db, 'prefix-archive-1', 'boot/foo/bar', 19]], + ['add_path', [$db, 'prefix-archive-1', 'boot/foo/blub', 13]], + ['add_path', [$db, 'prefix-archive-1', 'boot/grub', 20]], + ['add_path', [$db, 'prefix-archive-1', 'boot/grub/grub.cfg', 8]], + ], "Database is populated with the correct timestamps"); + +done_testing; diff --git a/t/helper/untaint.t b/t/helper/untaint.t new file mode 100644 index 0000000..f9d8a9c --- /dev/null +++ b/t/helper/untaint.t @@ -0,0 +1,19 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; + +use App::BorgRestore::Helper; + +ok(App::BorgRestore::Helper::untaint_archive_name('abc-1234:5+1') eq 'abc-1234:5+1'); +ok(App::BorgRestore::Helper::untaint_archive_name('abc') eq 'abc'); +ok(App::BorgRestore::Helper::untaint_archive_name('root-2016-09-30T15+02:00.checkpoint') eq 'root-2016-09-30T15+02:00.checkpoint'); + +dies_ok(sub{App::BorgRestore::Helper::untaint_archive_name('abc`"\'')}, 'special chars not allowed'); +dies_ok(sub{App::BorgRestore::Helper::untaint_archive_name('abc`')}, 'special chars not allowed'); +dies_ok(sub{App::BorgRestore::Helper::untaint_archive_name('abc"')}, 'special chars not allowed'); +dies_ok(sub{App::BorgRestore::Helper::untaint_archive_name('abc\'')}, 'special chars not allowed'); + + +done_testing; diff --git a/t/pod.t b/t/pod.t new file mode 100644 index 0000000..de15747 --- /dev/null +++ b/t/pod.t @@ -0,0 +1,8 @@ +use strict; +use warnings; + +use Test::More; +use Test::Pod; + +my @poddirs = qw( blib script ); +all_pod_files_ok( all_pod_files( @poddirs ) ); diff --git a/t/pod_coverage.t b/t/pod_coverage.t new file mode 100644 index 0000000..fc346af --- /dev/null +++ b/t/pod_coverage.t @@ -0,0 +1,9 @@ +use strict; +use warnings; + +use Test::More; +use Test::Pod::Coverage; + +pod_coverage_ok( "App::BorgRestore"); + +done_testing; diff --git a/t/search_path.t b/t/search_path.t new file mode 100644 index 0000000..ef6901c --- /dev/null +++ b/t/search_path.t @@ -0,0 +1,39 @@ +use strict; +use warnings; + +use Test::More; +use Test::MockObject; +use Test::Differences; + +use App::BorgRestore; +use App::BorgRestore::DB; +use Log::Any::Adapter ('TAP'); + +my $db = App::BorgRestore::DB->new(":memory:", 0); +my $app = App::BorgRestore->new_no_defaults({db => $db}); + +$db->add_archive_name("test1"); +my $archive_id1 = $db->get_archive_id("test1"); +$db->add_archive_name("test2"); +my $archive_id2 = $db->get_archive_id("test2"); +$db->add_archive_name("test3"); +my $archive_id3 = $db->get_archive_id("test3"); +$db->add_archive_name("test4"); +my $archive_id4 = $db->get_archive_id("test4"); + +$db->add_path($archive_id1, "test/path", 5); +$db->add_path($archive_id1, "test/foo", 4); +$db->add_path($archive_id1, "test", 5); + +$db->add_path($archive_id2, "test/path", 5); +$db->add_path($archive_id2, "test", 5); + +$db->add_path($archive_id3, "test/path", 10); +$db->add_path($archive_id3, "test/foo", 4); +$db->add_path($archive_id3, "test", 10); + +eq_or_diff($app->search_path("test"), ['test', 'test/foo', 'test/path']); +eq_or_diff($app->search_path("test%"), ['test', 'test/foo', 'test/path']); +eq_or_diff($app->search_path("%foo"), ['test/foo']); + +done_testing; diff --git a/t/timespec_to_seconds.t b/t/timespec_to_seconds.t new file mode 100644 index 0000000..0f367c9 --- /dev/null +++ b/t/timespec_to_seconds.t @@ -0,0 +1,21 @@ +use strict; +use warnings; + +use Test::More; + +use App::BorgRestore; + +my $app = App::BorgRestore->new_no_defaults(undef); + +is($app->_timespec_to_seconds('5s'), 5, '5 seconds'); +is($app->_timespec_to_seconds('5minutes'), 5*60, '5 minutes'); +is($app->_timespec_to_seconds('6d'), 6*60*60*24, '6 days'); +is($app->_timespec_to_seconds('8m'), 8*60*60*24*31, '8 months'); +is($app->_timespec_to_seconds('2y'), 2*60*60*24*365, '2 years'); + +is($app->_timespec_to_seconds('5sec'), undef, 'invalid unit returns undef'); +is($app->_timespec_to_seconds('5'), undef, 'missing unit returns undef'); +is($app->_timespec_to_seconds('blub'), undef, 'string returns undef'); +is($app->_timespec_to_seconds(''), undef, 'empty string returns undef'); + +done_testing; -- cgit v1.2.3-24-g4f1b