Browse Source

test: Refactor common unit test checks for needed things

Guillem Jover 7 years ago
parent
commit
6278c8e98f
8 changed files with 80 additions and 52 deletions
  1. 1 0
      debian/changelog
  2. 57 0
      scripts/Test/Dpkg.pm
  3. 3 6
      scripts/t/Dpkg_Shlibs_Cppfilt.t
  4. 4 12
      t/critic.t
  5. 6 15
      t/pod-spell.t
  6. 3 6
      t/pod.t
  7. 4 9
      t/strict.t
  8. 2 4
      t/syntax.t

+ 1 - 0
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

+ 57 - 0
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;

+ 3 - 6
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');
 

+ 4 - 12
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

+ 6 - 15
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;

+ 3 - 6
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);

+ 4 - 9
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();
 

+ 2 - 4
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();