]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
lei blob: add remote external support
[public-inbox.git] / t / solver_git.t
1 #!perl -w
2 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 use Cwd qw(abs_path);
8 require_git(2.6);
9 use Digest::SHA qw(sha1_hex);
10 use PublicInbox::Spawn qw(popen_rd which);
11 require_mods(qw(DBD::SQLite Search::Xapian Plack::Util));
12 my $git_dir = xqx([qw(git rev-parse --git-dir)], undef, {2 => \(my $null)});
13 $? == 0 or plan skip_all => "$0 must be run from a git working tree";
14 chomp $git_dir;
15
16 # needed for alternates, and --absolute-git-dir is only in git 2.13+
17 $git_dir = abs_path($git_dir);
18
19 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable Git SolverGit WWW));
20
21 my ($tmpdir, $for_destroy) = tmpdir();
22 my $ibx = create_inbox 'v2', version => 2,
23                         indexlevel => 'medium', sub {
24         my ($im) = @_;
25         $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
26         $im->add(eml_load 't/solve/0002-rename-with-modifications.patch') or
27                 BAIL_OUT;
28 };
29 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
30 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
31 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
32 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
33
34 test_lei({tmpdir => $tmpdir}, sub {
35         lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
36         is(sha1_hex("blob ".length($lei_out)."\0".$lei_out),
37                 $expect, 'blob contents output');
38         my $prev = $lei_out;
39         lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
40         is($lei_out, $prev, '--no-mail works');
41         ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
42                         'non-existent blob fails');
43         SKIP: {
44                 skip '/.git exists', 1 if -e '/.git';
45                 require PublicInbox::OnDestroy;
46                 opendir my $dh, '.' or xbail "opendir: $!";
47                 my $end = PublicInbox::OnDestroy->new($$, sub {
48                         chdir $dh or xbail "chdir: $!";
49                 });
50                 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
51                         "--git-dir=$git_dir");
52                 is($lei_out, $prev, '--git-dir works');
53
54                 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
55                         '--no-cwd works');
56
57                 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
58                         'non-existent blob fails');
59         }
60
61         # fallbacks
62         lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
63         lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
64 });
65
66 my $git = PublicInbox::Git->new($git_dir);
67 $ibx->{-repo_objs} = [ $git ];
68 my $res;
69 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
70 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
71 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
72                 'HTTP_HOST' => 'example.com' };
73 $solver->solve($psgi_env, $log, '69df7d5', {});
74 ok($res, 'solved a blob!');
75 my $wt_git = $res->[0];
76 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
77 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
78 is($res->[2], 'blob', 'type specified');
79 is($res->[3], 4405, 'size returned');
80
81 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
82 is_deeply([$expect, 'blob', 4405],
83           [$wt_git->check($res->[1])], 'wt check works');
84
85 my $oid = $expect;
86 for my $i (1..2) {
87         my $more;
88         my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
89         $s->solve($psgi_env, $log, $oid, {});
90         is($more->[1], $expect, 'resolved blob to long OID '.$i);
91         chop($oid);
92 }
93
94 $solver = undef;
95 $res = undef;
96 my $wt_git_dir = $wt_git->{git_dir};
97 $wt_git = undef;
98 ok(!-d $wt_git_dir, 'no references to WT held');
99
100 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
101 $solver->solve($psgi_env, $log, '0'x40, {});
102 is($res, undef, 'no error on z40');
103
104 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
105 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
106 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
107 is($res, undef, 'no error on a tag not in our repo');
108
109 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
110 $solver->solve($psgi_env, $log, '0a92431', {});
111 ok($res, 'resolved without hints');
112
113 my $hints = {
114         oid_a => '3435775',
115         path_a => 'HACKING',
116         path_b => 'CONTRIBUTING'
117 };
118 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
119 $solver->solve($psgi_env, $log, '0a92431', $hints);
120 my $hinted = $res;
121 # don't compare ::Git objects:
122 shift @$res; shift @$hinted;
123 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
124
125 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
126 SKIP: {
127         require_mods(@psgi, 7 + scalar(@psgi));
128         use_ok($_) for @psgi;
129         my $binfoo = "$ibx->{inboxdir}/binfoo.git";
130         my $l = "$ibx->{inboxdir}/inbox.lock";
131         -f $l or BAIL_OUT "BUG: $l missing: $!";
132         require_ok 'PublicInbox::ViewVCS';
133         my $big_size = do {
134                 no warnings 'once';
135                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
136         };
137         my %bin = (big => $big_size, small => 1);
138         my %oid; # (small|big) => OID
139         my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
140         my $acq = $lk->lock_for_scope;
141         my $stamp = "$binfoo/stamp";
142         if (open my $fh, '<', $stamp) {
143                 %oid = map { chomp; split(/=/, $_) } (<$fh>);
144         } else {
145                 PublicInbox::Import::init_bare($binfoo);
146                 my $cmd = [ qw(git hash-object -w --stdin) ];
147                 my $env = { GIT_DIR => $binfoo };
148                 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
149                 while (my ($label, $size) = each %bin) {
150                         pipe(my ($rin, $win)) or BAIL_OUT;
151                         my $rout = popen_rd($cmd , $env, { 0 => $rin });
152                         $rin = undef;
153                         print { $win } ("\0" x $size) or BAIL_OUT;
154                         close $win or BAIL_OUT;
155                         chomp(my $x = <$rout>);
156                         close $rout or BAIL_OUT "$?";
157                         print $fh "$label=$x\n" or BAIL_OUT;
158                         $oid{$label} = $x;
159                 }
160                 close $fh or BAIL_OUT;
161                 rename("$stamp.$$", $stamp) or BAIL_OUT;
162         }
163         undef $acq;
164         # ensure the PSGI frontend (ViewVCS) works:
165         my $name = $ibx->{name};
166         my $cfgpfx = "publicinbox.$name";
167         my $cfgpath = "$tmpdir/httpd-config";
168         open my $cfgfh, '>', $cfgpath or die;
169         print $cfgfh <<EOF or die;
170 [publicinbox "$name"]
171         address = $ibx->{-primary_address}
172         inboxdir = $ibx->{inboxdir}
173         coderepo = public-inbox
174         coderepo = binfoo
175         url = http://example.com/$name
176 [coderepo "public-inbox"]
177         dir = $git_dir
178         cgiturl = http://example.com/public-inbox
179 [coderepo "binfoo"]
180         dir = $binfoo
181         cgiturl = http://example.com/binfoo
182 EOF
183         close $cfgfh or die;
184         my $cfg = PublicInbox::Config->new($cfgpath);
185         my $www = PublicInbox::WWW->new($cfg);
186         my $client = sub {
187                 my ($cb) = @_;
188                 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
189                 my @warn;
190                 my $res = do {
191                         local $SIG{__WARN__} = sub { push @warn, @_ };
192                         $cb->(GET("/$name/$mid/"));
193                 };
194                 is_deeply(\@warn, [], 'no warnings from rendering diff');
195                 like($res->content, qr!>&#937;</a>!, 'omega escaped');
196
197                 $res = $cb->(GET("/$name/3435775/s/"));
198                 is($res->code, 200, 'success with existing blob');
199
200                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
201                 is($res->code, 404, 'failure with null OID');
202
203                 $res = $cb->(GET("/$name/$non_existent/s/"));
204                 is($res->code, 404, 'failure with null OID');
205
206                 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
207                 is($res->code, 200, 'shows commit (unabbreviated)');
208                 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
209                 is($res->code, 200, 'shows commit (abbreviated)');
210                 while (my ($label, $size) = each %bin) {
211                         $res = $cb->(GET("/$name/$oid{$label}/s/"));
212                         is($res->code, 200, "$label binary file");
213                         ok(index($res->content, "blob $size bytes") >= 0,
214                                 "showed $label binary blob size");
215                         $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
216                         is($res->code, 200, "$label raw binary download");
217                         is($res->content, "\0" x $size,
218                                 "$label content matches");
219                 }
220         };
221         test_psgi(sub { $www->call(@_) }, $client);
222         SKIP: {
223                 require_mods(qw(Plack::Test::ExternalServer), 7);
224                 my $env = { PI_CONFIG => $cfgpath };
225                 my $sock = tcp_server() or die;
226                 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
227                 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
228                 my $td = start_script($cmd, $env, { 3 => $sock });
229                 my ($h, $p) = tcp_host_port($sock);
230                 my $url = "http://$h:$p";
231                 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = $url;
232                 Plack::Test::ExternalServer::test_psgi(client => $client);
233                 skip 'no curl', 1 unless which('curl');
234
235                 mkdir "$tmpdir/ext" // xbail "mkdir $!";
236                 test_lei({tmpdir => "$tmpdir/ext"}, sub {
237                         my $rurl = "$url/$name";
238                         lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
239                         is(sha1_hex("blob ".length($lei_out)."\0".$lei_out),
240                                 $expect, 'blob contents output');
241                         ok(!lei(qw(blob -I), $rurl, $non_existent),
242                                         'non-existent blob fails');
243                 });
244         }
245 }
246
247 done_testing();