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