600_Dpkg_Changelog.t 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # -*- perl -*-
  2. # Before `make install' is performed this script should be runnable with
  3. # `make test'. After `make install' it should work as `perl Parse-DebianChangelog.t'
  4. #########################
  5. # change 'tests => 1' to 'tests => last_test_to_print';
  6. use strict;
  7. use warnings;
  8. use File::Basename;
  9. use XML::Simple;
  10. BEGIN {
  11. my $no_examples = 3;
  12. my $no_err_examples = 1;
  13. my $no_tests = $no_examples * 13
  14. + $no_err_examples * 2
  15. + 49;
  16. require Test::More;
  17. import Test::More tests => $no_tests, ;
  18. }
  19. BEGIN {
  20. use_ok('Parse::DebianChangelog');
  21. use_ok('Parse::DebianChangelog::ChangesFilters', ':all' );
  22. };
  23. #########################
  24. my $test = Parse::DebianChangelog->init( { infile => '/nonexistant',
  25. quiet => 1 } );
  26. ok( !defined($test), "fatal parse errors lead to init() returning undef");
  27. my $save_data;
  28. foreach my $file (qw(Changes t/examples/countme t/examples/shadow)) {
  29. my $changes = Parse::DebianChangelog->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 $html_out = $changes->html( { outfile => "t/$basename.html.tmp",
  39. template => "tmpl/default.tmpl" } );
  40. is( `tidy -qe t/$basename.html.tmp 2>&1`, '',
  41. 'Generated HTML has no tidy errors' );
  42. ok( ($changes->delete_filter( 'html::changes',
  43. \&common_licenses ))[0]
  44. == \&common_licenses );
  45. ok( ! $changes->delete_filter( 'html::changes',
  46. \&common_licenses ) );
  47. $changes->html( { outfile => "t/$basename.html.tmp.2",
  48. template => "tmpl/default.tmpl" } );
  49. is( `tidy -qe t/$basename.html.tmp.2 2>&1`, '',
  50. 'Generated HTML has no tidy errors' );
  51. $changes->add_filter( 'html::changes',
  52. \&common_licenses );
  53. my $html_out2 = $changes->html();
  54. # remove timestamps since they will differ
  55. $html_out =~ s/Generated .*? by//go;
  56. $html_out2 =~ s/Generated .*? by//go;
  57. is( $html_out, $html_out2 )
  58. and unlink "t/$basename.html.tmp", "t/$basename.html.tmp.2";
  59. my $str = $changes->dpkg_str();
  60. is( $str, `dpkg-parsechangelog -l$file`,
  61. 'Output of dpkg_str equal to output of dpkg-parsechangelog' );
  62. if ($file eq 't/examples/countme') {
  63. $save_data = $changes->rfc822_str({ all => 1 });
  64. # test range options
  65. cmp_ok( @data, '==', 7, "no options -> count" );
  66. my $all_versions = join( '/', map { $_->Version } @data);
  67. sub check_options {
  68. my ($changes, $data, $options, $count, $versions,
  69. $check_name) = @_;
  70. my @cnt = $changes->data( $options );
  71. cmp_ok( @cnt, '==', $count, "$check_name -> count" );
  72. if ($count == @$data) {
  73. is_deeply( \@cnt, $data, "$check_name -> returns all" );
  74. } else {
  75. is( join( "/", map { $_->Version } @cnt),
  76. $versions, "$check_name -> versions" );
  77. }
  78. }
  79. check_options( $changes, \@data,
  80. { count => 3 }, 3, '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  81. 'positve count' );
  82. check_options( $changes, \@data,
  83. { count => -3 }, 3,
  84. '1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1/1.5-1',
  85. 'negative count' );
  86. check_options( $changes, \@data,
  87. { count => 1 }, 1, '2:2.0-1',
  88. 'count 1' );
  89. check_options( $changes, \@data,
  90. { count => 1, default_all => 1 }, 1, '2:2.0-1',
  91. 'count 1 (d_a 1)' );
  92. check_options( $changes, \@data,
  93. { count => -1 }, 1, '1.5-1',
  94. 'count -1' );
  95. check_options( $changes, \@data,
  96. { count => 3, offset => 2 }, 3,
  97. '1:2.0~rc2-2/1:2.0~rc2-1sarge3/1:2.0~rc2-1sarge2',
  98. 'positve count + positive offset' );
  99. check_options( $changes, \@data,
  100. { count => -3, offset => 4 }, 3,
  101. '1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  102. 'negative count + positive offset' );
  103. check_options( $changes, \@data,
  104. { count => 4, offset => 5 }, 2,
  105. '1:2.0~rc2-1sarge1/1.5-1',
  106. 'positve count + positive offset (>max)' );
  107. check_options( $changes, \@data,
  108. { count => -4, offset => 2 }, 2,
  109. '2:2.0-1/1:2.0~rc2-3',
  110. 'negative count + positive offset (<0)' );
  111. check_options( $changes, \@data,
  112. { count => 3, offset => -4 }, 3,
  113. '1:2.0~rc2-1sarge3/1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1',
  114. 'positve count + negative offset' );
  115. check_options( $changes, \@data,
  116. { count => -3, offset => -3 }, 3,
  117. '1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  118. 'negative count + negative offset' );
  119. check_options( $changes, \@data,
  120. { count => 5, offset => -2 }, 2,
  121. '1:2.0~rc2-1sarge1/1.5-1',
  122. 'positve count + negative offset (>max)' );
  123. check_options( $changes, \@data,
  124. { count => -5, offset => -4 }, 3,
  125. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  126. 'negative count + negative offset (<0)' );
  127. check_options( $changes, \@data,
  128. { count => 7 }, 7, '',
  129. 'count 7 (max)' );
  130. check_options( $changes, \@data,
  131. { count => -7 }, 7, '',
  132. 'count -7 (-max)' );
  133. check_options( $changes, \@data,
  134. { count => 10 }, 7, '',
  135. 'count 10 (>max)' );
  136. check_options( $changes, \@data,
  137. { count => -10 }, 7, '',
  138. 'count -10 (<-max)' );
  139. check_options( $changes, \@data,
  140. { from => '1:2.0~rc2-1sarge3' }, 4,
  141. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2/1:2.0~rc2-1sarge3',
  142. 'from => "1:2.0~rc2-1sarge3"' );
  143. check_options( $changes, \@data,
  144. { since => '1:2.0~rc2-1sarge3' }, 3,
  145. '2:2.0-1/1:2.0~rc2-3/1:2.0~rc2-2',
  146. 'since => "1:2.0~rc2-1sarge3"' );
  147. check_options( $changes, \@data,
  148. { to => '1:2.0~rc2-1sarge2' }, 3,
  149. '1:2.0~rc2-1sarge2/1:2.0~rc2-1sarge1/1.5-1',
  150. 'to => "1:2.0~rc2-1sarge2"' );
  151. check_options( $changes, \@data,
  152. { until => '1:2.0~rc2-1sarge2' }, 2,
  153. '1:2.0~rc2-1sarge1/1.5-1',
  154. 'until => "1:2.0~rc2-1sarge2"' );
  155. #TODO: test combinations
  156. }
  157. if ($file eq 'Changes') {
  158. my $v = $data[0]->Version;
  159. $v =~ s/[a-z]$//;
  160. cmp_ok( $v, 'eq', $Parse::DebianChangelog::VERSION,
  161. 'version numbers in module and Changes match' );
  162. }
  163. my $oldest_version = $data[-1]->Version;
  164. $str = $changes->dpkg_str({ since => $oldest_version });
  165. is( $str, `dpkg-parsechangelog -v$oldest_version -l$file`,
  166. 'Output of dpkg_str equal to output of dpkg-parsechangelog' )
  167. or diag("oldest_version=$oldest_version");
  168. $str = $changes->rfc822_str();
  169. ok( 1 );
  170. $str = $changes->rfc822_str({ since => $oldest_version });
  171. ok( 1 );
  172. $str = $changes->xml( { outfile => "t/$basename.xml.tmp" });
  173. ok( XMLin($str, ForceArray => {},
  174. KeyAttr => {} ), "can read in the result of XMLout" );
  175. ok( (-s "t/$basename.xml.tmp") == length($str) );
  176. unlink( "t/$basename.xml.tmp" );
  177. }
  178. open CHANGES, '<', 't/examples/countme';
  179. my $string = join('',<CHANGES>);
  180. my $str_changes = Parse::DebianChangelog->init( { instring => $string,
  181. quiet => 1 } );
  182. my $errors = $str_changes->get_parse_errors();
  183. ok( !$errors,
  184. "Parse example changelog t/examples/countme without errors from string" );
  185. my $str_data = $str_changes->rfc822_str({ all => 1 });
  186. is( $str_data, $save_data,
  187. "Compare result of parse from string with result of parse from file" );
  188. foreach my $test (( [ 't/examples/misplaced-tz', 6 ])) {
  189. my $file = shift @$test;
  190. my $changes = Parse::DebianChangelog->init( { infile => $file,
  191. quiet => 1 } );
  192. my @errors = $changes->get_parse_errors();
  193. ok( @errors, 'errors occoured' );
  194. is_deeply( [ map { $_->[1] } @errors ], $test, 'check line numbers' );
  195. }