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 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
54 "--git-dir=$git_dir");
55 is($lei_out, $prev, '--git-dir works');
57 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
60 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
61 'non-existent blob fails');
65 lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
66 lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
69 my $git = PublicInbox::Git->new($git_dir);
70 $ibx->{-repo_objs} = [ $git ];
72 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
73 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
74 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
75 'HTTP_HOST' => 'example.com' };
76 $solver->solve($psgi_env, $log, '69df7d5', {});
77 ok($res, 'solved a blob!');
78 my $wt_git = $res->[0];
79 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
80 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
81 is($res->[2], 'blob', 'type specified');
82 is($res->[3], 4405, 'size returned');
84 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
85 is_deeply([$expect, 'blob', 4405],
86 [$wt_git->check($res->[1])], 'wt check works');
91 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
92 $s->solve($psgi_env, $log, $oid, {});
93 is($more->[1], $expect, 'resolved blob to long OID '.$i);
99 my $wt_git_dir = $wt_git->{git_dir};
101 ok(!-d $wt_git_dir, 'no references to WT held');
103 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
104 $solver->solve($psgi_env, $log, '0'x40, {});
105 is($res, undef, 'no error on z40');
107 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
108 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
109 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
110 is($res, undef, 'no error on a tag not in our repo');
112 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
113 $solver->solve($psgi_env, $log, '0a92431', {});
114 ok($res, 'resolved without hints');
119 path_b => 'CONTRIBUTING'
121 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
122 $solver->solve($psgi_env, $log, '0a92431', $hints);
124 # don't compare ::Git objects:
125 shift @$res; shift @$hinted;
126 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
128 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
130 require_mods(@psgi, 7 + scalar(@psgi));
131 use_ok($_) for @psgi;
132 my $binfoo = "$ibx->{inboxdir}/binfoo.git";
133 my $l = "$ibx->{inboxdir}/inbox.lock";
134 -f $l or BAIL_OUT "BUG: $l missing: $!";
135 require_ok 'PublicInbox::ViewVCS';
138 $PublicInbox::ViewVCS::MAX_SIZE + 1;
140 my %bin = (big => $big_size, small => 1);
141 my %oid; # (small|big) => OID
142 my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
143 my $acq = $lk->lock_for_scope;
144 my $stamp = "$binfoo/stamp";
145 if (open my $fh, '<', $stamp) {
146 %oid = map { chomp; split(/=/, $_) } (<$fh>);
148 PublicInbox::Import::init_bare($binfoo);
149 my $cmd = [ qw(git hash-object -w --stdin) ];
150 my $env = { GIT_DIR => $binfoo };
151 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
152 while (my ($label, $size) = each %bin) {
153 pipe(my ($rin, $win)) or BAIL_OUT;
154 my $rout = popen_rd($cmd , $env, { 0 => $rin });
156 print { $win } ("\0" x $size) or BAIL_OUT;
157 close $win or BAIL_OUT;
158 chomp(my $x = <$rout>);
159 close $rout or BAIL_OUT "$?";
160 print $fh "$label=$x\n" or BAIL_OUT;
163 close $fh or BAIL_OUT;
164 rename("$stamp.$$", $stamp) or BAIL_OUT;
167 # ensure the PSGI frontend (ViewVCS) works:
168 my $name = $ibx->{name};
169 my $cfgpfx = "publicinbox.$name";
170 my $cfgpath = "$tmpdir/httpd-config";
171 open my $cfgfh, '>', $cfgpath or die;
172 print $cfgfh <<EOF or die;
173 [publicinbox "$name"]
174 address = $ibx->{-primary_address}
175 inboxdir = $ibx->{inboxdir}
176 coderepo = public-inbox
178 url = http://example.com/$name
179 [coderepo "public-inbox"]
181 cgiturl = http://example.com/public-inbox
184 cgiturl = http://example.com/binfoo
187 my $cfg = PublicInbox::Config->new($cfgpath);
188 my $www = PublicInbox::WWW->new($cfg);
191 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
194 local $SIG{__WARN__} = sub { push @warn, @_ };
195 $cb->(GET("/$name/$mid/"));
197 is_deeply(\@warn, [], 'no warnings from rendering diff');
198 like($res->content, qr!>Ω</a>!, 'omega escaped');
200 $res = $cb->(GET("/$name/3435775/s/"));
201 is($res->code, 200, 'success with existing blob');
203 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
204 is($res->code, 404, 'failure with null OID');
206 $res = $cb->(GET("/$name/$non_existent/s/"));
207 is($res->code, 404, 'failure with null OID');
209 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
210 is($res->code, 200, 'shows commit (unabbreviated)');
211 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
212 is($res->code, 200, 'shows commit (abbreviated)');
213 while (my ($label, $size) = each %bin) {
214 $res = $cb->(GET("/$name/$oid{$label}/s/"));
215 is($res->code, 200, "$label binary file");
216 ok(index($res->content, "blob $size bytes") >= 0,
217 "showed $label binary blob size");
218 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
219 is($res->code, 200, "$label raw binary download");
220 is($res->content, "\0" x $size,
221 "$label content matches");
224 test_psgi(sub { $www->call(@_) }, $client);
226 require_mods(qw(Plack::Test::ExternalServer), 7);
227 my $env = { PI_CONFIG => $cfgpath };
228 my $sock = tcp_server() or die;
229 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
230 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
231 my $td = start_script($cmd, $env, { 3 => $sock });
232 my ($h, $p) = tcp_host_port($sock);
233 my $url = "http://$h:$p";
234 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = $url;
235 Plack::Test::ExternalServer::test_psgi(client => $client);
236 skip 'no curl', 1 unless which('curl');
238 mkdir "$tmpdir/ext" // xbail "mkdir $!";
239 test_lei({tmpdir => "$tmpdir/ext"}, sub {
240 my $rurl = "$url/$name";
241 lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
242 my $eml = PublicInbox::Eml->new($lei_out);
243 is(git_sha(1, $eml)->hexdigest, $expect,
244 'blob contents output');
245 ok(!lei(qw(blob -I), $rurl, $non_existent),
246 'non-existent blob fails');