UnitTest.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 68.7% 11 / 16 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 7 totaltotal 82.3% 112 / 136 CC 13 SCAR 10.5
CC
13
Cov
89%
read_config
5.1
init_test
4.0
create_credentials_file
4.0
CRAP 2.9
« blib/lib/Amazon/Credentials/HTTP/UserAgent.pm ↑ Summary blib/lib/Amazon/Credentials/Provider.pm »
1 package UnitTest;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Date::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use parent qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 our @EXPORT = qw(
15 init_test
16 format_time
17 TRUE
18 FALSE
19 FIVE_MINUTES
20 ISO_8601_FORMAT
21 );
22  
23 9 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ISO_8601_FORMAT => '%Y-%m-%dT%H:%M:%SZ',
25 TRUE => 1,
26 FALSE => 0,
27 FIVE_MINUTES => 5 * 60,
28 9 };
29  
30 9 ▶caller or __PACKAGE__->main();
Branch: 1 branch
BranchTrueFalseTotal
caller or __PACKAGE__->main 9 0 9
31  
32 ########################################################################
33 2 ▶sub format_time {
Subroutine format_time: called CC=1 SCAR=0.0
Pod undocumented
34 ########################################################################
35 2 my ($time) = @_;
36  
37 2 return time2str( ISO_8601_FORMAT, time + $time, 'GMT' );
38 }
39  
40 ########################################################################
41 7 ▶sub read_config {
Subroutine read_config: called CC=5 SCAR=16.2
Pod undocumented
42 ########################################################################
43 7 my ( $fh, $test ) = @_;
44  
45 7 ▶ $fh //= *DATA;
Condition33%1/3
$fh //= *DATA
l!l&&r!l&&!r
700
MC/DC0%0/2
$fh //= *DATA
$fh *DATA
Truth table33%1/3
A //= B
A= $fhB= *DATA
ABresult
1X1
011
000
46  
47 7 my %configs;
48  
49 my $config_name;
50  
51 7 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 266 7 273
52 266 chomp $line;
53 266 ▶ if ( $line =~ /^--- (.*) ---$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^--- (.*) ---$/) 21 245 266
54 21 $config_name = $1;
55 21 $configs{$1} = [];
56 21 next;
57 }
58  
59 245 push @{ $configs{$config_name} }, $line;
60 }
61  
62 7 close $fh;
63  
64 7 ▶ return $configs{$test} ? $configs{$test} : $configs{'01-credentials.t'};
Branch: 1 branch
BranchTrueFalseTotal
$configs{$test} ? : 7 0 7
65 }
66  
67 ########################################################################
68 7 ▶sub create_credentials_file {
Subroutine create_credentials_file: called CC=4 SCAR=13.9
Pod undocumented
69 ########################################################################
70 7 my ( $home, $credentials, $vars ) = @_;
71  
72 # poor mans templating...
73 7 foreach ( @{$credentials} ) {
74 115 ▶ next if !/(@[^@]+@)/;
Branch: 1 branch
BranchTrueFalseTotal
unless /(\@[^\@]+@)/ 114 1 115
75  
76 1 my $tmpl_var = $1;
77  
78 1 my $var = $tmpl_var;
79 1 $var =~ s/@//g;
80  
81 1 my $val = $vars->{$var};
82  
83 1 s/$tmpl_var/$val/g;
84 }
85  
86 7 mkdir "$home/.aws";
87  
88 7 ▶ open my $fh, '>', "$home/.aws/credentials"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "$home/.aws/credentials" or BAIL_OUT("could not create temporary credentials file") 7 0 7
89 or BAIL_OUT('could not create temporary credentials file');
90  
91 7 print {$fh} join "\n", @{$credentials};
92  
93 7 close $fh;
94  
95 7 return "$home/.aws/credentials";
96 }
97  
98 ########################################################################
99 7 ▶sub create_config_file {
Subroutine create_config_file: called CC=3 SCAR=11.4
Pod undocumented
100 ########################################################################
101 7 my ($home) = @_;
102  
103 7 ▶ if ( !-d "$home/.aws" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d "$home/.aws") 0 7 7
104 0 mkdir "$home/.aws";
105 }
106  
107 7 ▶ open my $fh, '>', "$home/.aws/config"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "$home/.aws/config" or BAIL_OUT("could not create temporary config file") 7 0 7
108 or BAIL_OUT('could not create temporary config file');
109  
110 7 print {$fh} join "\n", qw{[default] region=us-east-2};
111  
112 7 close $fh;
113  
114 7 return "$home/.aws/config";
115 }
116  
117 ########################################################################
118 7 ▶sub create_home_dir {
Subroutine create_home_dir: called CC=1 SCAR=0.0
Pod undocumented
119 ########################################################################
120 7 my ($cleanup) = @_;
121  
122 7 my $home = tempdir( 'amz-credentials-XXXXX', CLEANUP => $cleanup );
123  
124 7 $ENV{HOME} = $home;
125  
126 7 return $home;
127 }
128  
129 ########################################################################
130 7 ▶sub init_test {
Subroutine init_test: called CC=4 SCAR=14.0
Pod undocumented
131 ########################################################################
132 7 my (%args) = @_;
133  
134 7 ▶ $ENV{AWS_PROFILE} = $args{profile} // 'default';
Condition50%1/2
$args{"profile"} // "default"
l!l
07
MC/DC0%0/1
$args{"profile"} // "default"
$args{"profile"}
Truth table50%1/2
A
A= $args{"profile"}
Aresult
11
00
135  
136 7 ▶ $args{cleanup} //= TRUE;
Condition50%1/2
$args{"cleanup"} //= 1
l!l
07
MC/DC0%0/1
$args{"cleanup"} //= 1
$args{"cleanup"}
Truth table50%1/2
A
A= $args{"cleanup"}
Aresult
11
00
137  
138 7 ▶ my $credentials = read_config( *DATA, $args{test} // '01-credentials.t' );
Condition100%2/2
$args{"test"} // "01-credentials.t"
l!l
25
MC/DC100%1/1
$args{"test"} // "01-credentials.t"
$args{"test"}
Truth table100%2/2
A
A= $args{"test"}
Aresult
11
00
139  
140 7 my $home = create_home_dir( $args{cleanup} );
141  
142 7 create_credentials_file( $home, $credentials, $args{vars} );
143  
144 7 create_config_file($home);
145  
146 7 return $home;
147 }
148  
149 ########################################################################
150 0 ▶sub main {
Subroutine main: not called CC=1 SCAR=6.9
Pod undocumented
151 ########################################################################
152 0 return print {*STDERR} Dumper [
153 init_test(
154 cleanup => 0,
155 test => '01-credentials.t',
156 vars => { process => 'foo' },
157 )
158 ];
159 }
160  
161 9 1;
162  
163 __DATA__
« blib/lib/Amazon/Credentials/HTTP/UserAgent.pm ↑ Summary blib/lib/Amazon/Credentials/Provider.pm »