[Reproducible-commits] [dpkg] 58/74: test: Refactor common unit test checks for needed things

Mattia Rizzolo mattia at debian.org
Sun Jul 3 22:22:57 UTC 2016


This is an automated email from the git hooks/post-receive script.

mattia pushed a commit to annotated tag 1.18.8
in repository dpkg.

commit 6278c8e98fee08f8ce0c2dc17a07a28d31eb3584
Author: Guillem Jover <guillem at debian.org>
Date:   Thu Jun 16 19:56:24 2016 +0200

    test: Refactor common unit test checks for needed things
---
 debian/changelog                |  1 +
 scripts/Test/Dpkg.pm            | 57 +++++++++++++++++++++++++++++++++++++++++
 scripts/t/Dpkg_Shlibs_Cppfilt.t |  9 +++----
 t/critic.t                      | 16 +++---------
 t/pod-spell.t                   | 21 +++++----------
 t/pod.t                         |  9 +++----
 t/strict.t                      | 13 +++-------
 t/syntax.t                      |  6 ++---
 8 files changed, 80 insertions(+), 52 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index f9cd709..e0483c9 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -76,6 +76,7 @@ dpkg (1.18.8) UNRELEASED; urgency=medium
     - Bump perlcritic ValuesAndExpressions::RequireNumberSeparators minimum
       to 99999.
     - Add new pod-spell unit test.
+    - Refactor common unit test checks for needed things into Test::Dpkg.
   * Documentation:
     - Improve dpkg-buildpackage(1) on environment expectations.
     - Clarify the format of the db:Status-Abbrev virtual field in
diff --git a/scripts/Test/Dpkg.pm b/scripts/Test/Dpkg.pm
index 38e3352..48b04c6 100644
--- a/scripts/Test/Dpkg.pm
+++ b/scripts/Test/Dpkg.pm
@@ -21,10 +21,24 @@ use warnings;
 our $VERSION = '0.00';
 our @EXPORT_OK = qw(
     all_perl_files
+    test_needs_author
+    test_needs_module
+    test_needs_command
+    test_needs_srcdir_switch
+);
+our %EXPORT_TAGS = (
+    needs => [ qw(
+        test_needs_author
+        test_needs_module
+        test_needs_command
+        test_needs_srcdir_switch
+    ) ],
 );
 
 use Exporter qw(import);
 use File::Find;
+use IPC::Cmd qw(can_run);
+use Test::More;
 
 sub all_perl_files
 {
@@ -38,4 +52,47 @@ sub all_perl_files
     return @files;
 }
 
+sub test_needs_author
+{
+    if (not $ENV{DPKG_DEVEL_MODE}) {
+        plan skip_all => 'developer test';
+    }
+}
+
+sub test_needs_module
+{
+    my ($module, @imports) = @_;
+    my ($package) = caller;
+
+    require version;
+    my $version = '';
+    if (@imports >= 1 and version::is_lax($imports[0])) {
+        $version = shift @imports;
+    }
+
+    eval qq{
+        package $package;
+        use $module $version \@imports;
+        1;
+    } or do {
+        plan skip_all => "requires module $module $version";
+    }
+}
+
+sub test_needs_command
+{
+    my $command = shift;
+
+    if (not can_run($command)) {
+        plan skip_all => "requires command $command";
+    }
+}
+
+sub test_needs_srcdir_switch
+{
+    if (defined $ENV{srcdir}) {
+        chdir $ENV{srcdir} or BAIL_OUT("cannot chdir to source directory: $!");
+    }
+}
+
 1;
diff --git a/scripts/t/Dpkg_Shlibs_Cppfilt.t b/scripts/t/Dpkg_Shlibs_Cppfilt.t
index 2aeb476..6a76977 100644
--- a/scripts/t/Dpkg_Shlibs_Cppfilt.t
+++ b/scripts/t/Dpkg_Shlibs_Cppfilt.t
@@ -17,14 +17,11 @@ use strict;
 use warnings;
 
 use Test::More;
+use Test::Dpkg qw(:needs);
 
-use Dpkg::Path qw(find_command);
+test_needs_command('c++filt');
 
-if (find_command('c++filt')) {
-    plan tests => 124;
-} else {
-    plan skip_all => 'c++filt not available';
-}
+plan tests => 124;
 
 use_ok('Dpkg::Shlibs::Cppfilt');
 
