lib/App/teai/Lock.pm

filter: Statementstmt 90.6% 58 / 64 Branchbran 68.7% 22 / 32 Conditioncond 48.0% 12 / 25 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 71.8% 107 / 149 CC 27 SCAR 26.3
CC
27
Cov
76%
run
25.0
path
7.3
_open
6.1
CRAP 13.9
« lib/App/teai/Config.pm ↑ Summary lib/App/teai/Wire.pm »
1 package App::teai::Lock;
2 32 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 32 ▶use Fcntl qw(:flock O_RDWR O_CREAT O_EXCL F_SETFD);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use App::teai::Err;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 our $VERSION = '0.08';
11  
12 my $POLL = 0.2;
13  
14 18 ▶sub path (%o) {
Subroutine path: called CC=7 SCAR=19.9
Pod documented
15 18 ▶ return $ENV{TEAI_TEST_LOCK} if length $ENV{TEAI_TEST_LOCK};
Branch: 1 branch
BranchTrueFalseTotal
if length $ENV{"TEAI_TEST_LOCK"} 1 17 18
16 17 ▶ return '/tmp/teai-test.lock' if $o{global};
Branch: 1 branch
BranchTrueFalseTotal
if $o{"global"} 7 10 17
17 10 ▶ my $d = `git rev-parse --git-common-dir 2>/dev/null` // '';
Condition50%1/2
`git rev-parse --git-common-dir 2>/dev/null` // ""
l!l
100
MC/DC0%0/1
`git rev-parse --git-common-dir 2>/dev/null` // ""
`git rev-parse --git-common-dir 2>/dev/null`
Truth table50%1/2
A
A= `git rev-parse --git-common-dir 2>/dev/null`
Aresult
11
00
18 10 chomp $d;
19 10 ▶ $d = '.' if $? || $d eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $? or $d eq "" 2 8 10
Condition66%2/3
$? or $d eq ""
l!l&&r!l&&!r
208
MC/DC50%1/2
$? or $d eq ""
$? $d eq ""
Truth table66%2/3
A or B
A= $?B= $d eq ""
ABresult
1X1
011
000
20 10 ▶ $d = Cwd::abs_path($d) // Cwd::abs_path('.');
Condition33%1/3
Cwd::abs_path($d) // Cwd::abs_path(".")
l!l&&r!l&&!r
1000
MC/DC0%0/2
Cwd::abs_path($d) // Cwd::abs_path(".")
Cwd::abs_path($d) Cwd::abs_path(".")
Truth table33%1/3
A // B
A= Cwd::abs_path($d)B= Cwd::abs_path(".")
ABresult
1X1
011
000
21 10 return '/tmp/teai-test-' . substr(Digest::SHA::sha1_hex($d), 0, 12) . '.lock';
22 }
23  
24 15 ▶sub _open ($path) {
Subroutine _open: called CC=5 SCAR=18.1
25 15 my $fh;
26 15 ▶ return $fh if sysopen $fh, $path, O_RDWR;
Branch: 1 branch
BranchTrueFalseTotal
if sysopen $fh, $path, 2 7 8 15
27 8 for (1 .. 3) {
28 8 ▶ if (sysopen $fh, $path, O_RDWR | O_CREAT | O_EXCL, 0666) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen $fh, $path, 194, 438) 8 0 8
29 8 chmod 0666, $path;
30 8 return $fh;
31 }
32 0 ▶ return $fh if sysopen $fh, $path, O_RDWR;
Branch: 1 branch
BranchTrueFalseTotal
if sysopen $fh, $path, 2 0 0 0
33 }
34 0 App::teai::Err::fail(3, "cannot open $path: $!");
35 }
36  
37 15 ▶sub run (%o) {
Subroutine run: called CC=17 SCAR=32.2
Pod documented
38 15 ▶ my @cmd = @{ $o{cmd} // [] };
Condition50%1/2
$o{"cmd"} // []
l!l
150
MC/DC0%0/1
$o{"cmd"} // []
$o{"cmd"}
Truth table50%1/2
A
A= $o{"cmd"}
Aresult
11
00
39 15 ▶ App::teai::Err::fail(2, 'usage: teai lock [--wait SECONDS] [--global] -- COMMAND [ARG...]') unless @cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless @cmd 0 15 15
40 15 ▶ App::teai::Err::fail(2, "invalid wait '$o{wait}'") if defined $o{wait} && $o{wait} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o{"wait"} and $o{"wait"} < 0 0 15 15
Condition66%2/3
defined $o{"wait"} and $o{"wait"} < 0
!ll&&!rl&&r
4110
MC/DC0%0/2
defined $o{"wait"} and $o{"wait"} < 0
defined $o{"wait"} $o{"wait"} < 0
Truth table66%2/3
A and B
A= defined $o{"wait"}B= $o{"wait"} < 0
ABresult
0X0
100
111
41 15 my $path = path(global => $o{global});
42 15 my $fh = _open($path);
43 15 ▶ my $deadline = defined $o{wait} ? Time::HiRes::time() + $o{wait} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'wait'} ? : 11 4 15
44 15 ▶ until (flock $fh, LOCK_EX | LOCK_NB) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 6) 18 12 30
45 18 ▶ App::teai::Err::fail(3, "cannot lock $path: $!") unless $!{EWOULDBLOCK} || $!{EAGAIN} || $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"EWOULDBLOCK"} or $!{"EAGAIN"} or $!{"EINTR"} 0 18 18
Condition33%2/6
$!{"EWOULDBLOCK"} or $!{"EAGAIN"} or $!{"EINTR"}
l!l&&r!l&&!r
1800
$!{"EWOULDBLOCK"} or $!{"EAGAIN"}
l!l&&r!l&&!r
1800
MC/DC0%0/3
$!{"EWOULDBLOCK"} or $!{"EAGAIN"} or $!{"EINTR"}
$!{"EWOULDBLOCK"} $!{"EAGAIN"} $!{"EINTR"}
Truth table25%1/4
A or B or C
A= $!{"EWOULDBLOCK"}B= $!{"EAGAIN"}C= $!{"EINTR"}
ABCresult
1XX1
01X1
0011
0000
46 18 ▶ if (defined $deadline && Time::HiRes::time() >= $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deadline and &Time::HiRes::time() >= $deadline) 3 15 18
Condition66%2/3
defined $deadline and &Time::HiRes::time() >= $deadline
!ll&&!rl&&r
0153
MC/DC50%1/2
defined $deadline and &Time::HiRes::time() >= $deadline
defined $deadline &Time::HiRes::time() >= $deadline
Truth table66%2/3
A and B
A= defined $deadlineB= &Time::HiRes::time() >= $deadline
ABresult
0X0
100
111
47 3 App::teai::Err::fail(4, 'test lock busy');
48 }
49 15 Time::HiRes::sleep($POLL);
50 }
51 12 ▶ fcntl $fh, F_SETFD, 0 or App::teai::Err::fail(3, "fcntl: $!");
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 2, 0 or App::teai::Err::fail(3, "fcntl: $!") 12 0 12
52 12 ▶ my $pid = fork // App::teai::Err::fail(3, "fork: $!");
Condition33%1/3
fork // App::teai::Err::fail(3, "fork: $!")
l!l&&r!l&&!r
1200
MC/DC0%0/2
fork // App::teai::Err::fail(3, "fork: $!")
fork App::teai::Err::fail(3, "fork: $!")
Truth table33%1/3
A // B
A= forkB= App::teai::Err::fail(3, "fork: $!")
ABresult
1X1
011
000
53 12 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 6 6 12
54 6 $SIG{PIPE} = "DEFAULT";
55 6 ▶ { no warnings 'exec'; exec { $cmd[0] } @cmd; }
Subroutine BEGIN: called CC=1 SCAR=0.0
56 0 my $errno = $!;
57 0 my $e = "$errno";
58 0 print STDERR "teai: cannot run $cmd[0]: $e\n";
59 0 ▶ POSIX::_exit($errno == POSIX::EACCES() ? 126 : 127);
Branch: 1 branch
BranchTrueFalseTotal
$errno == 13 ? : 0 0 0
60 }
61 6 waitpid $pid, 0;
62 6 my $st = $?;
63 6 ▶ return $st & 127 ? 128 + ($st & 127) : $st >> 8;
Branch: 1 branch
BranchTrueFalseTotal
$st & 127 ? : 0 6 6
64 }
65  
66 1;
67 __END__
« lib/App/teai/Config.pm ↑ Summary lib/App/teai/Wire.pm »