Dpkg_Changelog.t 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 => 84;
  18. use File::Basename;
  19. use Dpkg::File;
  20. BEGIN {
  21. use_ok('Dpkg::Changelog');
  22. use_ok('Dpkg::Changelog::Debian');
  23. use_ok('Dpkg::Vendor', qw(get_current_vendor));
  24. };
  25. my $srcdir = $ENV{srcdir} || '.';
  26. my $datadir = $srcdir . '/t/Dpkg_Changelog';
  27. my $vendor = get_current_vendor();
  28. #########################
  29. foreach my $file ("$datadir/countme", "$datadir/shadow", "$datadir/fields",
  30. "$datadir/regressions") {
  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->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. Date: Sun, 13 Jan 2008 15:49:19 +0100
  152. Closes: 1000000 1111111 2222222
  153. Changes:
  154. fields (2.0-0etch1) stable; urgency=low
  155. .
  156. * Upload to stable (Closes: #1111111, #2222222)
  157. * Fix more stuff. (LP: #54321, #2424242)
  158. .
  159. fields (2.0-1) unstable frozen; urgency=medium
  160. .
  161. [ Frank Lichtenheld ]
  162. * Upload to unstable (Closes: #1111111, #2222222)
  163. * Fix stuff. (LP: #12345, #424242)
  164. .
  165. [ Raphaël Hertzog ]
  166. * New upstream release.
  167. - implements a
  168. - implements b
  169. * Update S-V.
  170. .
  171. fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
  172. .
  173. * Beta
  174. .
  175. fields (1.0) experimental; urgency=high,xb-userfield2=foobar
  176. .
  177. * First upload (Closes: #1000000)
  178. Xb-Userfield2: foobar
  179. Xc-Userfield: foobar
  180. ';
  181. if ($vendor eq 'Ubuntu') {
  182. $expected =~ s/^(Closes:.*)/$1\nLaunchpad-Bugs-Fixed: 12345 54321 424242 2424242/m;
  183. }
  184. cmp_ok($str, 'eq', $expected, 'fields handling');
  185. $str = $changes->dpkg({ offset => 1, count => 2 });
  186. $expected = 'Source: fields
  187. Version: 2.0-1
  188. Distribution: unstable frozen
  189. Urgency: medium
  190. Maintainer: Frank Lichtenheld <djpig@debian.org>
  191. Date: Sun, 12 Jan 2008 15:49:19 +0100
  192. Closes: 1111111 2222222
  193. Changes:
  194. fields (2.0-1) unstable frozen; urgency=medium
  195. .
  196. [ Frank Lichtenheld ]
  197. * Upload to unstable (Closes: #1111111, #2222222)
  198. * Fix stuff. (LP: #12345, #424242)
  199. .
  200. [ Raphaël Hertzog ]
  201. * New upstream release.
  202. - implements a
  203. - implements b
  204. * Update S-V.
  205. .
  206. fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
  207. .
  208. * Beta
  209. Xc-Userfield: foobar
  210. ';
  211. if ($vendor eq 'Ubuntu') {
  212. $expected =~ s/^(Closes:.*)/$1\nLaunchpad-Bugs-Fixed: 12345 424242/m;
  213. }
  214. cmp_ok($str, 'eq', $expected, 'fields handling 2');
  215. $str = $changes->rfc822({ offset => 2, count => 2 });
  216. $expected = 'Source: fields
  217. Version: 2.0~b1-1
  218. Distribution: unstable
  219. Urgency: low
  220. Maintainer: Frank Lichtenheld <frank@lichtenheld.de>
  221. Date: Sun, 11 Jan 2008 15:49:19 +0100
  222. Changes:
  223. fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
  224. .
  225. * Beta
  226. Xc-Userfield: foobar
  227. Source: fields
  228. Version: 1.0
  229. Distribution: experimental
  230. Urgency: high
  231. Maintainer: Frank Lichtenheld <djpig@debian.org>
  232. Date: Sun, 10 Jan 2008 15:49:19 +0100
  233. Closes: 1000000
  234. Changes:
  235. fields (1.0) experimental; urgency=high,xb-userfield2=foobar
  236. .
  237. * First upload (Closes: #1000000)
  238. Xb-Userfield2: foobar
  239. ';
  240. cmp_ok($str, 'eq', $expected, 'fields handling 3');
  241. # Test Dpkg::Changelog::Entry methods
  242. is($data[1]->get_version(), '2.0-1', 'get_version');
  243. is($data[1]->get_source(), 'fields', 'get_source');
  244. is(scalar $data[1]->get_distributions(), 'unstable', 'get_distribution');
  245. is(join('|', $data[1]->get_distributions()), 'unstable|frozen',
  246. 'get_distributions');
  247. is($data[3]->get_optional_fields(),
  248. "Urgency: high\nCloses: 1000000\nXb-Userfield2: foobar\n",
  249. 'get_optional_fields');
  250. is($data[1]->get_maintainer(), 'Frank Lichtenheld <djpig@debian.org>',
  251. 'get_maintainer');
  252. is($data[1]->get_timestamp(), 'Sun, 12 Jan 2008 15:49:19 +0100',
  253. 'get_timestamp');
  254. my @items = $data[1]->get_change_items();
  255. is($items[0], " [ Frank Lichtenheld ]\n", 'change items 1');
  256. is($items[4], ' * New upstream release.
  257. - implements a
  258. - implements b
  259. ', 'change items 2');
  260. is($items[5], " * Update S-V.\n", 'change items 3');
  261. }
  262. if ($file eq "$datadir/regressions") {
  263. my $f = $changes->dpkg();
  264. is("$f->{Version}", '0', 'version 0 correctly parsed');
  265. }
  266. SKIP: {
  267. skip('avoid spurious warning with only one entry', 2)
  268. if @data == 1;
  269. my $oldest_version = $data[-1]->{Version};
  270. $str = $changes->dpkg({ since => $oldest_version });
  271. $str = $changes->rfc822();
  272. ok(1, 'TODO check rfc822 output');
  273. $str = $changes->rfc822({ since => $oldest_version });
  274. ok(1, 'TODO check rfc822 output with ranges');
  275. }
  276. }
  277. foreach my $test (( [ "$datadir/misplaced-tz", 6 ])) {
  278. my $file = shift @$test;
  279. my $changes = Dpkg::Changelog::Debian->new(verbose => 0);
  280. $changes->load($file);
  281. my @errors = $changes->get_parse_errors();
  282. ok(@errors, 'errors occured');
  283. is_deeply( [ map { $_->[1] } @errors ], $test, 'check line numbers' );
  284. }