X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=t%2Fcommon.perl;h=e49a596524b17c5d559ed6f871bb80195e45fdef;hb=50c822076abee76c3133ffc3482488392eecedfb;hp=a3585bf692dd7c5f35dc420dc48fa186a144a967;hpb=1761fba7befab2681276ac8f123593610ad27e58;p=public-inbox.git diff --git a/t/common.perl b/t/common.perl index a3585bf6..e49a5965 100644 --- a/t/common.perl +++ b/t/common.perl @@ -1,16 +1,56 @@ -require IO::File; -use POSIX qw/dup/; +# Copyright (C) 2015-2018 all contributors +# License: AGPL-3.0+ + +use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD); +use POSIX qw(dup2); sub stream_to_string { - my ($cb) = @_; - my $headers; - my $io = IO::File->new_tmpfile; - my $dup = dup($io->fileno); - my $response = sub { $headers = \@_, $io }; - $cb->($response); - $io = IO::File->new; - $io->fdopen($dup, 'r+'); - $io->seek(0, 0); - $io->read(my $str, ($io->stat)[7]); + my ($res) = @_; + my $body = $res->[2]; + my $str = ''; + while (defined(my $chunk = $body->getline)) { + $str .= $chunk; + } + $body->close; $str; } + +sub spawn_listener { + my ($env, $cmd, $socks) = @_; + my $pid = fork; + defined $pid or die "fork failed: $!\n"; + if ($pid == 0) { + # pretend to be systemd (cf. sd_listen_fds(3)) + my $fd = 3; # 3 == SD_LISTEN_FDS_START + foreach my $s (@$socks) { + my $fl = fcntl($s, F_GETFD, 0); + if (($fl & FD_CLOEXEC) != FD_CLOEXEC) { + warn "got FD:".fileno($s)." w/o CLOEXEC\n"; + } + fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC); + dup2(fileno($s), $fd++) or die "dup2 failed: $!\n"; + } + $ENV{LISTEN_PID} = $$; + $ENV{LISTEN_FDS} = scalar @$socks; + %ENV = (%ENV, %$env) if $env; + exec @$cmd; + die "FAIL: ",join(' ', @$cmd), ": $!\n"; + } + $pid; +} + +sub require_git ($;$) { + my ($req, $maybe) = @_; + my ($req_maj, $req_min) = split(/\./, $req); + my ($cur_maj, $cur_min) = (`git --version` =~ /version (\d+)\.(\d+)/); + + my $req_int = ($req_maj << 24) | ($req_min << 16); + my $cur_int = ($cur_maj << 24) | ($cur_min << 16); + if ($cur_int < $req_int) { + return 0 if $maybe; + plan skip_all => "git $req+ required, have $git_ver"; + } + 1; +} + +1;