testrail-tests 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/usr/bin/env perl
  2. # ABSTRACT: List tests in a TestRail run matching the provided filters
  3. # PODNAME: testrail-tests
  4. =head1 SYNOPSIS
  5. testrail-tests [OPTIONS] | xargs prove -PTestrail=...
  6. =head1 DESCRIPTION
  7. testrail-tests - list tests in a run matching the provided filters.
  8. =head2 PARAMETERS:
  9. =head3 MANDATORY PARAMETERS
  10. -j --project [project]: desired project name.
  11. -r --run [run]: desired run name.
  12. =head3 SEMI-OPTIONAL PARAMETERS
  13. -p --plan [plan]: desired plan name. Required if the run passed is a child of a plan.
  14. -m --match [dir]: attempt to find filenames matching the test names in the provided dir.
  15. --no-match [dir]: attempt to find filenames that do not match test names in the provided dir.
  16. -n --no-recurse: if match (or no-match) passed, do not recurse subdirectories.
  17. =head3 OPTIONAL PARAMETERS
  18. -c --config [config]: configuration name to filter plans in run. Can be passed multiple times.
  19. -s --status [status]: only list tests marked as [status] in testrail. Can be passed multiple times.
  20. -a --assignedto [user]: only list tests assigned to user. Can be passed multiple times.
  21. =head3 CONFIG OPTIONS
  22. In your \$HOME, (or the current directory, if your system has no
  23. concept of a home directory) put a file called .testrailrc with
  24. key=value syntax separated by newlines.
  25. Valid Keys are: apiurl,user,password
  26. =head3 CONFIG OVERRIDES
  27. These override the config, if present.
  28. If neither are used, you will be prompted.
  29. --apiurl [url] : full URL to get to TestRail index document
  30. --password [key] : Your TestRail Password.
  31. --user [name] : Your TestRail User Name.
  32. =head2 TESTING OPTIONS:
  33. --mock: don't do any real HTTP requests.
  34. --help: show this output
  35. =cut
  36. use strict;
  37. use warnings;
  38. use utf8;
  39. use TestRail::API;
  40. use TestRail::Utils;
  41. use Getopt::Long;
  42. use File::HomeDir qw{my_home};
  43. use File::Find;
  44. use Cwd qw{abs_path};
  45. use File::Basename qw{basename};
  46. sub help {
  47. print("
  48. testrail-tests - list tests in a run matching the provided filters.
  49. USAGE:
  50. testrail-tests [OPTIONS] | xargs prove -PTestrail=...
  51. PARAMETERS:
  52. [MANDATORY PARAMETERS]
  53. -j --project [project]: desired project name.
  54. -r --run [run]: desired run name.
  55. [SEMI-OPTIONAL PARAMETERS]
  56. -p --plan [plan]: desired plan name. Required if the run passed is a child of a plan.
  57. -m --match [dir]: attempt to find filenames matching the test names in the provided dir.
  58. --no-match [dir]: attempt to find filenames that do not match the test names in the provided dir.
  59. -n --no-recurse: if match passed, do not recurse subdirectories.
  60. [OPTIONAL PARAMETERS]
  61. -c --config [config]: configuration name to filter plans in run. Can be passed multiple times.
  62. -s --status [status]: only list tests marked as [status] in testrail. Can be passed multiple times.
  63. -a --assignedto [user]: only list tests assigned to user. Can be passed multiple times.
  64. [CONFIG OPTIONS]
  65. In your \$HOME, (or the current directory, if your system has no
  66. concept of a home directory) put a file called .testrailrc with
  67. key=value syntax separated by newlines.
  68. Valid Keys are: apiurl,user,password
  69. [CONFIG OVERRIDES]
  70. These override the config, if present.
  71. If neither are used, you will be prompted.
  72. --apiurl [url] : full URL to get to TestRail index document
  73. --password [key] : Your TestRail Password.
  74. --user [name] : Your TestRail User Name.
  75. TESTING OPTIONS:
  76. --mock: don't do any real HTTP requests.
  77. --help: show this output
  78. ");
  79. exit 0;
  80. }
  81. my %opts;
  82. GetOptions(
  83. 'apiurl=s' => \$opts{'apiurl'},
  84. 'password=s' => \$opts{'pass'},
  85. 'user=s' => \$opts{'user'},
  86. 'j|project=s' => \$opts{'project'},
  87. 'p|plan=s' => \$opts{'plan'},
  88. 'r|run=s' => \$opts{'run'},
  89. 'c|config=s@' => \$opts{'configs'},
  90. 's|status=s@' => \$opts{'statuses'},
  91. 'a|assignedto=s@' => \$opts{'users'},
  92. 'mock' => \$opts{'mock'},
  93. 'm|match=s' => \$opts{'match'},
  94. 'no-match=s' => \$opts{'no-match'},
  95. 'n|no-recurse' => \$opts{'no-recurse'},
  96. 'h|help' => \$opts{'help'}
  97. );
  98. if ($opts{help}) { help(); }
  99. #Parse config file if we are missing api url/key or user
  100. my $homedir = my_home() || '.';
  101. if (-e $homedir . '/.testrailrc' && (!$opts{apiurl} || !$opts{pass} || !$opts{user}) ) {
  102. ($opts{apiurl},$opts{pass},$opts{user}) = TestRail::Utils::parseConfig($homedir);
  103. }
  104. #Interrogate user if they didn't provide info
  105. if (!$opts{apiurl}) {
  106. print "Type the API endpoint url for your testLink install below:\n";
  107. $opts{apiurl} = TestRail::Utils::userInput();
  108. }
  109. if (!$opts{user}) {
  110. print "Type your testLink user name below:\n";
  111. $opts{user} = TestRail::Utils::userInput();
  112. }
  113. if (!$opts{pass}) {
  114. print "Type the password for your testLink user below:\n";
  115. $opts{pass} = TestRail::Utils::userInput();
  116. }
  117. if (!$opts{apiurl} || !$opts{pass} || !$opts{user}) {
  118. print "ERROR: api url, username and password cannot be blank.\n";
  119. exit 1;
  120. }
  121. #Interrogate user if they didn't provide info
  122. if (!$opts{project}) {
  123. print "Type the name of the project you are testing under:\n";
  124. $opts{project} = TestRail::Utils::userInput();
  125. }
  126. # Interrogate user if options were not passed
  127. if (!$opts{run}) {
  128. print "Type the name of the existing run you would like to run against:\n";
  129. $opts{run} = TestRail::Utils::userInput();
  130. }
  131. if ($opts{mock}) {
  132. use Test::LWP::UserAgent::TestRailMock;
  133. $opts{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  134. $opts{debug} = 1;
  135. }
  136. my $tr = TestRail::API->new($opts{apiurl},$opts{user},$opts{pass},$opts{'debug'});
  137. $tr->{'browser'} = $opts{'browser'} if $opts{'browser'};
  138. $tr->{'debug'} = 0;
  139. my $project = $tr->getProjectByName($opts{'project'});
  140. if (!$project) {
  141. print "No such project '$opts{project}'.\n";
  142. exit 6;
  143. }
  144. my ($run,$plan);
  145. if ($opts{'plan'}) {
  146. $plan = $tr->getPlanByName($project->{'id'},$opts{'plan'});
  147. if (!$plan) {
  148. print "No such plan '$opts{plan}'!\n";
  149. exit 1;
  150. }
  151. $run = $tr->getChildRunByName($plan,$opts{'run'}, $opts{'configs'});
  152. } else {
  153. $run = $tr->getRunByName($project->{'id'},$opts{'run'});
  154. }
  155. if (!$run) {
  156. print "No such run '$opts{run}' matching the provided configs (if any).\n";
  157. exit 2;
  158. }
  159. my ($status_ids,$user_ids);
  160. #Process statuses
  161. if ($opts{'statuses'}) {
  162. eval { @$status_ids = $tr->statusNamesToIds(@{$opts{'statuses'}}); };
  163. if ($@) {
  164. print "$@\n";
  165. exit 4;
  166. }
  167. }
  168. #Process assignedto ids
  169. if ($opts{'users'}) {
  170. eval { @$user_ids = $tr->userNamesToIds(@{$opts{'users'}}); };
  171. if ($@) {
  172. print "$@\n";
  173. exit 5;
  174. }
  175. }
  176. my $cases = $tr->getTests($run->{'id'},$status_ids,$user_ids);
  177. if (!$cases) {
  178. print "No cases in TestRail!\n";
  179. exit 3;
  180. }
  181. my @tests = map {$_->{'title'}} @$cases;
  182. my @realtests;
  183. if ($opts{'match'} || $opts{'no-match'}) {
  184. my $dir = $opts{'match'} ? $opts{'match'} : $opts{'no-match'};
  185. if (!$opts{'no-recurse'}) {
  186. File::Find::find( sub { push(@realtests,$File::Find::name) if -f }, $dir );
  187. @tests = grep {my $real = $_; grep { basename($real) eq $_ } @tests} @realtests; #XXX if you have dups in your tree, be-ware
  188. } else {
  189. #Handle special windows case -- glob doesn't prepend abspath
  190. @realtests = glob("$dir/*");
  191. @tests = map {$^O eq 'MSWin32' ? "$dir/$_" : $_ } grep {my $fname = $_; grep { basename($_) eq $fname} @realtests } @tests;
  192. }
  193. @tests = grep {my $otest = basename($_); scalar(grep {basename($_) eq $otest} @tests) == 0} @realtests if $opts{'no-match'}; #invert the list in this case.
  194. }
  195. @tests = map { abs_path($_) } @tests if $opts{'match'};
  196. print join("\n",@tests)."\n" if scalar(@tests);
  197. exit 0;
  198. __END__
  199. L<TestRail::API>
  200. L<File::HomeDir> for the finding of .testrailrc
  201. =head1 SPECIAL THANKS
  202. Thanks to cPanel Inc, for graciously funding the creation of this module.