From: Vincent Pit Date: Wed, 24 Feb 2010 11:29:45 +0000 (+0100) Subject: Revamp t/20-each.t X-Git-Tag: v0.07~16 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=88bfe285dbd8fe5cb70f28ff9ad9dee16631dd52;p=perl%2Fmodules%2Frgit.git Revamp t/20-each.t --- diff --git a/t/20-each.t b/t/20-each.t index ea70268..41e679a 100644 --- a/t/20-each.t +++ b/t/20-each.t @@ -3,33 +3,50 @@ use strict; use warnings; -use Cwd qw/cwd abs_path/; +use Cwd qw/cwd abs_path/; use File::Spec::Functions qw/catdir catfile/; -use File::Temp qw/tempfile tempdir/; +use File::Temp qw/tempfile tempdir/; -use Test::More tests => 2 + 2 * 4 + 12 * (3 + 1 + 3 + 6); +use Test::More; use App::Rgit::Utils qw/:codes/; use App::Rgit; use lib 't/lib'; +use App::Rgit::TestUtils qw/can_run_git/; use App::Rgit::Policy::Callback; +my ($can_run, $reason) = can_run_git; +if ($can_run) { + plan tests => 2 + 2 * 4 + 12 * (3 + 1 + 3 + 6); +} else { + plan skip_all => "Can't run the mock git executable on this system: $reason"; +} + sub build { my ($tree, $prefix) = @_; + my @ret; + my $r = delete $tree->{_}; + while (my ($d, $v) = each %$tree) { if (ref $v) { - my $dir = catdir $prefix, $d; + my $dir = catdir($prefix, $d); mkdir $dir or die "mkdir($dir): $!"; + my @r = build($v, $dir); - push @ret, map [ - $_->[0], - ref eq 'main' ? @{$_}[1 .. 3] - : map { catdir($d, $_) } @{$_}[1 .. 3] - ], @r unless $r; + + unless ($r) { + for (@r) { + push @ret, [ + $_->[0], + ref eq 'main' ? @{$_}[1 .. 3] + : map catdir($d, $_), @{$_}[1 .. 3] + ]; + } + } } else { my $file = catfile($prefix, $d); open my $fh, '>', $file or die "open($file): $!"; @@ -37,6 +54,7 @@ sub build { close $fh; } } + return $r ? bless $r, 'main' : @ret; } @@ -48,61 +66,68 @@ my $repogit = { sub repo { my ($n, $bare) = @_; + return $bare ? [ $n, "$n.git", "$n.git", "$n.git" ] : [ $n, catdir($n, '.git'), $n, "$n.git" ] } my $tmpdir = tempdir(CLEANUP => 1); -my $cwd = cwd; +my $cwd = cwd; + chdir $tmpdir or die "chdir($tmpdir): $!"; + my @expected = sort { $a->[1] cmp $b->[1] } build({ x => { a => { - _ => repo('a', 0), - '.git' => $repogit + _ => repo('a', 0), + '.git' => $repogit, }, z => { '.git' => { refs => { dummy => 1 }, - } - } + }, + }, }, y => { 'b.git' => { _ => repo('b', 1), - %$repogit + %$repogit, }, 't' => { 't.git' => { - refs => { dummy => 1 }, + refs => { dummy => 1 }, objects => { dummy => 1 }, - } - } + }, + }, }, c => { - _ => repo('c', 0), - '.git' => $repogit - } + _ => repo('c', 0), + '.git' => $repogit, + }, }, '.'); + chdir $cwd or die "chdir($cwd): $!"; -is(@expected, 3, 'only three valid git repos'); -is(grep({ ref eq 'ARRAY' } @expected), 3, 'all of them are array references'); +is @expected, 3, 'only three valid git repos'; +is grep({ ref eq 'ARRAY' } @expected), 3, 'all of them are array references'; @expected = map [ - @$_, - map({ catdir($tmpdir, $_) } @{$_}[1 .. 3]), - $tmpdir, - '%n', '%x' - ], @expected; + @$_, + map(catdir($tmpdir, $_), @{$_}[1 .. 3]), + $tmpdir, + '%n', '%x' +], @expected; sub try { my ($cmd, $exp) = @_; + my ($fh, $filename) = tempfile(UNLINK => 1); + my $policy = App::Rgit::Policy->new( @_ > 2 ? (name => 'Callback', callback => $_[2]) : (name => 'Default') ); + my $ar = App::Rgit->new( git => abs_path('t/bin/git'), root => $tmpdir, @@ -110,7 +135,8 @@ sub try { args => [ abs_path($filename), $cmd, qw/%n %g %w %b %G %W %B %R %%n %x/ ], policy => $policy, ); - isnt($ar, undef, "each $cmd has a defined object"); + isa_ok $ar, 'App::Rgit', "each $cmd is an App::Rgit object"; + my $exit; my $fail = $cmd eq 'FAIL' ? 1 : 0; if ($fail) { @@ -118,20 +144,25 @@ sub try { } else { $exit = $ar->run; } - is($exit, $fail, "each $cmd returned $fail"); + is $exit, $fail, "each $cmd returned $fail"; + my @lines = split /\n/, do { local $/; <$fh> }; - my $res = [ map [ split /\|/, $_ ], @lines ]; - $exp = [ map [ $cmd, @$_ ], @$exp ]; + my $res = [ map [ split /\|/, $_ ], @lines ]; + $exp = [ map [ $cmd, @$_ ], @$exp ]; + for my $i (0 .. $#$exp) { my $e = $exp->[$i]; my $r = shift @$res; - isnt($r, undef, "each $cmd visited repository $i"); + isnt $r, undef, "each $cmd visited repository $i"; + SKIP: { - skip 'didn\'t visited that repo' => 10 unless defined $r; + skip 'didn\'t visited that repo' => 11 unless defined $r; + s/[\r\n]*$// for @$r; - is($r->[$_], $e->[$_], "each $cmd argument $_ for repository $i is ok") - for 0 .. 10; + for (0 .. 10) { + is $r->[$_], $e->[$_], "each $cmd argument $_ for repository $i is ok"; + } } } }