blib/lib/App/migrate.pm

filter: Statementstmt 94.4% 272 / 288 Branchbran 88.8% 144 / 162 Conditioncond 65.3% 32 / 49 MC/DCmcdc 44.8% 13 / 29 Subroutinesub 81.5% 31 / 38 Podpod 100% 6 / 6 totaltotal 87.0% 498 / 572 CC 101 SCAR 29.3
CC
101
Cov
90%
_preprocess
43.0
load
18.0
_on_error
12.0
CRAP 18.8
↑ Summary
1 package App::migrate;
2 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 ## no critic (RequireCarping)
8  
9 3 our $VERSION = 'v0.2.6';
10  
11 3 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Temp qw( tempfile ); # don't use Path::Tiny to have temp files in error $SHELL
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use constant KW_DEFINE => { map {$_=>1} qw( DEFINE DEFINE2 DEFINE4 ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use constant KW_VERSION => { map {$_=>1} qw( VERSION ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use constant KW_UP => { map {$_=>1} qw( before_upgrade upgrade ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use constant KW_DOWN => { map {$_=>1} qw( downgrade after_downgrade ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use constant KW_RESTORE => { map {$_=>1} qw( RESTORE ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use constant KW => { %{&KW_UP}, %{&KW_DOWN}, %{&KW_DEFINE}, %{&KW_RESTORE}, %{&KW_VERSION} };
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use constant DEFINE_TOKENS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use constant DEFINE2_TOKENS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use constant DEFINE4_TOKENS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 # cleanup temp files
26 0 ▶$SIG{HUP} = $SIG{HUP} // sub { exit 129 }; ## no critic (RequireLocalizedPunctuationVars ProhibitMagicNumbers)
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$SIG{"HUP"} // sub { exit 129; }
l!l&&r!l&&!r
030
MC/DC0%0/2
$SIG{"HUP"} // sub { exit 129; }
$SIG{"HUP"} sub { exit 129; }
Truth table33%1/3
A // B
A= $SIG{"HUP"}B= sub { exit 129; }
ABresult
1X1
011
000
27 0 ▶$SIG{INT} = $SIG{INT} // sub { exit 130 }; ## no critic (RequireLocalizedPunctuationVars ProhibitMagicNumbers)
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$SIG{"INT"} // sub { exit 130; }
l!l&&r!l&&!r
030
MC/DC0%0/2
$SIG{"INT"} // sub { exit 130; }
$SIG{"INT"} sub { exit 130; }
Truth table33%1/3
A // B
A= $SIG{"INT"}B= sub { exit 130; }
ABresult
1X1
011
000
28 0 ▶$SIG{QUIT}= $SIG{QUIT} // sub { exit 131 }; ## no critic (RequireLocalizedPunctuationVars ProhibitMagicNumbers)
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$SIG{"QUIT"} // sub { exit 131; }
l!l&&r!l&&!r
030
MC/DC0%0/2
$SIG{"QUIT"} // sub { exit 131; }
$SIG{"QUIT"} sub { exit 131; }
Truth table33%1/3
A // B
A= $SIG{"QUIT"}B= sub { exit 131; }
ABresult
1X1
011
000
29 0 ▶$SIG{TERM}= $SIG{TERM} // sub { exit 143 }; ## no critic (RequireLocalizedPunctuationVars ProhibitMagicNumbers)
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$SIG{"TERM"} // sub { exit 143; }
l!l&&r!l&&!r
030
MC/DC0%0/2
$SIG{"TERM"} // sub { exit 143; }
$SIG{"TERM"} sub { exit 143; }
Truth table33%1/3
A // B
A= $SIG{"TERM"}B= sub { exit 143; }
ABresult
1X1
011
000
30  
31  
32 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
33 3 my ($class) = @_;
34 3 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
35 paths => {}, # {prev_version}{next_version} = \@steps
36 on => {
37 BACKUP => \&_on_backup,
38 RESTORE => \&_on_restore,
39 VERSION => \&_on_version,
40 error => \&_on_error,
41 },
42 }, ref $class || $class;
43 3 return $self;
44 }
45  
46 13 ▶sub find_paths {
Subroutine find_paths: called CC=1 SCAR=0.0
Pod documented
47 13 my ($self, $from, $to) = @_;
48 13 return $self->_find_paths($to, $from);
49 }
50  
51 32 ▶sub get_steps {
Subroutine get_steps: called CC=6 SCAR=18.3
Pod documented
52 32 my ($self, $path) = @_;
53 32 ▶ my @path = @{ $path // [] };
Condition50%1/2
$path // []
l!l
320
MC/DC0%0/1
$path // []
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
54 32 ▶ croak 'Path must contain at least 2 versions' if 2 > @path;
Branch: 1 branch
BranchTrueFalseTotal
if 2 > @path 0 32 32
55 32 my @all_steps;
56 32 ▶ for (; 2 <= @path; shift @path) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 <= @path) 46 32 78
57 46 my ($prev, $next) = @path;
58 46 ▶ croak "Unknown version '$prev'" if !$self->{paths}{$prev};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"paths"}{$prev} 0 46 46
59 46 ▶ croak "No one-step migration from '$prev' to '$next'" if !$self->{paths}{$prev}{$next};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"paths"}{$prev}{$next} 0 46 46
60 46 push @all_steps, @{ $self->{paths}{$prev}{$next} };
61 }
62 32 return @all_steps;
63 }
64  
65 69 ▶sub load {
Subroutine load: called CC=18 SCAR=28.9
Pod documented
66 69 my ($self, $file) = @_;
67  
68 69 ▶ open my $fh, '<:encoding(UTF-8)', $file or croak "open($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $file or croak("open($file): $!") 68 1 69
69 68 ▶ croak "'$file' is not a plain file" if !-f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 2 66 68
70 66 my @op = _preprocess(_tokenize($fh, { file => $file, line => 0 }));
71 10 ▶ close $fh or croak "close($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("close($file): $!") 10 0 10
72  
73 10 my ($prev_version, $next_version, @steps) = (q{}, q{});
74 10 ▶ while (@op) {
Branch: 1 branch
BranchTrueFalseTotal
if (@op) 33 6 39
75 33 my $op = shift @op;
76 33 ▶ if (KW_VERSION->{$op->{op}}) {
Branch: 2 branches
BranchTrueFalseTotal
if ({VERSION => 1}->{$op->{'op'}}) { } 18 15 33
elsif ({before_upgrade => 1, upgrade => 1}->{$op->{'op'}}) { } 14 1 15
77 18 $next_version = $op->{args}[0];
78 18 ▶ if ($prev_version ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_version ne "") 11 7 18
79 $self->{paths}{ $prev_version }{ $next_version } ||= [
80 20 (grep { $_->{type} eq 'before_upgrade' } @steps),
81 11 ▶ (grep { $_->{type} eq 'upgrade' } @steps),
Condition50%1/2
$self->{"paths"}{$prev_version}{$next_version} ||= [grep({$_->{"type"} eq "before_upgrade";} @steps), grep({$_->{"type"} eq "upgrade";} @steps), {"type", "VERSION", "version", $next_version}]
l!l
011
MC/DC0%0/1
$self->{"paths"}{$prev_version}{$next_version} ||= [grep({$_->{"type"} eq "before_upgrade";} @steps), grep({$_->{"type"} eq "upgrade";} @steps), {"type", "VERSION", "version", $next_version}]
$self->{"paths"}{$prev_version}{$next_version}
Truth table50%1/2
A
A= $self->{"paths"}{$prev_version}{$next_version}
Aresult
11
00
82 {
83 type => 'VERSION',
84 version => $next_version,
85 },
86 ];
87 11 ▶ my $restore = first { KW_RESTORE->{$_->{type}} } @steps;
Subroutine __ANON__: called CC=1 SCAR=0.0
88 $self->{paths}{ $next_version }{ $prev_version } ||= [
89 $restore ? (
90 $restore,
91 ) : (
92 16 (grep { $_->{type} eq 'downgrade' } reverse @steps),
93 11 ▶ (grep { $_->{type} eq 'after_downgrade' } reverse @steps),
Branch: 1 branch
BranchTrueFalseTotal
$restore ? : 2 9 11
Condition50%1/2
$self->{"paths"}{$next_version}{$prev_version} ||= [$restore ? $restore : (grep({$_->{"type"} eq "downgrade";} reverse(@steps)), grep({$_->{"type"} eq "after_downgrade";} reverse(@steps))), {"type", "VERSION", "version", $prev_version}]
l!l
011
MC/DC0%0/1
$self->{"paths"}{$next_version}{$prev_version} ||= [$restore ? $restore : (grep({$_->{"type"} eq "downgrade";} reverse(@steps)), grep({$_->{"type"} eq "after_downgrade";} reverse(@steps))), {"type", "VERSION", "version", $prev_version}]
$self->{"paths"}{$next_version}{$prev_version}
Truth table50%1/2
A
A= $self->{"paths"}{$next_version}{$prev_version}
Aresult
11
00
94 ),
95 {
96 type => 'VERSION',
97 version => $prev_version,
98 },
99 ];
100 11 for (@{ $self->{paths}{ $prev_version }{ $next_version } }) {
101 21 $_->{prev_version} = $prev_version;
102 21 $_->{next_version} = $next_version;
103 }
104 11 for (@{ $self->{paths}{ $next_version }{ $prev_version } }) {
105 21 $_->{prev_version} = $next_version;
106 21 $_->{next_version} = $prev_version;
107 }
108 }
109 18 ($prev_version, $next_version, @steps) = ($next_version, q{});
110 }
111 elsif (KW_UP->{$op->{op}}) {
112 14 ▶ die _e($op, "Need 'VERSION' before '$op->{op}'") if $prev_version eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $prev_version eq "" 1 13 14
113 13 my ($cmd1, @args1) = @{ $op->{args} };
114 push @steps, {
115 type => $op->{op},
116 13 cmd => $cmd1,
117 args => \@args1,
118 };
119 die _e($op, "Need 'RESTORE' or 'downgrade' or 'after_downgrade' after '$op->{op}'")
120 13 ▶ if !( @op && (KW_DOWN->{$op[0]{op}} || KW_RESTORE->{$op[0]{op}}) );
Branch: 1 branch
BranchTrueFalseTotal
unless @op and {"after_downgrade" => 1,"downgrade" => 1}->{$op[0]{"op"}} || {"RESTORE" => 1}->{$op[0]{"op"}} 2 11 13
Condition100%6/6
@op and {"after_downgrade" => 1,"downgrade" => 1}->{$op[0]{"op"}} || {"RESTORE" => 1}->{$op[0]{"op"}}
!ll&&!rl&&r
1111
{"after_downgrade" => 1,"downgrade" => 1}->{$op[0]{"op"}} or {"RESTORE" => 1}->{$op[0]{"op"}}
l!l&&r!l&&!r
921
MC/DC100%3/3
@op and {"after_downgrade" => 1,"downgrade" => 1}->{$op[0]{"op"}} || {"RESTORE" => 1}->{$op[0]{"op"}}
@op {"after_downgrade" => 1,"downgrade" => 1}->{$op[0]{"op"}} {"RESTORE" => 1}->{$op[0]{"op"}}
Truth table100%4/4
A and B or C
A= @opB= {"after_downgrade" => 1,"downgrade" => 1}->{$op[0]{"op"}}C= {"RESTORE" => 1}->{$op[0]{"op"}}
ABCresult
0XX0
1000
11X1
1011
121 11 my $op2 = shift @op;
122 11 ▶ if (KW_RESTORE->{$op2->{op}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ({RESTORE => 1}->{$op2->{'op'}}) { } 2 9 11
123 2 push @steps, {
124 type => 'RESTORE',
125 version => $prev_version,
126 };
127 }
128 else {
129 9 my ($cmd2, @args2) = @{ $op2->{args} };
130 push @steps, {
131 type => $op2->{op},
132 9 cmd => $cmd2,
133 args => \@args2,
134 };
135 }
136 }
137 else {
138 1 die _e($op, "Need 'before_upgrade' or 'upgrade' before '$op->{op}'");
139 }
140 }
141  
142 6 return $self;
143 }
144  
145 7 ▶sub on {
Subroutine on: called CC=2 SCAR=7.0
Pod documented
146 7 my ($self, $e, $code) = @_;
147 7 ▶ croak "Unknown event $e" if !$self->{on}{$e};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"on"}{$e} 0 7 7
148 7 $self->{on}{$e} = $code;
149 7 return $self;
150 }
151  
152 12 ▶sub run {
Subroutine run: called CC=8 SCAR=23.0
Pod documented
153 12 my ($self, $path) = @_;
154 12 $self->get_steps($path); # validate full @path before starting
155 12 my @path = @{ $path };
156 12 my $from;
157 eval {
158 12 my $just_restored = 0;
159 12 ▶ for (; 2 <= @path; shift @path) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 <= @path) 17 12 29
160 17 my ($prev, $next) = @path;
161 17 ▶ if (!$just_restored) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($just_restored) 16 1 17
162 16 $self->_do({
163 type => 'BACKUP', # internal step type
164 version => $prev,
165 prev_version => $prev,
166 next_version => $next,
167 });
168 }
169 17 $just_restored = 0;
170 17 $from = $prev;
171 17 for my $step ($self->get_steps([$prev, $next])) {
172 32 $self->_do($step);
173 32 ▶ if ($step->{type} eq 'RESTORE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($step->{"type"} eq "RESTORE") 2 30 32
174 2 $just_restored = 1;
175 }
176 }
177 }
178 12 1;
179 }
180 12 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $just_restored = 0; for (; 2 <= @path; shift @path) { () = @path; unless ($just_restored) { $self->_do({"type", "BACKUP", "version", $prev, "prev_version", $prev, "next_version", $next}); } ; $just_restored = 0; $from = $prev; foreach my $step ($self->get_steps([$prev, $next])) { $self->_do($step); if ($step->{"type"} eq "RESTORE") { $just_restored = 1; } ; } ; } ; 1 } } or do { my $err = $@; if ($from) { eval { do { $self->_do({"type", "RESTORE", "version", $from, "prev_version", $from, "next_version", $path[-1]}, 1); warn "Successfully undone interrupted migration by RESTORE $from\n"; 1 } } or warn "Failed to RESTORE ${from}: $@"; } ; die $err } 12 0 12
181 0 my $err = $@;
182 0 ▶ if ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 0 0 0
183 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_do({"type", "RESTORE", "version", $from, "prev_version", $from, "next_version", $path[-1]}, 1); warn "Successfully undone interrupted migration by RESTORE $from\n"; 1 } } or warn "Failed to RESTORE ${from}: $@" 0 0 0
184 0 $self->_do({
185 type => 'RESTORE', # internal step type
186 version => $from,
187 prev_version => $from,
188 next_version => $path[-1],
189 }, 1);
190 0 warn "Successfully undone interrupted migration by RESTORE $from\n";
191 0 1;
192 } or warn "Failed to RESTORE $from: $@";
193 }
194 0 die $err;
195 };
196 12 return;
197 }
198  
199 29 ▶sub _data2arg {
Subroutine _data2arg: called CC=3 SCAR=11.0
200 29 my ($data) = @_;
201  
202 29 ▶ return if $data eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "" 21 8 29
203  
204 8 my ($fh, $file) = tempfile('migrate.XXXXXX', TMPDIR=>1, UNLINK=>1);
205 8 print {$fh} $data;
206 8 ▶ close $fh or croak "close($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("close($file): $!") 8 0 8
207  
208 8 return $file;
209 }
210  
211 48 ▶sub _do {
Subroutine _do: called CC=9 SCAR=22.0
212 48 my ($self, $step, $is_fatal) = @_;
213 48 local $ENV{MIGRATE_PREV_VERSION} = $step->{prev_version};
214 48 local $ENV{MIGRATE_NEXT_VERSION} = $step->{next_version};
215 eval {
216 48 ▶ if ($step->{type} eq 'BACKUP' or $step->{type} eq 'RESTORE' or $step->{type} eq 'VERSION') {
Branch: 1 branch
BranchTrueFalseTotal
if ($step->{'type'} eq 'BACKUP' or $step->{'type'} eq 'RESTORE' or $step->{'type'} eq 'VERSION') { } 35 13 48
Condition100%6/6
$step->{"type"} eq "BACKUP" or $step->{"type"} eq "RESTORE" or $step->{"type"} eq "VERSION"
l!l&&r!l&&!r
181713
$step->{"type"} eq "BACKUP" or $step->{"type"} eq "RESTORE"
l!l&&r!l&&!r
16230
MC/DC100%3/3
$step->{"type"} eq "BACKUP" or $step->{"type"} eq "RESTORE" or $step->{"type"} eq "VERSION"
$step->{"type"} eq "BACKUP" $step->{"type"} eq "RESTORE" $step->{"type"} eq "VERSION"
Truth table100%4/4
A or B or C
A= $step->{"type"} eq "BACKUP"B= $step->{"type"} eq "RESTORE"C= $step->{"type"} eq "VERSION"
ABCresult
1XX1
01X1
0011
0000
217 35 $self->{on}{ $step->{type} }->($step);
218 }
219 else {
220 13 my $cmd = $step->{cmd};
221 13 ▶ if ($cmd =~ /\A#!/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /\A#!/ms) 7 6 13
222 7 $cmd = _data2arg($cmd);
223 7 ▶ chmod 0700, $cmd or croak "chmod($cmd): $!"; ## no critic (ProhibitMagicNumbers)
Branch: 1 branch
BranchTrueFalseTotal
chmod 0700, $cmd or croak("chmod($cmd): $!") 7 0 7
224 }
225 13 ▶ system($cmd, @{ $step->{args} }) == 0 or die "'$step->{type}' failed: $cmd @{ $step->{args} }\n";
Branch: 1 branch
BranchTrueFalseTotal
system($cmd, @{$step->{"args"};}) == 0 or die qq['$step->{"type"}' failed: $cmd @{$step->{"args"};}\n] 10 3 13
226 10 print "\n";
227 }
228 45 1;
229 }
230 48 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { if ($step->{"type"} eq "BACKUP" or $step->{"type"} eq "RESTORE" or $step->{"type"} eq "VERSION") { $self->{"on"}{$step->{"type"}}($step); } else { my $cmd = $step->{"cmd"}; if ($cmd =~ /\A#!/ms) { $cmd = _data2arg($cmd); chmod 0700, $cmd or croak("chmod($cmd): $!"); } ; system($cmd, @{$step->{"args"};}) == 0 or die qq['$step->{"type"}' failed: $cmd @{$step->{"args"};}\n]; print "\n"; } ; 1 } } or do { die $@ if $is_fatal; warn $@; $self->{"on"}{"error"}->($step) } 45 3 48
231 3 ▶ die $@ if $is_fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $is_fatal 0 3 3
232 3 warn $@;
233 3 $self->{on}{error}->($step);
234 };
235 48 return;
236 }
237  
238 60 ▶sub _e {
Subroutine _e: called CC=2 SCAR=6.9
239 60 my ($t, $msg, $near) = @_;
240 60 ▶ return "parse error: $msg at $t->{loc}{file}:$t->{loc}{line}"
Branch: 1 branch
BranchTrueFalseTotal
length $near ? : 40 20 60
241 . (length $near ? " near '$near'\n" : "\n");
242 }
243  
244 25 ▶sub _find_paths {
Subroutine _find_paths: called CC=3 SCAR=11.0
245 25 my ($self, $to, @from) = @_;
246 25 ▶ my $p = $self->{paths}{ $from[-1] } || {};
Condition50%1/2
$self->{'paths'}{$from[-1]} || {}
l!l
250
MC/DC0%0/1
$self->{'paths'}{$from[-1]} || {}
$self->{'paths'}{$from[-1]}
Truth table50%1/2
A
A= $self->{'paths'}{$from[-1]}
Aresult
11
00
247 25 ▶ return [@from, $to] if $p->{$to};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{$to} 13 12 25
248 12 my %seen = map {$_=>1} @from;
249 12 return map {$self->_find_paths($to,@from,$_)} grep {!$seen{$_}} keys %{$p};
250 }
251  
252 0 ▶sub _on_backup {
Subroutine _on_backup: not called CC=1 SCAR=6.9
253 0 croak 'You need to define how to make BACKUP';
254 }
255  
256 0 ▶sub _on_restore {
Subroutine _on_restore: not called CC=1 SCAR=6.9
257 0 croak 'You need to define how to RESTORE from backup';
258 }
259  
260 10 ▶sub _on_version {
Subroutine _on_version: called CC=1 SCAR=0.0
261 # do nothing
262 }
263  
264 0 ▶sub _on_error {
Subroutine _on_error: not called CC=3 SCAR=24.8
265 0 warn <<'ERROR';
266  
267 YOU NEED TO MANUALLY FIX THIS ISSUE RIGHT NOW
268 When done, use:
269 exit to continue migration
270 exit 1 to interrupt migration and RESTORE from backup
271  
272 ERROR
273 0 ▶ system($ENV{SHELL} // '/bin/sh') == 0 or die "Migration interrupted\n";
Branch: 1 branch
BranchTrueFalseTotal
system($ENV{"SHELL"} // "/bin/sh") == 0 or die "Migration interrupted\n" 0 0 0
Condition0%0/2
$ENV{"SHELL"} // "/bin/sh"
l!l
00
MC/DC0%0/1
$ENV{"SHELL"} // "/bin/sh"
$ENV{"SHELL"}
Truth table0%0/2
A
A= $ENV{"SHELL"}
Aresult
11
00
274 0 return;
275 }
276  
277 58 ▶sub _preprocess { ## no critic (ProhibitExcessComplexity)
Subroutine _preprocess: called CC=43 SCAR=37.6
278 58 my @tokens = @_;
279 58 my @op;
280 my %macro;
281 58 ▶ while (@tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens) 99 10 109
282 99 my $t = shift @tokens;
283 99 ▶ if ($t->{op} =~ /\ADEFINE[24]?\z/ms) {
Branch: 5 branches
BranchTrueFalseTotal
if ($t->{'op'} =~ /\ADEFINE[24]?\z/ms) { } 34 65 99
elsif ({VERSION => 1}->{$t->{'op'}}) { } 33 32 65
elsif ({RESTORE => 1}->{$t->{'op'}}) { } 4 28 32
elsif ({before_upgrade => 1, upgrade => 1}->{$t->{'op'}} or {after_downgrade => 1, downgrade => 1}->{$t->{'op'}}) { } 23 5 28
elsif ($macro{$t->{'op'}}) { } 4 1 5
284 34 ▶ die _e($t, "'$t->{op}' must have one param", "@{$t->{args}}") if 1 != @{$t->{args}};
Branch: 1 branch
BranchTrueFalseTotal
if 1 != @{$t->{"args"};} 3 31 34
285 31 ▶ die _e($t, "Bad name for '$t->{op}'", $t->{args}[0]) if $t->{args}[0] !~ /\A(?!#)\S+\z/ms;
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"args"}[0] =~ /\A(?!#)\S+\z/ms 5 26 31
286 26 ▶ die _e($t, "No data allowed for '$t->{op}'", $t->{data}) if $t->{data} ne q{};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"data"} ne "" 3 23 26
287 23 my $name = $t->{args}[0];
288 23 ▶ die _e($t, "Can't redefine keyword '$name'") if KW->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if {"DEFINE" => 1,"DEFINE2" => 1,"DEFINE4" => 1,"RESTORE" => 1,"VERSION" => 1,"after_downgrade" => 1,"before_upgrade" => 1,"downgrade" => 1,"upgrade" => 1}->{$name} 3 20 23
289 20 ▶ die _e($t, "'$name' is already defined") if $macro{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $macro{$name} 3 17 20
290 17 ▶ if ($t->{op} eq 'DEFINE') {
Branch: 3 branches
BranchTrueFalseTotal
if ($t->{'op'} eq 'DEFINE') { } 3 14 17
elsif ($t->{'op'} eq 'DEFINE2') { } 8 6 14
elsif ($t->{'op'} eq 'DEFINE4') { } 6 0 6
291 3 ▶ die _e($t, q{Need operation after 'DEFINE'}) if @tokens < DEFINE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
if @tokens < 1 1 2 3
292 2 my $t1 = shift @tokens;
293 2 ▶ die _e($t1, q{First operation after 'DEFINE' must be 'before_upgrade' or 'upgrade' or 'downgrade' or 'after_downgrade'}, $t1->{op}) if !( KW_UP->{$t1->{op}} || KW_DOWN->{$t1->{op}} );
Branch: 1 branch
BranchTrueFalseTotal
unless {"before_upgrade" => 1,"upgrade" => 1}->{$t1->{"op"}} or {"after_downgrade" => 1,"downgrade" => 1}->{$t1->{"op"}} 1 1 2
Condition66%2/3
{"before_upgrade" => 1,"upgrade" => 1}->{$t1->{"op"}} or {"after_downgrade" => 1,"downgrade" => 1}->{$t1->{"op"}}
l!l&&r!l&&!r
101
MC/DC50%1/2
{"before_upgrade" => 1,"upgrade" => 1}->{$t1->{"op"}} or {"after_downgrade" => 1,"downgrade" => 1}->{$t1->{"op"}}
{"before_upgrade" => 1,"upgrade" => 1}->{$t1->{"op"}} {"after_downgrade" => 1,"downgrade" => 1}->{$t1->{"op"}}
Truth table66%2/3
A or B
A= {"before_upgrade" => 1,"upgrade" => 1}->{$t1->{"op"}}B= {"after_downgrade" => 1,"downgrade" => 1}->{$t1->{"op"}}
ABresult
1X1
011
000
294 1 $macro{$name} = [ $t1 ];
295 }
296 elsif ($t->{op} eq 'DEFINE2') {
297 8 ▶ die _e($t, q{Need two operations after 'DEFINE2'}) if @tokens < DEFINE2_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
if @tokens < 2 1 7 8
298 7 my $t1 = shift @tokens;
299 7 my $t2 = shift @tokens;
300 7 ▶ die _e($t1, q{First operation after 'DEFINE2' must be 'before_upgrade' or 'upgrade'}, $t1->{op}) if !KW_UP->{$t1->{op}};
Branch: 1 branch
BranchTrueFalseTotal
unless {"before_upgrade" => 1,"upgrade" => 1}->{$t1->{"op"}} 1 6 7
301 6 ▶ die _e($t2, q{Second operation after 'DEFINE2' must be 'downgrade' or 'after_downgrade'}, $t2->{op}) if !KW_DOWN->{$t2->{op}};
Branch: 1 branch
BranchTrueFalseTotal
unless {"after_downgrade" => 1,"downgrade" => 1}->{$t2->{"op"}} 1 5 6
302 5 $macro{$name} = [ $t1, $t2 ];
303 }
304 elsif ($t->{op} eq 'DEFINE4') {
305 6 ▶ die _e($t, q{Need four operations after 'DEFINE4'}) if @tokens < DEFINE4_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
if @tokens < 4 1 5 6
306 5 my $t1 = shift @tokens;
307 5 my $t2 = shift @tokens;
308 5 my $t3 = shift @tokens;
309 5 my $t4 = shift @tokens;
310 5 ▶ die _e($t1, q{First operation after 'DEFINE4' must be 'before_upgrade'}, $t1->{op}) if $t1->{op} ne 'before_upgrade';
Branch: 1 branch
BranchTrueFalseTotal
if $t1->{"op"} ne "before_upgrade" 1 4 5
311 4 ▶ die _e($t2, q{Second operation after 'DEFINE4' must be 'upgrade'}, $t2->{op}) if $t2->{op} ne 'upgrade';
Branch: 1 branch
BranchTrueFalseTotal
if $t2->{"op"} ne "upgrade" 1 3 4
312 3 ▶ die _e($t3, q{Third operation after 'DEFINE4' must be 'downgrade'}, $t3->{op}) if $t3->{op} ne 'downgrade';
Branch: 1 branch
BranchTrueFalseTotal
if $t3->{"op"} ne "downgrade" 1 2 3
313 2 ▶ die _e($t4, q{Fourth operation after 'DEFINE4' must be 'after_downgrade'}, $t4->{op}) if $t4->{op} ne 'after_downgrade';
Branch: 1 branch
BranchTrueFalseTotal
if $t4->{"op"} ne "after_downgrade" 1 1 2
314 1 $macro{$name} = [ $t1, $t4, $t2, $t3 ];
315 }
316 }
317 elsif (KW_VERSION->{$t->{op}}) {
318 33 ▶ die _e($t, q{'VERSION' must have one param}, "@{$t->{args}}") if 1 != @{$t->{args}};
Branch: 1 branch
BranchTrueFalseTotal
if 1 != @{$t->{"args"};} 2 31 33
319 die _e($t, q{Bad value for 'VERSION'}, $t->{args}[0])
320 31 ▶ if $t->{args}[0] !~ /\A\S+\z/ms || $t->{args}[0] =~ /[\x00-\x1F\x7F \/?*`"'\\]/ms;
Branch: 1 branch
BranchTrueFalseTotal
if not $t->{"args"}[0] =~ /\A\S+\z/ms or $t->{"args"}[0] =~ m([\x00-\x1F\x7F /?*`"'\\])ms 10 21 31
Condition100%3/3
not $t->{"args"}[0] =~ /\A\S+\z/ms or $t->{"args"}[0] =~ m([\x00-\x1F\x7F /?*`"'\\])ms
l!l&&r!l&&!r
3721
MC/DC100%2/2
not $t->{"args"}[0] =~ /\A\S+\z/ms or $t->{"args"}[0] =~ m([\x00-\x1F\x7F /?*`"'\\])ms
not $t->{"args"}[0] =~ /\A\S+\z/ms $t->{"args"}[0] =~ m([\x00-\x1F\x7F /?*`"'\\])ms
Truth table100%3/3
not(A) or B
A= not $t->{"args"}[0] =~ /\A\S+\z/msB= $t->{"args"}[0] =~ m([\x00-\x1F\x7F /?*`"'\\])ms
ABresult
1X1
011
000
321 21 ▶ die _e($t, q{No data allowed for 'VERSION'}, $t->{data}) if $t->{data} ne q{};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"data"} ne "" 1 20 21
322 push @op, {
323 loc => $t->{loc},
324 op => $t->{op},
325 20 args => [ $t->{args}[0] ],
326 };
327 }
328 elsif (KW_RESTORE->{$t->{op}}) {
329 4 ▶ die _e($t, q{'RESTORE' must have no params}, "@{$t->{args}}") if 0 != @{$t->{args}};
Branch: 1 branch
BranchTrueFalseTotal
if 0 != @{$t->{"args"};} 1 3 4
330 3 ▶ die _e($t, q{No data allowed for 'RESTORE'}, $t->{data}) if $t->{data} ne q{};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"data"} ne "" 1 2 3
331 push @op, {
332 loc => $t->{loc},
333 op => $t->{op},
334 2 args => [],
335 };
336 }
337 ▶ elsif (KW_UP->{$t->{op}} || KW_DOWN->{$t->{op}}) {
Condition100%3/3
{"before_upgrade" => 1,"upgrade" => 1}->{$t->{"op"}} or {"after_downgrade" => 1,"downgrade" => 1}->{$t->{"op"}}
l!l&&r!l&&!r
1585
MC/DC100%2/2
{"before_upgrade" => 1,"upgrade" => 1}->{$t->{"op"}} or {"after_downgrade" => 1,"downgrade" => 1}->{$t->{"op"}}
{"before_upgrade" => 1,"upgrade" => 1}->{$t->{"op"}} {"after_downgrade" => 1,"downgrade" => 1}->{$t->{"op"}}
Truth table100%3/3
A or B
A= {"before_upgrade" => 1,"upgrade" => 1}->{$t->{"op"}}B= {"after_downgrade" => 1,"downgrade" => 1}->{$t->{"op"}}
ABresult
1X1
011
000
338 23 ▶ die _e($t, "'$t->{op}' require command or data") if !@{$t->{args}} && $t->{data} !~ /\S/ms;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$t->{"args"};} or $t->{"data"} =~ /\S/ms 4 19 23
Condition100%3/3
@{$t->{"args"};} or $t->{"data"} =~ /\S/ms
l!l&&r!l&&!r
1454
MC/DC100%2/2
@{$t->{"args"};} or $t->{"data"} =~ /\S/ms
@{$t->{"args"};} $t->{"data"} =~ /\S/ms
Truth table100%3/3
A or B
A= @{$t->{"args"};}B= $t->{"data"} =~ /\S/ms
ABresult
1X1
011
000
339 push @op, {
340 loc => $t->{loc},
341 op => $t->{op},
342 args => [
343 19 @{$t->{args}} ? (@{$t->{args}}, _data2arg($t->{data}))
344 : _shebang($t->{data})
345 19 ▶ ],
Branch: 1 branch
BranchTrueFalseTotal
@{$t->{'args'};} ? : 14 5 19
346 };
347 }
348 elsif ($macro{ $t->{op} }) {
349 4 for (@{ $macro{ $t->{op} } }) {
350 my @args
351 9 ▶ = @{$_->{args}} ? (@{$_->{args}}, _data2arg($_->{data}))
Branch: 1 branch
BranchTrueFalseTotal
$_->{'data'} =~ /\S/ms ? : 2 6 8
352 : $_->{data} =~ /\S/ms ? _shebang($_->{data})
353 9 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
@{$_->{'args'};} ? : 1 8 9
354 ;
355 @args
356 3 ▶ = @args ? (@args, @{$t->{args}}, _data2arg($t->{data}))
Branch: 1 branch
BranchTrueFalseTotal
@{$t->{'args'};} ? : 4 2 6
357 6 ▶ : @{$t->{args}} ? (@{$t->{args}}, _data2arg($t->{data}))
Branch: 1 branch
BranchTrueFalseTotal
$t->{'data'} =~ /\S/ms ? : 1 1 2
358 : $t->{data} =~ /\S/ms ? _shebang($t->{data})
359 9 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 3 6 9
360 ;
361 9 ▶ die _e($t, "'$t->{op}' require command or data") if !@args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 1 8 9
362 push @op, {
363 loc => $t->{loc},
364 op => $_->{op},
365 8 args => \@args,
366 };
367 }
368 }
369 else {
370 1 die _e($t, "Unknown operation '$t->{op}'");
371 }
372 }
373 10 return @op;
374 }
375  
376 8 ▶sub _shebang {
Subroutine _shebang: called CC=3 SCAR=11.4
377 8 my ($script) = @_;
378 8 ▶ state $bin = (grep { -x "$_/bash" } split /:/ms, $ENV{PATH})[0] or die 'bash not found';
Branch: 1 branch
BranchTrueFalseTotal
state $bin = (grep {-x "$_/bash";} split(/:/ms, $ENV{"PATH"}, 0))[0] or die "bash not found" 8 0 8
379 8 ▶ return $script =~ /\A#!/ms ? $script : "#!$bin/bash -ex\n$script";
Branch: 1 branch
BranchTrueFalseTotal
$script =~ /\A#!/ms ? : 0 8 8
380 }
381  
382 66 ▶sub _tokenize {
Subroutine _tokenize: called CC=11 SCAR=24.0
383 66 my ($fh, $loc) = @_;
384 66 state $QUOTED = {
385 q{\\} => q{\\},
386 q{"} => q{\"},
387 'n' => "\n",
388 'r' => "\r",
389 't' => "\t",
390 };
391 66 my @tokens;
392 66 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 186 58 244
393 186 $loc->{line}++;
394 186 ▶ if (/\A#/ms) {
Branch: 3 branches
BranchTrueFalseTotal
if (/\A#/ms) { } 13 173 186
elsif (/\A(\S+)\s*(.*)\z/ms) { } 145 28 173
elsif (/\A(?:\r?\n|[ ][ ].*)\z/ms) { } 25 3 28
395 # skip comments
396 }
397 elsif (/\A(\S+)\s*(.*)\z/ms) {
398 # parse token's op and args
399 145 my ($op, $args) = ($1, $2);
400 145 my @args;
401 145 ▶ while ($args =~ /\G([^\s"\\]+|"[^"\\]*(?:\\[\\"nrt][^"\\]*)*")(?:\s+|\z)/msgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /\G([^\s"\\]+|"[^"\\]*(?:\\[\\"nrt][^"\\]*)*")(?:\s+|\z)/cgms) 103 145 248
402 103 my $param = $1;
403 103 ▶ if ($param =~ s/\A"(.*)"\z/$1/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ s/\A"(.*)"\z/$1/ms) 17 86 103
404 17 $param =~ s/\\([\\"nrt])/$QUOTED->{$1}/msg;
405 }
406 103 push @args, $param;
407 }
408 145 ▶ die _e({loc=>$loc}, 'Bad operation param', $1) if $args =~ /\G(.+)\z/msgc; ## no critic (ProhibitCaptureWithoutTest)
Branch: 1 branch
BranchTrueFalseTotal
if $args =~ /\G(.+)\z/cgms 4 141 145
409 push @tokens, {
410 141 loc => {%{ $loc }},
411 op => $op,
412 args=> \@args,
413 data=> q{},
414 };
415 }
416 elsif (/\A(?:\r?\n|[ ][ ].*)\z/ms) {
417 25 ▶ if (@tokens) {
Branch: 2 branches
BranchTrueFalseTotal
if (@tokens) { } 22 3 25
elsif (/\S/ms) { } 1 2 3
418 22 $tokens[-1]{data} .= $_;
419 }
420 elsif (/\S/ms) {
421 1 die _e({loc=>$loc}, 'Data before operation', $_);
422 }
423 else {
424 # skip /^\s*$/ before first token
425 }
426 }
427 else {
428 3 die _e({loc=>$loc}, 'Bad token', $_);
429 }
430 }
431 # post-process data
432 58 for (@tokens) {
433 141 $_->{data} =~ s/(\A(?:.*?\n)??)(?:\r?\n)*\z/$1/ms;
434 141 $_->{data} =~ s/^[ ][ ]//msg;
435 }
436 58 return @tokens;
437 }
438  
439  
440 3 1; # Magic true value required at end of module
441 __END__
↑ Summary