From: Vincent Pit Date: Wed, 24 Feb 2010 11:20:35 +0000 (+0100) Subject: Revamp t/21-once.t X-Git-Tag: v0.07~17 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=e09a125b42420241c34529697c250ad795f1c59e;p=perl%2Fmodules%2Frgit.git Revamp t/21-once.t And skip it if the mock git executable can't be run --- diff --git a/MANIFEST b/MANIFEST index 8e4b603..ded8e84 100644 --- a/MANIFEST +++ b/MANIFEST @@ -27,3 +27,4 @@ t/99-kwalitee.t t/bin/git t/bin/git.bat t/lib/App/Rgit/Policy/Callback.pm +t/lib/App/Rgit/TestUtils.pm diff --git a/t/21-once.t b/t/21-once.t index 65accd2..93a184c 100644 --- a/t/21-once.t +++ b/t/21-once.t @@ -3,36 +3,51 @@ use strict; use warnings; -use Cwd qw/abs_path/; +use Cwd qw/abs_path/; use File::Temp qw/tempfile/; -use Test::More tests => 9 * 5; +use Test::More; use App::Rgit; +use lib 't/lib'; +use App::Rgit::TestUtils qw/can_run_git/; + +my ($can_run, $reason) = can_run_git; +if ($can_run) { + plan tests => 9 * 5; +} else { + plan skip_all => "Can't run the mock git executable on this system: $reason"; +} + my @expected = ( ([ [ qw/%n %g %w %b %%/ ] ]) x 5 ); -local $ENV{GIT_DIR} = 't'; +local $ENV{GIT_DIR} = 't'; local $ENV{GIT_EXEC_PATH} = abs_path('t/bin/git'); for my $cmd (qw/daemon gui help init version/) { my ($fh, $filename) = tempfile(UNLINK => 1); + my $ar = App::Rgit->new( git => $ENV{GIT_EXEC_PATH}, root => $ENV{GIT_DIR}, cmd => $cmd, - args => [ abs_path($filename), $cmd, qw/%n %g %w %b %%/ ] + args => [ abs_path($filename), $cmd, qw/%n %g %w %b %%/ ], ); - isnt($ar, undef, "once $cmd has a defined object"); + isa_ok $ar, 'App::Rgit', "once $cmd is an App::Rgit object"; + my $exit = $ar->run; - is($exit, 0, "once $cmd returned 0"); + is $exit, 0, "once $cmd returned 0"; + my @lines = sort split /\n/, do { local $/; <$fh> }; - is(@lines, 1, "once $cmd visited only one repo"); + is @lines, 1, "once $cmd visited only one repo"; + my $r = [ split /\|/, defined $lines[0] ? $lines[0] : '' ]; my $e = [ $cmd, qw/%n %g %w %b %%/ ]; s/[\r\n]*$// for @$r; - is($r->[$_], $e->[$_], "once $cmd argument $_ is ok") - for 0 .. 5; + for (0 .. 5) { + is $r->[$_], $e->[$_], "once $cmd argument $_ is ok"; + } } 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;