6 use Cwd qw/cwd abs_path/;
7 use File::Spec (); # catdir, catfile
8 use File::Temp qw/tempfile tempdir/;
12 use App::Rgit::Utils qw/:codes/;
17 use App::Rgit::TestUtils qw/can_run_git/;
18 use App::Rgit::Policy::Callback;
20 my ($can_run, $reason) = can_run_git;
22 plan tests => 2 + 2 * 4 + 12 * (3 + 1 + 3 + 6);
24 plan skip_all => "Can't run the mock git executable on this system: $reason";
28 my ($tree, $prefix) = @_;
32 my $r = delete $tree->{_};
34 while (my ($d, $v) = each %$tree) {
36 my $dir = File::Spec->catdir($prefix, $d);
37 mkdir $dir or die "mkdir($dir): $!";
39 my @r = build($v, $dir);
45 ref eq 'main' ? @{$_}[1 .. 3]
46 : map File::Spec->catdir($d, $_), @{$_}[1 .. 3]
51 my $file = File::Spec->catfile($prefix, $d);
52 open my $fh, '>', $file or die "open($file): $!";
58 return $r ? bless $r, 'main' : @ret;
63 refs => { dummy => 1 },
64 objects => { dummy => 1 },
70 return $bare ? [ $n, "$n.git", "$n.git", "$n.git" ]
71 : [ $n, File::Spec->catdir($n, '.git'), $n, "$n.git" ]
74 my $tmpdir = tempdir(CLEANUP => 1);
77 chdir $tmpdir or die "chdir($tmpdir): $!";
79 my @expected = sort { $a->[1] cmp $b->[1] } build({
87 refs => { dummy => 1 },
98 refs => { dummy => 1 },
99 objects => { dummy => 1 },
109 chdir $cwd or die "chdir($cwd): $!";
111 is @expected, 3, 'only three valid git repos';
112 is grep({ ref eq 'ARRAY' } @expected), 3, 'all of them are array references';
116 map(File::Spec->catdir($tmpdir, $_), @{$_}[1 .. 3]),
122 my ($cmd, $exp) = @_;
124 my ($fh, $filename) = tempfile(UNLINK => 1);
126 my $policy = App::Rgit::Policy->new(
127 @_ > 2 ? (policy => 'Callback', callback => $_[2])
128 : (policy => 'Default')
131 my $ar = App::Rgit->new(
135 args => [ abs_path($filename), $cmd, qw/%n %g %w %b %G %W %B %R %%n %x/ ],
138 isa_ok $ar, 'App::Rgit', "each $cmd is an App::Rgit object";
141 my $fail = $cmd eq 'FAIL' ? 1 : 0;
143 ($exit, undef) = $ar->run;
147 is $exit, $fail, "each $cmd returned $fail";
149 my @lines = split /\n/, do { local $/; <$fh> };
150 my $res = [ map [ split /\|/, $_ ], @lines ];
151 $exp = [ map [ $cmd, @$_ ], @$exp ];
153 for my $i (0 .. $#$exp) {
156 isnt $r, undef, "each $cmd visited repository $i";
160 skip 'didn\'t visited that repo' => 11 unless defined $r;
162 s/[\r\n]*$// for @$r;
164 is $r->[$_], $e->[$_], "each $cmd argument $_ for repository $i is ok";
170 try 'commit', [ @expected ];
171 try 'FAIL', [ $expected[0] ];
172 try 'FAIL', [ @expected ],
175 try 'FAIL', [ map { ($expected[$_]) x 2 } 0 .. $#expected ],
176 sub { my $ret = $c ? undef : REDO; $c = 1 - $c; $ret };