dpkg (1.18.25) stretch; urgency=medium
[dpkg] / scripts / t / dpkg_buildpackage.t
CommitLineData
1479465f
GJ
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
16use strict;
17use warnings;
18
19use Test::More;
20use Test::Dpkg qw(:needs test_neutralize_checksums);
21
22use File::Spec::Functions qw(rel2abs);
23use File::Compare;
24use File::Path qw(make_path);
25use File::Copy;
26
27use Dpkg::IPC;
28use Dpkg::Build::Types;
29use Dpkg::Substvars;
30
31test_needs_command('fakeroot');
32
33plan tests => 12;
34
35my $srcdir = rel2abs($ENV{srcdir} || '.');
36my $datadir = "$srcdir/t/dpkg_buildpackage";
37my $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.
43delete $ENV{MAKEFLAGS};
44
45# Delete variables that can affect the tests.
46delete $ENV{SOURCE_DATE_EPOCH};
47
48# Delete other variables that can affect the tests.
49delete $ENV{$_} foreach grep { m/^DEB_/ } keys %ENV;
50
51make_path($tmpdir);
52
53chdir $tmpdir;
54
55my $tmpl_format = <<'TMPL_FORMAT';
563.0 (native)
57TMPL_FORMAT
58
59my $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
65TMPL_CHANGELOG
66
67my $tmpl_control = <<'TMPL_CONTROL';
68Source: ${source-name}
69Section: ${source-section}
70Priority: ${source-priority}
71Maintainer: ${maintainer}
72
73Package: test-binary-all
74Architecture: all
75Description: architecture independent binary package
76
77Package: test-binary-any
78Architecture: any
79Description: architecture dependent binary package
80TMPL_CONTROL
81
82my $tmpl_rules = <<'TMPL_RULES';
83#!/usr/bin/make -f
84
85DI := debian/${binary-name-all}
86DA := debian/${binary-name-any}
87
88clean:
89 rm -f debian/files
90 rm -rf $(DI) $(DA)
91
92build-indep:
93build-arch:
94build: build-indep build-arch
95
96binary-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
102binary-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
108binary: binary-indep binary-arch
109
110.PHONY: clean build-indep build-arch build binary-indexp binary-arch binary
111TMPL_RULES
112
113my %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
125sub 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
134sub 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
160sub 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
176sub 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
211my $basename = gen_source();
212
213test_build($basename, BUILD_SOURCE);
214test_build($basename, BUILD_ARCH_INDEP);
215test_build($basename, BUILD_ARCH_DEP);
216test_build($basename, BUILD_BINARY);
217test_build($basename, BUILD_FULL);
218
2191;