test: Move test_data_file() to test.h
[dpkg.git] / scripts / t / Dpkg_Changelog.t
blobc3e741364b33e1c2b172ad7fcbb32130d784e391
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 => 102;
20 use Test::Dpkg qw(:paths);
22 use File::Basename;
24 use Dpkg::File;
26 BEGIN {
27 use_ok('Dpkg::Changelog');
28 use_ok('Dpkg::Changelog::Debian');
29 use_ok('Dpkg::Vendor', qw(get_current_vendor));
32 my $datadir = test_get_data_path();
34 my $vendor = get_current_vendor();
36 #########################
38 foreach my $file ("$datadir/countme", "$datadir/shadow", "$datadir/fields",
39 "$datadir/regressions", "$datadir/date-format", "$datadir/stop-modeline") {
41 my $changes = Dpkg::Changelog::Debian->new(verbose => 0);
42 $changes->load($file);
44 my $content = file_slurp($file);
45 cmp_ok($content, 'eq', "$changes", "string output of Dpkg::Changelog on $file");
47 my $errors = $changes->get_parse_errors();
48 my $basename = basename( $file );
49 is($errors, '', "Parse example changelog $file without errors" );
51 my @data = @$changes;
52 ok(@data, 'data is not empty');
54 if ($file eq "$datadir/countme") {
55 # test range options
56 cmp_ok(@data, '==', 7, 'no options -> count');
57 my $all_versions = join( '/', map { $_->get_version() } @data);
59 sub check_options {
60 my (%opts) = @_;
62 my @cnt = $changes->get_range($opts{range});
63 cmp_ok(@cnt, '==', $opts{count}, "$opts{name} -> count");
64 if ($opts{count} == @{$opts{data}}) {
65 is_deeply(\@cnt, $opts{data}, "$opts{name} -> returns all");
66 } else {
67 is_deeply([ map { $_->get_version() } @cnt ],
68 $opts{versions}, "$opts{name} -> versions" );
72 my %ref = (
73 changes => $changes,
74 data => \@data,
77 check_options(%ref, range => { count => 3 },
78 count => 3,
79 versions => [ '2:2.0-1', '1:2.0~rc2-3', '1:2.0~rc2-2' ],
80 name => 'positive count');
81 check_options(%ref, range => { count => 3, reverse => 1 },
82 count => 3,
83 versions => [ '1:2.0~rc2-2', '1:2.0~rc2-3', '2:2.0-1' ],
84 name => 'positive reverse count');
85 check_options(%ref, range => { count => -3 },
86 count => 3,
87 versions => [
88 '1:2.0~rc2-1sarge2',
89 '1:2.0~rc2-1sarge1',
90 '1.5-1',
92 name => 'negative count');
93 check_options(%ref, range => { count => 1 },
94 count => 1,
95 versions => [ '2:2.0-1' ],
96 name => 'count 1');
97 check_options(%ref, range => { count => 1, default_all => 1 },
98 count => 1,
99 versions => [ '2:2.0-1' ],
100 name => 'count 1 (d_a 1)');
101 check_options(%ref, range => { count => -1 },
102 count => 1,
103 versions => [ '1.5-1' ],
104 name => 'count -1');
106 check_options(%ref, range => { count => 3, offset => 2 },
107 count => 3,
108 versions => [
109 '1:2.0~rc2-2',
110 '1:2.0~rc2-1sarge3',
111 '1:2.0~rc2-1sarge2',
113 name => 'positive count + positive offset');
114 check_options(%ref, range => { count => -3, offset => 4 },
115 count => 3,
116 versions => [
117 '1:2.0~rc2-3',
118 '1:2.0~rc2-2',
119 '1:2.0~rc2-1sarge3',
121 name => 'negative count + positive offset');
123 check_options(%ref, range => { count => 4, offset => 5 },
124 count => 2,
125 versions => [ '1:2.0~rc2-1sarge1', '1.5-1' ],
126 name => 'positive count + positive offset (>max)');
127 check_options(%ref, range => { count => -4, offset => 2 },
128 count => 2,
129 versions => [ '2:2.0-1', '1:2.0~rc2-3' ],
130 name => 'negative count + positive offset (<0)');
132 check_options(%ref, range => { count => 3, offset => -4 },
133 count => 3,
134 versions => [
135 '1:2.0~rc2-1sarge3',
136 '1:2.0~rc2-1sarge2',
137 '1:2.0~rc2-1sarge1',
139 name => 'positive count + negative offset');
140 check_options(%ref, range => { count => -3, offset => -3 },
141 count => 3,
142 versions => [
143 '1:2.0~rc2-3',
144 '1:2.0~rc2-2',
145 '1:2.0~rc2-1sarge3',
147 name => 'negative count + negative offset');
149 check_options(%ref, range => { count => 5, offset => -2 },
150 count => 2,
151 versions => [ '1:2.0~rc2-1sarge1', '1.5-1' ],
152 name => 'positive count + negative offset (>max)');
153 check_options(%ref, range => { count => -5, offset => -4 },
154 count => 3,
155 versions => [ '2:2.0-1', '1:2.0~rc2-3', '1:2.0~rc2-2' ],
156 name => 'negative count + negative offset (<0)');
158 check_options(%ref, range => { count => 7 },
159 count => 7,
160 name => 'count 7 (max)');
161 check_options(%ref, range => { count => -7 },
162 count => 7,
163 name => 'count -7 (-max)');
164 check_options(%ref, range => { count => 10 },
165 count => 7,
166 name => 'count 10 (>max)');
167 check_options(%ref, range => { count => -10 },
168 count => 7,
169 name => 'count -10 (<-max)');
171 check_options(%ref, range => { from => '1:2.0~rc2-1sarge3' },
172 count => 4,
173 versions => [
174 '2:2.0-1',
175 '1:2.0~rc2-3',
176 '1:2.0~rc2-2',
177 '1:2.0~rc2-1sarge3',
179 name => 'from => "1:2.0~rc2-1sarge3"');
180 check_options(%ref, range => { since => '1:2.0~rc2-1sarge3' },
181 count => 3,
182 versions => [
183 '2:2.0-1',
184 '1:2.0~rc2-3',
185 '1:2.0~rc2-2',
187 name => 'since => "1:2.0~rc2-1sarge3"');
188 $SIG{__WARN__} = sub {};
189 check_options(%ref, range => { since => 0 },
190 count => 7,
191 name => 'since => 0 returns all');
192 delete $SIG{__WARN__};
193 check_options(%ref, range => { to => '1:2.0~rc2-1sarge2' },
194 count => 3,
195 versions => [
196 '1:2.0~rc2-1sarge2',
197 '1:2.0~rc2-1sarge1',
198 '1.5-1',
200 name => 'to => "1:2.0~rc2-1sarge2"');
201 ## no critic (ControlStructures::ProhibitUntilBlocks)
202 check_options(%ref, range => { until => '1:2.0~rc2-1sarge2' },
203 count => 2,
204 versions => [ '1:2.0~rc2-1sarge1', '1.5-1' ],
205 name => 'until => "1:2.0~rc2-1sarge2"');
206 ## use critic
207 #TODO: test combinations
209 if ($file eq "$datadir/fields") {
210 my $str = $changes->format_range('dpkg', { all => 1 });
211 my $expected = 'Source: fields
212 Version: 2.0-0etch1
213 Distribution: stable
214 Urgency: high
215 Maintainer: Frank Lichtenheld <frank@lichtenheld.de>
216 Timestamp: 1200235759
217 Date: Sun, 13 Jan 2008 15:49:19 +0100
218 Closes: 1000000 1111111 2222222
219 Changes:
220 fields (2.0-0etch1) stable; urgency=low
222 * Upload to stable (Closes: #1111111, #2222222)
223 * Fix more stuff. (LP: #54321, #2424242)
225 fields (2.0-1) unstable frozen; urgency=medium
227 [ Frank Lichtenheld ]
228 * Upload to unstable (Closes: #1111111, #2222222)
229 * Fix stuff. (LP: #12345, #424242)
231 [ Raphaël Hertzog ]
232 * New upstream release.
233 - implements a
234 - implements b
235 * Update S-V.
237 fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
239 * Beta
241 fields (1.0) experimental; urgency=high,xb-userfield2=foobar
243 * First upload (Closes: #1000000)
244 Xb-Userfield2: foobar
245 Xc-Userfield: foobar
248 if ($vendor eq 'Ubuntu') {
249 $expected =~ s/^(Closes:.*)/$1\nLaunchpad-Bugs-Fixed: 12345 54321 424242 2424242/m;
251 cmp_ok($str, 'eq', $expected, 'fields handling');
253 $str = $changes->format_range('dpkg', { offset => 1, count => 2 });
254 $expected = 'Source: fields
255 Version: 2.0-1
256 Distribution: unstable frozen
257 Urgency: medium
258 Maintainer: Frank Lichtenheld <djpig@debian.org>
259 Timestamp: 1200149359
260 Date: Sun, 12 Jan 2008 15:49:19 +0100
261 Closes: 1111111 2222222
262 Changes:
263 fields (2.0-1) unstable frozen; urgency=medium
265 [ Frank Lichtenheld ]
266 * Upload to unstable (Closes: #1111111, #2222222)
267 * Fix stuff. (LP: #12345, #424242)
269 [ Raphaël Hertzog ]
270 * New upstream release.
271 - implements a
272 - implements b
273 * Update S-V.
275 fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
277 * Beta
278 Xc-Userfield: foobar
281 if ($vendor eq 'Ubuntu') {
282 $expected =~ s/^(Closes:.*)/$1\nLaunchpad-Bugs-Fixed: 12345 424242/m;
284 cmp_ok($str, 'eq', $expected, 'fields handling 2');
286 $str = $changes->format_range('rfc822', { offset => 2, count => 2 });
287 $expected = 'Source: fields
288 Version: 2.0~b1-1
289 Distribution: unstable
290 Urgency: low
291 Maintainer: Frank Lichtenheld <frank@lichtenheld.de>
292 Timestamp: 1200062959
293 Date: Sun, 11 Jan 2008 15:49:19 +0100
294 Changes:
295 fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
297 * Beta
298 Xc-Userfield: foobar
300 Source: fields
301 Version: 1.0
302 Distribution: experimental
303 Urgency: high
304 Maintainer: Frank Lichtenheld <djpig@debian.org>
305 Timestamp: 1199976559
306 Date: Sun, 10 Jan 2008 15:49:19 +0100
307 Closes: 1000000
308 Changes:
309 fields (1.0) experimental; urgency=high,xb-userfield2=foobar
311 * First upload (Closes: #1000000)
312 Xb-Userfield2: foobar
315 cmp_ok($str, 'eq', $expected, 'fields handling 3');
317 # Test Dpkg::Changelog::Entry methods
318 is($data[1]->get_version(), '2.0-1', 'get_version');
319 is($data[1]->get_source(), 'fields', 'get_source');
320 is(scalar $data[1]->get_distributions(), 'unstable', 'get_distribution');
321 is(join('|', $data[1]->get_distributions()), 'unstable|frozen',
322 'get_distributions');
323 is($data[3]->get_optional_fields(),
324 "Urgency: high\nCloses: 1000000\nXb-Userfield2: foobar\n",
325 'get_optional_fields');
326 is($data[1]->get_maintainer(), 'Frank Lichtenheld <djpig@debian.org>',
327 'get_maintainer');
328 is($data[1]->get_timestamp(), 'Sun, 12 Jan 2008 15:49:19 +0100',
329 'get_timestamp');
330 my @items = $data[1]->get_change_items();
331 is($items[0], " [ Frank Lichtenheld ]\n", 'change items 1');
332 is($items[4], ' * New upstream release.
333 - implements a
334 - implements b
335 ', 'change items 2');
336 is($items[5], " * Update S-V.\n", 'change items 3');
338 if ($file eq "$datadir/date-format") {
339 is($data[0]->get_timestamp(), '01 Jul 2100 23:59:59 -1200',
340 'get date w/o DoW, and negative timezone offset');
341 is($data[1]->get_timestamp(), 'Tue, 27 Feb 2050 12:00:00 +1245',
342 'get date w/ DoW, and positive timezone offset');
343 is($data[2]->get_timestamp(), 'Mon, 01 Jan 2000 00:00:00 +0000',
344 'get date w/ DoW, and zero timezone offset');
346 if ($file eq "$datadir/stop-modeline") {
347 is($changes->get_unparsed_tail(), "vim: et\n",
348 'get unparsed modeline at EOF');
350 if ($file eq "$datadir/regressions") {
351 my $f = ($changes->format_range('dpkg'))[0];
352 is("$f->{Version}", '0', 'version 0 correctly parsed');
355 SKIP: {
356 skip('avoid spurious warning with only one entry', 2)
357 if @data == 1;
359 my $str;
360 my $oldest_version = $data[-1]->{Version};
361 $str = $changes->format_range('dpkg', { since => $oldest_version });
363 $str = $changes->format_range('rfc822');
365 ok(1, 'TODO check rfc822 output');
367 $str = $changes->format_range('rfc822', { since => $oldest_version });
369 ok(1, 'TODO check rfc822 output with ranges');
373 foreach my $test (([ "$datadir/misplaced-tz", 6 ],
374 [ "$datadir/unreleased", 5, 7 ])) {
376 my $file = shift @$test;
377 my $changes = Dpkg::Changelog::Debian->new(verbose => 0);
378 $changes->load($file);
379 my @errors = $changes->get_parse_errors();
381 ok(@errors, 'errors occured');
382 is_deeply( [ map { $_->[1] } @errors ], $test, 'check line numbers' );