]> Sergey Matveev's repositories - public-inbox.git/blob - t/git.t
git: add git_quote
[public-inbox.git] / t / git.t
1 # Copyright (C) 2015-2018 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 Cwd qw/getcwd/;
9 use PublicInbox::Spawn qw(popen_rd);
10
11 eval { require IPC::Run } or plan skip_all => 'IPC::Run missing';
12 use_ok 'PublicInbox::Git';
13
14 {
15         is(system(qw(git init -q --bare), $dir), 0, 'created git directory');
16         my $cmd = [ 'git', "--git-dir=$dir", 'fast-import', '--quiet' ];
17
18         my $fi_data = getcwd().'/t/git.fast-import-data';
19         ok(-r $fi_data, "fast-import data readable (or run test at top level)");
20         IPC::Run::run($cmd, '<', $fi_data);
21         is($?, 0, 'fast-import succeeded');
22 }
23
24 {
25         my $gcf = PublicInbox::Git->new($dir);
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         {
37                 my $size;
38                 my $rv = $gcf->cat_file($f, sub {
39                         my ($in, $left) = @_;
40                         $size = $$left;
41                         'nothing'
42                 });
43                 is($rv, 'nothing', 'returned from callback without reading');
44                 is($size, $x[2], 'set size for callback correctly');
45         }
46
47         eval { $gcf->cat_file($f, sub { die 'OMG' }) };
48         like($@, qr/\bOMG\b/, 'died in callback propagated');
49         is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
50
51         {
52                 my ($buf, $r);
53                 my $rv = $gcf->cat_file($f, sub {
54                         my ($in, $left) = @_;
55                         $r = read($in, $buf, 2);
56                         $$left -= $r;
57                         'blah'
58                 });
59                 is($r, 2, 'only read 2 bytes');
60                 is($buf, '--', 'partial read succeeded');
61                 is($rv, 'blah', 'return value propagated');
62         }
63         is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
64 }
65
66 if (1) {
67         my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
68
69         # need a big file, use the AGPL-3.0 :p
70         my $big_data = getcwd().'/COPYING';
71         ok(-r $big_data, 'COPYING readable');
72         my $size = -s $big_data;
73         ok($size > 8192, 'file is big enough');
74
75         my $buf = '';
76         IPC::Run::run($cmd, '<', $big_data, '>', \$buf);
77         is(0, $?, 'hashed object successfully');
78         chomp $buf;
79
80         my $gcf = PublicInbox::Git->new($dir);
81         my $rsize;
82         is($gcf->cat_file($buf, sub {
83                 $rsize = ${$_[1]};
84                 'x';
85         }), 'x', 'checked input');
86         is($rsize, $size, 'got correct size on big file');
87
88         my $x = $gcf->cat_file($buf, \$rsize);
89         is($rsize, $size, 'got correct size ref on big file');
90         is(length($$x), $size, 'read correct number of bytes');
91
92         my $rline;
93         $gcf->cat_file($buf, sub {
94                 my ($in, $left) = @_;
95                 $rline = <$in>;
96                 $$left -= length($rline);
97         });
98         {
99                 open my $fh, '<', $big_data or die "open failed: $!\n";
100                 is($rline, <$fh>, 'first line matches');
101         };
102
103         my $all;
104         $gcf->cat_file($buf, sub {
105                 my ($in, $left) = @_;
106                 my $x = read($in, $all, $$left);
107                 $$left -= $x;
108         });
109         {
110                 open my $fh, '<', $big_data or die "open failed: $!\n";
111                 local $/;
112                 is($all, <$fh>, 'entire read matches');
113         };
114
115         my $ref = $gcf->qx(qw(cat-file blob), $buf);
116         is($all, $ref, 'qx read giant single string');
117
118         my @ref = $gcf->qx(qw(cat-file blob), $buf);
119         is($all, join('', @ref), 'qx returned array when wanted');
120         my $nl = scalar @ref;
121         ok($nl > 1, "qx returned array length of $nl");
122
123         $gcf->qx(qw(repack -adq));
124         ok($gcf->packed_bytes > 0, 'packed size is positive');
125 }
126
127 if ('alternates reloaded') {
128         my $alt = tempdir('pi-git-XXXXXX', TMPDIR => 1, CLEANUP => 1);
129         my @cmd = ('git', "--git-dir=$alt", qw(hash-object -w --stdin));
130         is(system(qw(git init -q --bare), $alt), 0, 'create alt directory');
131         open my $fh, '<', "$alt/config" or die "open failed: $!\n";
132         my $rd = popen_rd(\@cmd, {}, { 0 => fileno($fh) } );
133         close $fh or die "close failed: $!";
134         chomp(my $remote = <$rd>);
135         my $gcf = PublicInbox::Git->new($dir);
136         is($gcf->cat_file($remote), undef, "remote file not found");
137         open $fh, '>>', "$dir/objects/info/alternates" or
138                         die "open failed: $!\n";
139         print $fh "$alt/objects" or die "print failed: $!\n";
140         close $fh or die "close failed: $!";
141         my $found = $gcf->cat_file($remote);
142         open $fh, '<', "$alt/config" or die "open failed: $!\n";
143         my $config = eval { local $/; <$fh> };
144         is($$found, $config, 'alternates reloaded');
145 }
146
147 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
148 my $s;
149 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
150 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
151 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
152 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
153
154 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
155 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
156 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
157 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
158
159 done_testing();