testrail-tests 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. use Pod::Perldoc 3.10;
  49. sub help {
  50. @ARGV = ($0);
  51. Pod::Perldoc->run();
  52. exit 0;
  53. }
  54. my %opts;
  55. GetOptions(
  56. 'apiurl=s' => \$opts{'apiurl'},
  57. 'password=s' => \$opts{'pass'},
  58. 'user=s' => \$opts{'user'},
  59. 'j|project=s' => \$opts{'project'},
  60. 'p|plan=s' => \$opts{'plan'},
  61. 'r|run=s' => \$opts{'run'},
  62. 'c|config=s@' => \$opts{'configs'},
  63. 's|status=s@' => \$opts{'statuses'},
  64. 'a|assignedto=s@' => \$opts{'users'},
  65. 'mock' => \$opts{'mock'},
  66. 'm|match=s' => \$opts{'match'},
  67. 'no-match=s' => \$opts{'no-match'},
  68. 'n|no-recurse' => \$opts{'no-recurse'},
  69. 'e|encoding=s' => \$opts{'encoding'},
  70. 'h|help' => \$opts{'help'}
  71. );
  72. if ($opts{help}) { help(); }
  73. if ($opts{'match'} && $opts{'no-match'}) {
  74. print "Error! match and no-match options are mutually exclusive.\n";
  75. exit 255;
  76. }
  77. #Parse config file if we are missing api url/key or user
  78. my $homedir = my_home() || '.';
  79. if (-e $homedir . '/.testrailrc' && (!$opts{apiurl} || !$opts{pass} || !$opts{user}) ) {
  80. ($opts{apiurl},$opts{pass},$opts{user}) = TestRail::Utils::parseConfig($homedir,1);
  81. }
  82. TestRail::Utils::interrogateUser(\%opts,qw{apiurl user pass project run});
  83. if ($opts{mock}) {
  84. use Test::LWP::UserAgent::TestRailMock;
  85. $opts{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  86. $opts{debug} = 1;
  87. }
  88. my $tr = TestRail::API->new($opts{apiurl},$opts{user},$opts{pass},$opts{'encoding'},$opts{'debug'});
  89. $tr->{'browser'} = $opts{'browser'} if $opts{'browser'};
  90. $tr->{'debug'} = 0;
  91. my $project = $tr->getProjectByName($opts{'project'});
  92. if (!$project) {
  93. print "No such project '$opts{project}'.\n";
  94. exit 6;
  95. }
  96. my ($run,$plan);
  97. if ($opts{'plan'}) {
  98. $plan = $tr->getPlanByName($project->{'id'},$opts{'plan'});
  99. if (!$plan) {
  100. print "No such plan '$opts{plan}'!\n";
  101. exit 1;
  102. }
  103. $run = $tr->getChildRunByName($plan,$opts{'run'}, $opts{'configs'});
  104. } else {
  105. $run = $tr->getRunByName($project->{'id'},$opts{'run'});
  106. }
  107. if (!$run) {
  108. print "No such run '$opts{run}' matching the provided configs (if any).\n";
  109. exit 2;
  110. }
  111. my ($status_ids,$user_ids);
  112. #Process statuses
  113. if ($opts{'statuses'}) {
  114. eval { @$status_ids = $tr->statusNamesToIds(@{$opts{'statuses'}}); };
  115. if ($@) {
  116. print "$@\n";
  117. exit 4;
  118. }
  119. }
  120. #Process assignedto ids
  121. if ($opts{'users'}) {
  122. eval { @$user_ids = $tr->userNamesToIds(@{$opts{'users'}}); };
  123. if ($@) {
  124. print "$@\n";
  125. exit 5;
  126. }
  127. }
  128. my $cases = $tr->getTests($run->{'id'},$status_ids,$user_ids);
  129. if (!$cases) {
  130. print "No cases in TestRail!\n";
  131. exit 3;
  132. }
  133. my @tests = map {$_->{'title'}} @$cases;
  134. my @realtests;
  135. if ($opts{'match'} || $opts{'no-match'}) {
  136. my $dir = $opts{'match'} ? $opts{'match'} : $opts{'no-match'};
  137. if (!$opts{'no-recurse'}) {
  138. File::Find::find( sub { push(@realtests,$File::Find::name) if -f }, $dir );
  139. @tests = grep {my $real = $_; grep { basename($real) eq $_ } @tests} @realtests; #XXX if you have dups in your tree, be-ware
  140. } else {
  141. #Handle special windows case -- glob doesn't prepend abspath
  142. @realtests = glob("$dir/*");
  143. @tests = map {$^O eq 'MSWin32' ? "$dir/$_" : $_ } grep {my $fname = $_; grep { basename($_) eq $fname} @realtests } @tests;
  144. }
  145. @tests = grep {my $otest = basename($_); scalar(grep {basename($_) eq $otest} @tests) == 0} @realtests if $opts{'no-match'}; #invert the list in this case.
  146. }
  147. @tests = map { abs_path($_) } @tests if $opts{'match'};
  148. print join("\n",@tests)."\n" if scalar(@tests);
  149. exit 0;
  150. __END__
  151. L<TestRail::API>
  152. L<File::HomeDir> for the finding of .testrailrc
  153. =head1 SPECIAL THANKS
  154. Thanks to cPanel Inc, for graciously funding the creation of this distribution.