X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2Frgit.git;a=blobdiff_plain;f=t%2Flib%2FApp%2FRgit%2FTestUtils.pm;fp=t%2Flib%2FApp%2FRgit%2FTestUtils.pm;h=07b1bd7c4e5212260a9f9dcdf3c243cf7fe133b6;hp=0000000000000000000000000000000000000000;hb=e09a125b42420241c34529697c250ad795f1c59e;hpb=270525a9f32911ff55fc0ee7b263a9eb3cd0a3ce diff --git a/t/lib/App/Rgit/TestUtils.pm b/t/lib/App/Rgit/TestUtils.pm new file mode 100644 index 0000000..07b1bd7 --- /dev/null +++ b/t/lib/App/Rgit/TestUtils.pm @@ -0,0 +1,69 @@ +package App::Rgit::TestUtils; + +use strict; +use warnings; + +use Cwd qw/abs_path/; +use File::Temp qw/tempfile/; +use POSIX qw/WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG SIGINT SIGQUIT/; + +BEGIN { + no warnings 'redefine'; + *WIFEXITED = sub { 1 } unless eval { WIFEXITED(0); 1 }; + *WEXITSTATUS = sub { shift() >> 8 } unless eval { WEXITSTATUS(0); 1 }; + *WIFSIGNALED = sub { shift() & 127 } unless eval { WIFSIGNALED(0); 1 }; +} + +use base qw/Exporter/; + +our @EXPORT_OK = (qw/can_run_git/); + +sub can_run_git { + my ($fh, $filename) = tempfile(UNLINK => 1); + + my @ret = (1, ''); + +CHECK: + { + my @args = ( + abs_path($filename), + 'version', + ); + + my $git = 't/bin/git'; + if ($^O eq 'MSWin32') { + unless (-x $git) { + $git .= '.bat'; + unless (-x $git) { + @ret = (0, "no $git executable"); + last CHECK; + } + } + } else { + unless (-x $git) { + @ret = (0, "no $git executable"); + last CHECK; + } + } + + system { $git } $git, @args; + + if ($? == -1) { + @ret = (0, $! || "unknown"); + last CHECK; + } + + my $status; + $status = WEXITSTATUS($?) if WIFEXITED($?); + + if (WIFSIGNALED($?)) { + @ret = (0, 'process recieved signal ' . WTERMSIG($?)); + } elsif ($status) { + @ret = (0, "process exited with code $status"); + } + } + + return wantarray ? @ret : $ret[0]; +} + +1;