]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
testcommon: add require_mods method and use it
[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));
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 is('public-inbox 1.0.0',
42         $git->commit_title($v1_0_0_tag),
43         'commit_title works on 1.0.0');
44
45 is(undef, $git->commit_title('impossible'), 'undef on impossible object');
46
47 $ibx->{-repo_objs} = [ $git ];
48 my $res;
49 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
50 open my $log, '+>>', "$inboxdir/solve.log" or die "open: $!";
51 my $psgi_env = { 'psgi.errors' => *STDERR };
52 $solver->solve($psgi_env, $log, '69df7d5', {});
53 ok($res, 'solved a blob!');
54 my $wt_git = $res->[0];
55 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
56 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
57 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
58 is($res->[2], 'blob', 'type specified');
59 is($res->[3], 4405, 'size returned');
60
61 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
62 is_deeply([$expect, 'blob', 4405],
63           [$wt_git->check($res->[1])], 'wt check works');
64
65 if (0) { # TODO: check this?
66         seek($log, 0, 0);
67         my $z = do { local $/; <$log> };
68         diag $z;
69 }
70
71 my $oid = $expect;
72 for my $i (1..2) {
73         my $more;
74         my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
75         $s->solve($psgi_env, $log, $oid, {});
76         is($more->[1], $expect, 'resolved blob to long OID '.$i);
77         chop($oid);
78 }
79
80 $solver = undef;
81 $res = undef;
82 my $wt_git_dir = $wt_git->{git_dir};
83 $wt_git = undef;
84 ok(!-d $wt_git_dir, 'no references to WT held');
85
86 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
87 $solver->solve($psgi_env, $log, '0'x40, {});
88 is($res, undef, 'no error on z40');
89
90 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
91 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
92 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
93 is($res, undef, 'no error on a tag not in our repo');
94
95 $deliver_patch->('t/solve/0002-rename-with-modifications.patch');
96 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
97 $solver->solve($psgi_env, $log, '0a92431', {});
98 ok($res, 'resolved without hints');
99
100 my $hints = {
101         oid_a => '3435775',
102         path_a => 'HACKING',
103         path_b => 'CONTRIBUTING'
104 };
105 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
106 $solver->solve($psgi_env, $log, '0a92431', $hints);
107 my $hinted = $res;
108 # don't compare ::Git objects:
109 shift @$res; shift @$hinted;
110 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
111
112 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
113 SKIP: {
114         require_mods(@psgi, 7 + scalar(@psgi));
115         use_ok($_) for @psgi;
116         my $binfoo = "$inboxdir/binfoo.git";
117         system(qw(git init --bare -q), $binfoo) == 0 or die "git init: $?";
118         require_ok 'PublicInbox::ViewVCS';
119         my $big_size = do {
120                 no warnings 'once';
121                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
122         };
123         my %bin = (big => $big_size, small => 1);
124         my %oid; # (small|big) => OID
125         my $cmd = [ qw(git hash-object -w --stdin) ];
126         my $env = { GIT_DIR => $binfoo };
127         while (my ($label, $size) = each %bin) {
128                 pipe(my ($rout, $wout)) or die;
129                 pipe(my ($rin, $win)) or die;
130                 my $rdr = { 0 => fileno($rin), 1 => fileno($wout) };
131                 my $pid = spawn($cmd , $env, $rdr);
132                 $wout = $rin = undef;
133                 print { $win } ("\0" x $size) or die;
134                 close $win or die;
135                 chomp($oid{$label} = <$rout>);
136         }
137
138         # ensure the PSGI frontend (ViewVCS) works:
139         my $name = $ibx->{name};
140         my $cfgpfx = "publicinbox.$name";
141         my $cfg = PublicInbox::Config->new(\<<EOF);
142 $cfgpfx.address=$ibx->{address};
143 $cfgpfx.inboxdir=$inboxdir
144 $cfgpfx.coderepo=public-inbox
145 $cfgpfx.coderepo=binfoo
146 coderepo.public-inbox.dir=$git_dir
147 coderepo.public-inbox.cgiturl=http://example.com/public-inbox
148 coderepo.binfoo.dir=$binfoo
149 coderepo.binfoo.cgiturl=http://example.com/binfoo
150 EOF
151         my $www = PublicInbox::WWW->new($cfg);
152         test_psgi(sub { $www->call(@_) }, sub {
153                 my ($cb) = @_;
154                 my $res = $cb->(GET("/$name/3435775/s/"));
155                 is($res->code, 200, 'success with existing blob');
156
157                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
158                 is($res->code, 404, 'failure with null OID');
159
160                 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
161                 is($res->code, 200, 'shows commit');
162                 while (my ($label, $size) = each %bin) {
163                         $res = $cb->(GET("/$name/$oid{$label}/s/"));
164                         is($res->code, 200, "$label binary file");
165                         ok(index($res->content, "blob $size bytes") >= 0,
166                                 "showed $label binary blob size");
167                         $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
168                         is($res->code, 200, "$label raw binary download");
169                         is($res->content, "\0" x $size,
170                                 "$label content matches");
171                 }
172         });
173 }
174
175 done_testing();