]> git.vpit.fr Git - perl/modules/rgit.git/blob - t/lib/App/Rgit/TestUtils.pm
f702b25f57559b9eb43f60b7ee4cf5bc5da3a692
[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 }
17
18 use base qw/Exporter/;
19
20 our @EXPORT_OK = (qw/can_run_git/);
21
22 sub can_run_git {
23  my ($fh, $filename) = tempfile(UNLINK => 1);
24
25  my @ret = (1, '');
26
27 TRY:
28  {
29   my @args = (
30    abs_path($filename),
31    'version',
32   );
33
34   my $git = File::Spec->catfile(File::Spec->curdir, qw/t bin git/);
35   if ($^O eq 'MSWin32') {
36    unless (-x $git) {
37     $git .= '.bat';
38     unless (-x $git) {
39      @ret = (0, "no $git executable");
40      last TRY;
41     }
42    }
43   } else {
44    unless (-x $git) {
45     @ret = (0, "no $git executable");
46     last TRY;
47    }
48   }
49
50   system { $git } $git, @args;
51
52   if ($? == -1) {
53    @ret = (0, $! || "unknown");
54    last TRY;
55   }
56
57   my $status;
58   $status = WEXITSTATUS($?) if WIFEXITED($?);
59
60   if (WIFSIGNALED($?)) {
61    @ret = (0, 'process recieved signal ' . WTERMSIG($?));
62   } elsif ($status) {
63    @ret = (0, "process exited with code $status");
64   }
65  }
66
67  return wantarray ? @ret : $ret[0];
68 }
69
70 1;