Find.pm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # PODNAME: TestRail::Utils::Find
  2. # ABSTRACT: Find runs and tests according to user specifications.
  3. package TestRail::Utils::Find;
  4. use strict;
  5. use warnings;
  6. use Carp qw{confess cluck};
  7. use File::Find;
  8. use Cwd qw{abs_path};
  9. use File::Basename qw{basename};
  10. use TestRail::Utils;
  11. =head1 DESCRIPTION
  12. =head1 FUNCTIONS
  13. =head2 findRuns
  14. Find runs based on the options HASHREF provided.
  15. See the documentation for L<testrail-runs>, as the long argument names there correspond to hash keys.
  16. The primary routine of testrail-runs.
  17. =over 4
  18. =item HASHREF C<OPTIONS> - flags acceptable by testrail-tests
  19. =item TestRail::API C<HANDLE> - TestRail::API object
  20. =back
  21. Returns ARRAYREF of run definition HASHREFs.
  22. =cut
  23. sub findRuns {
  24. my ($opts,$tr) = @_;
  25. my ($status_ids);
  26. #Process statuses
  27. if ($opts->{'statuses'}) {
  28. @$status_ids = $tr->statusNamesToIds(@{$opts->{'statuses'}});
  29. }
  30. my $project = $tr->getProjectByName($opts->{'project'});
  31. confess("No such project '$opts->{project}'.\n") if !$project;
  32. my $pconfigs = [];
  33. $pconfigs = $tr->translateConfigNamesToIds($project->{'id'},$opts->{configs}) if $opts->{'configs'};
  34. my ($runs,$plans,$planRuns,$cruns,$found) = ([],[],[],[],0);
  35. $runs = $tr->getRuns($project->{'id'}) if (!$opts->{'configs'}); # If configs are passed, global runs are not in consideration.
  36. $plans = $tr->getPlans($project->{'id'});
  37. @$plans = map {$tr->getPlanByID($_->{'id'})} @$plans;
  38. foreach my $plan (@$plans) {
  39. $cruns = $tr->getChildRuns($plan);
  40. next if !$cruns;
  41. foreach my $run (@$cruns) {
  42. next if scalar(@$pconfigs) != scalar(@{$run->{'config_ids'}});
  43. #Compare run config IDs against desired, invalidate run if all conditions not satisfied
  44. $found = 0;
  45. foreach my $cid (@{$run->{'config_ids'}}) {
  46. $found++ if grep {$_ == $cid} @$pconfigs;
  47. }
  48. $run->{'created_on'} = $plan->{'created_on'};
  49. $run->{'milestone_id'} = $plan->{'milestone_id'};
  50. push(@$planRuns, $run) if $found == scalar(@{$run->{'config_ids'}});
  51. }
  52. }
  53. push(@$runs,@$planRuns);
  54. if ($opts->{'statuses'}) {
  55. @$runs = $tr->getRunSummary(@$runs);
  56. @$runs = grep { defined($_->{'run_status'}) } @$runs; #Filter stuff with no results
  57. foreach my $status (@{$opts->{'statuses'}}) {
  58. @$runs = grep { $_->{'run_status'}->{$status} } @$runs; #If it's positive, keep it. Otherwise forget it.
  59. }
  60. }
  61. #Sort FIFO/LIFO by milestone or creation date of run
  62. my $sortkey = 'created_on';
  63. if ($opts->{'milesort'}) {
  64. @$runs = map {
  65. my $run = $_;
  66. $run->{'milestone'} = $tr->getMilestoneByID($run->{'milestone_id'}) if $run->{'milestone_id'};
  67. my $milestone = $run->{'milestone'} ? $run->{'milestone'}->{'due_on'} : 0;
  68. $run->{'due_on'} = $milestone;
  69. $run
  70. } @$runs;
  71. $sortkey = 'due_on';
  72. }
  73. if ($opts->{'lifo'}) {
  74. @$runs = sort { $b->{$sortkey} <=> $a->{$sortkey} } @$runs;
  75. } else {
  76. @$runs = sort { $a->{$sortkey} <=> $b->{$sortkey} } @$runs;
  77. }
  78. return $runs;
  79. }
  80. =head2 getTests(opts,testrail)
  81. Get the tests specified by the options passed.
  82. =over 4
  83. =item HASHREF C<OPTS> - Options for getting the tests
  84. =over 4
  85. =item STRING C<PROJECT> - name of Project to look for tests in
  86. =item STRING C<RUN> - name of Run to get tests from
  87. =item STRING C<PLAN> (optional) - name of Plan to get run from
  88. =item ARRAYREF[STRING] C<CONFIGS> (optional) - names of configs run must satisfy, if part of a plan
  89. =item ARRAYREF[STRING] C<USERS> (optional) - names of users to filter cases by assignee
  90. =item ARRAYREF[STRING] C<STATUSES> (optional) - names of statuses to filter cases by
  91. =back
  92. =back
  93. Returns ARRAYREF of tests.
  94. =cut
  95. sub getTests {
  96. my ($opts,$tr) = @_;
  97. my ($project,$plan,$run) = TestRail::Utils::getRunInformation($tr,$opts);
  98. my ($status_ids,$user_ids);
  99. #Process statuses
  100. @$status_ids = $tr->statusNamesToIds(@{$opts->{'statuses'}}) if $opts->{'statuses'};
  101. #Process assignedto ids
  102. @$user_ids = $tr->userNamesToIds(@{$opts->{'users'}}) if $opts->{'users'};
  103. my $cases = $tr->getTests($run->{'id'},$status_ids,$user_ids);
  104. return $cases;
  105. }
  106. =head2 findTests(opts,case1,...,caseN)
  107. Given an ARRAY of tests, find tests meeting your criteria (or not) in the specified directory.
  108. =over 4
  109. =item HASHREF C<OPTS> - Options for finding tests:
  110. =over 4
  111. =item STRING C<MATCH> - Only return tests which exist in the path provided. Mutually exclusive with no-match.
  112. =item STRING C<NO-MATCH> - Only return tests which aren't in the path provided (orphan tests). Mutually exclusive with match.
  113. =item BOOL C<NO-RECURSE> - Do not do a recursive scan for files.
  114. =item BOOL C<NAMES-ONLY> - Only return the names of the tests rather than the entire test objects.
  115. =back
  116. =item ARRAY C<CASES> - Array of cases to translate to pathnames based on above options.
  117. =back
  118. Returns tests found that meet the criteria laid out in the options.
  119. Provides absolute path to tests if match is passed; this is the 'full_title' key if names-only is false/undef.
  120. Dies if mutually exclusive options are passed.
  121. =cut
  122. sub findTests {
  123. my ($opts,@cases) = @_;
  124. confess "Error! match and no-match options are mutually exclusive.\n" if ($opts->{'match'} && $opts->{'no-match'});
  125. my @tests = @cases;
  126. my (@realtests);
  127. if ($opts->{'match'} || $opts->{'no-match'}) {
  128. my $dir = $opts->{'match'} ? $opts->{'match'} : $opts->{'no-match'};
  129. if (!$opts->{'no-recurse'}) {
  130. File::Find::find( sub { push(@realtests,$File::Find::name) if -f }, $dir );
  131. @tests = grep {my $real = $_->{'title'}; grep { $real eq basename($_) } @realtests} @cases; #XXX if you have dups in your tree, be-ware
  132. } else {
  133. #Handle special windows case -- glob doesn't prepend abspath
  134. @realtests = glob("$dir/*");
  135. @tests = map {
  136. $_->{'title'} = "$dir/".$_->{'title'} if( $^O eq 'MSWin32' );
  137. $_
  138. } grep {my $fname = $_->{'title'}; grep { basename($_) eq $fname} @realtests } @cases;
  139. }
  140. @tests = map {{'title' => $_}} grep {my $otest = basename($_); scalar(grep {basename($_->{'title'}) eq $otest} @tests) == 0} @realtests if $opts->{'no-match'}; #invert the list in this case.
  141. }
  142. @tests = map { abs_path($opts->{'match'}.'/'.$_->{'title'}) } @tests if $opts->{'match'} && $opts->{'names-only'};
  143. @tests = map { $_->{'full_title'} = abs_path($opts->{'match'}.'/'.$_->{'title'}); $_ } @tests if $opts->{'match'} && !$opts->{'names-only'};
  144. @tests = map { $_->{'title'} } @tests if !$opts->{'match'} && $opts->{'names-only'};
  145. return @tests;
  146. }
  147. 1;
  148. __END__
  149. =head1 SPECIAL THANKS
  150. Thanks to cPanel Inc, for graciously funding the creation of this module.