Dpkg_Changelog.t 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. use strict;
  16. use warnings;
  17. use Test::More tests => 94;
  18. use Test::Dpkg qw(:paths);
  19. use File::Basename;
  20. use Dpkg::File;
  21. BEGIN {
  22. use_ok('Dpkg::Changelog');
  23. use_ok('Dpkg::Changelog::Debian');
  24. use_ok('Dpkg::Vendor', qw(get_current_vendor));
  25. };
  26. my $datadir = test_get_data_path('t/Dpkg_Changelog');
  27. my $vendor = get_current_vendor();
  28. #########################
  29. foreach my $file ("$datadir/countme", "$datadir/shadow", "$datadir/fields",
  30. "$datadir/regressions", "$datadir/date-format") {
  31. my $changes = Dpkg::Changelog::Debian->new(verbose => 0);
  32. $changes->load($file);
  33. open(my $clog_fh, '<', "$file") or die "can't open $file\n";
  34. my $content = file_slurp($clog_fh);
  35. close($clog_fh);
  36. cmp_ok($content, 'eq', "$changes", "string output of Dpkg::Changelog on $file");
  37. my $errors = $changes->get_parse_errors();
  38. my $basename = basename( $file );
  39. is($errors, '', "Parse example changelog $file without errors" );
  40. my @data = @$changes;
  41. ok(@data, 'data is not empty');
  42. my $str;
  43. if ($file eq "$datadir/countme") {
  44. # test range options
  45. cmp_ok(@data, '==', 7, 'no options -> count');
  46. my $all_versions = join( '/', map { $_->get_version() } @data);
  47. sub check_options {
  48. my ($changes, $data, $options, $count, $versions,
  49. $check_name) = @_;
  50. my @cnt = $changes->get_range($options);
  51. cmp_ok( @cnt, '==', $count, "$check_name -> count" );
  52. if ($count == @$data) {
  53. is_deeply( \@cnt, $data, "$check_name -> returns all" );
  54. } else {
  55. is( join( '/', map { $_->get_version() } @cnt),
  56. $versions, "$check_name -> versions" );
  57. }
  58. }
  59. check_options( $changes, \@data,
  60. { count => 3 }, 3, '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  61. 'positive count' );
  62. check_options( $changes, \@data,
  63. { count => -3 }, 3,
  64. '1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1/1.5-1',
  65. 'negative count' );
  66. check_options( $changes, \@data,
  67. { count => 1 }, 1, '2:2.0-1',
  68. 'count 1' );
  69. check_options( $changes, \@data,
  70. { count => 1, default_all => 1 }, 1, '2:2.0-1',
  71. 'count 1 (d_a 1)' );
  72. check_options( $changes, \@data,
  73. { count => -1 }, 1, '1.5-1',
  74. 'count -1' );
  75. check_options( $changes, \@data,
  76. { count => 3, offset => 2 }, 3,
  77. '1:2.0~rc2-2/1:2.0~rc2-1sarge3/1:2.0~rc2-1sarge2',
  78. 'positve count + positive offset' );
  79. check_options( $changes, \@data,
  80. { count => -3, offset => 4 }, 3,
  81. '1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  82. 'negative count + positive offset' );
  83. check_options( $changes, \@data,
  84. { count => 4, offset => 5 }, 2,
  85. '1:2.0~rc2-1sarge1/1.5-1',
  86. 'positve count + positive offset (>max)' );
  87. check_options( $changes, \@data,
  88. { count => -4, offset => 2 }, 2,
  89. '2:2.0-1/1:2.0~rc2-3',
  90. 'negative count + positive offset (<0)' );
  91. check_options( $changes, \@data,
  92. { count => 3, offset => -4 }, 3,
  93. '1:2.0~rc2-1sarge3/1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1',
  94. 'positve count + negative offset' );
  95. check_options( $changes, \@data,
  96. { count => -3, offset => -3 }, 3,
  97. '1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  98. 'negative count + negative offset' );
  99. check_options( $changes, \@data,
  100. { count => 5, offset => -2 }, 2,
  101. '1:2.0~rc2-1sarge1/1.5-1',
  102. 'positve count + negative offset (>max)' );
  103. check_options( $changes, \@data,
  104. { count => -5, offset => -4 }, 3,
  105. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  106. 'negative count + negative offset (<0)' );
  107. check_options( $changes, \@data,
  108. { count => 7 }, 7, '',
  109. 'count 7 (max)' );
  110. check_options( $changes, \@data,
  111. { count => -7 }, 7, '',
  112. 'count -7 (-max)' );
  113. check_options( $changes, \@data,
  114. { count => 10 }, 7, '',
  115. 'count 10 (>max)' );
  116. check_options( $changes, \@data,
  117. { count => -10 }, 7, '',
  118. 'count -10 (<-max)' );
  119. check_options( $changes, \@data,
  120. { from => '1:2.0~rc2-1sarge3' }, 4,
  121. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  122. 'from => "1:2.0~rc2-1sarge3"' );
  123. check_options( $changes, \@data,
  124. { since => '1:2.0~rc2-1sarge3' }, 3,
  125. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  126. 'since => "1:2.0~rc2-1sarge3"' );
  127. $SIG{__WARN__} = sub {};
  128. check_options( $changes, \@data,
  129. { since => 0 }, 7, '',
  130. 'since => 0 returns all');
  131. delete $SIG{__WARN__};
  132. check_options( $changes, \@data,
  133. { to => '1:2.0~rc2-1sarge2' }, 3,
  134. '1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1/1.5-1',
  135. 'to => "1:2.0~rc2-1sarge2"' );
  136. ## no critic (ControlStructures::ProhibitUntilBlocks)
  137. check_options( $changes, \@data,
  138. { until => '1:2.0~rc2-1sarge2' }, 2,
  139. '1:2.0~rc2-1sarge1/1.5-1',
  140. 'until => "1:2.0~rc2-1sarge2"' );
  141. ## use critic
  142. #TODO: test combinations
  143. }
  144. if ($file eq "$datadir/fields") {
  145. my $str = $changes->format_range('dpkg', { all => 1 });
  146. my $expected = 'Source: fields
  147. Version: 2.0-0etch1
  148. Distribution: stable
  149. Urgency: high
  150. Maintainer: Frank Lichtenheld <frank@lichtenheld.de>
  151. Timestamp: 1200235759
  152. Date: Sun, 13 Jan 2008 15:49:19 +0100
  153. Closes: 1000000 1111111 2222222
  154. Changes:
  155. fields (2.0-0etch1) stable; urgency=low
  156. .
  157. * Upload to stable (Closes: #1111111, #2222222)
  158. * Fix more stuff. (LP: #54321, #2424242)
  159. .
  160. fields (2.0-1) unstable frozen; urgency=medium
  161. .
  162. [ Frank Lichtenheld ]
  163. * Upload to unstable (Closes: #1111111, #2222222)
  164. * Fix stuff. (LP: #12345, #424242)
  165. .
  166. [ Raphaël Hertzog ]
  167. * New upstream release.
  168. - implements a
  169. - implements b
  170. * Update S-V.
  171. .
  172. fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
  173. .
  174. * Beta
  175. .
  176. fields (1.0) experimental; urgency=high,xb-userfield2=foobar
  177. .
  178. * First upload (Closes: #1000000)
  179. Xb-Userfield2: foobar
  180. Xc-Userfield: foobar
  181. ';
  182. if ($vendor eq 'Ubuntu') {
  183. $expected =~ s/^(Closes:.*)/$1\nLaunchpad-Bugs-Fixed: 12345 54321 424242 2424242/m;
  184. }
  185. cmp_ok($str, 'eq', $expected, 'fields handling');
  186. $str = $changes->format_range('dpkg', { offset => 1, count => 2 });
  187. $expected = 'Source: fields
  188. Version: 2.0-1
  189. Distribution: unstable frozen
  190. Urgency: medium
  191. Maintainer: Frank Lichtenheld <djpig@debian.org>
  192. Timestamp: 1200149359
  193. Date: Sun, 12 Jan 2008 15:49:19 +0100
  194. Closes: 1111111 2222222
  195. Changes:
  196. fields (2.0-1) unstable frozen; urgency=medium
  197. .
  198. [ Frank Lichtenheld ]
  199. * Upload to unstable (Closes: #1111111, #2222222)
  200. * Fix stuff. (LP: #12345, #424242)
  201. .
  202. [ Raphaël Hertzog ]
  203. * New upstream release.
  204. - implements a
  205. - implements b
  206. * Update S-V.
  207. .
  208. fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
  209. .
  210. * Beta
  211. Xc-Userfield: foobar
  212. ';
  213. if ($vendor eq 'Ubuntu') {
  214. $expected =~ s/^(Closes:.*)/$1\nLaunchpad-Bugs-Fixed: 12345 424242/m;
  215. }
  216. cmp_ok($str, 'eq', $expected, 'fields handling 2');
  217. $str = $changes->format_range('rfc822', { offset => 2, count => 2 });
  218. $expected = 'Source: fields
  219. Version: 2.0~b1-1
  220. Distribution: unstable
  221. Urgency: low
  222. Maintainer: Frank Lichtenheld <frank@lichtenheld.de>
  223. Timestamp: 1200062959
  224. Date: Sun, 11 Jan 2008 15:49:19 +0100
  225. Changes:
  226. fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
  227. .
  228. * Beta
  229. Xc-Userfield: foobar
  230. Source: fields
  231. Version: 1.0
  232. Distribution: experimental
  233. Urgency: high
  234. Maintainer: Frank Lichtenheld <djpig@debian.org>
  235. Timestamp: 1199976559
  236. Date: Sun, 10 Jan 2008 15:49:19 +0100
  237. Closes: 1000000
  238. Changes:
  239. fields (1.0) experimental; urgency=high,xb-userfield2=foobar
  240. .
  241. * First upload (Closes: #1000000)
  242. Xb-Userfield2: foobar
  243. ';
  244. cmp_ok($str, 'eq', $expected, 'fields handling 3');
  245. # Test Dpkg::Changelog::Entry methods
  246. is($data[1]->get_version(), '2.0-1', 'get_version');
  247. is($data[1]->get_source(), 'fields', 'get_source');
  248. is(scalar $data[1]->get_distributions(), 'unstable', 'get_distribution');
  249. is(join('|', $data[1]->get_distributions()), 'unstable|frozen',
  250. 'get_distributions');
  251. is($data[3]->get_optional_fields(),
  252. "Urgency: high\nCloses: 1000000\nXb-Userfield2: foobar\n",
  253. 'get_optional_fields');
  254. is($data[1]->get_maintainer(), 'Frank Lichtenheld <djpig@debian.org>',
  255. 'get_maintainer');
  256. is($data[1]->get_timestamp(), 'Sun, 12 Jan 2008 15:49:19 +0100',
  257. 'get_timestamp');
  258. my @items = $data[1]->get_change_items();
  259. is($items[0], " [ Frank Lichtenheld ]\n", 'change items 1');
  260. is($items[4], ' * New upstream release.
  261. - implements a
  262. - implements b
  263. ', 'change items 2');
  264. is($items[5], " * Update S-V.\n", 'change items 3');
  265. }
  266. if ($file eq "$datadir/date-format") {
  267. is($data[0]->get_timestamp(), '01 Jul 2100 23:59:59 -1200',
  268. 'get date w/o DoW, and negative timezone offset');
  269. is($data[1]->get_timestamp(), 'Tue, 27 Feb 2050 12:00:00 +1245',
  270. 'get date w/ DoW, and positive timezone offset');
  271. is($data[2]->get_timestamp(), 'Mon, 01 Jan 2000 00:00:00 +0000',
  272. 'get date w/ DoW, and zero timezone offset');
  273. }
  274. if ($file eq "$datadir/regressions") {
  275. my $f = ($changes->format_range('dpkg'))[0];
  276. is("$f->{Version}", '0', 'version 0 correctly parsed');
  277. }
  278. SKIP: {
  279. skip('avoid spurious warning with only one entry', 2)
  280. if @data == 1;
  281. my $oldest_version = $data[-1]->{Version};
  282. $str = $changes->format_range('dpkg', { since => $oldest_version });
  283. $str = $changes->format_range('rfc822');
  284. ok(1, 'TODO check rfc822 output');
  285. $str = $changes->format_range('rfc822', { since => $oldest_version });
  286. ok(1, 'TODO check rfc822 output with ranges');
  287. }
  288. }
  289. foreach my $test (([ "$datadir/misplaced-tz", 6 ],
  290. [ "$datadir/unreleased", 5, 7 ])) {
  291. my $file = shift @$test;
  292. my $changes = Dpkg::Changelog::Debian->new(verbose => 0);
  293. $changes->load($file);
  294. my @errors = $changes->get_parse_errors();
  295. ok(@errors, 'errors occured');
  296. is_deeply( [ map { $_->[1] } @errors ], $test, 'check line numbers' );
  297. }