600_Dpkg_Changelog.t 7.4 KB

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