Browse Source

test: Add a unit test to compile perl code with warnings

With «use warnings» some inermodule issues such as redefinitions of
symbols are not catched, because that is too late.

Define PERL in the unit test environment so that we can invoke the
detected interpreter.

And disable the now redundant syntax_ok() check from the strict.t unit
test.
Guillem Jover 8 years ago
parent
commit
536d46d7f6
5 changed files with 55 additions and 2 deletions
  1. 1 0
      Makefile.am
  2. 1 0
      check.am
  3. 2 0
      debian/changelog
  4. 1 2
      t/strict.t
  5. 50 0
      t/syntax.t

+ 1 - 0
Makefile.am

@@ -145,6 +145,7 @@ endif
 
 test_scripts = \
 	t/pod.t \
+	t/syntax.t \
 	t/strict.t \
 	t/critic.t \
 	$(nil)

+ 1 - 0
check.am

@@ -30,6 +30,7 @@ check-local: $(test_data) $(test_programs) $(test_scripts)
 	  $(TEST_ENV_VARS) \
 	  srcdir=$(srcdir) builddir=$(builddir) \
 	  CC=$(CC) \
+	  PERL=$(PERL) \
 	  PERL_DL_NONLAZY=1 \
 	  PERL5LIB=$(abs_top_srcdir)/scripts:$(abs_top_srcdir)/dselect/methods \
 	  PERL5OPT=$(TEST_COVERAGE) \

+ 2 - 0
debian/changelog

@@ -36,6 +36,8 @@ dpkg (1.18.5) UNRELEASED; urgency=medium
     - Only warn once when a diff patches a file multiple times in
       Dpkg::Source::Patch, and fix the warning message to make it clear that
       the diff might be patchig the file more than once, not just twice.
+  * Test suite:
+    - Add a unit test to compile perl code with warnings.
   * Documentation:
     - Say value instead of option in deb-control(5).
     - Mark debian changelog format in bold in dpkg-parsechangelog(1).

+ 1 - 2
t/strict.t

@@ -31,10 +31,9 @@ if (defined $ENV{srcdir}) {
 
 my @files = Test::Dpkg::all_perl_files();
 
-plan tests => scalar @files * 3;
+plan tests => scalar @files * 2;
 
 for my $file (@files) {
-    syntax_ok($file);
     strict_ok($file);
     warnings_ok($file);
 }

+ 50 - 0
t/syntax.t

@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Dpkg;
+
+if (defined $ENV{srcdir}) {
+    chdir $ENV{srcdir} or die "cannot chdir to source directory: $!";
+}
+
+my @files = Test::Dpkg::all_perl_files();
+
+plan tests => scalar @files;
+
+my $PERL = $ENV{PERL} // $^X // 'perl';
+
+# Detect compilation warnings that are not found with just «use warnings»,
+# such as redefinition of symbols from multiple imports. We cannot use
+# Test::Strict::syntax_ok because it does not pass -w to perl, and does not
+# check for other issues whenever perl states the syntax is ok.
+sub syntax_ok {
+    my $file = shift;
+
+    my $eval = `$PERL -cw \"$file\" 2>&1`;
+    my $ok = ($eval =~ s{^\Q$file\E syntax OK\n$}{}ms) && length $eval == 0;
+
+    ok($ok, "Compilation check $file");
+    if (not $ok) {
+        diag($eval);
+    }
+}
+
+for my $file (@files) {
+    syntax_ok($file);
+}