600_Dpkg_Changelog.t 9.8 KB

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