Dpkg::Vendor::Debian: Move time64 buildflags feature from future to abi
[dpkg.git] / scripts / t / dpkg_buildpackage.t
blobbfcd23fd9bd67b10269ea77ad4395605e36770e4
1 #!/usr/bin/perl
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.
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.
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/>.
16 use strict;
17 use warnings;
19 use Test::More;
20 use Test::Dpkg qw(:needs :paths test_neutralize_checksums);
22 use File::Spec::Functions qw(rel2abs);
23 use File::Compare;
24 use File::Path qw(make_path);
25 use File::Copy;
27 use Dpkg::File;
28 use Dpkg::IPC;
29 use Dpkg::BuildTypes;
30 use Dpkg::Substvars;
32 test_needs_command('fakeroot');
34 plan tests => 17;
36 my $srcdir = rel2abs($ENV{srcdir} || '.');
37 my $datadir = "$srcdir/t/dpkg_buildpackage";
38 my $tmpdir = test_get_temp_path();
40 $ENV{$_} = rel2abs($ENV{$_}) foreach qw(DPKG_DATADIR DPKG_ORIGINS_DIR);
42 # Any parallelization from the parent should be ignored, we are testing
43 # the makefiles serially anyway.
44 delete $ENV{MAKEFLAGS};
46 # Delete variables that can affect the tests.
47 delete $ENV{SOURCE_DATE_EPOCH};
49 # Delete other variables that can affect the tests.
50 delete $ENV{$_} foreach grep { m/^DEB_/ } keys %ENV;
52 # Set architecture variables to not require dpkg nor gcc.
53 $ENV{PATH} = "$srcdir/t/mock-bin:$ENV{PATH}";
55 chdir $tmpdir;
57 my $tmpl_format = <<'TMPL_FORMAT';
58 3.0 (native)
59 TMPL_FORMAT
61 my $tmpl_changelog = <<'TMPL_CHANGELOG';
62 ${source-name} (${source-version}) ${suite}; urgency=${urgency}
64 * Entry. Closes: #12345
66 -- ${maintainer} Thu, 30 Jun 2016 20:15:12 +0200
67 TMPL_CHANGELOG
69 my $tmpl_control = <<'TMPL_CONTROL';
70 Source: ${source-name}
71 Section: ${source-section}
72 Priority: ${source-priority}
73 Maintainer: ${maintainer}
75 Package: test-binary-all
76 Architecture: all
77 Description: architecture independent binary package
79 Package: test-binary-any
80 Architecture: any
81 Description: architecture dependent binary package
82 TMPL_CONTROL
84 my $tmpl_rules = <<'TMPL_RULES';
85 #!/usr/bin/make -f
87 DI := debian/${binary-name-all}
88 DA := debian/${binary-name-any}
90 # fakeroot confuses ASAN link order check.
91 export ASAN_OPTIONS = verify_asan_link_order=0
93 clean:
94 rm -f debian/files
95 rm -rf $(DI) $(DA)
97 build-indep:
98 build-arch:
99 build: build-indep build-arch
101 binary-indep: build-indep
102 rm -rf $(DI)
103 mkdir -p $(DI)/DEBIAN
104 dpkg-gencontrol -P$(DI) -p${binary-name-all}
105 dpkg-deb --build $(DI) ..
107 binary-arch: build-arch
108 rm -rf $(DA)
109 mkdir -p $(DA)/DEBIAN
110 dpkg-gencontrol -P$(DA) -p${binary-name-any}
111 dpkg-deb --build $(DA) ..
113 binary: binary-indep binary-arch
115 .PHONY: clean build-indep build-arch build binary-indexp binary-arch binary
116 TMPL_RULES
118 my %default_substvars = (
119 'source-name' => 'test-source',
120 'source-version' => 0,
121 'source-section' => 'test',
122 'source-priority' => 'optional',
123 'binary-name-all' => 'test-binary-all',
124 'binary-name-any' => 'test-binary-any',
125 'suite' => 'unstable',
126 'urgency' => 'low',
127 'maintainer' => 'Dpkg Developers <debian-dpkg@lists.debian.org>',
130 sub gen_from_tmpl
132 my ($pathname, $tmpl, $substvars) = @_;
134 file_dump($pathname, $substvars->substvars($tmpl));
137 sub gen_source
139 my (%options) = @_;
141 my $substvars = Dpkg::Substvars->new();
142 foreach my $var (%default_substvars) {
143 my $value = $options{$var} // $default_substvars{$var};
145 $substvars->set_as_auto($var, $value);
148 my $source = $substvars->get('source-name');
149 my $version = $substvars->get('source-version');
150 my $basename = "$source\_$version";
151 my $dirname = $basename =~ tr/_/-/r;
153 make_path("$dirname/debian/source");
155 gen_from_tmpl("$dirname/debian/source/format", $tmpl_format, $substvars);
156 gen_from_tmpl("$dirname/debian/changelog", $tmpl_changelog, $substvars);
157 gen_from_tmpl("$dirname/debian/control", $tmpl_control, $substvars);
158 gen_from_tmpl("$dirname/debian/rules", $tmpl_rules, $substvars);
160 return $basename;
163 sub test_diff
165 my $filename = shift;
167 my $expected_file = "$datadir/$filename";
168 my $generated_file = $filename;
170 test_neutralize_checksums($generated_file);
172 my $res = compare($expected_file, $generated_file);
173 if ($res) {
174 system "diff -u $expected_file $generated_file >&2";
176 ok($res == 0, "generated file matches expected one ($expected_file)");
179 sub test_build
181 my ($basename, $type) = @_;
182 my $dirname = $basename =~ tr/_/-/r;
184 set_build_type($type, 'buildtype', nocheck => 1);
185 my $typename = get_build_options_from_type();
187 my $stderr;
189 my @hook_names = qw(
190 preinit
191 init
192 preclean
193 source
194 build
195 binary
196 buildinfo
197 changes
198 postclean
199 check
200 sign
201 done
203 my @hook_opts = map {
204 "--hook-$_=$datadir/hook a=%a p=%p v=%v s=%s u=%u >>../$basename\_$typename.hook"
205 } @hook_names;
207 chdir $dirname;
208 spawn(exec => [ $ENV{PERL}, "$srcdir/dpkg-buildpackage.pl",
209 "--admindir=$datadir/dpkgdb",
210 '--host-arch=amd64',
211 '--ignore-builtin-builddeps',
212 '--no-sign',
213 "--build=$typename",
214 '--check-command=',
215 @hook_opts,
217 error_to_string => \$stderr,
218 wait_child => 1, nocheck => 1);
219 chdir '..';
221 ok($? == 0, "dpkg-buildpackage --build=$typename succeeded");
222 diag($stderr) unless $? == 0;
224 if (build_has_all(BUILD_ARCH_DEP)) {
225 # Rename the file to preserve on consecutive invocations.
226 move("$basename\_amd64.changes", "$basename\_$typename.changes");
229 test_diff("$basename\_$typename.hook");
231 if (build_has_all(BUILD_SOURCE)) {
232 test_diff("$basename.dsc");
235 test_diff("$basename\_$typename.changes");
238 my $basename = gen_source();
240 test_build($basename, BUILD_SOURCE);
241 test_build($basename, BUILD_ARCH_INDEP);
242 test_build($basename, BUILD_ARCH_DEP);
243 test_build($basename, BUILD_BINARY);
244 test_build($basename, BUILD_FULL);