Browse Source

Dpkg::Control: Add new autopkgtest control files support

Add new CTRL_TESTS control types, new Dpkg::Control::Tests and
Dpkg::Control::Tests::Entry modules, add support for the fields that
can appear on these control files, and update Dpkg::Index to handle
them as well.

[niels@thykier.net: Fix logic inversion. ]
Guillem Jover 8 years ago
parent
commit
0d159ba9b5

+ 4 - 0
debian/changelog

@@ -25,6 +25,10 @@ dpkg (1.18.8) UNRELEASED; urgency=medium
       supported. Add regression tests to avoid similar changes in the future.
       Closes: #824938
     - Add support for system and user config loading in Dpkg::Conf.
+    - Add support for autopkgtest control files, with new CTRL_TESTS control
+      type, new recognized fields to Dpkg::Control::Fields, and new modules
+      Dpkg::Control::Tests and Dpkg::Control::Tests::Entry. Also update
+      Dpkg::Index to support these.
   * Test suite:
     - Bump perlcritic ValuesAndExpressions::RequireNumberSeparators minimum
       to 99999.

+ 12 - 1
scripts/Dpkg/Control.pm

@@ -18,7 +18,7 @@ package Dpkg::Control;
 use strict;
 use warnings;
 
-our $VERSION = '1.01';
+our $VERSION = '1.02';
 our @EXPORT = qw(
     CTRL_UNKNOWN
     CTRL_INFO_SRC
@@ -35,6 +35,7 @@ our @EXPORT = qw(
     CTRL_COPYRIGHT_HEADER
     CTRL_COPYRIGHT_FILES
     CTRL_COPYRIGHT_LICENSE
+    CTRL_TESTS
 );
 
 use Exporter qw(import);
@@ -134,6 +135,10 @@ machine readable format.
 Corresponds to a license control block in a F<debian/copyright> file in
 machine readable format.
 
+=item CTRL_TESTS
+
+Corresponds to a package tests control file in F<debian/tests/control>.
+
 =back
 
 =head1 METHODS
@@ -193,6 +198,8 @@ sub set_options {
             $$self->{name} = g_('files stanza of copyright file');
         } elsif ($t == CTRL_COPYRIGHT_HEADER) {
             $$self->{name} = g_('license stanza of copyright file');
+        } elsif ($t == CTRL_TESTS) {
+            $$self->{name} = g_("package's tests control file");
         } elsif ($t == CTRL_REPO_RELEASE) {
             $$self->{name} = sprintf(g_("repository's %s file"), 'Release');
         } elsif ($t == CTRL_INDEX_SRC) {
@@ -233,6 +240,10 @@ sub get_type {
 
 =head1 CHANGES
 
+=head2 Version 1.02 (dpkg 1.18.8)
+
+New type: CTRL_TESTS.
+
 =head2 Version 1.01 (dpkg 1.18.5)
 
 New types: CTRL_REPO_RELEASE, CTRL_COPYRIGHT_HEADER, CTRL_COPYRIGHT_FILES,

+ 23 - 1
scripts/Dpkg/Control/FieldsCore.pm

@@ -153,6 +153,10 @@ our %FIELDS = (
     'Changes' => {
         allowed => ALL_CHANGES,
     },
+    'Classes' => {
+        allowed => CTRL_TESTS,
+        separator => FIELD_SEP_COMMA,
+    },
     'Closes' => {
         allowed => ALL_CHANGES,
         separator => FIELD_SEP_SPACE,
@@ -187,7 +191,7 @@ our %FIELDS = (
         allowed => ALL_CHANGES | CTRL_REPO_RELEASE,
     },
     'Depends' => {
-        allowed => ALL_PKG,
+        allowed => ALL_PKG | CTRL_TESTS,
         separator => FIELD_SEP_COMMA,
         dependency => 'normal',
         dep_order => 2,
@@ -213,6 +217,10 @@ our %FIELDS = (
     'Essential' => {
         allowed => ALL_PKG,
     },
+    'Features' => {
+        allowed => CTRL_TESTS,
+        separator => FIELD_SEP_SPACE,
+    },
     'Filename' => {
         allowed => CTRL_INDEX_PKG,
         separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
@@ -291,6 +299,10 @@ our %FIELDS = (
         dependency => 'union',
         dep_order => 8,
     },
+    'Restrictions' => {
+        allowed => CTRL_TESTS,
+        separator => FIELD_SEP_SPACE,
+    },
     'Section' => {
         allowed => CTRL_INFO_SRC | CTRL_INDEX_SRC | ALL_PKG,
     },
@@ -328,6 +340,16 @@ our %FIELDS = (
     'Task' => {
         allowed => ALL_PKG,
     },
+    'Test-Command' => {
+        allowed => CTRL_TESTS,
+    },
+    'Tests' => {
+        allowed => CTRL_TESTS,
+        separator => FIELD_SEP_SPACE,
+    },
+    'Tests-Directory' => {
+        allowed => CTRL_TESTS,
+    },
     'Testsuite' => {
         allowed => ALL_SRC,
         separator => FIELD_SEP_COMMA,

+ 83 - 0
scripts/Dpkg/Control/Tests.pm

@@ -0,0 +1,83 @@
+# Copyright © 2016 Guillem Jover <guillem@debian.org>
+#
+# 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/>.
+
+package Dpkg::Control::Tests;
+
+use strict;
+use warnings;
+
+our $VERSION = '1.00';
+
+use Dpkg::Control;
+use Dpkg::Control::Tests::Entry;
+use Dpkg::Index;
+
+use parent qw(Dpkg::Index);
+
+=encoding utf8
+
+=head1 NAME
+
+Dpkg::Control::Tests - parse files like debian/tests/control
+
+=head1 DESCRIPTION
+
+It provides an object to access data of files that follow the same
+syntax as F<debian/tests/control>.
+
+=head1 METHODS
+
+All the methods of Dpkg::Index are available. Those listed below are either
+new or overridden with a different behavior.
+
+=over 4
+
+=item $c = Dpkg::Control::Tests->new(%opts)
+
+Create a new Dpkg::Control::Tests object, which inherits from Dpkg::Index.
+
+=cut
+
+sub new {
+    my ($this, %opts) = @_;
+    my $class = ref($this) || $this;
+    my $self = Dpkg::Index->new(type => CTRL_TESTS, %opts);
+
+    return bless $self, $class;
+}
+
+=item $item = $tests->new_item()
+
+Creates a new item.
+
+=cut
+
+sub new_item {
+    my $self = shift;
+
+    return Dpkg::Control::Tests::Entry->new();
+}
+
+=back
+
+=head1 CHANGES
+
+=head2 Version 1.00 (dpkg 1.18.8)
+
+Mark the module as public.
+
+=cut
+
+1;

+ 91 - 0
scripts/Dpkg/Control/Tests/Entry.pm

@@ -0,0 +1,91 @@
+# Copyright © 2016 Guillem Jover <guillem@debian.org>
+#
+# 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/>.
+
+package Dpkg::Control::Tests::Entry;
+
+use strict;
+use warnings;
+
+our $VERSION = '1.00';
+
+use Dpkg::Gettext;
+use Dpkg::ErrorHandling;
+use Dpkg::Control;
+
+use parent qw(Dpkg::Control);
+
+=encoding utf8
+
+=head1 NAME
+
+Dpkg::Control::Tests::Entry - represents a test suite entry
+
+=head1 DESCRIPTION
+
+This object represents a test suite entry.
+
+=head1 METHODS
+
+All the methods of Dpkg::Control are available. Those listed below are either
+new or overridden with a different behavior.
+
+=over 4
+
+=item $entry = Dpkg::Control::Tests::Entry->new()
+
+Creates a new object. It does not represent a real control test entry
+until one has been successfully parsed or built from scratch.
+
+=cut
+
+sub new {
+    my ($this, %opts) = @_;
+    my $class = ref($this) || $this;
+
+    my $self = Dpkg::Control->new(type => CTRL_TESTS, %opts);
+    bless $self, $class;
+    return $self;
+}
+
+=item $entry->parse($fh, $desc)
+
+Parse a control test entry from a filehandle.
+
+=cut
+
+sub parse {
+    my ($self, $fh, $desc) = @_;
+
+    return if not $self->SUPER::parse($fh, $desc);
+
+    if (not exists $self->{'Tests'} and not exists $self->{'Test-Command'}) {
+        $self->parse_error($desc, g_('block lacks either %s or %s fields'),
+                           'Tests', 'Test-Command');
+    }
+
+    return 1;
+}
+
+=back
+
+=head1 CHANGES
+
+=head2 Version 1.00 (dpkg 1.18.8)
+
+Mark the module as public.
+
+=cut
+
+1;

+ 3 - 0
scripts/Dpkg/Control/Types.pm

@@ -33,6 +33,7 @@ our @EXPORT = qw(
     CTRL_COPYRIGHT_HEADER
     CTRL_COPYRIGHT_FILES
     CTRL_COPYRIGHT_LICENSE
+    CTRL_TESTS
 );
 
 use Exporter qw(import);
@@ -83,6 +84,8 @@ use constant {
     CTRL_COPYRIGHT_FILES => 4096,
     # License control block in debian/copyright.
     CTRL_COPYRIGHT_LICENSE => 8192,
+    # Package test suite control file in debian/tests/control.
+    CTRL_TESTS => 16384,
 };
 
 =head1 CHANGES

+ 6 - 1
scripts/Dpkg/Index.pm

@@ -78,7 +78,8 @@ contain one item with a given key. The function used depends on the
 type: for CTRL_INFO_PKG, CTRL_INDEX_SRC, CTRL_INDEX_PKG and CTRL_PKG_DEB
 it's simply the Package field; for CTRL_PKG_SRC and CTRL_INFO_SRC, it's
 the Source field; for CTRL_CHANGELOG it's the Source and the Version
-fields (concatenated with an intermediary "_"); for CTRL_FILE_CHANGES it's
+fields (concatenated with an intermediary "_"); for CTRL_TESTS is either
+the Tests or Test-Command fields; for CTRL_FILE_CHANGES it's
 the Source, Version and Architecture fields (concatenated with "_");
 for CTRL_FILE_VENDOR it's the Vendor field; for CTRL_FILE_STATUS it's the
 Package and Architecture fields (concatenated with "_"). Otherwise it's
@@ -109,6 +110,10 @@ sub set_options {
             $self->{get_key_func} = sub { return $_[0]->{Files}; };
         } elsif ($t == CTRL_COPYRIGHT_LICENSE) {
             $self->{get_key_func} = sub { return $_[0]->{License}; };
+        } elsif ($t == CTRL_TESTS) {
+            $self->{get_key_func} = sub {
+                return $_[0]->{Tests} || $_[0]->{'Test-Command'};
+            };
         } elsif ($t == CTRL_FILE_CHANGES) {
 	    $self->{get_key_func} = sub {
 		return $_[0]->{Source} . '_' . $_[0]->{Version} . '_' .

+ 6 - 0
scripts/Makefile.am

@@ -72,6 +72,8 @@ nobase_dist_perllib_DATA = \
 	Dpkg/Control/Info.pm \
 	Dpkg/Control/HashCore.pm \
 	Dpkg/Control/Hash.pm \
+	Dpkg/Control/Tests.pm \
+	Dpkg/Control/Tests/Entry.pm \
 	Dpkg/Control/Types.pm \
 	Dpkg/Deps.pm \
 	Dpkg/Dist/Files.pm \
@@ -218,6 +220,7 @@ test_scripts = \
 	t/Dpkg_Changelog.t \
 	t/Dpkg_Changelog_Ubuntu.t \
 	t/Dpkg_Control.t \
+	t/Dpkg_Control_Tests.t \
 	t/Dpkg_Index.t \
 	t/Dpkg_Substvars.t \
 	t/Dpkg_IPC.t \
@@ -293,6 +296,9 @@ test_data = \
 	t/Dpkg_Control/bogus-armor-inline.dsc \
 	t/Dpkg_Control/bogus-armor-nested.dsc \
 	t/Dpkg_Control/bogus-armor-spaces.dsc \
+	t/Dpkg_Control_Tests/tests-missing-fields \
+	t/Dpkg_Control_Tests/tests-plain-text \
+	t/Dpkg_Control_Tests/tests-valid \
 	t/Dpkg_Source_Quilt/parse/debian/patches/series \
 	t/Dpkg_Substvars/substvars1 \
 	t/Dpkg_Substvars/substvars2 \

+ 71 - 0
scripts/t/Dpkg_Control_Tests.t

@@ -0,0 +1,71 @@
+#!/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 tests => 5;
+
+BEGIN {
+    use_ok('Dpkg::Control::Tests');
+}
+
+my $srcdir = $ENV{srcdir} || '.';
+my $datadir = $srcdir . '/t/Dpkg_Control_Tests';
+
+sub parse_tests {
+    my $path = shift;
+
+    my $tests = Dpkg::Control::Tests->new();
+    eval {
+        $tests->load($path);
+        1;
+    } or return;
+
+    return $tests;
+}
+
+my $tests;
+
+$tests = parse_tests("$datadir/tests-missing-fields");
+is($tests, undef, 'autopkgtest missing required fields');
+
+$tests = parse_tests("$datadir/tests-plain-text");
+is($tests, undef, 'autopkgtest is not in deb822 format');
+
+my $expected = <<'TESTS';
+Tests: aaa, bbb, ccc
+
+Tests: danger, warning
+Restrictions: rw-build-tree, needs-root, breaks-testbed
+
+Tests: depends
+Depends: @, @builddeps@, extra-package
+
+Tests: dir
+Tests-Directory: .
+
+Tests: feature
+
+Tests: class
+Classes: self-test
+
+Test-Command: command arg1 arg2
+
+TESTS
+
+$tests = parse_tests("$datadir/tests-valid");
+ok(defined $tests, 'Valid autopkgtest control file');
+is($tests->output(), $expected, 'autopkgtest control file dumped');

+ 7 - 0
scripts/t/Dpkg_Control_Tests/tests-missing-fields

@@ -0,0 +1,7 @@
+Tests: aaa
+
+Test-Command: command
+
+Other:
+
+Tests: bbb

+ 6 - 0
scripts/t/Dpkg_Control_Tests/tests-plain-text

@@ -0,0 +1,6 @@
+This is a plain text file
+that does not contain
+
+  any of the expected fields nor values
+
+and should fail to load.

+ 18 - 0
scripts/t/Dpkg_Control_Tests/tests-valid

@@ -0,0 +1,18 @@
+Tests: aaa, bbb, ccc
+
+Tests: danger, warning
+Restrictions: rw-build-tree, needs-root, breaks-testbed
+
+Tests: depends
+Depends: @, @builddeps@, extra-package
+
+Tests: dir
+Tests-Directory: .
+
+Tests: feature
+Features:
+
+Tests: class
+Classes: self-test
+
+Test-Command: command arg1 arg2