Dpkg.pm 2.2 KB

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