testrail-tests 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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, or a valid API key (TestRail 4.2 and above).
  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. The match and no-match options are mutually exclusive, and will terminate execution if both are passed.
  60. -n --no-recurse: if match passed, do not recurse subdirectories.
  61. [OPTIONAL PARAMETERS]
  62. -c --config [config]: configuration name to filter plans in run. Can be passed multiple times.
  63. -s --status [status]: only list tests marked as [status] in testrail. Can be passed multiple times.
  64. -a --assignedto [user]: only list tests assigned to user. Can be passed multiple times.
  65. [CONFIG OPTIONS]
  66. In your \$HOME, (or the current directory, if your system has no
  67. concept of a home directory) put a file called .testrailrc with
  68. key=value syntax separated by newlines.
  69. Valid Keys are: apiurl,user,password
  70. [CONFIG OVERRIDES]
  71. These override the config, if present.
  72. If neither are used, you will be prompted.
  73. --apiurl [url] : full URL to get to TestRail index document
  74. --password [key] : Your TestRail Password or a valid API key (TestRail 4.2 and above).
  75. --user [name] : Your TestRail User Name.
  76. TESTING OPTIONS:
  77. --mock: don't do any real HTTP requests.
  78. --help: show this output
  79. ");
  80. exit 0;
  81. }
  82. my %opts;
  83. GetOptions(
  84. 'apiurl=s' => \$opts{'apiurl'},
  85. 'password=s' => \$opts{'pass'},
  86. 'user=s' => \$opts{'user'},
  87. 'j|project=s' => \$opts{'project'},
  88. 'p|plan=s' => \$opts{'plan'},
  89. 'r|run=s' => \$opts{'run'},
  90. 'c|config=s@' => \$opts{'configs'},
  91. 's|status=s@' => \$opts{'statuses'},
  92. 'a|assignedto=s@' => \$opts{'users'},
  93. 'mock' => \$opts{'mock'},
  94. 'm|match=s' => \$opts{'match'},
  95. 'no-match=s' => \$opts{'no-match'},
  96. 'n|no-recurse' => \$opts{'no-recurse'},
  97. 'h|help' => \$opts{'help'}
  98. );
  99. if ($opts{help}) { help(); }
  100. if ($opts{'match'} && $opts{'no-match'}) {
  101. print "Error! match and no-match options are mutually exclusive.\n";
  102. exit 255;
  103. }
  104. #Parse config file if we are missing api url/key or user
  105. my $homedir = my_home() || '.';
  106. if (-e $homedir . '/.testrailrc' && (!$opts{apiurl} || !$opts{pass} || !$opts{user}) ) {
  107. ($opts{apiurl},$opts{pass},$opts{user}) = TestRail::Utils::parseConfig($homedir,1);
  108. }
  109. #Interrogate user if they didn't provide info
  110. if (!$opts{apiurl}) {
  111. print "Type the API endpoint url for your testLink install below:\n";
  112. $opts{apiurl} = TestRail::Utils::userInput();
  113. }
  114. if (!$opts{user}) {
  115. print "Type your testLink user name below:\n";
  116. $opts{user} = TestRail::Utils::userInput();
  117. }
  118. if (!$opts{pass}) {
  119. print "Type the password for your testLink user below:\n";
  120. $opts{pass} = TestRail::Utils::userInput();
  121. }
  122. if (!$opts{apiurl} || !$opts{pass} || !$opts{user}) {
  123. print "ERROR: api url, username and password cannot be blank.\n";
  124. exit 1;
  125. }
  126. #Interrogate user if they didn't provide info
  127. if (!$opts{project}) {
  128. print "Type the name of the project you are testing under:\n";
  129. $opts{project} = TestRail::Utils::userInput();
  130. }
  131. # Interrogate user if options were not passed
  132. if (!$opts{run}) {
  133. print "Type the name of the existing run you would like to run against:\n";
  134. $opts{run} = TestRail::Utils::userInput();
  135. }
  136. if ($opts{mock}) {
  137. use Test::LWP::UserAgent::TestRailMock;
  138. $opts{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  139. $opts{debug} = 1;
  140. }
  141. my $tr = TestRail::API->new($opts{apiurl},$opts{user},$opts{pass},$opts{'debug'});
  142. $tr->{'browser'} = $opts{'browser'} if $opts{'browser'};
  143. $tr->{'debug'} = 0;
  144. my $project = $tr->getProjectByName($opts{'project'});
  145. if (!$project) {
  146. print "No such project '$opts{project}'.\n";
  147. exit 6;
  148. }
  149. my ($run,$plan);
  150. if ($opts{'plan'}) {
  151. $plan = $tr->getPlanByName($project->{'id'},$opts{'plan'});
  152. if (!$plan) {
  153. print "No such plan '$opts{plan}'!\n";
  154. exit 1;
  155. }
  156. $run = $tr->getChildRunByName($plan,$opts{'run'}, $opts{'configs'});
  157. } else {
  158. $run = $tr->getRunByName($project->{'id'},$opts{'run'});
  159. }
  160. if (!$run) {
  161. print "No such run '$opts{run}' matching the provided configs (if any).\n";
  162. exit 2;
  163. }
  164. my ($status_ids,$user_ids);
  165. #Process statuses
  166. if ($opts{'statuses'}) {
  167. eval { @$status_ids = $tr->statusNamesToIds(@{$opts{'statuses'}}); };
  168. if ($@) {
  169. print "$@\n";
  170. exit 4;
  171. }
  172. }
  173. #Process assignedto ids
  174. if ($opts{'users'}) {
  175. eval { @$user_ids = $tr->userNamesToIds(@{$opts{'users'}}); };
  176. if ($@) {
  177. print "$@\n";
  178. exit 5;
  179. }
  180. }
  181. my $cases = $tr->getTests($run->{'id'},$status_ids,$user_ids);
  182. if (!$cases) {
  183. print "No cases in TestRail!\n";
  184. exit 3;
  185. }
  186. my @tests = map {$_->{'title'}} @$cases;
  187. my @realtests;
  188. if ($opts{'match'} || $opts{'no-match'}) {
  189. my $dir = $opts{'match'} ? $opts{'match'} : $opts{'no-match'};
  190. if (!$opts{'no-recurse'}) {
  191. File::Find::find( sub { push(@realtests,$File::Find::name) if -f }, $dir );
  192. @tests = grep {my $real = $_; grep { basename($real) eq $_ } @tests} @realtests; #XXX if you have dups in your tree, be-ware
  193. } else {
  194. #Handle special windows case -- glob doesn't prepend abspath
  195. @realtests = glob("$dir/*");
  196. @tests = map {$^O eq 'MSWin32' ? "$dir/$_" : $_ } grep {my $fname = $_; grep { basename($_) eq $fname} @realtests } @tests;
  197. }
  198. @tests = grep {my $otest = basename($_); scalar(grep {basename($_) eq $otest} @tests) == 0} @realtests if $opts{'no-match'}; #invert the list in this case.
  199. }
  200. @tests = map { abs_path($_) } @tests if $opts{'match'};
  201. print join("\n",@tests)."\n" if scalar(@tests);
  202. exit 0;
  203. __END__
  204. L<TestRail::API>
  205. L<File::HomeDir> for the finding of .testrailrc
  206. =head1 SPECIAL THANKS
  207. Thanks to cPanel Inc, for graciously funding the creation of this module.