600_Dpkg_Changelog.t 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # -*- perl -*-
  2. use strict;
  3. use warnings;
  4. use File::Basename;
  5. BEGIN {
  6. my $no_examples = 2;
  7. my $no_err_examples = 1;
  8. my $no_tests = $no_examples * 4
  9. + $no_err_examples * 2
  10. + 48;
  11. require Test::More;
  12. import Test::More tests => $no_tests;
  13. }
  14. BEGIN {
  15. use_ok('Dpkg::Changelog');
  16. use_ok('Dpkg::Changelog::Debian');
  17. };
  18. my $srcdir = $ENV{srcdir} || '.';
  19. $srcdir .= '/t/600_Dpkg_Changelog';
  20. #########################
  21. my $test = Dpkg::Changelog::Debian->init( { infile => '/nonexistant',
  22. quiet => 1 } );
  23. ok( !defined($test), "fatal parse errors lead to init() returning undef");
  24. my $save_data;
  25. foreach my $file ("$srcdir/countme", "$srcdir/shadow") {
  26. my $changes = Dpkg::Changelog::Debian->init( { infile => $file,
  27. quiet => 1 } );
  28. my $errors = $changes->get_parse_errors();
  29. my $basename = basename( $file );
  30. # use Data::Dumper;
  31. # diag(Dumper($changes));
  32. ok( !$errors, "Parse example changelog $file without errors" );
  33. my @data = $changes->data;
  34. ok( @data, "data is not empty" );
  35. my $str = $changes->dpkg_str();
  36. # is( $str, `dpkg-parsechangelog -l$file`,
  37. # 'Output of dpkg_str equal to output of dpkg-parsechangelog' );
  38. if ($file eq "$srcdir/countme") {
  39. $save_data = $changes->rfc822_str({ all => 1 });
  40. # test range options
  41. cmp_ok( @data, '==', 7, "no options -> count" );
  42. my $all_versions = join( '/', map { $_->Version } @data);
  43. sub check_options {
  44. my ($changes, $data, $options, $count, $versions,
  45. $check_name) = @_;
  46. my @cnt = $changes->data( $options );
  47. cmp_ok( @cnt, '==', $count, "$check_name -> count" );
  48. if ($count == @$data) {
  49. is_deeply( \@cnt, $data, "$check_name -> returns all" );
  50. } else {
  51. is( join( "/", map { $_->Version } @cnt),
  52. $versions, "$check_name -> versions" );
  53. }
  54. }
  55. check_options( $changes, \@data,
  56. { count => 3 }, 3, '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  57. 'positve count' );
  58. check_options( $changes, \@data,
  59. { count => -3 }, 3,
  60. '1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1/1.5-1',
  61. 'negative count' );
  62. check_options( $changes, \@data,
  63. { count => 1 }, 1, '2:2.0-1',
  64. 'count 1' );
  65. check_options( $changes, \@data,
  66. { count => 1, default_all => 1 }, 1, '2:2.0-1',
  67. 'count 1 (d_a 1)' );
  68. check_options( $changes, \@data,
  69. { count => -1 }, 1, '1.5-1',
  70. 'count -1' );
  71. check_options( $changes, \@data,
  72. { count => 3, offset => 2 }, 3,
  73. '1:2.0~rc2-2/1:2.0~rc2-1sarge3/1:2.0~rc2-1sarge2',
  74. 'positve count + positive offset' );
  75. check_options( $changes, \@data,
  76. { count => -3, offset => 4 }, 3,
  77. '1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  78. 'negative count + positive offset' );
  79. check_options( $changes, \@data,
  80. { count => 4, offset => 5 }, 2,
  81. '1:2.0~rc2-1sarge1/1.5-1',
  82. 'positve count + positive offset (>max)' );
  83. check_options( $changes, \@data,
  84. { count => -4, offset => 2 }, 2,
  85. '2:2.0-1/1:2.0~rc2-3',
  86. 'negative count + positive offset (<0)' );
  87. check_options( $changes, \@data,
  88. { count => 3, offset => -4 }, 3,
  89. '1:2.0~rc2-1sarge3/1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1',
  90. 'positve count + negative offset' );
  91. check_options( $changes, \@data,
  92. { count => -3, offset => -3 }, 3,
  93. '1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  94. 'negative count + negative offset' );
  95. check_options( $changes, \@data,
  96. { count => 5, offset => -2 }, 2,
  97. '1:2.0~rc2-1sarge1/1.5-1',
  98. 'positve count + negative offset (>max)' );
  99. check_options( $changes, \@data,
  100. { count => -5, offset => -4 }, 3,
  101. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  102. 'negative count + negative offset (<0)' );
  103. check_options( $changes, \@data,
  104. { count => 7 }, 7, '',
  105. 'count 7 (max)' );
  106. check_options( $changes, \@data,
  107. { count => -7 }, 7, '',
  108. 'count -7 (-max)' );
  109. check_options( $changes, \@data,
  110. { count => 10 }, 7, '',
  111. 'count 10 (>max)' );
  112. check_options( $changes, \@data,
  113. { count => -10 }, 7, '',
  114. 'count -10 (<-max)' );
  115. check_options( $changes, \@data,
  116. { from => '1:2.0~rc2-1sarge3' }, 4,
  117. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  118. 'from => "1:2.0~rc2-1sarge3"' );
  119. check_options( $changes, \@data,
  120. { since => '1:2.0~rc2-1sarge3' }, 3,
  121. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  122. 'since => "1:2.0~rc2-1sarge3"' );
  123. check_options( $changes, \@data,
  124. { to => '1:2.0~rc2-1sarge2' }, 3,
  125. '1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1/1.5-1',
  126. 'to => "1:2.0~rc2-1sarge2"' );
  127. check_options( $changes, \@data,
  128. { until => '1:2.0~rc2-1sarge2' }, 2,
  129. '1:2.0~rc2-1sarge1/1.5-1',
  130. 'until => "1:2.0~rc2-1sarge2"' );
  131. #TODO: test combinations
  132. }
  133. # if ($file eq 'Changes') {
  134. # my $v = $data[0]->Version;
  135. # $v =~ s/[a-z]$//;
  136. # cmp_ok( $v, 'eq', $Parse::DebianChangelog::VERSION,
  137. # 'version numbers in module and Changes match' );
  138. # }
  139. my $oldest_version = $data[-1]->Version;
  140. $str = $changes->dpkg_str({ since => $oldest_version });
  141. # is( $str, `dpkg-parsechangelog -v$oldest_version -l$file`,
  142. # 'Output of dpkg_str equal to output of dpkg-parsechangelog' )
  143. # or diag("oldest_version=$oldest_version");
  144. $str = $changes->rfc822_str();
  145. ok( 1 );
  146. $str = $changes->rfc822_str({ since => $oldest_version });
  147. ok( 1 );
  148. }
  149. open CHANGES, '<', "$srcdir/countme";
  150. my $string = join('',<CHANGES>);
  151. my $str_changes = Dpkg::Changelog::Debian->init( { instring => $string,
  152. quiet => 1 } );
  153. my $errors = $str_changes->get_parse_errors();
  154. ok( !$errors,
  155. "Parse example changelog $srcdir/countme without errors from string" );
  156. my $str_data = $str_changes->rfc822_str({ all => 1 });
  157. is( $str_data, $save_data,
  158. "Compare result of parse from string with result of parse from file" );
  159. foreach my $test (( [ "$srcdir/misplaced-tz", 6 ])) {
  160. my $file = shift @$test;
  161. my $changes = Dpkg::Changelog::Debian->init( { infile => $file,
  162. quiet => 1 } );
  163. my @errors = $changes->get_parse_errors();
  164. ok( @errors, 'errors occoured' );
  165. is_deeply( [ map { $_->[1] } @errors ], $test, 'check line numbers' );
  166. }