File Coverage

bin/testrail-replay
Criterion Covered Total %
statement 69 76 90.7
branch 16 32 50.0
condition 4 11 36.3
subroutine 8 8 100.0
pod n/a
total 97 127 76.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             # ABSTRACT: List runs in a TestRail project matching the provided filters
3             # PODNAME: TestRail::Bin::Replay
4              
5             package TestRail::Bin::Replay;
6             $TestRail::Bin::Replay::VERSION = '0.052';
7 1     1   492 use strict;
  1         2  
  1         26  
8 1     1   4 use warnings;
  1         2  
  1         21  
9              
10 1     1   640 use TestRail::API;
  1         3  
  1         49  
11 1     1   477 use TestRail::Utils;
  1         3  
  1         35  
12 1     1   512 use TestRail::Utils::Find;
  1         3  
  1         54  
13              
14 1     1   670 use Getopt::Long qw{GetOptionsFromArray};
  1         9290  
  1         15  
15 1     1   537 use File::HomeDir qw{my_home};
  1         4881  
  1         636  
16              
17             if ( !caller() ) {
18             my ( $out, $code ) = run( 'args' => \@ARGV );
19             print $out;
20             exit $code;
21             }
22              
23             sub run {
24 4     4   22218 my %params = @_;
25 4         9 my $opts = {};
26              
27             # Parse config file
28 4   50     14 my $homedir = my_home() || '.';
29 4 50       207 if ( -e $homedir . '/.testrailrc' ) {
30 0         0 $opts = TestRail::Utils::parseConfig($homedir);
31             }
32              
33             GetOptionsFromArray(
34             $params{'args'},
35             'apiurl=s' => \$opts->{'apiurl'},
36             'password=s' => \$opts->{'password'},
37             'user=s' => \$opts->{'user'},
38             'j|project=s' => \$opts->{'project'},
39             'e|encoding=s' => \$opts->{'encoding'},
40             'p|plan' => \$opts->{'plan'},
41             'v|verbose' => \$opts->{'verbose'},
42             'w|watch' => \$opts->{'watch'},
43 4         57 'h|help' => \$opts->{'help'},
44             );
45              
46 4 100       3534 if ( $opts->{help} ) { return ( '', TestRail::Utils::help() ); }
  1         8  
47              
48 3         7 $opts->{'browser'} = $params{'browser'};
49 3         13 TestRail::Utils::interrogateUser( $opts, qw{apiurl user password project} );
50              
51 3         16 my $tr = TestRail::Utils::getHandle($opts);
52 3         35 my $project = $tr->getProjectByName( $opts->{'project'} );
53 3 50       18 return ( "No such project '$opts->{project}'", 2 ) unless $project;
54              
55 3         3 my $subject = $ARGV[0];
56 3         5 my ( $runs, $plan );
57              
58 3 100       8 if ( $opts->{'plan'} ) {
59 2         8 $plan = $tr->getPlanByName( $project->{'id'}, $subject );
60 2 50       15 $runs = $tr->getChildRuns($plan) if $plan;
61             }
62             else {
63 1         30 my $run = $tr->getRunByName( $project->{'id'}, $subject );
64 1 50       7 push( @$runs, $run ) if $run;
65             }
66              
67 3 50 33     23 return ( "No runs found matching your criterion.\n", 1 )
68             unless ref $runs eq 'ARRAY' && @$runs;
69              
70 3         5 my @tests;
71 3         7 foreach my $run (@$runs) {
72 3         8 my $tests = $tr->getTests( $run->{id} );
73 3         22 @$tests = map { $_->{config} = $run->{config}; $_ } @$tests;
  3         20  
  3         11  
74 3 50 33     15 push( @tests, @{$tests} ) if ref $tests eq 'ARRAY' && @$tests;
  3         24  
75             }
76              
77             #TODO get status ids for untested/retest, check em
78 3         11 my @retry_status_ids = $tr->statusNamesToIds(qw{untested retest});
79 3         23 my @bad_status_ids = $tr->statusNamesToIds(qw{failed todo_pass});
80              
81 3         12 my $rc = 0;
82 3         8 while ( my $test = shift @tests ) {
83 3         8 my $results = $tr->getTestResults( $test->{id}, 1 );
84 3 50 33     26 if ( !( ref $results eq 'ARRAY' ) || !@$results ) {
85 0 0       0 push( @tests, $test ) if $opts->{watch};
86 0         0 next;
87             }
88 3         5 my $result = $results->[0];
89 3 50       7 next unless ref $result eq 'HASH';
90 3 50       5 if ( !grep { $result->{status_id} eq $_ } @retry_status_ids ) {
  3         13  
91 3 50       6 print $result->{comment} . "\n" if $opts->{verbose};
92 3         6 my $line = $test->{title};
93 3 50       5 $line .= " ($test->{config})" if $test->{config};
94 3         5 $line .= " ...";
95 3 50       4 if ( grep { $result->{status_id} eq $_ } @bad_status_ids ) {
  3         9  
96 0         0 $rc = 3;
97 0         0 $line .= ' not';
98             }
99 3         144 print "# $opts->{apiurl}/index.php?/tests/view/$test->{id}\n";
100 3         35 print "$line ok\n";
101 3         23 next;
102             }
103 0 0       0 push( @tests, $test ) if $opts->{watch};
104              
105             # If we have to wait, back off a little to not slam the server
106 0         0 sleep 1;
107             }
108              
109 3         49 return ( "Done", $rc );
110             }
111              
112             1;
113              
114             =pod
115              
116             =encoding UTF-8
117              
118             =head1 NAME
119              
120             TestRail::Bin::Replay - List runs in a TestRail project matching the provided filters
121              
122             =head1 VERSION
123              
124             version 0.052
125              
126             =head1 SYNOPSIS
127              
128             testrail-replay [OPTIONS] NAME
129              
130             require `which testrail-runs`;
131             TestRail::Bin::Replay::run('args' => \@args);
132              
133             =head1 DESCRIPTION
134              
135             testrail-replay - Re-play the results of a test run or plan as though executed by prove.
136             Optionally wait for results to come in.
137              
138             Can be used as the modulino TestRail::Bin::Replay.
139             Has a single 'run' function which accepts a hash with the 'args' parameter being the array of arguments.
140              
141             =head1 PARAMETERS:
142              
143             =head2 MANDATORY PARAMETERS
144              
145             =over 4
146              
147             --apiurl : full URL to get to TestRail index document
148              
149             --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
150              
151             --user : Your TestRail User Name.
152              
153             -j --project : desired project name.
154              
155             =back
156              
157             All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
158              
159             =head2 OPTIONAL PARAMETERS
160              
161             =over 4
162              
163             -p --plan : Instead of a run to look for, look for a plan with the provided NAME.
164              
165             -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L for supported encodings.
166              
167             -v --verbose : Print full test output rather than summaries with links to the testrail result.
168              
169             -w --watch : Watch tests until all report results other than untested or re-test.
170              
171             =back
172              
173             =head1 CONFIGURATION FILE
174              
175             In your \$HOME, (or the current directory, if your system has no concept of a home directory) put a file called .testrailrc with key=value syntax separated by newlines.
176             Valid Keys are the same as documented by L.
177             All options specified thereby are overridden by passing the command-line switches above.
178              
179             =head1 MISCELLANEOUS OPTIONS:
180              
181             =over 4
182              
183             --help : show this output
184              
185             =back
186              
187             =head1 SPECIAL THANKS
188              
189             Thanks to cPanel Inc, for graciously funding the creation of this distribution.
190              
191             =head1 AUTHOR
192              
193             George S. Baugh
194              
195             =head1 SOURCE
196              
197             The development version is on github at L
198             and may be cloned from L
199              
200             =head1 COPYRIGHT AND LICENSE
201              
202             This software is copyright (c) 2022 by George S. Baugh.
203              
204             This is free software; you can redistribute it and/or modify it under
205             the same terms as the Perl 5 programming language system itself.
206              
207             =cut
208              
209             __END__