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);
11 use PublicInbox::Spawn qw(popen_rd which);
12 require_mods(qw(DBD::SQLite Search::Xapian Plack::Util));
13 my $git_dir = xqx([qw(git rev-parse --git-dir)], undef, {2 => \(my $null)});
14 $? == 0 or plan skip_all => "$0 must be run from a git working tree";
17 # needed for alternates, and --absolute-git-dir is only in git 2.13+
18 $git_dir = abs_path($git_dir);
20 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable Git SolverGit WWW));
21 my $patch2 = eml_load 't/solve/0002-rename-with-modifications.patch';
22 my $patch2_oid = git_sha(1, $patch2)->hexdigest;
24 my ($tmpdir, $for_destroy) = tmpdir();
25 my $ibx = create_inbox 'v2', version => 2,
26 indexlevel => 'medium', sub {
28 $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
29 $im->add($patch2) or BAIL_OUT;
31 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
32 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
33 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
34 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
36 test_lei({tmpdir => $tmpdir}, sub {
37 lei_ok('blob', '--mail', $patch2_oid, '-I', $ibx->{inboxdir},
38 \'--mail works for existing oid');
39 is($lei_out, $patch2->as_string, 'blob matches');
40 ok(!lei('blob', '--mail', '69df7d5', '-I', $ibx->{inboxdir}),
41 "--mail won't run solver");
43 lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
44 is(git_sha(1, PublicInbox::Eml->new($lei_out))->hexdigest,
45 $expect, 'blob contents output');
47 lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
48 is($lei_out, $prev, '--no-mail works');
49 ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
50 'non-existent blob fails');
52 skip '/.git exists', 1 if -e '/.git';
53 require PublicInbox::OnDestroy;
54 opendir my $dh, '.' or xbail "opendir: $!";
55 my $end = PublicInbox::OnDestroy->new($$, sub {
56 chdir $dh or xbail "chdir: $!";
58 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
59 "--git-dir=$git_dir");
60 is($lei_out, $prev, '--git-dir works');
62 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
65 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
66 'non-existent blob fails');
70 lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
71 lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
74 my $git = PublicInbox::Git->new($git_dir);
75 $ibx->{-repo_objs} = [ $git ];
77 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
78 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
79 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
80 'HTTP_HOST' => 'example.com' };
81 $solver->solve($psgi_env, $log, '69df7d5', {});
82 ok($res, 'solved a blob!');
83 my $wt_git = $res->[0];
84 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
85 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
86 is($res->[2], 'blob', 'type specified');
87 is($res->[3], 4405, 'size returned');
89 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
90 is_deeply([$expect, 'blob', 4405],
91 [$wt_git->check($res->[1])], 'wt check works');
96 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
97 $s->solve($psgi_env, $log, $oid, {});
98 is($more->[1], $expect, 'resolved blob to long OID '.$i);
104 my $wt_git_dir = $wt_git->{git_dir};
106 ok(!-d $wt_git_dir, 'no references to WT held');
108 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
109 $solver->solve($psgi_env, $log, '0'x40, {});
110 is($res, undef, 'no error on z40');
112 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
113 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
114 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
115 is($res, undef, 'no error on a tag not in our repo');
117 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
118 $solver->solve($psgi_env, $log, '0a92431', {});
119 ok($res, 'resolved without hints');
124 path_b => 'CONTRIBUTING'
126 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
127 $solver->solve($psgi_env, $log, '0a92431', $hints);
129 # don't compare ::Git objects:
130 shift @$res; shift @$hinted;
131 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
133 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
135 require_mods(@psgi, 7 + scalar(@psgi));
136 use_ok($_) for @psgi;
137 my $binfoo = "$ibx->{inboxdir}/binfoo.git";
138 my $l = "$ibx->{inboxdir}/inbox.lock";
139 -f $l or BAIL_OUT "BUG: $l missing: $!";
140 require_ok 'PublicInbox::ViewVCS';
143 $PublicInbox::ViewVCS::MAX_SIZE + 1;
145 my %bin = (big => $big_size, small => 1);
146 my %oid; # (small|big) => OID
147 my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
148 my $acq = $lk->lock_for_scope;
149 my $stamp = "$binfoo/stamp";
150 if (open my $fh, '<', $stamp) {
151 %oid = map { chomp; split(/=/, $_) } (<$fh>);
153 PublicInbox::Import::init_bare($binfoo);
154 my $cmd = [ qw(git hash-object -w --stdin) ];
155 my $env = { GIT_DIR => $binfoo };
156 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
157 while (my ($label, $size) = each %bin) {
158 pipe(my ($rin, $win)) or BAIL_OUT;
159 my $rout = popen_rd($cmd , $env, { 0 => $rin });
161 print { $win } ("\0" x $size) or BAIL_OUT;
162 close $win or BAIL_OUT;
163 chomp(my $x = <$rout>);
164 close $rout or BAIL_OUT "$?";
165 print $fh "$label=$x\n" or BAIL_OUT;
168 close $fh or BAIL_OUT;
169 rename("$stamp.$$", $stamp) or BAIL_OUT;
172 # ensure the PSGI frontend (ViewVCS) works:
173 my $name = $ibx->{name};
174 my $cfgpfx = "publicinbox.$name";
175 my $cfgpath = "$tmpdir/httpd-config";
176 open my $cfgfh, '>', $cfgpath or die;
177 print $cfgfh <<EOF or die;
178 [publicinbox "$name"]
179 address = $ibx->{-primary_address}
180 inboxdir = $ibx->{inboxdir}
181 coderepo = public-inbox
183 url = http://example.com/$name
184 [coderepo "public-inbox"]
186 cgiturl = http://example.com/public-inbox
189 cgiturl = http://example.com/binfoo
192 my $cfg = PublicInbox::Config->new($cfgpath);
193 my $www = PublicInbox::WWW->new($cfg);
196 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
199 local $SIG{__WARN__} = sub { push @warn, @_ };
200 $cb->(GET("/$name/$mid/"));
202 is_deeply(\@warn, [], 'no warnings from rendering diff');
203 like($res->content, qr!>Ω</a>!, 'omega escaped');
205 $res = $cb->(GET("/$name/3435775/s/"));
206 is($res->code, 200, 'success with existing blob');
208 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
209 is($res->code, 404, 'failure with null OID');
211 $res = $cb->(GET("/$name/$non_existent/s/"));
212 is($res->code, 404, 'failure with null OID');
214 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
215 is($res->code, 200, 'shows commit (unabbreviated)');
216 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
217 is($res->code, 200, 'shows commit (abbreviated)');
218 while (my ($label, $size) = each %bin) {
219 $res = $cb->(GET("/$name/$oid{$label}/s/"));
220 is($res->code, 200, "$label binary file");
221 ok(index($res->content, "blob $size bytes") >= 0,
222 "showed $label binary blob size");
223 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
224 is($res->code, 200, "$label raw binary download");
225 is($res->content, "\0" x $size,
226 "$label content matches");
229 test_psgi(sub { $www->call(@_) }, $client);
231 require_mods(qw(Plack::Test::ExternalServer), 7);
232 my $env = { PI_CONFIG => $cfgpath };
233 my $sock = tcp_server() or die;
234 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
235 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
236 my $td = start_script($cmd, $env, { 3 => $sock });
237 my ($h, $p) = tcp_host_port($sock);
238 my $url = "http://$h:$p";
239 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = $url;
240 Plack::Test::ExternalServer::test_psgi(client => $client);
241 skip 'no curl', 1 unless which('curl');
243 mkdir "$tmpdir/ext" // xbail "mkdir $!";
244 test_lei({tmpdir => "$tmpdir/ext"}, sub {
245 my $rurl = "$url/$name";
246 lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
247 my $eml = PublicInbox::Eml->new($lei_out);
248 is(git_sha(1, $eml)->hexdigest, $expect,
249 'blob contents output');
250 ok(!lei(qw(blob -I), $rurl, $non_existent),
251 'non-existent blob fails');