]> git.vpit.fr Git - perl/modules/rgit.git/blob - t/20-each.t
Update VPIT::TestHelpers to 15e8aee3
[perl/modules/rgit.git] / t / 20-each.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Cwd        qw<cwd abs_path>;
7 use File::Spec (); # catdir, catfile
8 use File::Temp qw<tempfile tempdir>;
9
10 use Test::More;
11
12 use App::Rgit::Utils qw<:codes>;
13 use App::Rgit;
14
15 use lib 't/lib';
16
17 use App::Rgit::TestUtils qw<can_run_git>;
18 use App::Rgit::Policy::Callback;
19
20 my ($can_run, $reason) = can_run_git;
21 if ($can_run) {
22  plan tests    => 2 + 2 * 4 + 12 * (3 + 1 + 3 + 6);
23 } else {
24  plan skip_all => "Can't run the mock git executable on this system: $reason";
25 }
26
27 sub build {
28  my ($tree, $prefix) = @_;
29
30  my @ret;
31
32  my $r = delete $tree->{_};
33
34  while (my ($d, $v) = each %$tree) {
35   if (ref $v) {
36    my $dir = File::Spec->catdir($prefix, $d);
37    mkdir $dir or die "mkdir($dir): $!";
38
39    my @r = build($v, $dir);
40
41    unless ($r) {
42     for (@r) {
43      push @ret, [
44       $_->[0],
45       ref eq 'main' ? @{$_}[1 .. 3]
46                     : map File::Spec->catdir($d, $_), @{$_}[1 .. 3]
47      ];
48     }
49    }
50   } else {
51    my $file = File::Spec->catfile($prefix, $d);
52    open my $fh, '>', $file or die "open($file): $!";
53    print $fh $v;
54    close $fh;
55   }
56  }
57
58  return $r ? bless $r, 'main' : @ret;
59 }
60
61 my $repogit = {
62  HEAD    => 1,
63  refs    => { dummy => 1 },
64  objects => { dummy => 1 },
65 };
66
67 sub repo {
68  my ($n, $bare) = @_;
69
70  return $bare ? [ $n, "$n.git",                       "$n.git", "$n.git" ]
71               : [ $n, File::Spec->catdir($n, '.git'), $n,       "$n.git" ]
72 }
73
74 my $tmpdir = tempdir(CLEANUP => 1);
75 my $cwd    = cwd;
76
77 chdir $tmpdir or die "chdir($tmpdir): $!";
78
79 my @expected = sort { $a->[1] cmp $b->[1] } build({
80  x => {
81   a => {
82    _      => repo('a', 0),
83    '.git' => $repogit,
84   },
85   z => {
86    '.git' => {
87     refs => { dummy => 1 },
88    },
89   },
90  },
91  y => {
92   'b.git' => {
93    _ => repo('b', 1),
94    %$repogit,
95   },
96   't' => {
97    't.git' => {
98     refs    => { dummy => 1 },
99     objects => { dummy => 1 },
100    },
101   },
102  },
103  c => {
104   _      => repo('c', 0),
105   '.git' => $repogit,
106  },
107 }, '.');
108
109 chdir $cwd or die "chdir($cwd): $!";
110
111 is @expected,                          3, 'only three valid git repos';
112 is grep({ ref eq 'ARRAY' } @expected), 3, 'all of them are array references';
113
114 @expected = map [
115  @$_,
116  map(File::Spec->catdir($tmpdir, $_), @{$_}[1 .. 3]),
117  $tmpdir,
118  '%n', '%x'
119 ], @expected;
120
121 sub try {
122  my ($cmd, $exp) = @_;
123
124  my ($fh, $filename) = tempfile(UNLINK => 1);
125
126  my $policy = App::Rgit::Policy->new(
127   @_ > 2 ? (policy => 'Callback', callback => $_[2])
128          : (policy => 'Default')
129  );
130
131  my $ar = App::Rgit->new(
132   git    => 't/bin/git',
133   root   => $tmpdir,
134   cmd    => $cmd,
135   args   => [ abs_path($filename), $cmd, qw<%n %g %w %b %G %W %B %R %%n %x> ],
136   policy => $policy,
137  );
138  isa_ok $ar, 'App::Rgit', "each $cmd is an App::Rgit object";
139
140  my $exit;
141  my $fail = $cmd eq 'FAIL' ? 1 : 0;
142  if ($fail) {
143   ($exit, undef) = $ar->run;
144  } else {
145   $exit = $ar->run;
146  }
147  is $exit, $fail, "each $cmd returned $fail";
148
149  my @lines = split /\n/, do { local $/; <$fh> };
150  my $res   = [ map [ split /\|/, $_ ], @lines ];
151  $exp      = [ map [ $cmd, @$_ ], @$exp ];
152
153  for my $i (0 .. $#$exp) {
154   my $e = $exp->[$i];
155   my $r = shift @$res;
156   isnt $r, undef, "each $cmd visited repository $i";
157
158 SKIP:
159   {
160    skip 'didn\'t visited that repo' => 11 unless defined $r;
161
162    s/[\r\n]*$// for @$r;
163    for (0 .. 10) {
164     is $r->[$_], $e->[$_], "each $cmd argument $_ for repository $i is ok";
165    }
166   }
167  }
168 }
169
170 try 'commit', [ @expected ];
171 try 'FAIL',   [ $expected[0] ];
172 try 'FAIL',   [ @expected ],
173               sub { NEXT | SAVE };
174 my $c = 0;
175 try 'FAIL',   [ map { ($expected[$_]) x 2 } 0 .. $#expected ],
176               sub { my $ret = $c ? undef : REDO; $c = 1 - $c; $ret };