| 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;
}
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;
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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;
}
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;
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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;
}
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;
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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;
}
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;
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 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 // []
MC/DC0%0/1
$path // []
$path
Truth table50%1/2
A
A= $path
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 32 | ▶ | croak 'Path must contain at least 2 versions' if 2 > @path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 32 | my @all_steps; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 32 | ▶ | for (; 2 <= @path; shift @path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 46 | my ($prev, $next) = @path; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 46 | ▶ | croak "Unknown version '$prev'" if !$self->{paths}{$prev}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 46 | ▶ | croak "No one-step migration from '$prev' to '$next'" if !$self->{paths}{$prev}{$next}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 68 | ▶ | croak "'$file' is not a plain file" if !-f $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 66 | my @op = _preprocess(_tokenize($fh, { file => $file, line => 0 })); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | close $fh or croak "close($file): $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 10 | my ($prev_version, $next_version, @steps) = (q{}, q{}); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 10 | ▶ | while (@op) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 33 | my $op = shift @op; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 33 | ▶ | if (KW_VERSION->{$op->{op}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 18 | $next_version = $op->{args}[0]; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 18 | ▶ | if ($prev_version ne q{}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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}]
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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}]
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
@op and {"after_downgrade" => 1,"downgrade" => 1}->{$op[0]{"op"}} || {"RESTORE" => 1}->{$op[0]{"op"}}
{"after_downgrade" => 1,"downgrade" => 1}->{$op[0]{"op"}} or {"RESTORE" => 1}->{$op[0]{"op"}}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 11 | my $op2 = shift @op; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 11 | ▶ | if (KW_RESTORE->{$op2->{op}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 17 | my ($prev, $next) = @path; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 17 | ▶ | if (!$just_restored) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 2 | $just_restored = 1; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 12 | ▶ | or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | if ($from) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | eval { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$step->{"type"} eq "BACKUP" or $step->{"type"} eq "RESTORE" or $step->{"type"} eq "VERSION"
$step->{"type"} eq "BACKUP" or $step->{"type"} eq "RESTORE"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 7 | $cmd = _data2arg($cmd); | |||||||||||||||||||||||||||||||||||||||||
| 223 | 7 | ▶ | chmod 0700, $cmd or croak "chmod($cmd): $!"; ## no critic (ProhibitMagicNumbers) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 13 | ▶ | system($cmd, @{ $step->{args} }) == 0 or die "'$step->{type}' failed: $cmd @{ $step->{args} }\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 10 | print "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 45 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 48 | ▶ | or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 3 | ▶ | die $@ if $is_fatal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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]} || {}
MC/DC0%0/1
$self->{'paths'}{$from[-1]} || {}
$self->{'paths'}{$from[-1]}
Truth table50%1/2
A
A= $self->{'paths'}{$from[-1]}
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 25 | ▶ | return [@from, $to] if $p->{$to}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
$ENV{"SHELL"} // "/bin/sh"
MC/DC0%0/1
$ENV{"SHELL"} // "/bin/sh"
$ENV{"SHELL"}
Truth table0%0/2
A
A= $ENV{"SHELL"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 99 | my $t = shift @tokens; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 99 | ▶ | if ($t->{op} =~ /\ADEFINE[24]?\z/ms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 34 | ▶ | die _e($t, "'$t->{op}' must have one param", "@{$t->{args}}") if 1 != @{$t->{args}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 31 | ▶ | die _e($t, "Bad name for '$t->{op}'", $t->{args}[0]) if $t->{args}[0] !~ /\A(?!#)\S+\z/ms; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 26 | ▶ | die _e($t, "No data allowed for '$t->{op}'", $t->{data}) if $t->{data} ne q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 23 | my $name = $t->{args}[0]; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 23 | ▶ | die _e($t, "Can't redefine keyword '$name'") if KW->{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 20 | ▶ | die _e($t, "'$name' is already defined") if $macro{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 17 | ▶ | if ($t->{op} eq 'DEFINE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 3 | ▶ | die _e($t, q{Need operation after 'DEFINE'}) if @tokens < DEFINE_TOKENS; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
{"before_upgrade" => 1,"upgrade" => 1}->{$t1->{"op"}} or {"after_downgrade" => 1,"downgrade" => 1}->{$t1->{"op"}}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 4 | ▶ | die _e($t2, q{Second operation after 'DEFINE4' must be 'upgrade'}, $t2->{op}) if $t2->{op} ne 'upgrade'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 3 | ▶ | die _e($t3, q{Third operation after 'DEFINE4' must be 'downgrade'}, $t3->{op}) if $t3->{op} ne 'downgrade'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $t->{"args"}[0] =~ /\A\S+\z/ms or $t->{"args"}[0] =~ m([\x00-\x1F\x7F /?*`"'\\])ms
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
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 21 | ▶ | die _e($t, q{No data allowed for 'VERSION'}, $t->{data}) if $t->{data} ne q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 3 | ▶ | die _e($t, q{No data allowed for 'RESTORE'}, $t->{data}) if $t->{data} ne q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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"}}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 23 | ▶ | die _e($t, "'$t->{op}' require command or data") if !@{$t->{args}} && $t->{data} !~ /\S/ms; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$t->{"args"};} or $t->{"data"} =~ /\S/ms
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | : $_->{data} =~ /\S/ms ? _shebang($_->{data}) | ||||||||||||||||||||||||||||||||||||||||||
| 353 | 9 | ▶ | : () | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 355 | @args | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 3 | ▶ | = @args ? (@args, @{$t->{args}}, _data2arg($t->{data})) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | 6 | ▶ | : @{$t->{args}} ? (@{$t->{args}}, _data2arg($t->{data})) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | : $t->{data} =~ /\S/ms ? _shebang($t->{data}) | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 9 | ▶ | : () | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 9 | ▶ | die _e($t, "'$t->{op}' require command or data") if !@args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 8 | ▶ | return $script =~ /\A#!/ms ? $script : "#!$bin/bash -ex\n$script"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 186 | $loc->{line}++; | |||||||||||||||||||||||||||||||||||||||||
| 394 | 186 | ▶ | if (/\A#/ms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | 103 | my $param = $1; | |||||||||||||||||||||||||||||||||||||||||
| 403 | 103 | ▶ | if ($param =~ s/\A"(.*)"\z/$1/ms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||