testrail-tests 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. =head1 PARAMETERS:
  9. =head2 MANDATORY PARAMETERS
  10. =over 4
  11. --apiurl : full URL to get to TestRail index document
  12. --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  13. --user : Your TestRail User Name.
  14. -j --project : desired project name.
  15. -r --run : desired run name.
  16. =back
  17. All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
  18. =head2 SEMI-OPTIONAL PARAMETERS
  19. =over 4
  20. -p --plan : desired plan name. Required if the run passed is a child of a plan.
  21. -m --match : attempt to find filenames matching the test names in the provided dir.
  22. --no-match : attempt to find filenames that do not match test names in the provided dir.
  23. -n --no-recurse : if match (or no-match) passed, do not recurse subdirectories.
  24. -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
  25. =back
  26. =head2 OPTIONAL PARAMETERS
  27. =over 4
  28. -c --config : configuration name to filter plans in run. Can be passed multiple times.
  29. -s --status : only list tests marked as [status] in testrail. Can be passed multiple times.
  30. -a --assignedto : only list tests assigned to user. Can be passed multiple times.
  31. =back
  32. =head1 CONFIGURATION FILE
  33. 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.
  34. Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
  35. All options specified thereby are overridden by passing the command-line switches above.
  36. =head1 MISCELLANEOUS OPTIONS:
  37. =over 4
  38. --mock : don't do any real HTTP requests. Used only by tests.
  39. --help : show this output
  40. =back
  41. =cut
  42. use strict;
  43. use warnings;
  44. use utf8;
  45. use TestRail::API;
  46. use TestRail::Utils;
  47. use Getopt::Long;
  48. use File::HomeDir qw{my_home};
  49. use File::Find;
  50. use Cwd qw{abs_path};
  51. use File::Basename qw{basename};
  52. my $opts ={};
  53. #Parse config file if we are missing api url/key or user
  54. my $homedir = my_home() || '.';
  55. if (-e $homedir . '/.testrailrc') {
  56. $opts = TestRail::Utils::parseConfig($homedir);
  57. }
  58. GetOptions(
  59. 'apiurl=s' => \$opts->{'apiurl'},
  60. 'password=s' => \$opts->{'password'},
  61. 'user=s' => \$opts->{'user'},
  62. 'j|project=s' => \$opts->{'project'},
  63. 'p|plan=s' => \$opts->{'plan'},
  64. 'r|run=s' => \$opts->{'run'},
  65. 'c|config=s@' => \$opts->{'configs'},
  66. 's|status=s@' => \$opts->{'statuses'},
  67. 'a|assignedto=s@' => \$opts->{'users'},
  68. 'mock' => \$opts->{'mock'},
  69. 'm|match=s' => \$opts->{'match'},
  70. 'no-match=s' => \$opts->{'no-match'},
  71. 'n|no-recurse' => \$opts->{'no-recurse'},
  72. 'e|encoding=s' => \$opts->{'encoding'},
  73. 'h|help' => \$opts->{'help'}
  74. );
  75. if ($opts->{help}) { TestRail::Utils::help(); }
  76. if ($opts->{'match'} && $opts->{'no-match'}) {
  77. print "Error! match and no-match options are mutually exclusive.\n";
  78. exit 255;
  79. }
  80. TestRail::Utils::interrogateUser($opts,qw{apiurl user password project run});
  81. if ($opts->{mock}) {
  82. use Test::LWP::UserAgent::TestRailMock;
  83. $opts->{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  84. $opts->{debug} = 1;
  85. }
  86. my $tr = TestRail::API->new($opts->{apiurl},$opts->{user},$opts->{password},$opts->{'encoding'},$opts->{'debug'});
  87. $tr->{'browser'} = $opts->{'browser'} if $opts->{'browser'};
  88. $tr->{'debug'} = 0;
  89. my $project = $tr->getProjectByName($opts->{'project'});
  90. if (!$project) {
  91. print "No such project '$opts->{project}'.\n";
  92. exit 6;
  93. }
  94. my ($run,$plan);
  95. if ($opts->{'plan'}) {
  96. $plan = $tr->getPlanByName($project->{'id'},$opts->{'plan'});
  97. if (!$plan) {
  98. print "No such plan '$opts->{plan}'!\n";
  99. exit 1;
  100. }
  101. $run = $tr->getChildRunByName($plan,$opts->{'run'}, $opts->{'configs'});
  102. } else {
  103. $run = $tr->getRunByName($project->{'id'},$opts->{'run'});
  104. }
  105. if (!$run) {
  106. print "No such run '$opts->{run}' matching the provided configs (if any).\n";
  107. exit 2;
  108. }
  109. my ($status_ids,$user_ids);
  110. #Process statuses
  111. if ($opts->{'statuses'}) {
  112. eval { @$status_ids = $tr->statusNamesToIds(@{$opts->{'statuses'}}); };
  113. if ($@) {
  114. print "$@\n";
  115. exit 4;
  116. }
  117. }
  118. #Process assignedto ids
  119. if ($opts->{'users'}) {
  120. eval { @$user_ids = $tr->userNamesToIds(@{$opts->{'users'}}); };
  121. if ($@) {
  122. print "$@\n";
  123. exit 5;
  124. }
  125. }
  126. my $cases = $tr->getTests($run->{'id'},$status_ids,$user_ids);
  127. if (!$cases) {
  128. print "No cases in TestRail!\n";
  129. exit 3;
  130. }
  131. my @tests = map {$_->{'title'}} @$cases;
  132. my @realtests;
  133. if ($opts->{'match'} || $opts->{'no-match'}) {
  134. my $dir = $opts->{'match'} ? $opts->{'match'} : $opts->{'no-match'};
  135. if (!$opts->{'no-recurse'}) {
  136. File::Find::find( sub { push(@realtests,$File::Find::name) if -f }, $dir );
  137. @tests = grep {my $real = $_; grep { basename($real) eq $_ } @tests} @realtests; #XXX if you have dups in your tree, be-ware
  138. } else {
  139. #Handle special windows case -- glob doesn't prepend abspath
  140. @realtests = glob("$dir/*");
  141. @tests = map {$^O eq 'MSWin32' ? "$dir/$_" : $_ } grep {my $fname = $_; grep { basename($_) eq $fname} @realtests } @tests;
  142. }
  143. @tests = grep {my $otest = basename($_); scalar(grep {basename($_) eq $otest} @tests) == 0} @realtests if $opts->{'no-match'}; #invert the list in this case.
  144. }
  145. @tests = map { abs_path($_) } @tests if $opts->{'match'};
  146. print join("\n",@tests)."\n" if scalar(@tests);
  147. exit 0;
  148. __END__
  149. L<TestRail::API>
  150. L<File::HomeDir> for the finding of .testrailrc
  151. =head1 SPECIAL THANKS
  152. Thanks to cPanel Inc, for graciously funding the creation of this distribution.