Dpkg.pm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. );
  27. our %EXPORT_TAGS = (
  28. needs => [ qw(
  29. test_needs_author
  30. test_needs_module
  31. test_needs_command
  32. test_needs_srcdir_switch
  33. ) ],
  34. );
  35. use Exporter qw(import);
  36. use File::Find;
  37. use IPC::Cmd qw(can_run);
  38. use Test::More;
  39. sub test_get_perl_dirs
  40. {
  41. return qw(t src/t lib utils/t scripts dselect);
  42. }
  43. sub all_perl_files
  44. {
  45. my @files;
  46. my $scan_perl_files = sub {
  47. push @files, $File::Find::name if m/\.(pl|pm|t)$/;
  48. };
  49. find($scan_perl_files, test_get_perl_dirs());
  50. return @files;
  51. }
  52. sub test_needs_author
  53. {
  54. if (not $ENV{DPKG_DEVEL_MODE} and not $ENV{AUTHOR_TESTING}) {
  55. plan skip_all => 'developer test';
  56. }
  57. }
  58. sub test_needs_module
  59. {
  60. my ($module, @imports) = @_;
  61. my ($package) = caller;
  62. require version;
  63. my $version = '';
  64. if (@imports >= 1 and version::is_lax($imports[0])) {
  65. $version = shift @imports;
  66. }
  67. eval qq{
  68. package $package;
  69. use $module $version \@imports;
  70. 1;
  71. } or do {
  72. plan skip_all => "requires module $module $version";
  73. }
  74. }
  75. sub test_needs_command
  76. {
  77. my $command = shift;
  78. if (not can_run($command)) {
  79. plan skip_all => "requires command $command";
  80. }
  81. }
  82. sub test_needs_srcdir_switch
  83. {
  84. if (defined $ENV{srcdir}) {
  85. chdir $ENV{srcdir} or BAIL_OUT("cannot chdir to source directory: $!");
  86. }
  87. }
  88. 1;