]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
make Plack optional for non-WWW and non-httpd users
[public-inbox.git] / t / solver_git.t
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Cwd qw(abs_path);
7 use PublicInbox::TestCommon;
8 require_git(2.6);
9 use PublicInbox::Spawn qw(spawn);
10 require_mods(qw(DBD::SQLite Search::Xapian Plack::Util));
11 chomp(my $git_dir = `git rev-parse --git-dir 2>/dev/null`);
12 plan skip_all => "$0 must be run from a git working tree" if $?;
13
14 # needed for alternates, and --absolute-git-dir is only in git 2.13+
15 $git_dir = abs_path($git_dir);
16
17 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable MIME Git SolverGit WWW));
18
19 my ($inboxdir, $for_destroy) = tmpdir();
20 my $opts = {
21         inboxdir => $inboxdir,
22         name => 'test-v2writable',
23         version => 2,
24         -primary_address => 'test@example.com',
25 };
26 my $ibx = PublicInbox::Inbox->new($opts);
27 my $im = PublicInbox::V2Writable->new($ibx, 1);
28 $im->{parallel} = 0;
29
30 my $deliver_patch = sub ($) {
31         open my $fh, '<', $_[0] or die "open: $!";
32         my $mime = PublicInbox::MIME->new(do { local $/; <$fh> });
33         $im->add($mime);
34         $im->done;
35 };
36
37 $deliver_patch->('t/solve/0001-simple-mod.patch');
38 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
39
40 my $git = PublicInbox::Git->new($git_dir);
41 $ibx->{-repo_objs} = [ $git ];
42 my $res;
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 $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');
54
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');
58
59 if (0) { # TODO: check this?
60         seek($log, 0, 0);
61         my $z = do { local $/; <$log> };
62         diag $z;
63 }
64
65 my $oid = $expect;
66 for my $i (1..2) {
67         my $more;
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);
71         chop($oid);
72 }
73
74 $solver = undef;
75 $res = undef;
76 my $wt_git_dir = $wt_git->{git_dir};
77 $wt_git = undef;
78 ok(!-d $wt_git_dir, 'no references to WT held');
79
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');
83
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');
88
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');
93
94 my $hints = {
95         oid_a => '3435775',
96         path_a => 'HACKING',
97         path_b => 'CONTRIBUTING'
98 };
99 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
100 $solver->solve($psgi_env, $log, '0a92431', $hints);
101 my $hinted = $res;
102 # don't compare ::Git objects:
103 shift @$res; shift @$hinted;
104 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
105
106 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
107 SKIP: {
108         require_mods(@psgi, 7 + scalar(@psgi));
109         use_ok($_) for @psgi;
110         my $binfoo = "$inboxdir/binfoo.git";
111         system(qw(git init --bare -q), $binfoo) == 0 or die "git init: $?";
112         require_ok 'PublicInbox::ViewVCS';
113         my $big_size = do {
114                 no warnings 'once';
115                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
116         };
117         my %bin = (big => $big_size, small => 1);
118         my %oid; # (small|big) => OID
119         my $cmd = [ qw(git hash-object -w --stdin) ];
120         my $env = { GIT_DIR => $binfoo };
121         while (my ($label, $size) = each %bin) {
122                 pipe(my ($rout, $wout)) or die;
123                 pipe(my ($rin, $win)) or die;
124                 my $rdr = { 0 => $rin, 1 => $wout };
125                 my $pid = spawn($cmd , $env, $rdr);
126                 $wout = $rin = undef;
127                 print { $win } ("\0" x $size) or die;
128                 close $win or die;
129                 chomp($oid{$label} = <$rout>);
130         }
131
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};
140         inboxdir = $inboxdir
141         coderepo = public-inbox
142         coderepo = binfoo
143         url = http://example.com/$name
144 [coderepo "public-inbox"]
145         dir = $git_dir
146         cgiturl = http://example.com/public-inbox
147 [coderepo "binfoo"]
148         dir = $binfoo
149         cgiturl = http://example.com/binfoo
150 EOF
151         close $cfgfh or die;
152         my $cfg = PublicInbox::Config->new($cfgpath);
153         my $www = PublicInbox::WWW->new($cfg);
154         my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
155         my $client = sub {
156                 my ($cb) = @_;
157                 my $res = $cb->(GET("/$name/3435775/s/"));
158                 is($res->code, 200, 'success with existing blob');
159
160                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
161                 is($res->code, 404, 'failure with null OID');
162
163                 $res = $cb->(GET("/$name/$non_existent/s/"));
164                 is($res->code, 404, 'failure with null OID');
165
166                 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
167                 is($res->code, 200, 'shows commit');
168                 while (my ($label, $size) = each %bin) {
169                         $res = $cb->(GET("/$name/$oid{$label}/s/"));
170                         is($res->code, 200, "$label binary file");
171                         ok(index($res->content, "blob $size bytes") >= 0,
172                                 "showed $label binary blob size");
173                         $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
174                         is($res->code, 200, "$label raw binary download");
175                         is($res->content, "\0" x $size,
176                                 "$label content matches");
177                 }
178         };
179         test_psgi(sub { $www->call(@_) }, $client);
180         SKIP: {
181                 require_mods(qw(Plack::Test::ExternalServer), 7);
182                 my $env = { PI_CONFIG => $cfgpath };
183                 my $sock = tcp_server() or die;
184                 my ($out, $err) = map { "$inboxdir/std$_.log" } qw(out err);
185                 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
186                 my $td = start_script($cmd, $env, { 3 => $sock });
187                 my ($h, $p) = ($sock->sockhost, $sock->sockport);
188                 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
189                 Plack::Test::ExternalServer::test_psgi(client => $client);
190         }
191 }
192
193 done_testing();