600_Dpkg_Changelog.t 9.9 KB

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