]> Sergey Matveev's repositories - public-inbox.git/blob - t/git.t
run update-copyrights from gnulib for 2019
[public-inbox.git] / t / git.t
1 # Copyright (C) 2015-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 File::Temp qw/tempdir/;
7 my $dir = tempdir('pi-git-XXXXXX', TMPDIR => 1, CLEANUP => 1);
8 use PublicInbox::Spawn qw(popen_rd);
9
10 eval { require IPC::Run } or plan skip_all => 'IPC::Run missing';
11 use_ok 'PublicInbox::Git';
12
13 {
14         is(system(qw(git init -q --bare), $dir), 0, 'created git directory');
15         my $cmd = [ 'git', "--git-dir=$dir", 'fast-import', '--quiet' ];
16
17         my $fi_data = './t/git.fast-import-data';
18         ok(-r $fi_data, "fast-import data readable (or run test at top level)");
19         IPC::Run::run($cmd, '<', $fi_data);
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
40 if (1) {
41         my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
42
43         # need a big file, use the AGPL-3.0 :p
44         my $big_data = './COPYING';
45         ok(-r $big_data, 'COPYING readable');
46         my $size = -s $big_data;
47         ok($size > 8192, 'file is big enough');
48
49         my $buf = '';
50         IPC::Run::run($cmd, '<', $big_data, '>', \$buf);
51         is(0, $?, 'hashed object successfully');
52         chomp $buf;
53
54         my $gcf = PublicInbox::Git->new($dir);
55         my $rsize;
56         my $x = $gcf->cat_file($buf, \$rsize);
57         is($rsize, $size, 'got correct size ref on big file');
58         is(length($$x), $size, 'read correct number of bytes');
59
60         my $ref = $gcf->qx(qw(cat-file blob), $buf);
61         my @ref = $gcf->qx(qw(cat-file blob), $buf);
62         my $nl = scalar @ref;
63         ok($nl > 1, "qx returned array length of $nl");
64
65         $gcf->qx(qw(repack -adq));
66         ok($gcf->packed_bytes > 0, 'packed size is positive');
67 }
68
69 if ('alternates reloaded') {
70         my $alt = tempdir('pi-git-XXXXXX', TMPDIR => 1, CLEANUP => 1);
71         my @cmd = ('git', "--git-dir=$alt", qw(hash-object -w --stdin));
72         is(system(qw(git init -q --bare), $alt), 0, 'create alt directory');
73         open my $fh, '<', "$alt/config" or die "open failed: $!\n";
74         my $rd = popen_rd(\@cmd, {}, { 0 => fileno($fh) } );
75         close $fh or die "close failed: $!";
76         chomp(my $remote = <$rd>);
77         my $gcf = PublicInbox::Git->new($dir);
78         is($gcf->cat_file($remote), undef, "remote file not found");
79         open $fh, '>>', "$dir/objects/info/alternates" or
80                         die "open failed: $!\n";
81         print $fh "$alt/objects" or die "print failed: $!\n";
82         close $fh or die "close failed: $!";
83         my $found = $gcf->cat_file($remote);
84         open $fh, '<', "$alt/config" or die "open failed: $!\n";
85         my $config = eval { local $/; <$fh> };
86         is($$found, $config, 'alternates reloaded');
87
88         ok(!$gcf->cleanup, 'cleanup can expire');
89         ok(!$gcf->cleanup, 'cleanup idempotent');
90
91         my $t = $gcf->modified;
92         ok($t <= time, 'repo not modified in the future');
93         isnt($t, 0, 'repo not modified in 1970')
94 }
95
96 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
97 my $s;
98 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
99 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
100 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
101 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
102
103 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
104 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
105 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
106 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
107
108 done_testing();