summaryrefslogtreecommitdiffstats
path: root/scripts/t/Dpkg_Changelog.t
blob: 4d046fec124111bb88bfc38f94008f124f64c227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/perl
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Test::More tests => 102;
use Test::Dpkg qw(:paths);

use File::Basename;

use Dpkg::File;

BEGIN {
    use_ok('Dpkg::Changelog');
    use_ok('Dpkg::Changelog::Debian');
    use_ok('Dpkg::Vendor', qw(get_current_vendor));
};

my $datadir = test_get_data_path();

my $vendor = get_current_vendor();

#########################

foreach my $file ("$datadir/countme", "$datadir/shadow", "$datadir/fields",
    "$datadir/regressions", "$datadir/date-format", "$datadir/stop-modeline") {

    my $changes = Dpkg::Changelog::Debian->new(verbose => 0);
    $changes->load($file);

    my $content = file_slurp($file);
    cmp_ok($content, 'eq', "$changes", "string output of Dpkg::Changelog on $file");

    my $errors = $changes->get_parse_errors();
    my $basename = basename( $file );
    is($errors, '', "Parse example changelog $file without errors" );

    my @data = @$changes;
    ok(@data, 'data is not empty');

    my $str;
    if ($file eq "$datadir/countme") {
	# test range options
	cmp_ok(@data, '==', 7, 'no options -> count');
	my $all_versions = join( '/', map { $_->get_version() } @data);

	sub check_options {
            my (%opts) = @_;

            my @cnt = $changes->get_range($opts{range});
            cmp_ok(@cnt, '==', $opts{count}, "$opts{name} -> count");
            if ($opts{count} == @{$opts{data}}) {
                is_deeply(\@cnt, $opts{data}, "$opts{name} -> returns all");
	    } else {
                is_deeply([ map { $_->get_version() } @cnt ],
                          $opts{versions}, "$opts{name} -> versions" );
	    }
	}

        my %ref = (
            changes => $changes,
            data => \@data,
        );

        check_options(%ref, range => { count => 3 },
                      count => 3,
                      versions => [ '2:2.0-1', '1:2.0~rc2-3', '1:2.0~rc2-2' ],
                      name => 'positive count');
        check_options(%ref, range => { count => 3, reverse => 1 },
                      count => 3,
                      versions => [ '1:2.0~rc2-2', '1:2.0~rc2-3', '2:2.0-1' ],
                      name => 'positive reverse count');
        check_options(%ref, range => { count => -3 },
                      count => 3,
                      versions => [
                            '1:2.0~rc2-1sarge2',
                            '1:2.0~rc2-1sarge1',
                            '1.5-1',
                      ],
                      name => 'negative count');
        check_options(%ref, range => { count => 1 },
                      count => 1,
                      versions => [ '2:2.0-1' ],
                      name => 'count 1');
        check_options(%ref, range => { count => 1, default_all => 1 },
                      count => 1,
                      versions => [ '2:2.0-1' ],
                      name => 'count 1 (d_a 1)');
        check_options(%ref, range => { count => -1 },
                      count => 1,
                      versions => [ '1.5-1' ],
                      name => 'count -1');

        check_options(%ref, range => { count => 3, offset => 2 },
                      count => 3,
                      versions => [
                            '1:2.0~rc2-2',
                            '1:2.0~rc2-1sarge3',
                            '1:2.0~rc2-1sarge2',
                      ],
                      name => 'positive count + positive offset');
        check_options(%ref, range => { count => -3, offset => 4 },
                      count => 3,
                      versions => [
                            '1:2.0~rc2-3',
                            '1:2.0~rc2-2',
                            '1:2.0~rc2-1sarge3',
                      ],
                      name => 'negative count + positive offset');

        check_options(%ref, range => { count => 4, offset => 5 },
                      count => 2,
                      versions => [ '1:2.0~rc2-1sarge1', '1.5-1' ],
                      name => 'positive count + positive offset (>max)');
        check_options(%ref, range =>  { count => -4, offset => 2 },
                      count => 2,
                      versions => [ '2:2.0-1', '1:2.0~rc2-3' ],
                      name => 'negative count + positive offset (<0)');

        check_options(%ref, range => { count => 3, offset => -4 },
                      count => 3,
                      versions => [
                        '1:2.0~rc2-1sarge3',
                        '1:2.0~rc2-1sarge2',
                        '1:2.0~rc2-1sarge1',
                      ],
                      name => 'positive count + negative offset');
        check_options(%ref, range => { count => -3, offset => -3 },
                      count => 3,
                      versions => [
                          '1:2.0~rc2-3',
                          '1:2.0~rc2-2',
                          '1:2.0~rc2-1sarge3',
                      ],
                      name => 'negative count + negative offset');

        check_options(%ref, range => { count => 5, offset => -2 },
                      count => 2,
                      versions => [ '1:2.0~rc2-1sarge1', '1.5-1' ],
                      name => 'positive count + negative offset (>max)');
        check_options(%ref, range => { count => -5, offset => -4 },
                      count => 3,
                      versions => [ '2:2.0-1', '1:2.0~rc2-3', '1:2.0~rc2-2' ],
                      name => 'negative count + negative offset (<0)');

        check_options(%ref, range => { count => 7 },
                      count => 7,
                      name => 'count 7 (max)');
        check_options(%ref, range => { count => -7 },
                      count => 7,
                      name => 'count -7 (-max)');
        check_options(%ref, range => { count => 10 },
                      count => 7,
                      name => 'count 10 (>max)');
        check_options(%ref, range => { count => -10 },
                      count => 7,
                      name => 'count -10 (<-max)');

        check_options(%ref, range => { from => '1:2.0~rc2-1sarge3' },
                      count => 4,
                      versions => [
                          '2:2.0-1',
                          '1:2.0~rc2-3',
                          '1:2.0~rc2-2',
                          '1:2.0~rc2-1sarge3',
                      ],
                      name => 'from => "1:2.0~rc2-1sarge3"');
        check_options(%ref, range => { since => '1:2.0~rc2-1sarge3' },
                      count => 3,
                      versions => [
                            '2:2.0-1',
                            '1:2.0~rc2-3',
                            '1:2.0~rc2-2',
                      ],
                      name => 'since => "1:2.0~rc2-1sarge3"');
        $SIG{__WARN__} = sub {};
        check_options(%ref, range => { since => 0 },
                      count => 7,
                      name => 'since => 0 returns all');
        delete $SIG{__WARN__};
        check_options(%ref, range => { to => '1:2.0~rc2-1sarge2' },
                      count => 3,
                      versions => [
                            '1:2.0~rc2-1sarge2',
                            '1:2.0~rc2-1sarge1',
                            '1.5-1',
                      ],
                      name => 'to => "1:2.0~rc2-1sarge2"');
        ## no critic (ControlStructures::ProhibitUntilBlocks)
        check_options(%ref, range => { until => '1:2.0~rc2-1sarge2' },
                      count => 2,
                      versions => [ '1:2.0~rc2-1sarge1', '1.5-1' ],
                      name => 'until => "1:2.0~rc2-1sarge2"');
        ## use critic
        #TODO: test combinations
    }
    if ($file eq "$datadir/fields") {
	my $str = $changes->format_range('dpkg', { all => 1 });
	my $expected = 'Source: fields
Version: 2.0-0etch1
Distribution: stable
Urgency: high
Maintainer: Frank Lichtenheld <frank@lichtenheld.de>
Timestamp: 1200235759
Date: Sun, 13 Jan 2008 15:49:19 +0100
Closes: 1000000 1111111 2222222
Changes:
 fields (2.0-0etch1) stable; urgency=low
 .
   * Upload to stable (Closes: #1111111, #2222222)
   * Fix more stuff. (LP: #54321, #2424242)
 .
 fields (2.0-1) unstable  frozen; urgency=medium
 .
   [ Frank Lichtenheld ]
   * Upload to unstable (Closes: #1111111, #2222222)
   * Fix stuff. (LP: #12345, #424242)
 .
   [ Raphaël Hertzog ]
   * New upstream release.
     - implements a
     - implements b
   * Update S-V.
 .
 fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
 .
   * Beta
 .
 fields (1.0) experimental; urgency=high,xb-userfield2=foobar
 .
   * First upload (Closes: #1000000)
Xb-Userfield2: foobar
Xc-Userfield: foobar

';
	if ($vendor eq 'Ubuntu') {
	    $expected =~ s/^(Closes:.*)/$1\nLaunchpad-Bugs-Fixed: 12345 54321 424242 2424242/m;
	}
	cmp_ok($str, 'eq', $expected, 'fields handling');

	$str = $changes->format_range('dpkg', { offset => 1, count => 2 });
	$expected = 'Source: fields
Version: 2.0-1
Distribution: unstable frozen
Urgency: medium
Maintainer: Frank Lichtenheld <djpig@debian.org>
Timestamp: 1200149359
Date: Sun, 12 Jan 2008 15:49:19 +0100
Closes: 1111111 2222222
Changes:
 fields (2.0-1) unstable  frozen; urgency=medium
 .
   [ Frank Lichtenheld ]
   * Upload to unstable (Closes: #1111111, #2222222)
   * Fix stuff. (LP: #12345, #424242)
 .
   [ Raphaël Hertzog ]
   * New upstream release.
     - implements a
     - implements b
   * Update S-V.
 .
 fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
 .
   * Beta
Xc-Userfield: foobar

';
	if ($vendor eq 'Ubuntu') {
	    $expected =~ s/^(Closes:.*)/$1\nLaunchpad-Bugs-Fixed: 12345 424242/m;
	}
	cmp_ok($str, 'eq', $expected, 'fields handling 2');

	$str = $changes->format_range('rfc822', { offset => 2, count => 2 });
	$expected = 'Source: fields
Version: 2.0~b1-1
Distribution: unstable
Urgency: low
Maintainer: Frank Lichtenheld <frank@lichtenheld.de>
Timestamp: 1200062959
Date: Sun, 11 Jan 2008 15:49:19 +0100
Changes:
 fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
 .
   * Beta
Xc-Userfield: foobar

Source: fields
Version: 1.0
Distribution: experimental
Urgency: high
Maintainer: Frank Lichtenheld <djpig@debian.org>
Timestamp: 1199976559
Date: Sun, 10 Jan 2008 15:49:19 +0100
Closes: 1000000
Changes:
 fields (1.0) experimental; urgency=high,xb-userfield2=foobar
 .
   * First upload (Closes: #1000000)
Xb-Userfield2: foobar

';
	cmp_ok($str, 'eq', $expected, 'fields handling 3');

	# Test Dpkg::Changelog::Entry methods
	is($data[1]->get_version(), '2.0-1', 'get_version');
	is($data[1]->get_source(), 'fields', 'get_source');
	is(scalar $data[1]->get_distributions(), 'unstable', 'get_distribution');
	is(join('|', $data[1]->get_distributions()), 'unstable|frozen',
	    'get_distributions');
	is($data[3]->get_optional_fields(),
	    "Urgency: high\nCloses: 1000000\nXb-Userfield2: foobar\n",
	    'get_optional_fields');
	is($data[1]->get_maintainer(), 'Frank Lichtenheld <djpig@debian.org>',
	    'get_maintainer');
	is($data[1]->get_timestamp(), 'Sun, 12 Jan 2008 15:49:19 +0100',
	    'get_timestamp');
	my @items = $data[1]->get_change_items();
	is($items[0], "  [ Frank Lichtenheld ]\n", 'change items 1');
	is($items[4], '  * New upstream release.
    - implements a
    - implements b
', 'change items 2');
	is($items[5], "  * Update S-V.\n", 'change items 3');
    }
    if ($file eq "$datadir/date-format") {
        is($data[0]->get_timestamp(), '01 Jul 2100 23:59:59 -1200',
           'get date w/o DoW, and negative timezone offset');
        is($data[1]->get_timestamp(), 'Tue, 27 Feb 2050 12:00:00 +1245',
           'get date w/ DoW, and positive timezone offset');
        is($data[2]->get_timestamp(), 'Mon, 01 Jan 2000 00:00:00 +0000',
           'get date w/ DoW, and zero timezone offset');
    }
    if ($file eq "$datadir/stop-modeline") {
        is($changes->get_unparsed_tail(), "vim: et\n",
           'get unparsed modeline at EOF');
    }
    if ($file eq "$datadir/regressions") {
	my $f = ($changes->format_range('dpkg'))[0];
	is("$f->{Version}", '0', 'version 0 correctly parsed');
    }

    SKIP: {
	skip('avoid spurious warning with only one entry', 2)
	    if @data == 1;

	my $oldest_version = $data[-1]->{Version};
	$str = $changes->format_range('dpkg', { since => $oldest_version });

	$str = $changes->format_range('rfc822');

	ok(1, 'TODO check rfc822 output');

	$str = $changes->format_range('rfc822', { since => $oldest_version });

	ok(1, 'TODO check rfc822 output with ranges');
    }
}

foreach my $test (([ "$datadir/misplaced-tz", 6 ],
                   [ "$datadir/unreleased", 5, 7 ])) {

    my $file = shift @$test;
    my $changes = Dpkg::Changelog::Debian->new(verbose => 0);
    $changes->load($file);
    my @errors = $changes->get_parse_errors();

    ok(@errors, 'errors occured');
    is_deeply( [ map { $_->[1] } @errors ], $test, 'check line numbers' );
}