]> git.vpit.fr Git - perl/modules/rgit.git/blob - t/lib/App/Rgit/TestUtils.pm
Yet less namespace pollution
[perl/modules/rgit.git] / t / lib / App / Rgit / TestUtils.pm
1 package App::Rgit::TestUtils;
2
3 use strict;
4 use warnings;
5
6 use Cwd        qw/abs_path/;
7 use File::Temp qw/tempfile/;
8 use File::Spec (); # curdir, catfile
9 use POSIX      qw/WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG/;
10
11 BEGIN {
12  no warnings 'redefine';
13  *WIFEXITED   = sub { 1 }             unless eval { WIFEXITED(0);   1 };
14  *WEXITSTATUS = sub { shift() >> 8 }  unless eval { WEXITSTATUS(0); 1 };
15  *WIFSIGNALED = sub { shift() & 127 } unless eval { WIFSIGNALED(0); 1 };
16  *WTERMSIG    = sub { shift() & 127 } unless eval { WTERMSIG(0);    1 };
17 }
18
19 use base qw/Exporter/;
20
21 our @EXPORT_OK = (qw/can_run_git/);
22
23 sub can_run_git {
24  my ($fh, $filename) = tempfile(UNLINK => 1);
25
26  my @ret = (1, '');
27
28 TRY:
29  {
30   my @args = (
31    abs_path($filename),
32    'version',
33   );
34
35   my $git = File::Spec->catfile(File::Spec->curdir, qw/t bin git/);
36   if ($^O eq 'MSWin32') {
37    unless (-x $git) {
38     $git .= '.bat';
39     unless (-x $git) {
40      @ret = (0, "no $git executable");
41      last TRY;
42     }
43    }
44   } else {
45    unless (-x $git) {
46     @ret = (0, "no $git executable");
47     last TRY;
48    }
49   }
50
51   system { $git } $git, @args;
52
53   if ($? == -1) {
54    @ret = (0, $! || "unknown");
55    last TRY;
56   }
57
58   my $status;
59   $status = WEXITSTATUS($?) if WIFEXITED($?);
60
61   if (WIFSIGNALED($?)) {
62    @ret = (0, 'process recieved signal ' . WTERMSIG($?));
63   } elsif ($status) {
64    @ret = (0, "process exited with code $status");
65   }
66  }
67
68  return wantarray ? @ret : $ret[0];
69 }
70
71 1;