Dpkg.pm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright © 2015 Guillem Jover <guillem@debian.org>
  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 <https://www.gnu.org/licenses/>.
  15. package Test::Dpkg;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.00';
  19. our @EXPORT_OK = qw(
  20. all_perl_files
  21. test_get_perl_dirs
  22. test_needs_author
  23. test_needs_module
  24. test_needs_command
  25. test_needs_srcdir_switch
  26. test_neutralize_checksums
  27. );
  28. our %EXPORT_TAGS = (
  29. needs => [ qw(
  30. test_needs_author
  31. test_needs_module
  32. test_needs_command
  33. test_needs_srcdir_switch
  34. ) ],
  35. );
  36. use Exporter qw(import);
  37. use File::Find;
  38. use IPC::Cmd qw(can_run);
  39. use Test::More;
  40. sub test_get_perl_dirs
  41. {
  42. return qw(t src/t lib utils/t scripts dselect);
  43. }
  44. sub all_perl_files
  45. {
  46. my @files;
  47. my $scan_perl_files = sub {
  48. push @files, $File::Find::name if m/\.(pl|pm|t)$/;
  49. };
  50. find($scan_perl_files, test_get_perl_dirs());
  51. return @files;
  52. }
  53. sub test_needs_author
  54. {
  55. if (not $ENV{DPKG_DEVEL_MODE} and not $ENV{AUTHOR_TESTING}) {
  56. plan skip_all => 'developer test';
  57. }
  58. }
  59. sub test_needs_module
  60. {
  61. my ($module, @imports) = @_;
  62. my ($package) = caller;
  63. require version;
  64. my $version = '';
  65. if (@imports >= 1 and version::is_lax($imports[0])) {
  66. $version = shift @imports;
  67. }
  68. eval qq{
  69. package $package;
  70. use $module $version \@imports;
  71. 1;
  72. } or do {
  73. plan skip_all => "requires module $module $version";
  74. }
  75. }
  76. sub test_needs_command
  77. {
  78. my $command = shift;
  79. if (not can_run($command)) {
  80. plan skip_all => "requires command $command";
  81. }
  82. }
  83. sub test_needs_srcdir_switch
  84. {
  85. if (defined $ENV{srcdir}) {
  86. chdir $ENV{srcdir} or BAIL_OUT("cannot chdir to source directory: $!");
  87. }
  88. }
  89. sub test_neutralize_checksums
  90. {
  91. my $filename = shift;
  92. my $filenamenew = "$filename.new";
  93. open my $fhnew, '>', $filenamenew or die;
  94. open my $fh, '<', $filename or die;
  95. while (<$fh>) {
  96. s/^ ([0-9a-f]{32,}) [1-9][0-9]* /q{ } . $1 =~ tr{0-9a-f}{0}r . q{ 0 }/e;
  97. s{^
  98. (
  99. \s
  100. # Digest
  101. 0{32,}
  102. \s
  103. # Size
  104. 0
  105. # Optional Section and Priority
  106. (?:\s[\w]*\s[\w]*)?
  107. \s
  108. # Filename (package and version)
  109. [^_]*_[^_]*
  110. )
  111. # Filename (architecture and extension)
  112. _[^.]*\.(buildinfo)
  113. $
  114. }{$1_20160101T123000z-00000000.$2}x;
  115. print { $fhnew } $_;
  116. }
  117. close $fh or die;
  118. close $fhnew or die;
  119. rename $filenamenew, $filename or die;
  120. }
  121. 1;