1 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
9 use PublicInbox::Spawn qw(popen_rd);
10 require_mods(qw(DBD::SQLite Search::Xapian Plack::Util));
11 my $git_dir = xqx([qw(git rev-parse --git-dir)], undef, {2 => \(my $null)});
12 $? == 0 or plan skip_all => "$0 must be run from a git working tree";
15 # needed for alternates, and --absolute-git-dir is only in git 2.13+
16 $git_dir = abs_path($git_dir);
18 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable Git SolverGit WWW));
20 my ($inboxdir, $for_destroy) = tmpdir();
22 inboxdir => $inboxdir,
23 name => 'test-v2writable',
25 -primary_address => 'test@example.com',
27 my $ibx = PublicInbox::Inbox->new($opts);
28 my $im = PublicInbox::V2Writable->new($ibx, 1);
31 my $deliver_patch = sub ($) {
32 $im->add(eml_load($_[0]));
36 $deliver_patch->('t/solve/0001-simple-mod.patch');
37 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
38 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
40 my $git = PublicInbox::Git->new($git_dir);
41 $ibx->{-repo_objs} = [ $git ];
43 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
44 open my $log, '+>>', "$inboxdir/solve.log" or die "open: $!";
45 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
46 'HTTP_HOST' => 'example.com' };
47 $solver->solve($psgi_env, $log, '69df7d5', {});
48 ok($res, 'solved a blob!');
49 my $wt_git = $res->[0];
50 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
51 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
52 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
53 is($res->[2], 'blob', 'type specified');
54 is($res->[3], 4405, 'size returned');
56 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
57 is_deeply([$expect, 'blob', 4405],
58 [$wt_git->check($res->[1])], 'wt check works');
60 if (0) { # TODO: check this?
62 my $z = do { local $/; <$log> };
69 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
70 $s->solve($psgi_env, $log, $oid, {});
71 is($more->[1], $expect, 'resolved blob to long OID '.$i);
77 my $wt_git_dir = $wt_git->{git_dir};
79 ok(!-d $wt_git_dir, 'no references to WT held');
81 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
82 $solver->solve($psgi_env, $log, '0'x40, {});
83 is($res, undef, 'no error on z40');
85 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
86 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
87 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
88 is($res, undef, 'no error on a tag not in our repo');
90 $deliver_patch->('t/solve/0002-rename-with-modifications.patch');
91 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
92 $solver->solve($psgi_env, $log, '0a92431', {});
93 ok($res, 'resolved without hints');
98 path_b => 'CONTRIBUTING'
100 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
101 $solver->solve($psgi_env, $log, '0a92431', $hints);
103 # don't compare ::Git objects:
104 shift @$res; shift @$hinted;
105 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
107 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
109 require_mods(@psgi, 7 + scalar(@psgi));
110 use_ok($_) for @psgi;
111 my $binfoo = "$inboxdir/binfoo.git";
112 require PublicInbox::Import;
113 PublicInbox::Import::init_bare($binfoo);
114 require_ok 'PublicInbox::ViewVCS';
117 $PublicInbox::ViewVCS::MAX_SIZE + 1;
119 my %bin = (big => $big_size, small => 1);
120 my %oid; # (small|big) => OID
121 my $cmd = [ qw(git hash-object -w --stdin) ];
122 my $env = { GIT_DIR => $binfoo };
123 while (my ($label, $size) = each %bin) {
124 pipe(my ($rin, $win)) or die;
125 my $rout = popen_rd($cmd , $env, { 0 => $rin });
127 print { $win } ("\0" x $size) or die;
129 chomp($oid{$label} = <$rout>);
130 close $rout or die "$?";
133 # ensure the PSGI frontend (ViewVCS) works:
134 my $name = $ibx->{name};
135 my $cfgpfx = "publicinbox.$name";
136 my $cfgpath = "$inboxdir/httpd-config";
137 open my $cfgfh, '>', $cfgpath or die;
138 print $cfgfh <<EOF or die;
139 [publicinbox "$name"]
140 address = $ibx->{address};
142 coderepo = public-inbox
144 url = http://example.com/$name
145 [coderepo "public-inbox"]
147 cgiturl = http://example.com/public-inbox
150 cgiturl = http://example.com/binfoo
153 my $cfg = PublicInbox::Config->new($cfgpath);
154 my $www = PublicInbox::WWW->new($cfg);
155 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
158 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
161 local $SIG{__WARN__} = sub { push @warn, @_ };
162 $cb->(GET("/$name/$mid/"));
164 is_deeply(\@warn, [], 'no warnings from rendering diff');
165 like($res->content, qr!>Ω</a>!, 'omega escaped');
167 $res = $cb->(GET("/$name/3435775/s/"));
168 is($res->code, 200, 'success with existing blob');
170 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
171 is($res->code, 404, 'failure with null OID');
173 $res = $cb->(GET("/$name/$non_existent/s/"));
174 is($res->code, 404, 'failure with null OID');
176 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
177 is($res->code, 200, 'shows commit (unabbreviated)');
178 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
179 is($res->code, 200, 'shows commit (abbreviated)');
180 while (my ($label, $size) = each %bin) {
181 $res = $cb->(GET("/$name/$oid{$label}/s/"));
182 is($res->code, 200, "$label binary file");
183 ok(index($res->content, "blob $size bytes") >= 0,
184 "showed $label binary blob size");
185 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
186 is($res->code, 200, "$label raw binary download");
187 is($res->content, "\0" x $size,
188 "$label content matches");
191 test_psgi(sub { $www->call(@_) }, $client);
193 require_mods(qw(Plack::Test::ExternalServer), 7);
194 my $env = { PI_CONFIG => $cfgpath };
195 my $sock = tcp_server() or die;
196 my ($out, $err) = map { "$inboxdir/std$_.log" } qw(out err);
197 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
198 my $td = start_script($cmd, $env, { 3 => $sock });
199 my ($h, $p) = ($sock->sockhost, $sock->sockport);
200 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
201 Plack::Test::ExternalServer::test_psgi(client => $client);