blib/lib/TestRail/Utils.pm

filter: Statementstmt 80.9% 98 / 121 Branchbran 60.6% 40 / 66 Conditioncond 66.6% 6 / 9 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 88.8% 16 / 18 Podpod 100% 8 / 8 totaltotal 74.7% 169 / 226 CC 38 SCAR 23.4
CC
38
Cov
73%
TAP2TestFiles
25.0
userInput
20.0
getRunInformation
9.0
CRAP 10.4
« bin/testrail-report ↑ Summary blib/lib/TestRail/API.pm »
1 # ABSTRACT: Utilities for the testrail command line functions, and their main loops.
2 # PODNAME: TestRail::Utils
3  
4 package TestRail::Utils;
5 28 $TestRail::Utils::VERSION = '0.052';
6 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 28 ▶use Carp qw{confess cluck};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Pod::Usage ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use TestRail::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 28 ▶use IO::Interactive::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 28 ▶use Term::ANSIColor 2.01 qw(colorstrip);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 28 ▶use Term::ReadKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 28 ▶use Scalar::Util qw{blessed};
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 17 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod documented
19 17 Pod::Usage::pod2usage(
20 '-verbose' => 2,
21 '-noperldoc' => 1,
22 '-exitval' => 'NOEXIT'
23 );
24 17 return 0;
25 }
26  
27 0 ▶sub userInput {
Subroutine userInput: not called CC=4 SCAR=30.0
Pod documented
28 0 my ($echo_ok) = @_;
29 0 my $input = "";
30  
31 # I'm going to be super paranoid here and consider everything to be sensitive info by default.
32 0 ▶ Term::ReadKey::ReadMode('noecho') unless $echo_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $echo_ok 0 0 0
33 {
34 0 ▶ local $SIG{'INT'} = sub { Term::ReadKey::ReadMode(0); exit 130; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 $input = <STDIN>;
36 0 ▶ chomp($input) if $input;
Branch: 1 branch
BranchTrueFalseTotal
if $input 0 0 0
37 }
38 0 ▶ Term::ReadKey::ReadMode(0) unless $echo_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $echo_ok 0 0 0
39 0 print "\n";
40 0 return $input;
41 }
42  
43 43 ▶sub interrogateUser {
Subroutine interrogateUser: called CC=4 SCAR=18.9
Pod documented
44 43 my ( $options, @keys ) = @_;
45 43 foreach my $key (@keys) {
46 188 ▶ if ( !$options->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{$key}) 0 188 188
47 0 print "Type the $key for your TestRail install below:\n";
48 0 $options->{$key} = TestRail::Utils::userInput( $key ne 'password' );
49 0 ▶ die "$key cannot be blank!" unless $options->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{$key} 0 0 0
50 }
51 }
52 43 return $options;
53 }
54  
55 2 ▶sub parseConfig {
Subroutine parseConfig: called CC=5 SCAR=16.1
Pod documented
56 2 my ( $homedir, $login_only ) = @_;
57 2 my $results = {};
58 2 my $arr = [];
59  
60 2 ▶ open( my $fh, '<', $homedir . '/.testrailrc' )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $homedir . '/.testrailrc' or return undef, undef, undef 2 0 2
61 or return ( undef, undef, undef ); #couldn't open!
62 2 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 34 2 36
63 34 chomp;
64 34 @$arr = split( /=/, $_ );
65 34 ▶ if ( scalar(@$arr) != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$arr != 2) 4 30 34
66 4 warn("Could not parse $_ in '$homedir/.testrailrc'!\n");
67 4 next;
68 }
69 30 $results->{ lc( $arr->[0] ) } = $arr->[1];
70 }
71 2 close($fh);
72 2 ▶ return ( $results->{'apiurl'}, $results->{'password'}, $results->{'user'} )
Branch: 1 branch
BranchTrueFalseTotal
if $login_only 1 1 2
73 if $login_only;
74 1 return $results;
75 }
76  
77 392 ▶sub getFilenameFromTapLine {
Subroutine getFilenameFromTapLine: called CC=8 SCAR=20.9
Pod documented
78 392 my $orig = shift;
79  
80 392 $orig =~ s/ *$//g; # Strip all trailing whitespace
81  
82 #Special case
83 392 my ($is_skipall) = $orig =~ /(.*)\.+ skipped:/;
84 392 ▶ return $is_skipall if $is_skipall;
Branch: 1 branch
BranchTrueFalseTotal
if $is_skipall 2 390 392
85  
86 390 my @process_split = split( / /, $orig );
87 390 ▶ return 0 unless scalar(@process_split);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @process_split 27 363 390
88 363 my $dotty =
89 pop @process_split; #remove the ........ (may repeat a number of times)
90 363 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $dotty =~ /\d/ 119 244 363
91 if $dotty =~
92 /\d/; #Apparently looking for literal dots returns numbers too. who knew?
93 244 chomp $dotty;
94 244 my $line = join( ' ', @process_split );
95  
96 #IF it ends in a bunch of dots
97 #AND it isn't an ok/not ok
98 #AND it isn't a comment
99 #AND it isn't blank
100 #THEN it's a test name
101  
102 244 ▶ return $line
Branch: 1 branch
BranchTrueFalseTotal
if $dotty =~ /^\.+$/ and not $line =~ /^ok|not ok/ and not $line =~ /^# / and $line 40 204 244
Condition66%6/9
$dotty =~ /^\.+$/ and not $line =~ /^ok|not ok/ and not $line =~ /^# / and $line
!ll&&!rl&&r
204040
$dotty =~ /^\.+$/ and not $line =~ /^ok|not ok/ and not $line =~ /^# /
!ll&&!rl&&r
204040
$dotty =~ /^\.+$/ and not $line =~ /^ok|not ok/
!ll&&!rl&&r
204040
MC/DC25%1/4
$dotty =~ /^\.+$/ and not $line =~ /^ok|not ok/ and not $line =~ /^# / and $line
$dotty =~ /^\.+$/ not $line =~ /^ok|not ok/ not $line =~ /^# / $line
Truth table40%2/5
A and not(B) and not(C) and D
A= $dotty =~ /^\.+$/B= not $line =~ /^ok|not ok/C= not $line =~ /^# /D= $line
ABCDresult
0XXX0
10XX0
110X0
11100
11111
103 if ( $dotty =~ /^\.+$/
104 && !( $line =~ /^ok|not ok/ )
105 && !( $line =~ /^# / )
106 && $line );
107 204 return 0;
108 }
109  
110 9 ▶sub TAP2TestFiles {
Subroutine TAP2TestFiles: called CC=11 SCAR=32.2
Pod documented
111 9 my $file = shift;
112 9 my ( $fh, $fcontents, @files );
113  
114 9 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 9 0 9
115 9 open( $fh, '<', $file );
116 9 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 197 9 206
117 197 $_ = colorstrip($_); #strip prove brain damage
118  
119 197 ▶ if ( getFilenameFromTapLine($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (getFilenameFromTapLine($_)) 17 180 197
120 17 ▶ push( @files, $fcontents ) if $fcontents;
Branch: 1 branch
BranchTrueFalseTotal
if $fcontents 8 9 17
121 17 $fcontents = '';
122 }
123 197 $fcontents .= $_;
124 }
125 9 close($fh);
126 9 ▶ push( @files, $fcontents ) if $fcontents;
Branch: 1 branch
BranchTrueFalseTotal
if $fcontents 9 0 9
127 }
128 else {
129 #Just read STDIN, print help if no file was passed
130 0 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
if IO::Interactive::Tiny::is_interactive() 0 0 0
131 "ERROR: no file passed, and no data piped in! See --help for usage.\n"
132 if IO::Interactive::Tiny::is_interactive();
133 0 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 0 0 0
134 0 $_ = colorstrip($_); #strip prove brain damage
135 0 ▶ if ( getFilenameFromTapLine($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (getFilenameFromTapLine($_)) 0 0 0
136 0 ▶ push( @files, $fcontents ) if $fcontents;
Branch: 1 branch
BranchTrueFalseTotal
if $fcontents 0 0 0
137 0 $fcontents = '';
138 }
139 0 $fcontents .= $_;
140 }
141 0 ▶ push( @files, $fcontents ) if $fcontents;
Branch: 1 branch
BranchTrueFalseTotal
if $fcontents 0 0 0
142 }
143 9 return @files;
144 }
145  
146 46 ▶sub getRunInformation {
Subroutine getRunInformation: called CC=9 SCAR=22.0
Pod documented
147 46 my ( $tr, $opts ) = @_;
148 46 ▶ confess("First argument must be instance of TestRail::API")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($tr) eq "TestRail::API" 0 46 46
149 unless blessed($tr) eq 'TestRail::API';
150  
151 46 my $project = $tr->getProjectByName( $opts->{'project'} );
152 46 ▶ confess "No such project '$opts->{project}'.\n" if !$project;
Branch: 1 branch
BranchTrueFalseTotal
unless $project 1 45 46
153  
154 45 my ( $run, $plan );
155  
156 45 ▶ if ( $opts->{'plan'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'plan'}) { } 24 21 45
157 24 $plan = $tr->getPlanByName( $project->{'id'}, $opts->{'plan'} );
158 24 ▶ confess "No such plan '$opts->{plan}'!\n" if !$plan;
Branch: 1 branch
BranchTrueFalseTotal
unless $plan 1 23 24
159 $run =
160 23 $tr->getChildRunByName( $plan, $opts->{'run'}, $opts->{'configs'} );
161 }
162 else {
163 21 $run = $tr->getRunByName( $project->{'id'}, $opts->{'run'} );
164 }
165  
166 44 ▶ confess
Branch: 1 branch
BranchTrueFalseTotal
unless $run 3 41 44
167 "No such run '$opts->{run}' matching the provided configs (if any).\n"
168 if !$run;
169  
170 #If the run/plan has a milestone set, then return it too
171 41 my $milestone;
172 41 ▶ my $mid = $plan ? $plan->{'milestone_id'} : $run->{'milestone_id'};
Branch: 1 branch
BranchTrueFalseTotal
$plan ? : 20 21 41
173 41 ▶ if ($mid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mid) 20 21 41
174 20 $milestone = $tr->getMilestoneByID($mid);
175 20 ▶ confess "Could not fetch run milestone!"
Branch: 1 branch
BranchTrueFalseTotal
unless $milestone 0 20 20
176 unless $milestone; #hope this doesn't happen
177 }
178  
179 41 return ( $project, $plan, $run, $milestone );
180 }
181  
182 37 ▶sub getHandle {
Subroutine getHandle: called CC=3 SCAR=11.2
Pod documented
183 37 my $opts = shift;
184  
185 37 ▶ $opts->{'debug'} = 1 if ( $opts->{'browser'} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"browser"} 37 0 37
186  
187 my $tr = TestRail::API->new(
188 $opts->{apiurl}, $opts->{user}, $opts->{password},
189 37 $opts->{'encoding'}, $opts->{'debug'}
190 );
191 37 ▶ if ( $opts->{'browser'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"browser"}) 37 0 37
192 37 $tr->{'browser'} = $opts->{'browser'};
193 37 $tr->{'debug'} = 0;
194 }
195 37 return $tr;
196 }
197  
198 28 1;
199  
200 __END__
« bin/testrail-report ↑ Summary blib/lib/TestRail/API.pm »