600_Dpkg_Changelog.t 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # -*- perl -*-
  2. use strict;
  3. use warnings;
  4. use File::Basename;
  5. BEGIN {
  6. my $no_examples = 3;
  7. my $no_err_examples = 1;
  8. my $no_tests = $no_examples * 4
  9. + $no_err_examples * 2
  10. + 24 # countme
  11. + 2 # fields
  12. + 24;
  13. require Test::More;
  14. import Test::More tests => $no_tests;
  15. }
  16. BEGIN {
  17. use_ok('Dpkg::Changelog');
  18. use_ok('Dpkg::Changelog::Debian');
  19. };
  20. my $srcdir = $ENV{srcdir} || '.';
  21. $srcdir .= '/t/600_Dpkg_Changelog';
  22. #########################
  23. my $test = Dpkg::Changelog::Debian->init( { infile => '/nonexistant',
  24. quiet => 1 } );
  25. ok( !defined($test), "fatal parse errors lead to init() returning undef");
  26. my $save_data;
  27. foreach my $file ("$srcdir/countme", "$srcdir/shadow", "$srcdir/fields") {
  28. my $changes = Dpkg::Changelog::Debian->init( { infile => $file,
  29. quiet => 1 } );
  30. my $errors = $changes->get_parse_errors();
  31. my $basename = basename( $file );
  32. # use Data::Dumper;
  33. # diag(Dumper($changes));
  34. ok( !$errors, "Parse example changelog $file without errors" );
  35. my @data = $changes->data;
  36. ok( @data, "data is not empty" );
  37. my $str = $changes->dpkg_str();
  38. # is( $str, `dpkg-parsechangelog -l$file`,
  39. # 'Output of dpkg_str equal to output of dpkg-parsechangelog' );
  40. if ($file eq "$srcdir/countme") {
  41. $save_data = $changes->rfc822_str({ all => 1 });
  42. # test range options
  43. cmp_ok( @data, '==', 7, "no options -> count" );
  44. my $all_versions = join( '/', map { $_->{Version} } @data);
  45. sub check_options {
  46. my ($changes, $data, $options, $count, $versions,
  47. $check_name) = @_;
  48. my @cnt = $changes->data( $options );
  49. cmp_ok( @cnt, '==', $count, "$check_name -> count" );
  50. if ($count == @$data) {
  51. is_deeply( \@cnt, $data, "$check_name -> returns all" );
  52. } else {
  53. is( join( "/", map { $_->{Version} } @cnt),
  54. $versions, "$check_name -> versions" );
  55. }
  56. }
  57. check_options( $changes, \@data,
  58. { count => 3 }, 3, '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  59. 'positve count' );
  60. check_options( $changes, \@data,
  61. { count => -3 }, 3,
  62. '1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1/1.5-1',
  63. 'negative count' );
  64. check_options( $changes, \@data,
  65. { count => 1 }, 1, '2:2.0-1',
  66. 'count 1' );
  67. check_options( $changes, \@data,
  68. { count => 1, default_all => 1 }, 1, '2:2.0-1',
  69. 'count 1 (d_a 1)' );
  70. check_options( $changes, \@data,
  71. { count => -1 }, 1, '1.5-1',
  72. 'count -1' );
  73. check_options( $changes, \@data,
  74. { count => 3, offset => 2 }, 3,
  75. '1:2.0~rc2-2/1:2.0~rc2-1sarge3/1:2.0~rc2-1sarge2',
  76. 'positve count + positive offset' );
  77. check_options( $changes, \@data,
  78. { count => -3, offset => 4 }, 3,
  79. '1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  80. 'negative count + positive offset' );
  81. check_options( $changes, \@data,
  82. { count => 4, offset => 5 }, 2,
  83. '1:2.0~rc2-1sarge1/1.5-1',
  84. 'positve count + positive offset (>max)' );
  85. check_options( $changes, \@data,
  86. { count => -4, offset => 2 }, 2,
  87. '2:2.0-1/1:2.0~rc2-3',
  88. 'negative count + positive offset (<0)' );
  89. check_options( $changes, \@data,
  90. { count => 3, offset => -4 }, 3,
  91. '1:2.0~rc2-1sarge3/1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1',
  92. 'positve count + negative offset' );
  93. check_options( $changes, \@data,
  94. { count => -3, offset => -3 }, 3,
  95. '1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  96. 'negative count + negative offset' );
  97. check_options( $changes, \@data,
  98. { count => 5, offset => -2 }, 2,
  99. '1:2.0~rc2-1sarge1/1.5-1',
  100. 'positve count + negative offset (>max)' );
  101. check_options( $changes, \@data,
  102. { count => -5, offset => -4 }, 3,
  103. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  104. 'negative count + negative offset (<0)' );
  105. check_options( $changes, \@data,
  106. { count => 7 }, 7, '',
  107. 'count 7 (max)' );
  108. check_options( $changes, \@data,
  109. { count => -7 }, 7, '',
  110. 'count -7 (-max)' );
  111. check_options( $changes, \@data,
  112. { count => 10 }, 7, '',
  113. 'count 10 (>max)' );
  114. check_options( $changes, \@data,
  115. { count => -10 }, 7, '',
  116. 'count -10 (<-max)' );
  117. check_options( $changes, \@data,
  118. { from => '1:2.0~rc2-1sarge3' }, 4,
  119. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  120. 'from => "1:2.0~rc2-1sarge3"' );
  121. check_options( $changes, \@data,
  122. { since => '1:2.0~rc2-1sarge3' }, 3,
  123. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  124. 'since => "1:2.0~rc2-1sarge3"' );
  125. check_options( $changes, \@data,
  126. { to => '1:2.0~rc2-1sarge2' }, 3,
  127. '1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1/1.5-1',
  128. 'to => "1:2.0~rc2-1sarge2"' );
  129. check_options( $changes, \@data,
  130. { until => '1:2.0~rc2-1sarge2' }, 2,
  131. '1:2.0~rc2-1sarge1/1.5-1',
  132. 'until => "1:2.0~rc2-1sarge2"' );
  133. #TODO: test combinations
  134. }
  135. if ($file eq "$srcdir/fields") {
  136. my $str = $changes->dpkg_str({ all => 1 });
  137. my $expected = 'Source: fields
  138. Version: 2.0-0etch1
  139. Distribution: stable
  140. Urgency: high
  141. Maintainer: Frank Lichtenheld <frank@lichtenheld.de>
  142. Date: Sun, 13 Jan 2008 15:49:19 +0100
  143. Closes: 1000000 1111111 1111111 2222222 2222222
  144. Changes:
  145. fields (2.0-0etch1) stable; urgency=low
  146. .
  147. * Upload to stable (Closes: #1111111, #2222222)
  148. .
  149. fields (2.0-1) unstable; urgency=medium
  150. .
  151. * Upload to unstable (Closes: #1111111, #2222222)
  152. .
  153. fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
  154. .
  155. * Beta
  156. .
  157. fields (1.0) experimental; urgency=high
  158. .
  159. * First upload (Closes: #1000000)
  160. Xc-Userfield: foobar
  161. ';
  162. cmp_ok($str,'eq',$expected,"fields handling");
  163. $str = $changes->dpkg_str({ offset => 1, count => 2 });
  164. $expected = 'Source: fields
  165. Version: 2.0-1
  166. Distribution: unstable
  167. Urgency: medium
  168. Maintainer: Frank Lichtenheld <djpig@debian.org>
  169. Date: Sun, 12 Jan 2008 15:49:19 +0100
  170. Closes: 1111111 2222222
  171. Changes:
  172. fields (2.0-1) unstable; urgency=medium
  173. .
  174. * Upload to unstable (Closes: #1111111, #2222222)
  175. .
  176. fields (2.0~b1-1) unstable; urgency=low,xc-userfield=foobar
  177. .
  178. * Beta
  179. Xc-Userfield: foobar
  180. ';
  181. cmp_ok($str,'eq',$expected,"fields handling 2");
  182. }
  183. # if ($file eq 'Changes') {
  184. # my $v = $data[0]->Version;
  185. # $v =~ s/[a-z]$//;
  186. # cmp_ok( $v, 'eq', $Parse::DebianChangelog::VERSION,
  187. # 'version numbers in module and Changes match' );
  188. # }
  189. my $oldest_version = $data[-1]->{Version};
  190. $str = $changes->dpkg_str({ since => $oldest_version });
  191. # is( $str, `dpkg-parsechangelog -v$oldest_version -l$file`,
  192. # 'Output of dpkg_str equal to output of dpkg-parsechangelog' )
  193. # or diag("oldest_version=$oldest_version");
  194. $str = $changes->rfc822_str();
  195. ok( 1 );
  196. $str = $changes->rfc822_str({ since => $oldest_version });
  197. ok( 1 );
  198. }
  199. open CHANGES, '<', "$srcdir/countme";
  200. my $string = join('',<CHANGES>);
  201. my $str_changes = Dpkg::Changelog::Debian->init( { instring => $string,
  202. quiet => 1 } );
  203. my $errors = $str_changes->get_parse_errors();
  204. ok( !$errors,
  205. "Parse example changelog $srcdir/countme without errors from string" );
  206. my $str_data = $str_changes->rfc822_str({ all => 1 });
  207. is( $str_data, $save_data,
  208. "Compare result of parse from string with result of parse from file" );
  209. foreach my $test (( [ "$srcdir/misplaced-tz", 6 ])) {
  210. my $file = shift @$test;
  211. my $changes = Dpkg::Changelog::Debian->init( { infile => $file,
  212. quiet => 1 } );
  213. my @errors = $changes->get_parse_errors();
  214. ok( @errors, 'errors occoured' );
  215. is_deeply( [ map { $_->[1] } @errors ], $test, 'check line numbers' );
  216. }