1 # Copyright (C) 2019-2020 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';
39 my $git = PublicInbox::Git->new($git_dir);
40 $ibx->{-repo_objs} = [ $git ];
42 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
43 open my $log, '+>>', "$inboxdir/solve.log" or die "open: $!";
44 my $psgi_env = { 'psgi.errors' => *STDERR, 'psgi.url_scheme' => 'http',
45 'HTTP_HOST' => 'example.com' };
46 $solver->solve($psgi_env, $log, '69df7d5', {});
47 ok($res, 'solved a blob!');
48 my $wt_git = $res->[0];
49 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
50 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
51 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
52 is($res->[2], 'blob', 'type specified');
53 is($res->[3], 4405, 'size returned');
55 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
56 is_deeply([$expect, 'blob', 4405],
57 [$wt_git->check($res->[1])], 'wt check works');
59 if (0) { # TODO: check this?
61 my $z = do { local $/; <$log> };
68 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
69 $s->solve($psgi_env, $log, $oid, {});
70 is($more->[1], $expect, 'resolved blob to long OID '.$i);
76 my $wt_git_dir = $wt_git->{git_dir};
78 ok(!-d $wt_git_dir, 'no references to WT held');
80 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
81 $solver->solve($psgi_env, $log, '0'x40, {});
82 is($res, undef, 'no error on z40');
84 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
85 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
86 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
87 is($res, undef, 'no error on a tag not in our repo');
89 $deliver_patch->('t/solve/0002-rename-with-modifications.patch');
90 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
91 $solver->solve($psgi_env, $log, '0a92431', {});
92 ok($res, 'resolved without hints');
97 path_b => 'CONTRIBUTING'
99 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
100 $solver->solve($psgi_env, $log, '0a92431', $hints);
102 # don't compare ::Git objects:
103 shift @$res; shift @$hinted;
104 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
106 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
108 require_mods(@psgi, 7 + scalar(@psgi));
109 use_ok($_) for @psgi;
110 my $binfoo = "$inboxdir/binfoo.git";
111 require PublicInbox::Import;
112 PublicInbox::Import::init_bare($binfoo);
113 require_ok 'PublicInbox::ViewVCS';
116 $PublicInbox::ViewVCS::MAX_SIZE + 1;
118 my %bin = (big => $big_size, small => 1);
119 my %oid; # (small|big) => OID
120 my $cmd = [ qw(git hash-object -w --stdin) ];
121 my $env = { GIT_DIR => $binfoo };
122 while (my ($label, $size) = each %bin) {
123 pipe(my ($rin, $win)) or die;
124 my $rout = popen_rd($cmd , $env, { 0 => $rin });
126 print { $win } ("\0" x $size) or die;
128 chomp($oid{$label} = <$rout>);
129 close $rout or die "$?";
132 # ensure the PSGI frontend (ViewVCS) works:
133 my $name = $ibx->{name};
134 my $cfgpfx = "publicinbox.$name";
135 my $cfgpath = "$inboxdir/httpd-config";
136 open my $cfgfh, '>', $cfgpath or die;
137 print $cfgfh <<EOF or die;
138 [publicinbox "$name"]
139 address = $ibx->{address};
141 coderepo = public-inbox
143 url = http://example.com/$name
144 [coderepo "public-inbox"]
146 cgiturl = http://example.com/public-inbox
149 cgiturl = http://example.com/binfoo
152 my $cfg = PublicInbox::Config->new($cfgpath);
153 my $www = PublicInbox::WWW->new($cfg);
154 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
157 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
160 local $SIG{__WARN__} = sub { push @warn, @_ };
161 $cb->(GET("/$name/$mid/"));
163 is_deeply(\@warn, [], 'no warnings from rendering diff');
164 like($res->content, qr!>Ω</a>!, 'omega escaped');
166 $res = $cb->(GET("/$name/3435775/s/"));
167 is($res->code, 200, 'success with existing blob');
169 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
170 is($res->code, 404, 'failure with null OID');
172 $res = $cb->(GET("/$name/$non_existent/s/"));
173 is($res->code, 404, 'failure with null OID');
175 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
176 is($res->code, 200, 'shows commit');
177 while (my ($label, $size) = each %bin) {
178 $res = $cb->(GET("/$name/$oid{$label}/s/"));
179 is($res->code, 200, "$label binary file");
180 ok(index($res->content, "blob $size bytes") >= 0,
181 "showed $label binary blob size");
182 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
183 is($res->code, 200, "$label raw binary download");
184 is($res->content, "\0" x $size,
185 "$label content matches");
188 test_psgi(sub { $www->call(@_) }, $client);
190 require_mods(qw(Plack::Test::ExternalServer), 7);
191 my $env = { PI_CONFIG => $cfgpath };
192 my $sock = tcp_server() or die;
193 my ($out, $err) = map { "$inboxdir/std$_.log" } qw(out err);
194 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
195 my $td = start_script($cmd, $env, { 3 => $sock });
196 my ($h, $p) = ($sock->sockhost, $sock->sockport);
197 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
198 Plack::Test::ExternalServer::test_psgi(client => $client);