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