chiark / gitweb /
lib/dpkg/tarfn.c: Kludge `tar_header_decode' to handle spurious `errno'.
[dpkg] / scripts / t / dpkg_buildpackage.t
1 #!/usr/bin/perl
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16 use strict;
17 use warnings;
18
19 use Test::More;
20 use Test::Dpkg qw(:needs test_neutralize_checksums);
21
22 use File::Spec::Functions qw(rel2abs);
23 use File::Compare;
24 use File::Path qw(make_path);
25 use File::Copy;
26
27 use Dpkg::IPC;
28 use Dpkg::Build::Types;
29 use Dpkg::Substvars;
30
31 test_needs_command('fakeroot');
32
33 plan tests => 12;
34
35 my $srcdir = rel2abs($ENV{srcdir} || '.');
36 my $datadir = "$srcdir/t/dpkg_buildpackage";
37 my $tmpdir = 't.tmp/dpkg_buildpackage';
38
39 $ENV{$_} = rel2abs($ENV{$_}) foreach qw(DPKG_DATADIR DPKG_ORIGINS_DIR);
40
41 # Any parallelization from the parent should be ignored, we are testing
42 # the makefiles serially anyway.
43 delete $ENV{MAKEFLAGS};
44
45 # Delete variables that can affect the tests.
46 delete $ENV{SOURCE_DATE_EPOCH};
47
48 # Delete other variables that can affect the tests.
49 delete $ENV{$_} foreach grep { m/^DEB_/ } keys %ENV;
50
51 make_path($tmpdir);
52
53 chdir $tmpdir;
54
55 my $tmpl_format = <<'TMPL_FORMAT';
56 3.0 (native)
57 TMPL_FORMAT
58
59 my $tmpl_changelog = <<'TMPL_CHANGELOG';
60 ${source-name} (${source-version}) ${suite}; urgency=${urgency}
61
62   * Entry. Closes: #12345
63
64  -- ${maintainer}  Thu, 30 Jun 2016 20:15:12 +0200
65 TMPL_CHANGELOG
66
67 my $tmpl_control = <<'TMPL_CONTROL';
68 Source: ${source-name}
69 Section: ${source-section}
70 Priority: ${source-priority}
71 Maintainer: ${maintainer}
72
73 Package: test-binary-all
74 Architecture: all
75 Description: architecture independent binary package
76
77 Package: test-binary-any
78 Architecture: any
79 Description: architecture dependent binary package
80 TMPL_CONTROL
81
82 my $tmpl_rules = <<'TMPL_RULES';
83 #!/usr/bin/make -f
84
85 DI := debian/${binary-name-all}
86 DA := debian/${binary-name-any}
87
88 clean:
89         rm -f debian/files
90         rm -rf $(DI) $(DA)
91
92 build-indep:
93 build-arch:
94 build: build-indep build-arch
95
96 binary-indep: build-indep
97         rm -rf $(DI)
98         mkdir -p $(DI)/DEBIAN
99         dpkg-gencontrol -P$(DI) -p${binary-name-all}
100         dpkg-deb --build $(DI) ..
101
102 binary-arch: build-arch
103         rm -rf $(DA)
104         mkdir -p $(DA)/DEBIAN
105         dpkg-gencontrol -P$(DA) -p${binary-name-any}
106         dpkg-deb --build $(DA) ..
107
108 binary: binary-indep binary-arch
109
110 .PHONY: clean build-indep build-arch build binary-indexp binary-arch binary
111 TMPL_RULES
112
113 my %default_substvars = (
114     'source-name' => 'test-source',
115     'source-version' => 0,
116     'source-section' => 'test',
117     'source-priority' => 'optional',
118     'binary-name-all' => 'test-binary-all',
119     'binary-name-any' => 'test-binary-any',
120     'suite' => 'unstable',
121     'urgency' => 'low',
122     'maintainer' => 'Dpkg Developers <debian-dpkg@lists.debian.org>',
123 );
124
125 sub gen_from_tmpl
126 {
127     my ($pathname, $tmpl, $substvars) = @_;
128
129     open my $fh, '>', $pathname or die;
130     print { $fh } $substvars->substvars($tmpl);
131     close $fh or die;
132 }
133
134 sub gen_source
135 {
136     my (%options) = @_;
137
138     my $substvars = Dpkg::Substvars->new();
139     foreach my $var (%default_substvars) {
140         my $value = $options{$var} // $default_substvars{$var};
141
142         $substvars->set_as_auto($var, $value);
143     }
144
145     my $source = $substvars->get('source-name');
146     my $version = $substvars->get('source-version');
147     my $basename = "$source\_$version";
148     my $dirname = $basename =~ tr/_/-/r;
149
150     make_path("$dirname/debian/source");
151
152     gen_from_tmpl("$dirname/debian/source/format", $tmpl_format, $substvars);
153     gen_from_tmpl("$dirname/debian/changelog", $tmpl_changelog, $substvars);
154     gen_from_tmpl("$dirname/debian/control", $tmpl_control, $substvars);
155     gen_from_tmpl("$dirname/debian/rules", $tmpl_rules, $substvars);
156
157     return $basename;
158 }
159
160 sub test_diff
161 {
162     my $filename = shift;
163
164     my $expected_file = "$datadir/$filename";
165     my $generated_file =  $filename;
166
167     test_neutralize_checksums($generated_file);
168
169     my $res = compare($expected_file, $generated_file);
170     if ($res) {
171         system "diff -u $expected_file $generated_file >&2";
172     }
173     ok($res == 0, "generated file matches expected one ($expected_file)");
174 }
175
176 sub test_build
177 {
178     my ($basename, $type) = @_;
179     my $dirname = $basename =~ tr/_/-/r;
180
181     set_build_type($type, 'buildtype', nocheck => 1);
182     my $typename = get_build_options_from_type();
183
184     my $stderr;
185
186     chdir $dirname;
187     spawn(exec => [ $ENV{PERL}, "$srcdir/dpkg-buildpackage.pl",
188                     '--host-arch=amd64',
189                     '--unsigned-source', '--unsigned-changes',
190                     '--unsigned-buildinfo',
191                     "--build=$typename", '--check-command=' ],
192           error_to_string => \$stderr,
193           wait_child => 1, nocheck => 1);
194     chdir '..';
195
196     ok($? == 0, "dpkg-buildpackage --build=$typename succeeded");
197     diag($stderr) unless $? == 0;
198
199     if (build_has_all(BUILD_ARCH_DEP)) {
200         # Rename the file to preserve on consecutive invocations.
201         move("$basename\_amd64.changes", "$basename\_$typename.changes");
202     }
203
204     if (build_has_all(BUILD_SOURCE)) {
205         test_diff("$basename.dsc");
206     }
207
208     test_diff("$basename\_$typename.changes");
209 }
210
211 my $basename = gen_source();
212
213 test_build($basename, BUILD_SOURCE);
214 test_build($basename, BUILD_ARCH_INDEP);
215 test_build($basename, BUILD_ARCH_DEP);
216 test_build($basename, BUILD_BINARY);
217 test_build($basename, BUILD_FULL);
218
219 1;