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>
6 use PublicInbox::TestCommon;
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";
16 # needed for alternates, and --absolute-git-dir is only in git 2.13+
17 $git_dir = abs_path($git_dir);
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;
23 my ($tmpdir, $for_destroy) = tmpdir();
24 my $ibx = create_inbox 'v2', version => 2,
25 indexlevel => 'medium', sub {
27 $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
28 $im->add($patch2) or BAIL_OUT;
30 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
31 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
32 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
33 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
35 test_lei({tmpdir => $tmpdir}, sub {
36 lei_ok('blob', '--mail', $patch2_oid, '-I', $ibx->{inboxdir},
37 \'--mail works for existing oid');
38 is($lei_out, $patch2->as_string, 'blob matches');
39 ok(!lei('blob', '--mail', '69df7d5', '-I', $ibx->{inboxdir}),
40 "--mail won't run solver");
42 lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
43 is(git_sha(1, \$lei_out)->hexdigest, $expect, 'blob contents output');
45 lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
46 is($lei_out, $prev, '--no-mail works');
47 ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
48 'non-existent blob fails');
50 skip '/.git exists', 1 if -e '/.git';
51 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
52 "--git-dir=$git_dir");
53 is($lei_out, $prev, '--git-dir works');
55 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
58 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
59 'non-existent blob fails');
63 lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
64 lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
67 my $git = PublicInbox::Git->new($git_dir);
68 $ibx->{-repo_objs} = [ $git ];
70 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
71 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
72 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
73 'HTTP_HOST' => 'example.com' };
74 $solver->solve($psgi_env, $log, '69df7d5', {});
75 ok($res, 'solved a blob!');
76 my $wt_git = $res->[0];
77 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
78 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
79 is($res->[2], 'blob', 'type specified');
80 is($res->[3], 4405, 'size returned');
82 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
83 is_deeply([$expect, 'blob', 4405],
84 [$wt_git->check($res->[1])], 'wt check works');
89 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
90 $s->solve($psgi_env, $log, $oid, {});
91 is($more->[1], $expect, 'resolved blob to long OID '.$i);
97 my $wt_git_dir = $wt_git->{git_dir};
99 ok(!-d $wt_git_dir, 'no references to WT held');
101 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
102 $solver->solve($psgi_env, $log, '0'x40, {});
103 is($res, undef, 'no error on z40');
105 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
106 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
107 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
108 is($res, undef, 'no error on a tag not in our repo');
110 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
111 $solver->solve($psgi_env, $log, '0a92431', {});
112 ok($res, 'resolved without hints');
117 path_b => 'CONTRIBUTING'
119 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
120 $solver->solve($psgi_env, $log, '0a92431', $hints);
122 # don't compare ::Git objects:
123 shift @$res; shift @$hinted;
124 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
126 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
128 require_mods(@psgi, 7 + scalar(@psgi));
129 use_ok($_) for @psgi;
130 my $binfoo = "$ibx->{inboxdir}/binfoo.git";
131 my $l = "$ibx->{inboxdir}/inbox.lock";
132 -f $l or BAIL_OUT "BUG: $l missing: $!";
133 require_ok 'PublicInbox::ViewVCS';
136 $PublicInbox::ViewVCS::MAX_SIZE + 1;
138 my %bin = (big => $big_size, small => 1);
139 my %oid; # (small|big) => OID
140 my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
141 my $acq = $lk->lock_for_scope;
142 my $stamp = "$binfoo/stamp";
143 if (open my $fh, '<', $stamp) {
144 %oid = map { chomp; split(/=/, $_) } (<$fh>);
146 PublicInbox::Import::init_bare($binfoo);
147 my $cmd = [ qw(git hash-object -w --stdin) ];
148 my $env = { GIT_DIR => $binfoo };
149 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
150 while (my ($label, $size) = each %bin) {
151 pipe(my ($rin, $win)) or BAIL_OUT;
152 my $rout = popen_rd($cmd , $env, { 0 => $rin });
154 print { $win } ("\0" x $size) or BAIL_OUT;
155 close $win or BAIL_OUT;
156 chomp(my $x = <$rout>);
157 close $rout or BAIL_OUT "$?";
158 print $fh "$label=$x\n" or BAIL_OUT;
161 close $fh or BAIL_OUT;
162 rename("$stamp.$$", $stamp) or BAIL_OUT;
165 # ensure the PSGI frontend (ViewVCS) works:
166 my $name = $ibx->{name};
167 my $cfgpfx = "publicinbox.$name";
168 my $cfgpath = "$tmpdir/httpd-config";
169 open my $cfgfh, '>', $cfgpath or die;
170 print $cfgfh <<EOF or die;
171 [publicinbox "$name"]
172 address = $ibx->{-primary_address}
173 inboxdir = $ibx->{inboxdir}
174 coderepo = public-inbox
176 url = http://example.com/$name
177 [coderepo "public-inbox"]
179 cgiturl = http://example.com/public-inbox
182 cgiturl = http://example.com/binfoo
185 my $cfg = PublicInbox::Config->new($cfgpath);
186 my $www = PublicInbox::WWW->new($cfg);
189 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
192 local $SIG{__WARN__} = sub { push @warn, @_ };
193 $cb->(GET("/$name/$mid/"));
195 is_deeply(\@warn, [], 'no warnings from rendering diff');
196 like($res->content, qr!>Ω</a>!, 'omega escaped');
198 $res = $cb->(GET("/$name/3435775/s/"));
199 is($res->code, 200, 'success with existing blob');
201 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
202 is($res->code, 404, 'failure with null OID');
204 $res = $cb->(GET("/$name/$non_existent/s/"));
205 is($res->code, 404, 'failure with null OID');
207 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
208 is($res->code, 200, 'shows commit (unabbreviated)');
209 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
210 is($res->code, 200, 'shows commit (abbreviated)');
211 while (my ($label, $size) = each %bin) {
212 $res = $cb->(GET("/$name/$oid{$label}/s/"));
213 is($res->code, 200, "$label binary file");
214 ok(index($res->content, "blob $size bytes") >= 0,
215 "showed $label binary blob size");
216 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
217 is($res->code, 200, "$label raw binary download");
218 is($res->content, "\0" x $size,
219 "$label content matches");
222 test_psgi(sub { $www->call(@_) }, $client);
224 require_mods(qw(Plack::Test::ExternalServer), 7);
225 my $env = { PI_CONFIG => $cfgpath };
226 my $sock = tcp_server() or die;
227 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
228 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
229 my $td = start_script($cmd, $env, { 3 => $sock });
230 my ($h, $p) = tcp_host_port($sock);
231 my $url = "http://$h:$p";
232 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = $url;
233 Plack::Test::ExternalServer::test_psgi(client => $client);
234 skip 'no curl', 1 unless which('curl');
236 mkdir "$tmpdir/ext" // xbail "mkdir $!";
237 test_lei({tmpdir => "$tmpdir/ext"}, sub {
238 my $rurl = "$url/$name";
239 lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
240 is(git_sha(1, \$lei_out)->hexdigest, $expect,
241 'blob contents output');
242 ok(!lei(qw(blob -I), $rurl, $non_existent),
243 'non-existent blob fails');