]> Sergey Matveev's repositories - public-inbox.git/blob - t/common.perl
git: ensure ->modified returns an integer
[public-inbox.git] / t / common.perl
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
4 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
5 use POSIX qw(dup2);
6
7 sub stream_to_string {
8         my ($res) = @_;
9         my $body = $res->[2];
10         my $str = '';
11         while (defined(my $chunk = $body->getline)) {
12                 $str .= $chunk;
13         }
14         $body->close;
15         $str;
16 }
17
18 sub spawn_listener {
19         my ($env, $cmd, $socks) = @_;
20         my $pid = fork;
21         defined $pid or die "fork failed: $!\n";
22         if ($pid == 0) {
23                 # pretend to be systemd (cf. sd_listen_fds(3))
24                 my $fd = 3; # 3 == SD_LISTEN_FDS_START
25                 foreach my $s (@$socks) {
26                         my $fl = fcntl($s, F_GETFD, 0);
27                         if (($fl & FD_CLOEXEC) != FD_CLOEXEC) {
28                                 warn "got FD:".fileno($s)." w/o CLOEXEC\n";
29                         }
30                         fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC);
31                         dup2(fileno($s), $fd++) or die "dup2 failed: $!\n";
32                 }
33                 $ENV{LISTEN_PID} = $$;
34                 $ENV{LISTEN_FDS} = scalar @$socks;
35                 %ENV = (%ENV, %$env) if $env;
36                 exec @$cmd;
37                 die "FAIL: ",join(' ', @$cmd), ": $!\n";
38         }
39         $pid;
40 }
41
42 sub require_git ($;$) {
43         my ($req, $maybe) = @_;
44         my ($req_maj, $req_min) = split(/\./, $req);
45         my ($cur_maj, $cur_min) = (`git --version` =~ /version (\d+)\.(\d+)/);
46
47         my $req_int = ($req_maj << 24) | ($req_min << 16);
48         my $cur_int = ($cur_maj << 24) | ($cur_min << 16);
49         if ($cur_int < $req_int) {
50                 return 0 if $maybe;
51                 plan skip_all => "git $req+ required, have $git_ver";
52         }
53         1;
54 }
55
56 1;