testrail-tests 8.5 KB

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