X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F20-each.t;h=ca87e8a0aa373134f1e3af9a54480adfaaf96ae1;hb=b70184fed49dd0135e68ab3491475f50521992f3;hp=12223f6f0ee855d62010fd89fb9234c9a06dcd2b;hpb=19bee8f647bfe6c94c5cb3cf6a60d7a43cbca222;p=perl%2Fmodules%2Frgit.git diff --git a/t/20-each.t b/t/20-each.t index 12223f6..ca87e8a 100644 --- a/t/20-each.t +++ b/t/20-each.t @@ -4,46 +4,117 @@ use strict; use warnings; use Cwd qw/cwd abs_path/; -use File::Spec::Functions qw/catdir/; -use File::Temp qw/tempfile/; +use File::Spec::Functions qw/catdir catfile/; +use File::Temp qw/tempfile tempdir/; -use Test::More tests => 3 * 2; +use Test::More tests => 2 + 2 * 2 + 11 * (3 + 1); use App::Rgit; -my $n = 3; +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; + 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; + } else { + my $file = catfile($prefix, $d); + open my $fh, '>', $file or die "open($file): $!"; + print $fh $v; + close $fh; + } + } + return $r ? bless $r, 'main' : @ret; +} -my @expected = ( - undef, - [ [ 'a', 'a/.git', 'a', 'a.git' ] ], - [ [ 'b', 'b.git', 'b.git', 'b.git' ] ], - [ - [ 'c', 'x/c/.git', 'x/c', 'x/c.git' ], - [ 'd', 'y/d.git', 'y/d.git', 'y/d.git' ], - ], -); +my $repogit = { + HEAD => 1, + refs => { dummy => 1 }, + objects => { dummy => 1 }, +}; + +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 @repos = (undef, - map { catdir $cwd, 't', 'repos', sprintf("%02d", $_) } 1 .. $n); -for my $i (1 .. $n) { - for my $a (@{$expected[$i]}) { - $a->[$_+3] = catdir($repos[$i], $a->[$_]) for 1 .. 3; - push @$a, $repos[$i], '^'; +chdir $tmpdir or die "chdir($tmpdir): $!"; +my @expected = sort { $a->[1] cmp $b->[1] } build({ + x => { + a => { + _ => repo('a', 0), + '.git' => $repogit + }, + z => { + '.git' => { + refs => { dummy => 1 }, + } + } + }, + y => { + 'b.git' => { + _ => repo('b', 1), + %$repogit + }, + 't' => { + 't.git' => { + refs => { dummy => 1 }, + objects => { dummy => 1 }, + } + } + }, + c => { + _ => 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'); -for (1 .. $n) { +@expected = map [ + @$_, + map({ catdir($tmpdir, $_) } @{$_}[1 .. 3]), + $tmpdir, + '^n' + ], @expected; + +for my $cmd (qw/commit FAIL/) { my ($fh, $filename) = tempfile(UNLINK => 1); - my $exit = App::Rgit->new( + my $ar = App::Rgit->new( git => abs_path('t/bin/git'), - root => $repos[$_], - cmd => 'commit', - args => [ abs_path($filename), 'commit', qw/^n ^g ^w ^b ^G ^W ^B ^R ^^/ ] - )->run; - is($exit, 0, "each $_ returned 0"); - my @lines = sort split /\n/, do { local $/; <$fh> }; + root => $tmpdir, + cmd => $cmd, + args => [ abs_path($filename), $cmd, qw/^n ^g ^w ^b ^G ^W ^B ^R ^^n/ ] + ); + isnt($ar, undef, "each $cmd has a defined object"); + my $exit = $ar->run; + my $fail = $cmd eq 'FAIL' ? 1 : 0; + is($exit, $fail, "each $cmd returned $fail"); + my @lines = split /\n/, do { local $/; <$fh> }; my $res = [ map [ split /\|/, $_ ], @lines ]; - my $exp = [ map [ 'commit', @$_ ], @{$expected[$_]} ]; - is_deeply($res, $exp, "each $_ did the right thing"); + my $exp = [ map [ $cmd, @$_ ], $fail ? $expected[0] : @expected ]; + for my $i (0 .. $#$exp) { + my $e = $exp->[$i]; + my $r = shift @$res; + isnt($r, undef, "each $cmd visited repository $i"); +SKIP: + { + skip 'didn\'t visited that repo' => 10 unless defined $r; + is($r->[$_], $e->[$_], "each $cmd argument $_ for repository $i is ok") + for 0 .. 9; + } + } }