Dpkg.pm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_get_data_path
  23. test_needs_author
  24. test_needs_module
  25. test_needs_command
  26. test_needs_srcdir_switch
  27. test_neutralize_checksums
  28. );
  29. our %EXPORT_TAGS = (
  30. needs => [ qw(
  31. test_needs_author
  32. test_needs_module
  33. test_needs_command
  34. test_needs_srcdir_switch
  35. ) ],
  36. paths => [ qw(
  37. all_perl_files
  38. test_get_perl_dirs
  39. test_get_data_path
  40. ) ],
  41. );
  42. use Exporter qw(import);
  43. use File::Find;
  44. use IPC::Cmd qw(can_run);
  45. use Test::More;
  46. sub test_get_data_path
  47. {
  48. my $path = shift;
  49. my $srcdir = $ENV{srcdir} || '.';
  50. return "$srcdir/$path";
  51. }
  52. sub test_get_perl_dirs
  53. {
  54. return qw(t src/t lib utils/t scripts dselect);
  55. }
  56. sub all_perl_files
  57. {
  58. my @files;
  59. my $scan_perl_files = sub {
  60. push @files, $File::Find::name if m/\.(pl|pm|t)$/;
  61. };
  62. find($scan_perl_files, test_get_perl_dirs());
  63. return @files;
  64. }
  65. sub test_needs_author
  66. {
  67. if (not $ENV{DPKG_DEVEL_MODE} and not $ENV{AUTHOR_TESTING}) {
  68. plan skip_all => 'developer test';
  69. }
  70. }
  71. sub test_needs_module
  72. {
  73. my ($module, @imports) = @_;
  74. my ($package) = caller;
  75. require version;
  76. my $version = '';
  77. if (@imports >= 1 and version::is_lax($imports[0])) {
  78. $version = shift @imports;
  79. }
  80. eval qq{
  81. package $package;
  82. use $module $version \@imports;
  83. 1;
  84. } or do {
  85. plan skip_all => "requires module $module $version";
  86. }
  87. }
  88. sub test_needs_command
  89. {
  90. my $command = shift;
  91. if (not can_run($command)) {
  92. plan skip_all => "requires command $command";
  93. }
  94. }
  95. sub test_needs_srcdir_switch
  96. {
  97. if (defined $ENV{srcdir}) {
  98. chdir $ENV{srcdir} or BAIL_OUT("cannot chdir to source directory: $!");
  99. }
  100. }
  101. sub test_neutralize_checksums
  102. {
  103. my $filename = shift;
  104. my $filenamenew = "$filename.new";
  105. open my $fhnew, '>', $filenamenew or die;
  106. open my $fh, '<', $filename or die;
  107. while (<$fh>) {
  108. s/^ ([0-9a-f]{32,}) [1-9][0-9]* /q{ } . $1 =~ tr{0-9a-f}{0}r . q{ 0 }/e;
  109. print { $fhnew } $_;
  110. }
  111. close $fh or die;
  112. close $fhnew or die;
  113. rename $filenamenew, $filename or die;
  114. }
  115. 1;