]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
99ffb9e353cb691c125e5866ebfdfc8474e84f90
[public-inbox.git] / t / solver_git.t
1 #!perl -w
2 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 use Cwd qw(abs_path);
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 ($tmpdir, $for_destroy) = tmpdir();
21 my $ibx = create_inbox 'v2', version => 2,
22                         indexlevel => 'medium', sub {
23         my ($im) = @_;
24         $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
25         $im->add(eml_load 't/solve/0002-rename-with-modifications.patch') or
26                 BAIL_OUT;
27 };
28 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
29 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
30 my $git = PublicInbox::Git->new($git_dir);
31 $ibx->{-repo_objs} = [ $git ];
32 my $res;
33 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
34 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
35 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
36                 'HTTP_HOST' => 'example.com' };
37 $solver->solve($psgi_env, $log, '69df7d5', {});
38 ok($res, 'solved a blob!');
39 my $wt_git = $res->[0];
40 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
41 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
42 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
43 is($res->[2], 'blob', 'type specified');
44 is($res->[3], 4405, 'size returned');
45
46 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
47 is_deeply([$expect, 'blob', 4405],
48           [$wt_git->check($res->[1])], 'wt check works');
49
50 my $oid = $expect;
51 for my $i (1..2) {
52         my $more;
53         my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
54         $s->solve($psgi_env, $log, $oid, {});
55         is($more->[1], $expect, 'resolved blob to long OID '.$i);
56         chop($oid);
57 }
58
59 $solver = undef;
60 $res = undef;
61 my $wt_git_dir = $wt_git->{git_dir};
62 $wt_git = undef;
63 ok(!-d $wt_git_dir, 'no references to WT held');
64
65 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
66 $solver->solve($psgi_env, $log, '0'x40, {});
67 is($res, undef, 'no error on z40');
68
69 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
70 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
71 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
72 is($res, undef, 'no error on a tag not in our repo');
73
74 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
75 $solver->solve($psgi_env, $log, '0a92431', {});
76 ok($res, 'resolved without hints');
77
78 my $hints = {
79         oid_a => '3435775',
80         path_a => 'HACKING',
81         path_b => 'CONTRIBUTING'
82 };
83 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
84 $solver->solve($psgi_env, $log, '0a92431', $hints);
85 my $hinted = $res;
86 # don't compare ::Git objects:
87 shift @$res; shift @$hinted;
88 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
89
90 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
91 SKIP: {
92         require_mods(@psgi, 7 + scalar(@psgi));
93         use_ok($_) for @psgi;
94         my $binfoo = "$ibx->{inboxdir}/binfoo.git";
95         my $l = "$ibx->{inboxdir}/inbox.lock";
96         -f $l or BAIL_OUT "BUG: $l missing: $!";
97         require_ok 'PublicInbox::ViewVCS';
98         my $big_size = do {
99                 no warnings 'once';
100                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
101         };
102         my %bin = (big => $big_size, small => 1);
103         my %oid; # (small|big) => OID
104         my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
105         my $acq = $lk->lock_for_scope;
106         my $stamp = "$binfoo/stamp";
107         if (open my $fh, '<', $stamp) {
108                 %oid = map { chomp; split(/=/, $_) } (<$fh>);
109         } else {
110                 PublicInbox::Import::init_bare($binfoo);
111                 my $cmd = [ qw(git hash-object -w --stdin) ];
112                 my $env = { GIT_DIR => $binfoo };
113                 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
114                 while (my ($label, $size) = each %bin) {
115                         pipe(my ($rin, $win)) or BAIL_OUT;
116                         my $rout = popen_rd($cmd , $env, { 0 => $rin });
117                         $rin = undef;
118                         print { $win } ("\0" x $size) or BAIL_OUT;
119                         close $win or BAIL_OUT;
120                         chomp(my $x = <$rout>);
121                         close $rout or BAIL_OUT "$?";
122                         print $fh "$label=$x\n" or BAIL_OUT;
123                         $oid{$label} = $x;
124                 }
125                 close $fh or BAIL_OUT;
126                 rename("$stamp.$$", $stamp) or BAIL_OUT;
127         }
128         undef $acq;
129         # ensure the PSGI frontend (ViewVCS) works:
130         my $name = $ibx->{name};
131         my $cfgpfx = "publicinbox.$name";
132         my $cfgpath = "$tmpdir/httpd-config";
133         open my $cfgfh, '>', $cfgpath or die;
134         print $cfgfh <<EOF or die;
135 [publicinbox "$name"]
136         address = $ibx->{-primary_address}
137         inboxdir = $ibx->{inboxdir}
138         coderepo = public-inbox
139         coderepo = binfoo
140         url = http://example.com/$name
141 [coderepo "public-inbox"]
142         dir = $git_dir
143         cgiturl = http://example.com/public-inbox
144 [coderepo "binfoo"]
145         dir = $binfoo
146         cgiturl = http://example.com/binfoo
147 EOF
148         close $cfgfh or die;
149         my $cfg = PublicInbox::Config->new($cfgpath);
150         my $www = PublicInbox::WWW->new($cfg);
151         my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
152         my $client = sub {
153                 my ($cb) = @_;
154                 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
155                 my @warn;
156                 my $res = do {
157                         local $SIG{__WARN__} = sub { push @warn, @_ };
158                         $cb->(GET("/$name/$mid/"));
159                 };
160                 is_deeply(\@warn, [], 'no warnings from rendering diff');
161                 like($res->content, qr!>&#937;</a>!, 'omega escaped');
162
163                 $res = $cb->(GET("/$name/3435775/s/"));
164                 is($res->code, 200, 'success with existing blob');
165
166                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
167                 is($res->code, 404, 'failure with null OID');
168
169                 $res = $cb->(GET("/$name/$non_existent/s/"));
170                 is($res->code, 404, 'failure with null OID');
171
172                 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
173                 is($res->code, 200, 'shows commit (unabbreviated)');
174                 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
175                 is($res->code, 200, 'shows commit (abbreviated)');
176                 while (my ($label, $size) = each %bin) {
177                         $res = $cb->(GET("/$name/$oid{$label}/s/"));
178                         is($res->code, 200, "$label binary file");
179                         ok(index($res->content, "blob $size bytes") >= 0,
180                                 "showed $label binary blob size");
181                         $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
182                         is($res->code, 200, "$label raw binary download");
183                         is($res->content, "\0" x $size,
184                                 "$label content matches");
185                 }
186         };
187         test_psgi(sub { $www->call(@_) }, $client);
188         SKIP: {
189                 require_mods(qw(Plack::Test::ExternalServer), 7);
190                 my $env = { PI_CONFIG => $cfgpath };
191                 my $sock = tcp_server() or die;
192                 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
193                 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
194                 my $td = start_script($cmd, $env, { 3 => $sock });
195                 my ($h, $p) = tcp_host_port($sock);
196                 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
197                 Plack::Test::ExternalServer::test_psgi(client => $client);
198         }
199 }
200
201 done_testing();