]> Sergey Matveev's repositories - public-inbox.git/blob - t/gcf2_client.t
gcf2: require git dir with OID
[public-inbox.git] / t / gcf2_client.t
1 #!perl -w
2 # Copyright (C) 2020 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 PublicInbox::TestCommon;
6 use Test::More;
7 use Cwd qw(getcwd);
8 use PublicInbox::Import;
9
10 require_mods('PublicInbox::Gcf2');
11 use_ok 'PublicInbox::Gcf2Client';
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $git_a = "$tmpdir/a.git";
14 my $git_b = "$tmpdir/b.git";
15 PublicInbox::Import::init_bare($git_a);
16 PublicInbox::Import::init_bare($git_b);
17 my $fi_data = './t/git.fast-import-data';
18 my $rdr = {};
19 open $rdr->{0}, '<', $fi_data or BAIL_OUT $!;
20 xsys([qw(git fast-import --quiet)], { GIT_DIR => $git_a }, $rdr);
21 is($?, 0, 'fast-import succeeded');
22
23 my $tree = 'fdbc43725f21f485051c17463b50185f4c3cf88c';
24 my $called = 0;
25 my $err_f = "$tmpdir/err";
26 {
27         local $ENV{PATH} = getcwd()."/blib/script:$ENV{PATH}";
28         open my $err, '>', $err_f or BAIL_OUT $!;
29         my $gcf2c = PublicInbox::Gcf2Client::new({ 2 => $err });
30         $gcf2c->cat_async("$tree $git_a", sub {
31                 my ($bref, $oid, $type, $size, $arg) = @_;
32                 is($oid, $tree, 'got expected OID');
33                 is($size, 30, 'got expected length');
34                 is($type, 'tree', 'got tree type');
35                 is(length($$bref), 30, 'got a tree');
36                 is($arg, 'hi', 'arg passed');
37                 $called++;
38         }, 'hi');
39         $gcf2c->cat_async_wait;
40
41         open $err, '<', $err_f or BAIL_OUT $!;
42         my $estr = do { local $/; <$err> };
43         is($estr, '', 'nothing in stderr');
44
45         my $trunc = substr($tree, 0, 39);
46         $gcf2c->cat_async("$trunc $git_a", sub {
47                 my ($bref, $oid, $type, $size, $arg) = @_;
48                 is(undef, $bref, 'missing bref is undef');
49                 is($oid, $trunc, 'truncated OID printed');
50                 is($type, 'missing', 'type is "missing"');
51                 is($size, undef, 'size is undef');
52                 is($arg, 'bye', 'arg passed when missing');
53                 $called++;
54         }, 'bye');
55         $gcf2c->cat_async_wait;
56
57         open $err, '<', $err_f or BAIL_OUT $!;
58         $estr = do { local $/; <$err> };
59         like($estr, qr/retrying/, 'warned about retry');
60
61         # try failed alternates lookup
62         open $err, '>', $err_f or BAIL_OUT $!;
63         $gcf2c = PublicInbox::Gcf2Client::new({ 2 => $err });
64         $gcf2c->cat_async("$tree $git_b", sub {
65                 my ($bref, $oid, $type, $size, $arg) = @_;
66                 is(undef, $bref, 'missing bref from alt is undef');
67                 $called++;
68         });
69         $gcf2c->cat_async_wait;
70         open $err, '<', $err_f or BAIL_OUT $!;
71         $estr = do { local $/; <$err> };
72         like($estr, qr/retrying/, 'warned about retry before alt update');
73
74         # now try successful alternates lookup
75         open my $alt, '>>', "$git_b/objects/info/alternates" or BAIL_OUT $!;
76         print $alt "$git_a/objects\n" or BAIL_OUT $!;
77         close $alt or BAIL_OUT;
78         my $expect = xqx(['git', "--git-dir=$git_a", qw(cat-file tree), $tree]);
79         $gcf2c->cat_async("$tree $git_a", sub {
80                 my ($bref, $oid, $type, $size, $arg) = @_;
81                 is($oid, $tree, 'oid match on alternates retry');
82                 is($$bref, $expect, 'tree content matched');
83                 $called++;
84         });
85         $gcf2c->cat_async_wait;
86 }
87 is($called, 4, 'cat_async callbacks hit');
88 done_testing;