Switch from pkg-config to pkgconf
[dpkg.git] / scripts / t / Dpkg_Deps.t
blob8694adffab4ec1e1526b991618174e19a4f25cdf
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 tests => 82;
21 use Dpkg::Arch qw(get_host_arch);
22 use Dpkg::Version;
24 $ENV{DEB_BUILD_ARCH} = 'amd64';
25 $ENV{DEB_HOST_ARCH} = 'amd64';
27 use_ok('Dpkg::Deps');
29 is(deps_concat(), '', 'Concatenate an empty list');
30 is(deps_concat(undef), '', 'Concatenate list with undef');
31 is(deps_concat(''), '', 'Concatenate an empty string');
32 is(deps_concat('', undef), '', 'Concatenate empty string with undef');
33 is(deps_concat('dep-a', undef, 'dep-b'), 'dep-a, dep-b',
34 'Concatenate two strings with intermixed undef');
36 sub test_dep_parse_option {
37 my %options = @_;
39 eval {
40 my $dep_croak = deps_parse('pkg', %options);
42 my $options = join ' ', map { "$_=$options{$_}" } keys %options;
43 ok(defined $@, "Parse with bogus arch options $options");
46 test_dep_parse_option(host_arch => 'all');
47 test_dep_parse_option(host_arch => 'any');
48 test_dep_parse_option(host_arch => 'linux-any');
49 test_dep_parse_option(host_arch => 'unknown-arch');
50 test_dep_parse_option(build_arch => 'all');
51 test_dep_parse_option(build_arch => 'any');
52 test_dep_parse_option(build_arch => 'linux-any');
53 test_dep_parse_option(build_arch => 'unknown-arch');
55 my $field_multiline = ' , , libgtk2.0-common (= 2.10.13-1) , libatk1.0-0 (>=
56 1.13.2), libc6 (>= 2.5-5), libcairo2 (>= 1.4.0), libcupsys2 (>= 1.2.7) <profile.a>
57 <profile.b
58 profile.c>
59 <profile.d>,
60 libfontconfig1 (>= 2.4.0), libglib2.0-0 ( >= 2.12.9), libgnutls13 (>=
61 1.6.3-0), libjpeg62, python (<< 2.5) , , ';
62 my $field_multiline_sorted = 'libatk1.0-0 (>= 1.13.2), libc6 (>= 2.5-5), libcairo2 (>= 1.4.0), libcupsys2 (>= 1.2.7) <profile.a> <profile.b profile.c> <profile.d>, libfontconfig1 (>= 2.4.0), libglib2.0-0 (>= 2.12.9), libgnutls13 (>= 1.6.3-0), libgtk2.0-common (= 2.10.13-1), libjpeg62, python (<< 2.5)';
64 my $dep_multiline = deps_parse($field_multiline);
65 $dep_multiline->sort();
66 is($dep_multiline->output(), $field_multiline_sorted, 'Parse, sort and output');
68 my $dep_sorted = deps_parse('pkgz, pkgz | pkgb, pkgz | pkga, pkga (>= 1.0), pkgz (<= 2.0)');
69 $dep_sorted->sort();
70 is($dep_sorted->output(), 'pkga (>= 1.0), pkgz, pkgz | pkga, pkgz | pkgb, pkgz (<= 2.0)', 'Check sort() algorithm');
72 my $dep_subset = deps_parse('libatk1.0-0 (>> 1.10), libc6, libcairo2');
73 is($dep_multiline->implies($dep_subset), 1, 'Dep implies subset of itself');
74 is($dep_subset->implies($dep_multiline), undef, "Subset doesn't imply superset");
75 my $dep_opposite = deps_parse('python (>= 2.5)');
76 is($dep_opposite->implies($dep_multiline), 0, 'Opposite condition implies NOT the depends');
78 my $dep_or1 = deps_parse('a|b (>=1.0)|c (>= 2.0)');
79 my $dep_or2 = deps_parse('x|y|a|b|c (<= 0.5)|c (>=1.5)|d|e');
80 is($dep_or1->implies($dep_or2), 1, 'Implication between OR 1/2');
81 is($dep_or2->implies($dep_or1), undef, 'Implication between OR 2/2');
83 my $dep_ma_host = deps_parse('libcairo2');
84 my $dep_ma_any = deps_parse('libcairo2:any');
85 my $dep_ma_build = deps_parse('libcairo2:native', build_dep => 1);
86 my $dep_ma_explicit = deps_parse('libcairo2:amd64');
87 is($dep_ma_host->implies($dep_ma_any), undef, 'foo !-> foo:any');
88 is($dep_ma_build->implies($dep_ma_any), undef, 'foo:native !-> foo:any');
89 is($dep_ma_explicit->implies($dep_ma_any), undef, 'foo:<arch> !-> foo:any');
90 is($dep_ma_any->implies($dep_ma_host), undef, 'foo:any !-> foo');
91 is($dep_ma_any->implies($dep_ma_build), undef, 'foo:any !-> foo:native');
92 is($dep_ma_any->implies($dep_ma_explicit), undef, 'foo:any !-> foo:<arch>');
93 is($dep_ma_host->implies($dep_ma_host), 1, 'foo -> foo');
94 is($dep_ma_any->implies($dep_ma_any), 1, 'foo:any -> foo:any');
95 is($dep_ma_build->implies($dep_ma_build), 1, 'foo:native -> foo:native');
96 is($dep_ma_explicit->implies($dep_ma_explicit), 1, 'foo:<arch>-> foo:<arch>');
98 my $field_tests = 'self, @, @builddeps@';
99 $SIG{__WARN__} = sub {};
100 my $dep_tests_fail = deps_parse($field_tests);
101 is($dep_tests_fail, undef, 'normal deps with @ in pkgname');
102 delete $SIG{__WARN__};
103 my $dep_tests_pass = deps_parse($field_tests, tests_dep => 1);
104 is($dep_tests_pass->output(), $field_tests, 'tests deps with @ in pkgname');
106 my $field_arch = 'libc6 (>= 2.5) [!alpha !hurd-i386], libc6.1 [alpha], libc0.1 [hurd-i386]';
107 my $dep_i386 = deps_parse($field_arch, reduce_arch => 1, host_arch => 'i386');
108 my $dep_alpha = deps_parse($field_arch, reduce_arch => 1, host_arch => 'alpha');
109 my $dep_hurd = deps_parse($field_arch, reduce_arch => 1, host_arch => 'hurd-i386');
110 is($dep_i386->output(), 'libc6 (>= 2.5)', 'Arch reduce 1/3');
111 is($dep_alpha->output(), 'libc6.1', 'Arch reduce 2/3');
112 is($dep_hurd->output(), 'libc0.1', 'Arch reduce 3/3');
114 my $field_profile = 'dep1 <!stage1 !nocheck>, ' .
115 'dep2 <stage1 !nocheck>, ' .
116 'dep3 <nocheck !stage1>, ' .
117 'dep4 <stage1 nocheck>, ' .
118 'dep5 <stage1>, dep6 <!stage1>, ' .
119 'dep7 <stage1> | dep8 <nocheck>, ' .
120 'dep9 <!stage1> <!nocheck>, ' .
121 'dep10 <stage1> <!nocheck>, ' .
122 'dep11 <stage1> <nocheck>, '.
123 'dep12 <!nocheck> <!stage1>, ' .
124 'dep13 <nocheck> <!stage1>, ' .
125 'dep14 <nocheck> <stage1>';
126 my $dep_noprof = deps_parse($field_profile, reduce_profiles => 1, build_profiles => []);
127 my $dep_stage1 = deps_parse($field_profile, reduce_profiles => 1, build_profiles => ['stage1']);
128 my $dep_nocheck = deps_parse($field_profile, reduce_profiles => 1, build_profiles => ['nocheck']);
129 my $dep_stage1nocheck = deps_parse($field_profile, reduce_profiles => 1, build_profiles => ['stage1', 'nocheck']);
130 is($dep_noprof->output(), 'dep1, dep6, dep9, dep10, dep12, dep13', 'Profile reduce 1/4');
131 is($dep_stage1->output(), 'dep2, dep5, dep7, dep9, dep10, dep11, dep12, dep14', 'Profile reduce 2/4');
132 is($dep_nocheck->output(), 'dep3, dep6, dep8, dep9, dep11, dep12, dep13, dep14', 'Profile reduce 3/4');
133 is($dep_stage1nocheck->output(), 'dep4, dep5, dep7 | dep8, dep10, dep11, dep13, dep14', 'Profile reduce 4/4');
135 $dep_noprof = deps_parse($field_profile);
136 $dep_noprof->reduce_profiles([]);
137 $dep_stage1 = deps_parse($field_profile);
138 $dep_stage1->reduce_profiles(['stage1']);
139 $dep_nocheck = deps_parse($field_profile);
140 $dep_nocheck->reduce_profiles(['nocheck']);
141 $dep_stage1nocheck = deps_parse($field_profile);
142 $dep_stage1nocheck->reduce_profiles(['stage1', 'nocheck']);
143 is($dep_noprof->output(), 'dep1, dep6, dep9, dep10, dep12, dep13', 'Profile post-reduce 1/4');
144 is($dep_stage1->output(), 'dep2, dep5, dep7, dep9, dep10, dep11, dep12, dep14', 'Profile post-reduce 2/4');
145 is($dep_nocheck->output(), 'dep3, dep6, dep8, dep9, dep11, dep12, dep13, dep14', 'Profile post-reduce 3/4');
146 is($dep_stage1nocheck->output(), 'dep4, dep5, dep7 | dep8, dep10, dep11, dep13, dep14', 'Profile post-reduce 4/4');
148 my $field_restrict = 'dep1 <!bootstrap !restrict>, ' .
149 'dep2 <bootstrap restrict>, ' .
150 'dep3 <!restrict>, ' .
151 'dep4 <restrict>, ' .
152 'dep5 <!bootstrap> <!restrict>, ' .
153 'dep6 <bootstrap> <restrict>';
154 my $dep_restrict = deps_parse($field_restrict, reduce_restrictions => 1, build_profiles => []);
155 is($dep_restrict->output(), 'dep1, dep3, dep5', 'Unknown restrictions reduce');
157 $dep_restrict = deps_parse($field_restrict);
158 $dep_restrict->reduce_profiles([]);
159 is($dep_restrict->output(), 'dep1, dep3, dep5', 'Unknown restrictions post-reduce');
161 my $facts = Dpkg::Deps::KnownFacts->new();
162 $facts->add_installed_package('mypackage', '1.3.4-1', get_host_arch(), 'no');
163 $facts->add_installed_package('mypackage2', '1.3.4-1', 'somearch', 'no');
164 $facts->add_installed_package('pkg-ma-foreign', '1.3.4-1', 'somearch', 'foreign');
165 $facts->add_installed_package('pkg-ma-foreign2', '1.3.4-1', get_host_arch(), 'foreign');
166 $facts->add_installed_package('pkg-ma-allowed', '1.3.4-1', 'somearch', 'allowed');
167 $facts->add_installed_package('pkg-ma-allowed2', '1.3.4-1', 'somearch', 'allowed');
168 $facts->add_installed_package('pkg-ma-allowed3', '1.3.4-1', get_host_arch(), 'allowed');
169 $facts->add_installed_package('pkg-indep-normal', '1.3.4-1', 'all', 'no');
170 $facts->add_installed_package('pkg-indep-foreign', '1.3.4-1', 'all', 'foreign');
171 $facts->add_provided_package('myvirtual', undef, undef, 'mypackage');
172 $facts->add_provided_package('myvirtual2', REL_EQ, '1.0-1', 'mypackage');
173 $facts->add_provided_package('myvirtual3', REL_GE, '2.0-1', 'mypackage');
175 my $field_duplicate = 'libc6 (>= 2.3), libc6 (>= 2.6-1), mypackage (>=
176 1.3), myvirtual | something, python (>= 2.5), mypackage2, pkg-ma-foreign,
177 pkg-ma-foreign2, pkg-ma-allowed:any, pkg-ma-allowed2, pkg-ma-allowed3';
178 my $dep_dup = deps_parse($field_duplicate);
179 $dep_dup->simplify_deps($facts, $dep_opposite);
180 is($dep_dup->output(), 'libc6 (>= 2.6-1), mypackage2, pkg-ma-allowed2',
181 'Simplify deps');
183 my $dep_ma_all_normal_implicit_native = deps_parse('pkg-indep-normal', build_dep => 1);
184 my $dep_ma_all_normal_explicit_native = deps_parse('pkg-indep-normal:native', build_dep => 1);
185 my $dep_ma_all_foreign_implicit_native = deps_parse('pkg-indep-foreign', build_dep => 1);
186 my $dep_ma_all_foreign_explicit_native = deps_parse('pkg-indep-foreign:native', build_dep => 1);
187 $dep_ma_all_normal_implicit_native->simplify_deps($facts);
188 is($dep_ma_all_normal_implicit_native->output(), '',
189 'Simplify arch:all m-a:no w/ implicit :native (satisfied)');
190 $dep_ma_all_normal_explicit_native->simplify_deps($facts);
191 is($dep_ma_all_normal_explicit_native->output(), '',
192 'Simplify arch:all m-a:no w/ explicit :native (satisfied)');
193 $dep_ma_all_foreign_implicit_native->simplify_deps($facts);
194 is($dep_ma_all_foreign_implicit_native->output(), '',
195 'Simplify arch:all m-a:foreign w/ implicit :native (satisfied)');
196 $dep_ma_all_foreign_explicit_native->simplify_deps($facts);
197 is($dep_ma_all_foreign_explicit_native->output(), 'pkg-indep-foreign:native',
198 'Simplify arch:all m-a:foreign w/ explicit :native (unsatisfied)');
200 TODO: {
202 local $TODO = 'not yet implemented';
204 my $dep_or_eq = deps_parse('pkg-a | pkg-b | pkg-a');
205 $dep_or_eq->simplify_deps($facts);
206 is($dep_or_eq->output(), 'pkg-a | pkg-b',
207 'Simplify duped ORed, equal names');
209 $dep_or_eq = deps_parse('pkg-a (= 10) | pkg-b | pkg-a (= 10)');
210 $dep_or_eq->simplify_deps($facts);
211 is($dep_or_eq->output(), 'pkg-a (= 10) | pkg-b',
212 'Simplify duped ORed, matching version');
214 my $dep_or_subset = deps_parse('pkg-a (>= 10) | pkg-b | pkg-a (= 10)');
215 $dep_or_eq->simplify_deps($facts);
216 is($dep_or_eq->output(), 'pkg-a (= 10) | pkg-b',
217 'Simplify duped ORed, subset version');
219 $dep_or_subset = deps_parse('pkg-a (>= 10) <profile> | pkg-b | pkg-a (= 10) <profile>');
220 $dep_or_eq->simplify_deps($facts);
221 is($dep_or_eq->output(), 'pkg-a (= 10) <profile> | pkg-b',
222 'Simplify duped ORed, subset version');
224 } # TODO
226 my $field_virtual = 'myvirtual | other';
227 my $dep_virtual = deps_parse($field_virtual);
228 $dep_virtual->simplify_deps($facts);
229 is($dep_virtual->output(), '',
230 'Simplify unversioned depends with unversioned virtual (satisfied)');
232 $field_virtual = 'myvirtual (>= 1.0) | other';
233 $dep_virtual = deps_parse($field_virtual);
234 $dep_virtual->simplify_deps($facts);
235 is($dep_virtual->output(), 'myvirtual (>= 1.0) | other',
236 'Simplify versioned depends on unversioned virtual (unsatisfied)');
238 $field_virtual = 'myvirtual2 (>= 0.0) | other';
239 $dep_virtual = deps_parse($field_virtual);
240 $dep_virtual->simplify_deps($facts);
241 is($dep_virtual->output(), '',
242 'Simplify versioned depends on versioned virtual (satisfied)');
244 $field_virtual = 'myvirtual2 (>= 2.0) | other';
245 $dep_virtual = deps_parse($field_virtual);
246 $dep_virtual->simplify_deps($facts);
247 is($dep_virtual->output(), 'myvirtual2 (>= 2.0) | other',
248 'Simplify versioned depends on versioned virtual (unsatisfied)');
250 $field_virtual = 'myvirtual3 (= 2.0-1)';
251 $dep_virtual = deps_parse($field_virtual);
252 $dep_virtual->simplify_deps($facts);
253 is($dep_virtual->output(), 'myvirtual3 (= 2.0-1)',
254 'Simplify versioned depends on GT versioned virtual (unsatisfied/ignored)');
256 my $field_dup_union = 'libc6 (>> 2.3), libc6 (>= 2.6-1), fake (<< 2.0),
257 fake(>> 3.0), fake (= 2.5), python (<< 2.5), python (= 2.4)';
258 my $dep_dup_union = deps_parse($field_dup_union, union => 1);
259 $dep_dup_union->simplify_deps($facts);
260 is($dep_dup_union->output(), 'libc6 (>> 2.3), fake (<< 2.0), fake (>> 3.0), fake (= 2.5), python (<< 2.5)', 'Simplify union deps');
262 $dep_dup_union = deps_parse('sipsak (<= 0.9.6-2.1), sipsak (<= 0.9.6-2.2)', union => 1);
263 $dep_dup_union->simplify_deps($facts);
264 is($dep_dup_union->output(), 'sipsak (<= 0.9.6-2.2)', 'Simplify union deps 2');
266 my $dep_red = deps_parse('abc | xyz, two, abc');
267 $dep_red->simplify_deps($facts, $dep_opposite);
268 is($dep_red->output(), 'abc, two', 'Simplification respect order');
269 is("$dep_red", $dep_red->output(), 'Stringification == output()');
271 my $dep_profiles = deps_parse('dupe <stage1 cross>, dupe <stage1 cross>');
272 $dep_profiles->simplify_deps($facts);
273 is($dep_profiles->output(), 'dupe <stage1 cross>',
274 'Simplification respects duplicated profiles');
276 TODO: {
278 local $TODO = 'not yet implemented';
280 $dep_profiles = deps_parse('tool <!cross>, tool <stage1 cross>');
281 $dep_profiles->simplify_deps($facts);
282 is($dep_profiles->output(), 'tool <!cross> <stage1 cross>',
283 'Simplify restriction formulas');
285 } # TODO
287 $dep_profiles = deps_parse('libfoo-dev:native <!stage1>, libfoo-dev <!stage1 cross>', build_dep => 1);
288 $dep_profiles->simplify_deps($facts);
289 is($dep_profiles->output(),
290 'libfoo-dev:native <!stage1>, libfoo-dev <!stage1 cross>',
291 'Simplification respects archqualifiers and profiles');
293 my $dep_archqual = deps_parse('pkg, pkg:any');
294 $dep_archqual->simplify_deps($facts);
295 is($dep_archqual->output(), 'pkg, pkg:any',
296 'Simplify respect arch-qualified ANDed dependencies 1/2');
298 $dep_archqual = deps_parse('pkg:amd64, pkg:any, pkg:i386');
299 $dep_archqual->simplify_deps($facts);
300 is($dep_archqual->output(), 'pkg:amd64, pkg:any, pkg:i386',
301 'Simplify respects arch-qualified ANDed dependencies 2/2');
303 $dep_archqual = deps_parse('pkg | pkg:any');
304 $dep_archqual->simplify_deps($facts);
305 is($dep_archqual->output(), 'pkg | pkg:any',
306 'Simplify respect arch-qualified ORed dependencies 1/2');
308 $dep_archqual = deps_parse('pkg:amd64 | pkg:i386 | pkg:any');
309 $dep_archqual->simplify_deps($facts);
310 is($dep_archqual->output(), 'pkg:amd64 | pkg:i386 | pkg:any',
311 'Simplify respect arch-qualified ORed dependencies 2/2');
313 my $dep_version = deps_parse('pkg, pkg (= 1.0)');
314 $dep_version->simplify_deps($facts);
315 is($dep_version->output(), 'pkg (= 1.0)', 'Simplification merges versions');
317 my $dep_empty1 = deps_parse('');
318 is($dep_empty1->output(), '', 'Empty dependency');
320 my $dep_empty2 = deps_parse(' , , ', union => 1);
321 is($dep_empty2->output(), '', "' , , ' is also an empty dependency");
323 # Check sloppy but acceptable dependencies
325 my $dep_sloppy_version = deps_parse('package (= 1.0 )');
326 is($dep_sloppy_version->output(), 'package (= 1.0)', 'sloppy version restriction');
328 my $dep_sloppy_arch = deps_parse('package [ alpha ]');
329 is($dep_sloppy_arch->output(), 'package [alpha]', 'sloppy arch restriction');
331 my $dep_sloppy_profile = deps_parse('package < !profile > < other >');
332 is($dep_sloppy_profile->output(), 'package <!profile> <other>',
333 'sloppy profile restriction');
335 $SIG{__WARN__} = sub {};
337 my $dep_bad_version = deps_parse('package (= 1.0) (>= 2.0)');
338 is($dep_bad_version, undef, 'Bogus repeated version restriction');
340 my $dep_bad_arch = deps_parse('package [alpha] [amd64]');
341 is($dep_bad_arch, undef, 'Bogus repeated arch restriction');
343 my $dep_bad_multiline = deps_parse("a, foo\nbar, c");
344 is($dep_bad_multiline, undef, 'invalid dependency split over multiple line');
346 delete $SIG{__WARN__};
348 my $dep_iter = deps_parse('a, b:armel, c | d:armhf, d:mips (>> 1.2)');
349 my %dep_arches;
350 my %dep_pkgs;
351 deps_iterate($dep_iter, sub {
352 my $dep = shift;
354 $dep_pkgs{$dep->{package}} = 1;
355 if ($dep->{archqual}) {
356 $dep_arches{$dep->{archqual}} = 1;
358 return 1;
360 my @dep_arches = sort keys %dep_arches;
361 my @dep_pkgs = sort keys %dep_pkgs;
362 is("@dep_arches", 'armel armhf mips', 'Dependency iterator, get arches');
363 is("@dep_pkgs", 'a b c d', 'Dependency iterator, get packages');