]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
use "\&" where possible when referring to subroutines
[public-inbox.git] / t / solver_git.t
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>
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(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";
13 chomp $git_dir;
14
15 # needed for alternates, and --absolute-git-dir is only in git 2.13+
16 $git_dir = abs_path($git_dir);
17
18 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable Git SolverGit WWW));
19
20 my ($inboxdir, $for_destroy) = tmpdir();
21 my $opts = {
22         inboxdir => $inboxdir,
23         name => 'test-v2writable',
24         version => 2,
25         -primary_address => 'test@example.com',
26 };
27 my $ibx = PublicInbox::Inbox->new($opts);
28 my $im = PublicInbox::V2Writable->new($ibx, 1);
29 $im->{parallel} = 0;
30
31 my $deliver_patch = sub ($) {
32         $im->add(eml_load($_[0]));
33         $im->done;
34 };
35
36 $deliver_patch->('t/solve/0001-simple-mod.patch');
37 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
38
39 my $git = PublicInbox::Git->new($git_dir);
40 $ibx->{-repo_objs} = [ $git ];
41 my $res;
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');
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         require PublicInbox::Import;
112         PublicInbox::Import::init_bare($binfoo);
113         require_ok 'PublicInbox::ViewVCS';
114         my $big_size = do {
115                 no warnings 'once';
116                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
117         };
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 });
125                 $rin = undef;
126                 print { $win } ("\0" x $size) or die;
127                 close $win or die;
128                 chomp($oid{$label} = <$rout>);
129                 close $rout or die "$?";
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 $mid = '20190401081523.16213-1-BOFH@YHBT.net';
158                 my @warn;
159                 my $res = do {
160                         local $SIG{__WARN__} = sub { push @warn, @_ };
161                         $cb->(GET("/$name/$mid/"));
162                 };
163                 is_deeply(\@warn, [], 'no warnings from rendering diff');
164                 like($res->content, qr!>&#937;</a>!, 'omega escaped');
165
166                 $res = $cb->(GET("/$name/3435775/s/"));
167                 is($res->code, 200, 'success with existing blob');
168
169                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
170                 is($res->code, 404, 'failure with null OID');
171
172                 $res = $cb->(GET("/$name/$non_existent/s/"));
173                 is($res->code, 404, 'failure with null OID');
174
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");
186                 }
187         };
188         test_psgi(sub { $www->call(@_) }, $client);
189         SKIP: {
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);
199         }
200 }
201
202 done_testing();