X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=t%2Fgcf2.t;h=d12a4420b5c01099bbb0b8bbd0ef8d94460afb8a;hb=4eee5af6011cc8cdefb66c9729952c7eff5c0b0b;hp=c67efb6c459a4a8fb8cc7afc0417bb6ad201e84b;hpb=e2f85d6bda87a8c6b25cc340b569ba0a20c2a1eb;p=public-inbox.git diff --git a/t/gcf2.t b/t/gcf2.t index c67efb6c..d12a4420 100644 --- a/t/gcf2.t +++ b/t/gcf2.t @@ -1,5 +1,5 @@ #!perl -w -# Copyright (C) 2020 all contributors +# Copyright (C) 2020-2021 all contributors # License: AGPL-3.0+ use strict; use PublicInbox::TestCommon; @@ -7,70 +7,122 @@ use Test::More; use Fcntl qw(:seek); use IO::Handle (); use POSIX qw(_exit); +use Cwd qw(abs_path); require_mods('PublicInbox::Gcf2'); use_ok 'PublicInbox::Gcf2'; +use PublicInbox::Import; +my ($tmpdir, $for_destroy) = tmpdir(); + my $gcf2 = PublicInbox::Gcf2::new(); is(ref($gcf2), 'PublicInbox::Gcf2', '::new works'); +my $COPYING = 'dba13ed2ddf783ee8118c6a581dbf75305f816a3'; +open my $agpl, '<', 'COPYING' or BAIL_OUT "AGPL-3 missing: $!"; +$agpl = do { local $/; <$agpl> }; + +PublicInbox::Import::init_bare($tmpdir); +my $fi_data = './t/git.fast-import-data'; +my $rdr = {}; +open $rdr->{0}, '<', $fi_data or BAIL_OUT $!; +xsys([qw(git fast-import --quiet)], { GIT_DIR => $tmpdir }, $rdr); +is($?, 0, 'fast-import succeeded'); +$gcf2->add_alternate("$tmpdir/objects"); + +{ + my ($r, $w); + pipe($r, $w) or BAIL_OUT $!; + my $tree = 'fdbc43725f21f485051c17463b50185f4c3cf88c'; + $gcf2->cat_oid(fileno($w), $tree); + close $w; + is("$tree tree 30\n", <$r>, 'tree header ok'); + $r = do { local $/; <$r> }; + is(chop($r), "\n", 'got trailing newline'); + is(length($r), 30, 'tree length matches'); +} + chomp(my $objdir = xqx([qw(git rev-parse --git-path objects)])); if ($objdir =~ /\A--git-path\n/) { # git <2.5 chomp($objdir = xqx([qw(git rev-parse --git-dir)])); $objdir .= '/objects'; - $objdir = undef unless -d $objdir; } +if ($objdir && -d $objdir) { + $objdir = abs_path($objdir); + open my $alt, '>>', "$tmpdir/objects/info/alternates" or + BAIL_OUT $!; + print $alt $objdir, "\n" or BAIL_OUT $!; + close $alt or BAIL_OUT $!; -my $COPYING = 'dba13ed2ddf783ee8118c6a581dbf75305f816a3'; -open my $agpl, '<', 'COPYING' or BAIL_OUT "AGPL-3 missing: $!"; -$agpl = do { local $/; <$agpl> }; + # calling gcf2->add_alternate on an already-added path won't + # cause alternates to be reloaded, so we do + # $gcf2->add_alternate($objdir) later on instead of + # $gcf2->add_alternate("$tmpdir/objects"); + # $objdir = "$tmpdir/objects"; +} else { + $objdir = undef +} + +my $nr = $ENV{TEST_LEAK_NR}; +my $cat = $ENV{TEST_LEAK_CAT} // 10; +diag "checking for leaks... (TEST_LEAK_NR=$nr TEST_LEAK_CAT=$cat)" if $nr; SKIP: { - skip 'not in git worktree', 15 unless defined($objdir); + skip 'not in git worktree', 21 unless defined($objdir); $gcf2->add_alternate($objdir); + eval { $gcf2->add_alternate($objdir) }; + ok(!$@, 'no error adding alternate redundantly'); + if ($nr) { + diag "adding alternate $nr times redundantly"; + $gcf2->add_alternate($objdir) for (1..$nr); + diag 'done adding redundant alternates'; + } + open my $fh, '+>', undef or BAIL_OUT "open: $!"; - my $fd = fileno($fh); $fh->autoflush(1); - $gcf2->cat_oid($fd, 'invalid'); + ok(!$gcf2->cat_oid(fileno($fh), 'invalid'), 'invalid fails'); seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; - is(do { local $/; <$fh> }, "invalid missing\n", 'got missing message'); + is(do { local $/; <$fh> }, '', 'nothing written'); + open $fh, '+>', undef or BAIL_OUT "open: $!"; + ok(!$gcf2->cat_oid(fileno($fh), '0'x40), 'z40 fails'); seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; - $gcf2->cat_oid($fd, '0'x40); - seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; - is(do { local $/; <$fh> }, ('0'x40)." missing\n", - 'got missing message for 0x40'); + is(do { local $/; <$fh> }, '', 'nothing written for z40'); - seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; - $gcf2->cat_oid($fd, $COPYING); - my $buf; + open $fh, '+>', undef or BAIL_OUT "open: $!"; my $ck_copying = sub { my ($desc) = @_; seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; - is(<$fh>, "$COPYING blob 34520\n", 'got expected header'); - $buf = do { local $/; <$fh> }; + is(<$fh>, "$COPYING blob 34520\n", "got expected header $desc"); + my $buf = do { local $/; <$fh> }; is(chop($buf), "\n", 'got trailing \\n'); is($buf, $agpl, "AGPL matches ($desc)"); }; + ok($gcf2->cat_oid(fileno($fh), $COPYING), 'cat_oid normal'); $ck_copying->('regular file'); - $^O eq 'linux' or skip('pipe tests are Linux-only', 12); - my $size = -s $fh; + $gcf2 = PublicInbox::Gcf2::new(); + $gcf2->add_alternate("$tmpdir/objects"); + open $fh, '+>', undef or BAIL_OUT "open: $!"; + ok($gcf2->cat_oid(fileno($fh), $COPYING), 'cat_oid alternate'); + $ck_copying->('alternates after reopen'); + + $^O eq 'linux' or skip('pipe tests are Linux-only', 14); for my $blk (1, 0) { my ($r, $w); pipe($r, $w) or BAIL_OUT $!; fcntl($w, 1031, 4096) or - skip('Linux too old for F_SETPIPE_SZ', 12); + skip('Linux too old for F_SETPIPE_SZ', 14); $w->blocking($blk); seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; truncate($fh, 0) or BAIL_OUT "truncate: $!"; - defined(my $pid = fork) or BAIL_OUT "fork: $!"; + my $pid = fork // BAIL_OUT "fork: $!"; if ($pid == 0) { close $w; tick; # wait for parent to block on writev - $buf = do { local $/; <$r> }; + my $buf = do { local $/; <$r> }; print $fh $buf or _exit(1); _exit(0); } - $gcf2->cat_oid(fileno($w), $COPYING); + ok($gcf2->cat_oid(fileno($w), $COPYING), "cat blocking=$blk"); close $w or BAIL_OUT "close: $!"; is(waitpid($pid, 0), $pid, 'child exited'); is($?, 0, 'no error in child'); @@ -86,11 +138,9 @@ SKIP: { } } -if (my $nr = $ENV{TEST_LEAK_NR}) { +if ($nr) { open my $null, '>', '/dev/null' or BAIL_OUT "open /dev/null: $!"; my $fd = fileno($null); - my $cat = $ENV{TEST_LEAK_CAT} // 10; - diag "checking for leaks... (TEST_LEAK_NR=$nr TEST_LEAK_CAT=$cat)"; local $SIG{PIPE} = 'IGNORE'; my ($r, $w); pipe($r, $w);