]> Sergey Matveev's repositories - public-inbox.git/blob - t/git.t
git: fatalize async callback errors by default
[public-inbox.git] / t / git.t
1 # Copyright (C) 2015-2021 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 Test::More;
5 use PublicInbox::TestCommon;
6 my ($dir, $for_destroy) = tmpdir();
7 use PublicInbox::Import;
8 use POSIX qw(strftime);
9
10 use_ok 'PublicInbox::Git';
11
12 {
13         PublicInbox::Import::init_bare($dir);
14         my $fi_data = './t/git.fast-import-data';
15         open my $fh, '<', $fi_data or die
16                 "fast-import data readable (or run test at top level: $!";
17         my $rdr = { 0 => $fh };
18         xsys([qw(git fast-import --quiet)], { GIT_DIR => $dir }, $rdr);
19         is($?, 0, 'fast-import succeeded');
20 }
21 {
22         my $git = PublicInbox::Git->new($dir);
23         my @s = $git->date_parse('1970-01-01T00:00:00Z');
24         is($s[0], 0, 'parsed epoch');
25         local $ENV{TZ} = 'UTC';
26         @s = $git->date_parse('1993-10-02 01:02:09', '2010-10-02 01:03:04');
27         is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s[0])),
28                 '1993-10-02T01:02:09Z', 'round trips');
29         is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s[1])),
30                 '2010-10-02T01:03:04Z', '2nd arg round trips');
31         @s = $git->date_parse('1993-10-02');
32         is(strftime('%Y-%m-%d', gmtime($s[0])), '1993-10-02',
33                 'round trips date-only');
34 }
35
36 {
37         my $gcf = PublicInbox::Git->new($dir);
38         is($gcf->modified, 749520000, 'modified time detected from commit');
39         my $f = 'HEAD:foo.txt';
40         my @x = $gcf->check($f);
41         is(scalar @x, 3, 'returned 3 element array for existing file');
42         like($x[0], qr/\A[a-f0-9]{40}\z/, 'returns obj ID in 1st element');
43         is('blob', $x[1], 'returns obj type in 2nd element');
44         like($x[2], qr/\A\d+\z/, 'returns obj size in 3rd element');
45
46         my $raw = $gcf->cat_file($f);
47         is($x[2], length($$raw), 'length matches');
48
49         is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
50         is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
51
52         my $oid = $x[0];
53         my $arg = { 'foo' => 'bar' };
54         my $res = [];
55         my $missing = [];
56         $gcf->cat_async($oid, sub {
57                 my ($bref, $oid_hex, $type, $size, $arg) = @_;
58                 $res = [ @_ ];
59         }, $arg);
60         $gcf->cat_async('non-existent', sub {
61                 my ($bref, $oid_hex, $type, $size, $arg) = @_;
62                 $missing = [ @_ ];
63         }, $arg);
64         $gcf->async_wait_all;
65         my ($bref, $oid_hex, $type, $size, $arg_res) = @$res;
66         is_deeply([$oid_hex, $type, $size], \@x, 'got expected header');
67         is($arg_res, $arg, 'arg passed to cat_async');
68         is_deeply($raw, $bref, 'blob result matches');
69         is_deeply($missing, [ undef, 'non-existent', 'missing', undef, $arg],
70                 'non-existent blob gives expected result');
71
72         $res = [];
73         $gcf->cat_async($oid, sub { push @$res, \@_ });
74         $gcf->cat_async($oid, sub { die 'HI' });
75         $gcf->cat_async($oid, sub { push @$res, \@_ });
76         eval { $gcf->async_wait_all };
77         like($@, qr/\bHI\b/, 'die in callback propagates');
78         is(scalar(@$res), 2, 'two results');
79         is_deeply($res->[0], [ $raw, @x, undef ], '1st cb result');
80         is_deeply($res->[1], [ undef, $oid, undef, undef, undef ],
81                 '2nd cb aborted ');
82
83         my @w;
84         local $PublicInbox::Git::async_warn = 1;
85         local $SIG{__WARN__} = sub { push @w, @_ };
86         $res = [];
87         $gcf->cat_async($oid, sub { push @$res, \@_ });
88         $gcf->cat_async($oid, sub { die 'HI' });
89         $gcf->cat_async($oid, sub { push @$res, \@_ });
90         eval { $gcf->async_wait_all };
91         is(scalar(@$res), 2, 'two results');
92         is_deeply($res->[0], [ $raw, @x, undef ], '1st cb result');
93         is_deeply($res->[1], [ $raw, @x, undef ], '2st cb result');
94         like("@w", qr/\bHI\b/, 'die turned to warning');
95 }
96
97 if (1) {
98         # need a big file, use the AGPL-3.0 :p
99         my $big_data = './COPYING';
100         ok(-r $big_data, 'COPYING readable');
101         my $size = -s $big_data;
102         ok($size > 8192, 'file is big enough');
103         open my $fh, '<', $big_data or die;
104         my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
105         my $buf = xqx($cmd, { GIT_DIR => $dir }, { 0 => $fh });
106         is(0, $?, 'hashed object successfully');
107         chomp $buf;
108
109         my $gcf = PublicInbox::Git->new($dir);
110         my @x = $gcf->cat_file($buf);
111         is($x[2], 'blob', 'got blob on wantarray');
112         is($x[3], $size, 'got correct size ref on big file');
113         is(length(${$x[0]}), $size, 'read correct number of bytes');
114
115         my $ref = $gcf->qx(qw(cat-file blob), $buf);
116         is($?, 0, 'no error on scalar success');
117         my @ref = $gcf->qx(qw(cat-file blob), $buf);
118         is($?, 0, 'no error on wantarray success');
119         my $nl = scalar @ref;
120         ok($nl > 1, "qx returned array length of $nl");
121         is(join('', @ref), $ref, 'qx array and scalar context both work');
122
123         $gcf->qx(qw(repack -adq));
124         ok($gcf->packed_bytes > 0, 'packed size is positive');
125         my $rdr;
126         open $rdr->{2}, '+>', '/dev/null' or xbail "open $!";
127         $gcf->qx([qw(rev-parse --verify bogus)], undef, $rdr);
128         isnt($?, 0, '$? set on failure: '.$?);
129 }
130
131 SKIP: {
132         require_git(2.6, 7) or skip('need git 2.6+ for --batch-all-objects', 7);
133         my ($alt, $alt_obj) = tmpdir();
134         my $hash_obj = [ 'git', "--git-dir=$alt", qw(hash-object -w --stdin) ];
135         PublicInbox::Import::init_bare($alt);
136         open my $fh, '<', "$alt/config" or die "open failed: $!\n";
137         chomp(my $remote = xqx($hash_obj, undef, { 0 => $fh }));
138         my $gcf = PublicInbox::Git->new($dir);
139         is($gcf->cat_file($remote), undef, "remote file not found");
140         open $fh, '>>', "$dir/objects/info/alternates" or
141                         die "open failed: $!\n";
142         print $fh "$alt/objects\n" or die "print failed: $!\n";
143         close $fh or die "close failed: $!";
144         my $found = $gcf->cat_file($remote);
145         open $fh, '<', "$alt/config" or die "open failed: $!\n";
146         my $config = eval { local $/; <$fh> };
147         is($$found, $config, 'alternates reloaded');
148
149         # with the async interface
150         my ($async_alt, $async_dir_obj) = tmpdir();
151         PublicInbox::Import::init_bare($async_alt);
152         my @exist = map { chomp; [ split / / ] } (xqx(['git', "--git-dir=$dir",
153                         qw(cat-file --batch-all-objects --batch-check)]));
154         my $results = [];
155         my $cb = sub {
156                 my ($bref, $oid, $type, $size) = @_;
157                 push @$results, [ $oid, $type, $size ];
158         };
159         for my $i (0..5) {
160                 $gcf->cat_async($exist[$i]->[0], $cb, $results);
161                 next if $i != 3;
162
163                 # stick a new alternate into a running async pipeline
164                 $hash_obj->[1] = "--git-dir=$async_alt";
165                 $remote = xqx($hash_obj, undef, { 0 => \'async' });
166                 chomp $remote;
167                 open $fh, '>>', "$dir/objects/info/alternates" or
168                                 die "open failed: $!\n";
169                 print $fh "$async_alt/objects\n" or die "print failed: $!\n";
170                 close $fh or die "close failed: $!";
171                 # trigger cat_async_retry:
172                 $gcf->cat_async($remote, $cb, $results);
173         }
174         $gcf->async_wait_all;
175         my $expect = [ @exist[0..3], [ $remote, 'blob', 5 ], @exist[4..5] ];
176         is_deeply($results, $expect, 'got expected results');
177
178         ok(!$gcf->cleanup, 'cleanup can expire');
179         ok(!$gcf->cleanup, 'cleanup idempotent');
180
181         my $t = $gcf->modified;
182         ok($t <= time, 'repo not modified in the future');
183         isnt($t, 0, 'repo not modified in 1970')
184 }
185
186 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
187 my $s;
188 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
189 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
190 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
191 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
192
193 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
194 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
195 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
196 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
197 is(git_quote($s = "hello\x06world"), '"hello\\006world"', 'quoted \\x06');
198 is(git_unquote($s = '"hello\\006world"'), "hello\x06world", 'unquoted \\x06');
199
200 done_testing();