]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
t/*.t: avoid sharing "my" variables in subs
[public-inbox.git] / t / solver_git.t
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Cwd qw(abs_path);
7 require './t/common.perl';
8 require_git(2.6);
9 use PublicInbox::Spawn qw(spawn);
10
11 my @mods = qw(DBD::SQLite Search::Xapian);
12 foreach my $mod (@mods) {
13         eval "require $mod";
14         plan skip_all => "$mod missing for $0" if $@;
15 }
16 chomp(my $git_dir = `git rev-parse --git-dir 2>/dev/null`);
17 plan skip_all => "$0 must be run from a git working tree" if $?;
18
19 # needed for alternates, and --absolute-git-dir is only in git 2.13+
20 $git_dir = abs_path($git_dir);
21
22 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable MIME Git SolverGit WWW));
23
24 my ($inboxdir, $for_destroy) = tmpdir();
25 my $opts = {
26         inboxdir => $inboxdir,
27         name => 'test-v2writable',
28         version => 2,
29         -primary_address => 'test@example.com',
30 };
31 my $ibx = PublicInbox::Inbox->new($opts);
32 my $im = PublicInbox::V2Writable->new($ibx, 1);
33 $im->{parallel} = 0;
34
35 my $deliver_patch = sub ($) {
36         open my $fh, '<', $_[0] or die "open: $!";
37         my $mime = PublicInbox::MIME->new(do { local $/; <$fh> });
38         $im->add($mime);
39         $im->done;
40 };
41
42 $deliver_patch->('t/solve/0001-simple-mod.patch');
43 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
44
45 my $git = PublicInbox::Git->new($git_dir);
46 is('public-inbox 1.0.0',
47         $git->commit_title($v1_0_0_tag),
48         'commit_title works on 1.0.0');
49
50 is(undef, $git->commit_title('impossible'), 'undef on impossible object');
51
52 $ibx->{-repo_objs} = [ $git ];
53 my $res;
54 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
55 open my $log, '+>>', "$inboxdir/solve.log" or die "open: $!";
56 my $psgi_env = { 'psgi.errors' => *STDERR };
57 $solver->solve($psgi_env, $log, '69df7d5', {});
58 ok($res, 'solved a blob!');
59 my $wt_git = $res->[0];
60 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
61 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
62 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
63 is($res->[2], 'blob', 'type specified');
64 is($res->[3], 4405, 'size returned');
65
66 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
67 is_deeply([$expect, 'blob', 4405],
68           [$wt_git->check($res->[1])], 'wt check works');
69
70 if (0) { # TODO: check this?
71         seek($log, 0, 0);
72         my $z = do { local $/; <$log> };
73         diag $z;
74 }
75
76 my $oid = $expect;
77 for my $i (1..2) {
78         my $more;
79         my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
80         $s->solve($psgi_env, $log, $oid, {});
81         is($more->[1], $expect, 'resolved blob to long OID '.$i);
82         chop($oid);
83 }
84
85 $solver = undef;
86 $res = undef;
87 my $wt_git_dir = $wt_git->{git_dir};
88 $wt_git = undef;
89 ok(!-d $wt_git_dir, 'no references to WT held');
90
91 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
92 $solver->solve($psgi_env, $log, '0'x40, {});
93 is($res, undef, 'no error on z40');
94
95 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
96 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
97 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
98 is($res, undef, 'no error on a tag not in our repo');
99
100 $deliver_patch->('t/solve/0002-rename-with-modifications.patch');
101 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
102 $solver->solve($psgi_env, $log, '0a92431', {});
103 ok($res, 'resolved without hints');
104
105 my $hints = {
106         oid_a => '3435775',
107         path_a => 'HACKING',
108         path_b => 'CONTRIBUTING'
109 };
110 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
111 $solver->solve($psgi_env, $log, '0a92431', $hints);
112 my $hinted = $res;
113 # don't compare ::Git objects:
114 shift @$res; shift @$hinted;
115 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
116
117 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
118 SKIP: {
119         my @missing;
120         for my $mod (@psgi) {
121                 eval("require $mod") or push(@missing, $mod);
122         }
123         skip("missing: ".join(', ', @missing), 7 + scalar(@psgi)) if @missing;
124         use_ok($_) for @psgi;
125         my $binfoo = "$inboxdir/binfoo.git";
126         system(qw(git init --bare -q), $binfoo) == 0 or die "git init: $?";
127         require_ok 'PublicInbox::ViewVCS';
128         my $big_size = do {
129                 no warnings 'once';
130                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
131         };
132         my %bin = (big => $big_size, small => 1);
133         my %oid; # (small|big) => OID
134         my $cmd = [ qw(git hash-object -w --stdin) ];
135         my $env = { GIT_DIR => $binfoo };
136         while (my ($label, $size) = each %bin) {
137                 pipe(my ($rout, $wout)) or die;
138                 pipe(my ($rin, $win)) or die;
139                 my $rdr = { 0 => fileno($rin), 1 => fileno($wout) };
140                 my $pid = spawn($cmd , $env, $rdr);
141                 $wout = $rin = undef;
142                 print { $win } ("\0" x $size) or die;
143                 close $win or die;
144                 chomp($oid{$label} = <$rout>);
145         }
146
147         # ensure the PSGI frontend (ViewVCS) works:
148         my $name = $ibx->{name};
149         my $cfgpfx = "publicinbox.$name";
150         my $cfg = PublicInbox::Config->new(\<<EOF);
151 $cfgpfx.address=$ibx->{address};
152 $cfgpfx.inboxdir=$inboxdir
153 $cfgpfx.coderepo=public-inbox
154 $cfgpfx.coderepo=binfoo
155 coderepo.public-inbox.dir=$git_dir
156 coderepo.public-inbox.cgiturl=http://example.com/public-inbox
157 coderepo.binfoo.dir=$binfoo
158 coderepo.binfoo.cgiturl=http://example.com/binfoo
159 EOF
160         my $www = PublicInbox::WWW->new($cfg);
161         test_psgi(sub { $www->call(@_) }, sub {
162                 my ($cb) = @_;
163                 my $res = $cb->(GET("/$name/3435775/s/"));
164                 is($res->code, 200, 'success with existing blob');
165
166                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
167                 is($res->code, 404, 'failure with null OID');
168
169                 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
170                 is($res->code, 200, 'shows commit');
171                 while (my ($label, $size) = each %bin) {
172                         $res = $cb->(GET("/$name/$oid{$label}/s/"));
173                         is($res->code, 200, "$label binary file");
174                         ok(index($res->content, "blob $size bytes") >= 0,
175                                 "showed $label binary blob size");
176                         $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
177                         is($res->code, 200, "$label raw binary download");
178                         is($res->content, "\0" x $size,
179                                 "$label content matches");
180                 }
181         });
182 }
183
184 done_testing();