]> Sergey Matveev's repositories - public-inbox.git/blob - t/git.t
testcommon: spawn-aware system() and qx[] workalikes
[public-inbox.git] / t / git.t
1 # Copyright (C) 2015-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 PublicInbox::TestCommon;
7 my ($dir, $for_destroy) = tmpdir();
8 use PublicInbox::Spawn qw(popen_rd);
9 use PublicInbox::Import;
10
11 use_ok 'PublicInbox::Git';
12
13 {
14         PublicInbox::Import::init_bare($dir);
15         my $fi_data = './t/git.fast-import-data';
16         open my $fh, '<', $fi_data or die
17                 "fast-import data readable (or run test at top level: $!";
18         my $rdr = { 0 => $fh };
19         xsys([qw(git fast-import --quiet)], { GIT_DIR => $dir }, $rdr);
20         is($?, 0, 'fast-import succeeded');
21 }
22
23 {
24         my $gcf = PublicInbox::Git->new($dir);
25         is($gcf->modified, 749520000, 'modified time detected from commit');
26         my $f = 'HEAD:foo.txt';
27         my @x = $gcf->check($f);
28         is(scalar @x, 3, 'returned 3 element array for existing file');
29         like($x[0], qr/\A[a-f0-9]{40}\z/, 'returns obj ID in 1st element');
30         is('blob', $x[1], 'returns obj type in 2nd element');
31         like($x[2], qr/\A\d+\z/, 'returns obj size in 3rd element');
32
33         my $raw = $gcf->cat_file($f);
34         is($x[2], length($$raw), 'length matches');
35
36         is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
37         is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
38
39         my $oid = $x[0];
40         my $arg = { 'foo' => 'bar' };
41         my $res = [];
42         my $missing = [];
43         $gcf->cat_async_begin;
44         $gcf->cat_async($oid, sub {
45                 my ($bref, $oid_hex, $type, $size, $arg) = @_;
46                 $res = [ @_ ];
47         }, $arg);
48         $gcf->cat_async('non-existent', sub {
49                 my ($bref, $oid_hex, $type, $size, $arg) = @_;
50                 $missing = [ @_ ];
51         }, $arg);
52         $gcf->cat_async_wait;
53         my ($bref, $oid_hex, $type, $size, $arg_res) = @$res;
54         is_deeply([$oid_hex, $type, $size], \@x, 'got expected header');
55         is($arg_res, $arg, 'arg passed to cat_async');
56         is_deeply($raw, $bref, 'blob result matches');
57         is_deeply($missing, [ undef, undef, undef, undef, $arg],
58                 'non-existent blob gives expected result');
59 }
60
61 if (1) {
62         # need a big file, use the AGPL-3.0 :p
63         my $big_data = './COPYING';
64         ok(-r $big_data, 'COPYING readable');
65         my $size = -s $big_data;
66         ok($size > 8192, 'file is big enough');
67         open my $fh, '<', $big_data or die;
68         my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
69         my $buf = xqx($cmd, { GIT_DIR => $dir }, { 0 => $fh });
70         is(0, $?, 'hashed object successfully');
71         chomp $buf;
72
73         my $gcf = PublicInbox::Git->new($dir);
74         my $rsize;
75         my $x = $gcf->cat_file($buf, \$rsize);
76         is($rsize, $size, 'got correct size ref on big file');
77         is(length($$x), $size, 'read correct number of bytes');
78
79         my $ref = $gcf->qx(qw(cat-file blob), $buf);
80         my @ref = $gcf->qx(qw(cat-file blob), $buf);
81         my $nl = scalar @ref;
82         ok($nl > 1, "qx returned array length of $nl");
83
84         $gcf->qx(qw(repack -adq));
85         ok($gcf->packed_bytes > 0, 'packed size is positive');
86 }
87
88 if ('alternates reloaded') {
89         my ($alt, $alt_obj) = tmpdir();
90         my @cmd = ('git', "--git-dir=$alt", qw(hash-object -w --stdin));
91         PublicInbox::Import::init_bare($alt);
92         open my $fh, '<', "$alt/config" or die "open failed: $!\n";
93         my $rd = popen_rd(\@cmd, {}, { 0 => $fh } );
94         close $fh or die "close failed: $!";
95         chomp(my $remote = <$rd>);
96         my $gcf = PublicInbox::Git->new($dir);
97         is($gcf->cat_file($remote), undef, "remote file not found");
98         open $fh, '>>', "$dir/objects/info/alternates" or
99                         die "open failed: $!\n";
100         print $fh "$alt/objects" or die "print failed: $!\n";
101         close $fh or die "close failed: $!";
102         my $found = $gcf->cat_file($remote);
103         open $fh, '<', "$alt/config" or die "open failed: $!\n";
104         my $config = eval { local $/; <$fh> };
105         is($$found, $config, 'alternates reloaded');
106
107         ok(!$gcf->cleanup, 'cleanup can expire');
108         ok(!$gcf->cleanup, 'cleanup idempotent');
109
110         my $t = $gcf->modified;
111         ok($t <= time, 'repo not modified in the future');
112         isnt($t, 0, 'repo not modified in 1970')
113 }
114
115 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
116 my $s;
117 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
118 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
119 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
120 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
121
122 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
123 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
124 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
125 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
126
127 done_testing();