| 1 | package CLI::Simple::TempDir; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use File::Temp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use Scalar::Util qw(blessed reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 18 | my %GENERATED_ACCESSORS; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 18 | our $VERSION = '2.6.0'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | ######################################################################## | ||||||||||||||||||||||||||||
| 16 | 14 | ▶ | sub create_temp_dir { | ||||||||||||||||||||||||||
|
Subroutine
create_temp_dir: called CC=26 SCAR=32.7
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | ######################################################################## | ||||||||||||||||||||||||||||
| 18 | 14 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 14 | ▶ | croak 'create_temp_dir requires an object instance' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | if !blessed($self); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 14 | ▶ | croak 'create_temp_dir requires a hash-based object' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | if reftype($self) ne 'HASH'; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 14 | ▶ | my $no_accessors = delete $args{no_accessors} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{"no_accessors"} // 0
MC/DC100%1/1
delete $args{"no_accessors"} // 0
delete $args{"no_accessors"}
Truth table100%2/2
A
A= delete $args{"no_accessors"}
| |||||||||||||||||||||||||||||
| 27 | 14 | ▶ | my $cleanup = delete $args{cleanup} // 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{"cleanup"} // 1
MC/DC100%1/1
delete $args{"cleanup"} // 1
delete $args{"cleanup"}
Truth table100%2/2
A
A= delete $args{"cleanup"}
| |||||||||||||||||||||||||||||
| 28 | 14 | my $has_accessor = exists $args{accessor}; | |||||||||||||||||||||||||||
| 29 | 14 | my $accessor = delete $args{accessor}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 14 | ▶ | croak 'accessor and no_accessors cannot be combined' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$no_accessors and $has_accessor
MC/DC100%2/2
$no_accessors and $has_accessor
$no_accessors
$has_accessor
Truth table100%3/3
A and B
A= $no_accessorsB= $has_accessor
| |||||||||||||||||||||||||||||
| 32 | if $no_accessors && $has_accessor; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 13 | ▶ | $accessor //= 'temp_dir'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$accessor //= "temp_dir"
MC/DC100%1/1
$accessor //= "temp_dir"
$accessor
Truth table100%2/2
A
A= $accessor
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 13 | ▶ | croak 'cleanup must be 0 or 1' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cleanup and $cleanup =~ /\A[01]\z/
MC/DC50%1/2
defined $cleanup and $cleanup =~ /\A[01]\z/
defined $cleanup
$cleanup =~ /\A[01]\z/
Truth table66%2/3
A and B
A= defined $cleanupB= $cleanup =~ /\A[01]\z/
| |||||||||||||||||||||||||||||
| 37 | if !defined $cleanup || $cleanup !~ /\A[01]\z/; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 12 | ▶ | croak 'no_accessors must be 0 or 1' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $no_accessors and $no_accessors =~ /\A[01]\z/
MC/DC0%0/2
defined $no_accessors and $no_accessors =~ /\A[01]\z/
defined $no_accessors
$no_accessors =~ /\A[01]\z/
Truth table33%1/3
A and B
A= defined $no_accessorsB= $no_accessors =~ /\A[01]\z/
| |||||||||||||||||||||||||||||
| 40 | if !defined $no_accessors || $no_accessors !~ /\A[01]\z/; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | my %supported = map { $_ => 1 } qw(dir template); | |||||||||||||||||||||||||||
| 43 | 12 | foreach my $key ( keys %args ) { | |||||||||||||||||||||||||||
| 44 | croak sprintf q{Unknown temporary directory attribute: %s}, $key | ||||||||||||||||||||||||||||
| 45 | 3 | ▶ | if !exists $supported{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 11 | my $class = ref $self; | |||||||||||||||||||||||||||
| 49 | 11 | ▶ | if ( !$no_accessors ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 10 | ▶ | croak sprintf q{Invalid accessor name: %s}, $accessor | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | if $accessor !~ /\A[a-zA-Z_]\w*\z/; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 9 | foreach my $prefix (qw(get set)) { | |||||||||||||||||||||||||||
| 54 | 18 | my $method = $prefix . q{_} . $accessor; | |||||||||||||||||||||||||||
| 55 | croak sprintf q{Accessor already exists: %s}, $method | ||||||||||||||||||||||||||||
| 56 | 18 | ▶ | if $self->can($method) && !$GENERATED_ACCESSORS{$class}{$method}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->can($method) and not $GENERATED_ACCESSORS{$class}{$method}
MC/DC0%0/2
$self->can($method) and not $GENERATED_ACCESSORS{$class}{$method}
$self->can($method)
not $GENERATED_ACCESSORS{$class}{$method}
Truth table66%2/3
A and not(B)
A= $self->can($method)B= not $GENERATED_ACCESSORS{$class}{$method}
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 10 | my %dir_args = ( CLEANUP => $cleanup ); | |||||||||||||||||||||||||||
| 61 | 10 | ▶ | $dir_args{DIR} = $args{dir} if exists $args{dir}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 10 | ▶ | $dir_args{TEMPLATE} = $args{template} if exists $args{template}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 10 | my $resource = File::Temp->newdir(%dir_args); | |||||||||||||||||||||||||||
| 65 | 10 | my $path = $resource->dirname; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 10 | ▶ | $self->{_cli_simple_temp_dirs} //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_cli_simple_temp_dirs"} //= []
MC/DC100%1/1
$self->{"_cli_simple_temp_dirs"} //= []
$self->{"_cli_simple_temp_dirs"}
Truth table100%2/2
A
A= $self->{"_cli_simple_temp_dirs"}
| |||||||||||||||||||||||||||||
| 68 | 10 | push @{ $self->{_cli_simple_temp_dirs} }, $resource; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 10 | ▶ | if ( !$no_accessors ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 9 | foreach my $prefix (qw(get set)) { | |||||||||||||||||||||||||||
| 72 | 18 | my $method = $prefix . q{_} . $accessor; | |||||||||||||||||||||||||||
| 73 | 18 | ▶ | if ( !$GENERATED_ACCESSORS{$class}{$method} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 18 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 12 | ▶ | if ( $prefix eq 'get' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 6 | ▶ | *{"${class}::${method}"} = sub { return $_[0]->{$accessor}; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 6 | ▶ | *{"${class}::${method}"} = sub { $_[0]->{$accessor} = $_[1]; return $_[0]->{$accessor}; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 12 | $GENERATED_ACCESSORS{$class}{$method} = 1; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 9 | my $setter = q{set_} . $accessor; | |||||||||||||||||||||||||||
| 85 | 9 | $self->$setter($path); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 10 | return $path; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 18 | 1; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||||||||||