]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
solver: check one git coderepo and inbox at a time
[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 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
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                 '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');
55
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');
59
60 if (0) { # TODO: check this?
61         seek($log, 0, 0);
62         my $z = do { local $/; <$log> };
63         diag $z;
64 }
65
66 my $oid = $expect;
67 for my $i (1..2) {
68         my $more;
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);
72         chop($oid);
73 }
74
75 $solver = undef;
76 $res = undef;
77 my $wt_git_dir = $wt_git->{git_dir};
78 $wt_git = undef;
79 ok(!-d $wt_git_dir, 'no references to WT held');
80
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');
84
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');
89
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');
94
95 my $hints = {
96         oid_a => '3435775',
97         path_a => 'HACKING',
98         path_b => 'CONTRIBUTING'
99 };
100 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
101 $solver->solve($psgi_env, $log, '0a92431', $hints);
102 my $hinted = $res;
103 # don't compare ::Git objects:
104 shift @$res; shift @$hinted;
105 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
106
107 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
108 SKIP: {
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';
115         my $big_size = do {
116                 no warnings 'once';
117                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
118         };
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 });
126                 $rin = undef;
127                 print { $win } ("\0" x $size) or die;
128                 close $win or die;
129                 chomp($oid{$label} = <$rout>);
130                 close $rout or die "$?";
131         }
132
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};
141         inboxdir = $inboxdir
142         coderepo = public-inbox
143         coderepo = binfoo
144         url = http://example.com/$name
145 [coderepo "public-inbox"]
146         dir = $git_dir
147         cgiturl = http://example.com/public-inbox
148 [coderepo "binfoo"]
149         dir = $binfoo
150         cgiturl = http://example.com/binfoo
151 EOF
152         close $cfgfh or die;
153         my $cfg = PublicInbox::Config->new($cfgpath);
154         my $www = PublicInbox::WWW->new($cfg);
155         my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
156         my $client = sub {
157                 my ($cb) = @_;
158                 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
159                 my @warn;
160                 my $res = do {
161                         local $SIG{__WARN__} = sub { push @warn, @_ };
162                         $cb->(GET("/$name/$mid/"));
163                 };
164                 is_deeply(\@warn, [], 'no warnings from rendering diff');
165                 like($res->content, qr!>&#937;</a>!, 'omega escaped');
166
167                 $res = $cb->(GET("/$name/3435775/s/"));
168                 is($res->code, 200, 'success with existing blob');
169
170                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
171                 is($res->code, 404, 'failure with null OID');
172
173                 $res = $cb->(GET("/$name/$non_existent/s/"));
174                 is($res->code, 404, 'failure with null OID');
175
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");
189                 }
190         };
191         test_psgi(sub { $www->call(@_) }, $client);
192         SKIP: {
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);
202         }
203 }
204
205 done_testing();