lib/Test2/Harness/UI/Util.pm

filter: Statementstmt 55.7% 63 / 113 Branchbran 28.0% 14 / 50 Conditioncond 13.6% 3 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 64.7% 11 / 17 Podpod 0.0% 0 / 10 totaltotal 40.4% 91 / 225 CC 36 SCAR 38.0
CC
36
Cov
43%
config_from_settings
156.0
find_job
30.0
share_file
20.0
CRAP 44.8
« lib/Test2/Harness/UI/Sweeper.pm ↑ Summary lib/Test2/Harness/UI/Controller/Coverage.pm »
1 package Test2::Harness::UI::Util;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 our $VERSION = '0.000150';
6  
7 8 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use File::ShareDir();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use Test2::Harness::Util qw/mod2file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Test2::Harness::UI::UUID qw/uuid_inflate/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Importer Importer => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 our @EXPORT = qw/share_dir share_file qdb_driver dbd_driver config_from_settings find_job format_duration parse_duration is_invalid_subtest_name/;
17  
18 8 my %SCHEMA_TO_QDB_DRIVER = (
19 mariadb => 'MySQL',
20 mysql => 'MySQL',
21 postgresql => 'PostgreSQL',
22 );
23  
24 8 my %SCHEMA_TO_DBD_DRIVER = (
25 mariadb => 'DBD::MariaDB',
26 mysql => 'DBD::mysql',
27 postgresql => 'DBD::postgresql',
28 );
29  
30 8 my %BAD_ST_NAME = (
31 '__ANON__' => 1,
32 'unnamed' => 1,
33 'unnamed subtest' => 1,
34 'unnamed summary' => 1,
35 '<UNNAMED ASSERTION>' => 1,
36 );
37  
38 0 ▶sub is_invalid_subtest_name {
Subroutine is_invalid_subtest_name: not called CC=2 SCAR=17.9
Pod undocumented
39 0 my ($name) = @_;
40 0 ▶ return $BAD_ST_NAME{$name} // 0;
Condition0%0/2
$BAD_ST_NAME{$name} // 0
l!l
00
MC/DC0%0/1
$BAD_ST_NAME{$name} // 0
$BAD_ST_NAME{$name}
Truth table0%0/2
A
A= $BAD_ST_NAME{$name}
Aresult
11
00
41 }
42  
43 0 ▶sub find_job {
Subroutine find_job: not called CC=5 SCAR=34.0
Pod undocumented
44 0 my ($schema, $uuid, $try) = @_;
45  
46 0 ▶ $uuid = uuid_inflate($uuid) or croak "Invalid job identifier";
Branch: 1 branch
BranchTrueFalseTotal
$uuid = uuid_inflate($uuid) or croak("Invalid job identifier") 0 0 0
47  
48 0 my $jobs = $schema->resultset('Job');
49  
50 0 ▶ if (length $try) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $try) 0 0 0
51 0 ▶ return $jobs->search({job_id => $uuid}, {order_by => {'-desc' => 'job_try'}, limit => 1})->first
Branch: 1 branch
BranchTrueFalseTotal
if $try == -1 0 0 0
52 if $try == -1;
53  
54 0 return $jobs->search({job_id => $uuid, job_try => $try})->first;
55 }
56  
57 0 ▶ return $jobs->search({job_key => $uuid})->first
Condition0%0/3
$jobs->search({'job_key', $uuid})->first || $jobs->search({'job_id', $uuid}, {'order_by', {'-desc', 'job_try'}, 'limit', 1})->first
l!l&&r!l&&!r
000
MC/DC0%0/2
$jobs->search({'job_key', $uuid})->first || $jobs->search({'job_id', $uuid}, {'order_by', {'-desc', 'job_try'}, 'limit', 1})->first
$jobs->search({'job_key', $uuid})->first $jobs->search({'job_id', $uuid}, {'order_by', {'-desc', 'job_try'}, 'limit', 1})->first
Truth table0%0/3
A || B
A= $jobs->search({'job_key', $uuid})->firstB= $jobs->search({'job_id', $uuid}, {'order_by', {'-desc', 'job_try'}, 'limit', 1})->first
ABresult
1X1
011
000
58 || $jobs->search({job_id => $uuid}, {order_by => {'-desc' => 'job_try'}, limit => 1})->first;
59 }
60  
61 4 ▶sub base_name {
Subroutine base_name: called CC=1 SCAR=0.0
Pod undocumented
62 4 my ($in) = @_;
63  
64 4 my $out = lc($in);
65 4 $out =~ s/\.sql$//;
66 4 $out =~ s/\d+$//g;
67  
68 4 return $out;
69 }
70  
71 4 ▶sub qdb_driver {
Subroutine qdb_driver: called CC=1 SCAR=0.0
Pod undocumented
72 4 my $base = base_name(@_);
73 4 return $SCHEMA_TO_QDB_DRIVER{$base};
74 }
75  
76 0 ▶sub dbd_driver {
Subroutine dbd_driver: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my $base = base_name(@_);
78 0 return $SCHEMA_TO_DBD_DRIVER{$base};
79 }
80  
81 0 ▶sub share_file {
Subroutine share_file: not called CC=4 SCAR=30.0
Pod undocumented
82 0 my ($file) = @_;
83  
84 return File::ShareDir::dist_file('Test2-Harness-UI' => $file)
85 0 ▶ unless 'dev' eq ($ENV{T2_HARNESS_UI_ENV} || '');
Branch: 1 branch
BranchTrueFalseTotal
unless "dev" eq ($ENV{"T2_HARNESS_UI_ENV"} || "") 0 0 0
Condition0%0/2
$ENV{'T2_HARNESS_UI_ENV'} || ''
l!l
00
MC/DC0%0/1
$ENV{'T2_HARNESS_UI_ENV'} || ''
$ENV{'T2_HARNESS_UI_ENV'}
Truth table0%0/2
A
A= $ENV{'T2_HARNESS_UI_ENV'}
Aresult
11
00
86  
87 0 my $path = "share/$file";
88 0 ▶ croak "Could not find '$file'" unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 0 0
89  
90 0 return $path;
91 }
92  
93 0 ▶sub share_dir {
Subroutine share_dir: not called CC=4 SCAR=30.0
Pod undocumented
94 0 my ($dir) = @_;
95  
96 0 my $path;
97  
98 0 ▶ if ('dev' eq ($ENV{T2_HARNESS_UI_ENV} || '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ('dev' eq ($ENV{'T2_HARNESS_UI_ENV'} || '')) { } 0 0 0
Condition0%0/2
$ENV{'T2_HARNESS_UI_ENV'} || ''
l!l
00
MC/DC0%0/1
$ENV{'T2_HARNESS_UI_ENV'} || ''
$ENV{'T2_HARNESS_UI_ENV'}
Truth table0%0/2
A
A= $ENV{'T2_HARNESS_UI_ENV'}
Aresult
11
00
99 0 $path = "share/$dir";
100 }
101 else {
102 0 my $root = File::ShareDir::dist_dir('Test2-Harness-UI');
103 0 $path = "$root/$dir";
104 }
105  
106 0 ▶ croak "Could not find '$dir'" unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 0 0
107  
108 0 return $path;
109 }
110  
111 0 ▶sub config_from_settings {
Subroutine config_from_settings: not called CC=12 SCAR=50.5
Pod undocumented
112 0 my ($settings) = @_;
113  
114 0 ▶ my $db = $settings->prefix('yathui-db') or die "No DB settings";
Branch: 1 branch
BranchTrueFalseTotal
my $db = $settings->prefix("yathui-db") or die "No DB settings" 0 0 0
115  
116 0 ▶ if (my $cmod = $db->config) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmod = $db->config) 0 0 0
117 0 my $file = mod2file($cmod);
118 0 require $file;
119  
120 0 return $cmod->yath_ui_config(%$$db);
121 }
122  
123 0 my $dsn = $db->dsn;
124  
125 0 ▶ unless ($dsn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dsn) 0 0 0
126 0 $dsn = "";
127  
128 0 my $driver = $db->driver;
129 0 my $name = $db->name;
130  
131 0 ▶ $dsn .= "dbi:$driver" if $driver;
Branch: 1 branch
BranchTrueFalseTotal
if $driver 0 0 0
132 0 ▶ $dsn .= ":dbname=$name" if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
133  
134 0 ▶ if (my $socket = $db->socket) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $socket = $db->socket) { } 0 0 0
135 0 my $ld = lc($driver);
136 0 ▶ if ($ld eq 'pg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ld eq 'pg') { } 0 0 0
137 0 $dsn .= ";host=$socket";
138 }
139 else {
140 0 $dsn .= ";${ld}_socket=$socket";
141 }
142 }
143 else {
144 0 my $host = $db->host;
145 0 my $port = $db->port;
146  
147 0 ▶ $dsn .= ";host=$host" if $host;
Branch: 1 branch
BranchTrueFalseTotal
if $host 0 0 0
148 0 ▶ $dsn .= ";port=$port" if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 0 0 0
149 }
150 }
151  
152 0 require Test2::Harness::UI::Config;
153 0 ▶ return Test2::Harness::UI::Config->new(
Condition0%0/4
$db->user // ""
l!l
00
$db->pass // ""
l!l
00
MC/DC0%0/1
$db->user // ""
$db->user
MC/DC0%0/1
$db->pass // ""
$db->pass
Truth table0%0/2
A
A= $db->user
Aresult
11
00
Truth table0%0/2
A
A= $db->pass
Aresult
11
00
154 dbi_dsn => $dsn,
155 dbi_user => $db->user // '',
156 dbi_pass => $db->pass // '',
157 );
158 }
159  
160 1 ▶sub format_duration {
Subroutine format_duration: called CC=6 SCAR=18.9
Pod undocumented
161 1 my $seconds = shift;
162  
163 1 my $minutes = int($seconds / 60);
164 1 my $hours = int($minutes / 60);
165 1 my $days = int($hours / 24);
166  
167 1 $minutes %= 60;
168 1 $hours %= 24;
169  
170 1 $seconds -= $minutes * 60;
171 1 $seconds -= $hours * 60 * 60;
172 1 $seconds -= $days * 60 * 60 * 24;
173  
174 1 my @dur;
175 1 ▶ push @dur => sprintf("%02dd", $days) if $days;
Branch: 1 branch
BranchTrueFalseTotal
if $days 1 0 1
176 1 ▶ push @dur => sprintf("%02dh", $hours) if @dur || $hours;
Branch: 1 branch
BranchTrueFalseTotal
if @dur or $hours 1 0 1
Condition33%1/3
@dur or $hours
l!l&&r!l&&!r
100
MC/DC0%0/2
@dur or $hours
@dur $hours
Truth table33%1/3
A or B
A= @durB= $hours
ABresult
1X1
011
000
177 1 ▶ push @dur => sprintf("%02dm", $minutes) if @dur || $minutes;
Branch: 1 branch
BranchTrueFalseTotal
if @dur or $minutes 1 0 1
Condition33%1/3
@dur or $minutes
l!l&&r!l&&!r
100
MC/DC0%0/2
@dur or $minutes
@dur $minutes
Truth table33%1/3
A or B
A= @durB= $minutes
ABresult
1X1
011
000
178 1 push @dur => sprintf("%07.4fs", $seconds);
179  
180 1 return join ':' => @dur;
181 }
182  
183 7 ▶sub parse_duration {
Subroutine parse_duration: called CC=9 SCAR=22.3
Pod undocumented
184 7 my $duration = shift;
185  
186 7 ▶ return 0 unless $duration;
Branch: 1 branch
BranchTrueFalseTotal
unless $duration 1 6 7
187  
188 6 ▶ return $duration unless $duration =~ m/:?.*[dhms]$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $duration =~ /:?.*[dhms]$/i 1 5 6
189  
190 5 my $out = 0;
191  
192 5 my (@parts) = split ':' => $duration;
193 5 for my $part (@parts) {
194 8 my ($num, $type) = ($part =~ m/^([0-9\.]+)([dhms])$/);
195  
196 8 ▶ unless ($num && $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num and $type) 0 8 8
Condition33%1/3
$num and $type
!ll&&!rl&&r
008
MC/DC0%0/2
$num and $type
$num $type
Truth table33%1/3
A and B
A= $numB= $type
ABresult
0X0
100
111
197 0 warn "invalid duration section '$part'";
198 0 next;
199 }
200  
201 8 ▶ if ($type eq 'd') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'd') { } 2 6 8
elsif ($type eq 'h') { } 2 4 6
elsif ($type eq 'm') { } 2 2 4
202 2 $out += ($num * 60 * 60 * 24);
203 }
204 elsif ($type eq 'h') {
205 2 $out += ($num * 60 * 60);
206 }
207 elsif ($type eq 'm') {
208 2 $out += ($num * 60);
209 }
210 else {
211 2 $out += $num;
212 }
213 }
214  
215 5 return $out;
216 }
217  
218  
219 8 1;
220  
221 __END__
« lib/Test2/Harness/UI/Sweeper.pm ↑ Summary lib/Test2/Harness/UI/Controller/Coverage.pm »