diff --git a/t/critic.t b/t/critic.t
index 01e0cb7..108f121 100644
--- a/t/critic.t
+++ b/t/critic.t
@@ -17,19 +17,11 @@ use strict;
 use warnings;
 
 use Test::More;
-use Test::Dpkg;
+use Test::Dpkg qw(:needs);
 
-unless (defined $ENV{DPKG_DEVEL_MODE}) {
-    plan skip_all => 'not running in development mode';
-}
-
-if (defined $ENV{srcdir}) {
-    chdir $ENV{srcdir} or die "cannot chdir to source directory: $!";
-}
-
-if (not eval { require Test::Perl::Critic }) {
-    plan skip_all => 'Test::Perl::Critic required to criticize code';
-}
+test_needs_author();
+test_needs_module('Test::Perl::Critic');
+test_needs_srcdir_switch();
 
 my @policies = qw(
     BuiltinFunctions::ProhibitBooleanGrep
diff --git a/t/pod-spell.t b/t/pod-spell.t
index 4d21e3e..a70fc6f 100644
--- a/t/pod-spell.t
+++ b/t/pod-spell.t
@@ -17,27 +17,18 @@ use strict;
 use warnings;
 
 use Test::More;
-use Test::Dpkg;
+use Test::Dpkg qw(:needs);
 
-use IPC::Cmd qw(can_run);
-
-if (defined $ENV{srcdir}) {
-    chdir $ENV{srcdir} or die "cannot chdir to source directory: $!";
-}
-
-plan skip_all => 'author test' unless $ENV{AUTHOR_TESTING};
-
-eval 'use Test::Spelling';
-plan skip_all => 'Test::Spelling required for spell checking POD' if $@;
-
-if (not can_run('aspell')) {
-    plan skip_all => 'aspell required for spell checking POD';
-}
+test_needs_author();
+test_needs_module('Test::Spelling');
+test_needs_command('aspell');
 
 if (qx(aspell dicts) !~ m/en_US/) {
     plan skip_all => 'aspell en_US dictionary required for spell checking POD';
 }
 
+test_needs_srcdir_switch();
+
 my @files = Test::Dpkg::all_perl_files();
 
 plan tests => scalar @files;
diff --git a/t/pod.t b/t/pod.t
index 312105a..e68ab2f 100644
--- a/t/pod.t
+++ b/t/pod.t
@@ -17,13 +17,10 @@ use strict;
 use warnings;
 
 use Test::More;
+use Test::Dpkg qw(:needs);
 
-eval 'use Test::Pod 1.00';
-plan skip_all => 'Test::Pod 1.00 required for testing POD' if $@;
-
-if (defined $ENV{srcdir}) {
-    chdir $ENV{srcdir} or die "cannot chdir to source directory: $!";
-}
+test_needs_module('Test::Pod', '1.00');
+test_needs_srcdir_switch();
 
 my @dirs = qw(scripts/Dpkg);
 my @files = qw(scripts/Dpkg.pm);
diff --git a/t/strict.t b/t/strict.t
index 183d9a3..62c792f 100644
--- a/t/strict.t
+++ b/t/strict.t
@@ -17,17 +17,12 @@ use strict;
 use warnings;
 
 use Test::More;
-use Test::Dpkg;
+use Test::Dpkg qw(:needs);
 
-eval q{
-    use Test::Strict;
-    $Test::Strict::TEST_WARNINGS = 1;
-};
-plan skip_all => 'Test::Strict required for testing syntax' if $@;
+test_needs_module('Test::Strict');
+test_needs_srcdir_switch();
 
-if (defined $ENV{srcdir}) {
-    chdir $ENV{srcdir} or die "cannot chdir to source directory: $!";
-}
+eval '$Test::Strict::TEST_WARNINGS = 1';
 
 my @files = Test::Dpkg::all_perl_files();
 
diff --git a/t/syntax.t b/t/syntax.t
index 2ae3d76..4dfe63d 100644
--- a/t/syntax.t
+++ b/t/syntax.t
@@ -17,11 +17,9 @@ use strict;
 use warnings;
 
 use Test::More;
-use Test::Dpkg;
+use Test::Dpkg qw(:needs);
 
-if (defined $ENV{srcdir}) {
-    chdir $ENV{srcdir} or die "cannot chdir to source directory: $!";
-}
+test_needs_srcdir_switch();
 
 my @files = Test::Dpkg::all_perl_files();
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/dpkg.git



More information about the Reproducible-commits mailing list