Find.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 Scalar::Util qw{blessed};
  8. use List::MoreUtils qw{uniq};
  9. use File::Find;
  10. use Cwd qw{abs_path};
  11. use File::Basename qw{basename};
  12. use TestRail::Utils;
  13. =head1 DESCRIPTION
  14. =head1 FUNCTIONS
  15. =head2 findRuns
  16. Find runs based on the options HASHREF provided.
  17. See the documentation for L<testrail-runs>, as the long argument names there correspond to hash keys.
  18. The primary routine of testrail-runs.
  19. =over 4
  20. =item HASHREF C<OPTIONS> - flags acceptable by testrail-tests
  21. =item TestRail::API C<HANDLE> - TestRail::API object
  22. =back
  23. Returns ARRAYREF of run definition HASHREFs.
  24. =cut
  25. sub findRuns {
  26. my ($opts,$tr) = @_;
  27. confess("TestRail handle must be provided as argument 2") unless blessed($tr) eq 'TestRail::API';
  28. my ($status_labels);
  29. #Process statuses
  30. if ($opts->{'statuses'}) {
  31. @$status_labels = $tr->statusNamesToLabels(@{$opts->{'statuses'}});
  32. }
  33. my $project = $tr->getProjectByName($opts->{'project'});
  34. confess("No such project '$opts->{project}'.\n") if !$project;
  35. my $pconfigs = [];
  36. @$pconfigs = $tr->translateConfigNamesToIds($project->{'id'},@{$opts->{configs}}) if $opts->{'configs'};
  37. my ($runs,$plans,$planRuns,$cruns,$found) = ([],[],[],[],0);
  38. $runs = $tr->getRuns($project->{'id'}) if (!$opts->{'configs'}); # If configs are passed, global runs are not in consideration.
  39. $plans = $tr->getPlans($project->{'id'});
  40. @$plans = map {$tr->getPlanByID($_->{'id'})} @$plans;
  41. foreach my $plan (@$plans) {
  42. $cruns = $tr->getChildRuns($plan);
  43. next if !$cruns;
  44. foreach my $run (@$cruns) {
  45. next if scalar(@$pconfigs) != scalar(@{$run->{'config_ids'}});
  46. #Compare run config IDs against desired, invalidate run if all conditions not satisfied
  47. $found = 0;
  48. foreach my $cid (@{$run->{'config_ids'}}) {
  49. $found++ if grep {$_ == $cid} @$pconfigs;
  50. }
  51. $run->{'created_on'} = $plan->{'created_on'};
  52. $run->{'milestone_id'} = $plan->{'milestone_id'};
  53. push(@$planRuns, $run) if $found == scalar(@{$run->{'config_ids'}});
  54. }
  55. }
  56. push(@$runs,@$planRuns);
  57. if ($opts->{'statuses'}) {
  58. @$runs = $tr->getRunSummary(@$runs);
  59. @$runs = grep { defined($_->{'run_status'}) } @$runs; #Filter stuff with no results
  60. foreach my $status (@$status_labels) {
  61. @$runs = grep { $_->{'run_status'}->{$status} } @$runs; #If it's positive, keep it. Otherwise forget it.
  62. }
  63. }
  64. #Sort FIFO/LIFO by milestone or creation date of run
  65. my $sortkey = 'created_on';
  66. if ($opts->{'milesort'}) {
  67. @$runs = map {
  68. my $run = $_;
  69. $run->{'milestone'} = $tr->getMilestoneByID($run->{'milestone_id'}) if $run->{'milestone_id'};
  70. my $milestone = $run->{'milestone'} ? $run->{'milestone'}->{'due_on'} : 0;
  71. $run->{'due_on'} = $milestone;
  72. $run
  73. } @$runs;
  74. $sortkey = 'due_on';
  75. }
  76. #Suppress 'no such option' warnings
  77. @$runs = map { $_->{$sortkey} //= ''; $_ } @$runs;
  78. if ($opts->{'lifo'}) {
  79. @$runs = sort { $b->{$sortkey} cmp $a->{$sortkey} } @$runs;
  80. } else {
  81. @$runs = sort { $a->{$sortkey} cmp $b->{$sortkey} } @$runs;
  82. }
  83. return $runs;
  84. }
  85. =head2 getTests(opts,testrail)
  86. Get the tests specified by the options passed.
  87. =over 4
  88. =item HASHREF C<OPTS> - Options for getting the tests
  89. =over 4
  90. =item STRING C<PROJECT> - name of Project to look for tests in
  91. =item STRING C<RUN> - name of Run to get tests from
  92. =item STRING C<PLAN> (optional) - name of Plan to get run from
  93. =item ARRAYREF[STRING] C<CONFIGS> (optional) - names of configs run must satisfy, if part of a plan
  94. =item ARRAYREF[STRING] C<USERS> (optional) - names of users to filter cases by assignee
  95. =item ARRAYREF[STRING] C<STATUSES> (optional) - names of statuses to filter cases by
  96. =back
  97. =back
  98. Returns ARRAYREF of tests, and the run in which they belong.
  99. =cut
  100. sub getTests {
  101. my ($opts,$tr) = @_;
  102. confess("TestRail handle must be provided as argument 2") unless blessed($tr) eq 'TestRail::API';
  103. my (undef,undef,$run) = TestRail::Utils::getRunInformation($tr,$opts);
  104. my ($status_ids,$user_ids);
  105. #Process statuses
  106. @$status_ids = $tr->statusNamesToIds(@{$opts->{'statuses'}}) if $opts->{'statuses'};
  107. #Process assignedto ids
  108. @$user_ids = $tr->userNamesToIds(@{$opts->{'users'}}) if $opts->{'users'};
  109. my $cases = $tr->getTests($run->{'id'},$status_ids,$user_ids);
  110. return ($cases,$run);
  111. }
  112. =head2 findTests(opts,case1,...,caseN)
  113. Given an ARRAY of tests, find tests meeting your criteria (or not) in the specified directory.
  114. =over 4
  115. =item HASHREF C<OPTS> - Options for finding tests:
  116. =over 4
  117. =item STRING C<MATCH> - Only return tests which exist in the path provided, and in TestRail. Mutually exclusive with no-match, orphans.
  118. =item STRING C<NO-MATCH> - Only return tests which are in the path provided, but not in TestRail. Mutually exclusive with match, orphans.
  119. =item STRING C<ORPHANS> - Only return tests which are in TestRail, and not in the path provided. Mutually exclusive with match, no-match
  120. =item BOOL C<NO-RECURSE> - Do not do a recursive scan for files.
  121. =item BOOL C<NAMES-ONLY> - Only return the names of the tests rather than the entire test objects.
  122. =item STRING C<EXTENSION> (optional) - Only return files ending with the provided text (e.g. .t, .test, .pl, .pm)
  123. =item CODE C<FINDER> (optional) - Use the provided sub to get the list of files on disk. Provides the directory & extension based on above options as arguments. Must return list of tests.
  124. =back
  125. =item ARRAY C<CASES> - Array of cases to translate to pathnames based on above options.
  126. =back
  127. Returns tests found that meet the criteria laid out in the options.
  128. Provides absolute path to tests if match is passed; this is the 'full_title' key if names-only is false/undef.
  129. Dies if mutually exclusive options are passed.
  130. =cut
  131. sub findTests {
  132. my ($opts,@cases) = @_;
  133. confess "Error! match and no-match options are mutually exclusive.\n" if ($opts->{'match'} && $opts->{'no-match'});
  134. confess "Error! match and orphans options are mutually exclusive.\n" if ($opts->{'match'} && $opts->{'orphans'});
  135. confess "Error! no-match and orphans options are mutually exclusive.\n" if ($opts->{'orphans'} && $opts->{'no-match'});
  136. my @tests = @cases;
  137. my (@realtests);
  138. my $ext = $opts->{'extension'} // '';
  139. if ($opts->{'match'} || $opts->{'no-match'} || $opts->{'orphans'}) {
  140. my @tmpArr = ();
  141. my $dir = ($opts->{'match'} || $opts->{'orphans'}) ? ($opts->{'match'} || $opts->{'orphans'}) : $opts->{'no-match'};
  142. confess "No such directory '$dir'" if ! -d $dir;
  143. if (ref($opts->{finder}) eq 'CODE') {
  144. @realtests = $opts->{finder}->($dir,$ext)
  145. } else {
  146. if (!$opts->{'no-recurse'}) {
  147. File::Find::find( sub { push(@realtests,$File::Find::name) if -f && m/\Q$ext\E$/ }, $dir );
  148. } else {
  149. @realtests = glob("$dir/*$ext");
  150. }
  151. }
  152. foreach my $case (@cases) {
  153. foreach my $path (@realtests) {
  154. next unless $case->{'title'} eq basename($path);
  155. $case->{'path'} = $path;
  156. push(@tmpArr, $case);
  157. last;
  158. }
  159. }
  160. @tmpArr = grep {my $otest = $_; !(grep {$otest->{'title'} eq $_->{'title'}} @tmpArr) } @tests if $opts->{'orphans'};
  161. @tests = @tmpArr;
  162. @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.
  163. }
  164. @tests = map { abs_path($_->{'path'}) } @tests if $opts->{'match'} && $opts->{'names-only'};
  165. @tests = map { $_->{'full_title'} = abs_path($_->{'path'}); $_ } @tests if $opts->{'match'} && !$opts->{'names-only'};
  166. @tests = map { $_->{'title'} } @tests if !$opts->{'match'} && $opts->{'names-only'};
  167. return @tests;
  168. }
  169. =head2 getCases
  170. Get cases in a testsuite matching your parameters passed
  171. =cut
  172. sub getCases {
  173. my ($opts,$tr) = @_;
  174. confess("First argument must be instance of TestRail::API") unless blessed($tr) eq 'TestRail::API';
  175. my $project = $tr->getProjectByName($opts->{'project'});
  176. confess "No such project '$opts->{project}'.\n" if !$project;
  177. my $suite = $tr->getTestSuiteByName($project->{'id'},$opts->{'testsuite'});
  178. confess "No such testsuite '$opts->{testsuite}'.\n" if !$suite;
  179. $opts->{'testsuite_id'} = $suite->{'id'};
  180. my $section;
  181. $section = $tr->getSectionByName($project->{'id'},$suite->{'id'},$opts->{'section'}) if $opts->{'section'};
  182. confess "No such section '$opts->{section}.\n" if $opts->{'section'} && !$section;
  183. my $section_id;
  184. $section_id = $section->{'id'} if ref $section eq "HASH";
  185. my $type_ids;
  186. @$type_ids = $tr->typeNamesToIds(@{$opts->{'types'}}) if ref $opts->{'types'} eq 'ARRAY';
  187. #Above will confess if anything's the matter
  188. #TODO Translate opts into filters
  189. my $filters = {
  190. 'section_id' => $section_id,
  191. 'type_id' => $type_ids
  192. };
  193. return $tr->getCases($project->{'id'},$suite->{'id'},$filters);
  194. }
  195. =head2 findCases(opts,@cases)
  196. Find orphan, missing and needing-update cases.
  197. They are returned as the hash keys 'orphans', 'missing', and 'updates' respectively.
  198. The testsuite_id is also returned in the output hashref.
  199. Option hash keys for input are 'no-missing', 'orphans', and 'update'.
  200. Returns HASHREF.
  201. =cut
  202. sub findCases {
  203. my ($opts,@cases) = @_;
  204. confess('testsuite_id parameter mandatory in options HASHREF') unless defined $opts->{'testsuite_id'};
  205. confess('Directory parameter mandatory in options HASHREF.') unless defined $opts->{'directory'};
  206. confess('No such directory "'.$opts->{'directory'}."\"\n") unless -d $opts->{'directory'};
  207. my $ret = {'testsuite_id' => $opts->{'testsuite_id'}};
  208. if (!$opts->{'no-missing'}) {
  209. my $mopts = {
  210. 'no-match' => $opts->{'directory'},
  211. 'names-only' => 1,
  212. 'extension' => $opts->{'extension'}
  213. };
  214. my @missing = findTests($mopts,@cases);
  215. $ret->{'missing'} = \@missing;
  216. }
  217. if ($opts->{'orphans'}) {
  218. my $oopts = {
  219. 'orphans' => $opts->{'directory'},
  220. 'extension' => $opts->{'extension'}
  221. };
  222. my @orphans = findTests($oopts,@cases);
  223. $ret->{'orphans'} = \@orphans;
  224. }
  225. if ($opts->{'update'}) {
  226. my $uopts = {
  227. 'match' => $opts->{'directory'},
  228. 'extension' => $opts->{'extension'}
  229. };
  230. my @updates = findTests($uopts,@cases);
  231. $ret->{'update'} = \@updates;
  232. }
  233. return $ret;
  234. }
  235. =head2 getResults(options, $prior_runs, @cases)
  236. Get results for tests by name, filtered by the provided options, and skipping any runs found in the provided ARRAYREF of run IDs.
  237. Probably should have called this findResults, but we all prefer to get results right?
  238. Returns ARRAYREF of results, and an ARRAYREF of seen plan IDs
  239. =cut
  240. sub getResults {
  241. my ($tr,$opts,$prior_runs,@cases) = @_;
  242. my $res = {};
  243. my $projects = $tr->getProjects();
  244. my $prior_plans = [];
  245. #TODO obey status filtering
  246. #TODO obey result notes text grepping
  247. foreach my $project (@$projects) {
  248. next if $opts->{projects} && !( grep { $_ eq $project->{'name'} } @{$opts->{'projects'}} );
  249. my $runs = $tr->getRuns($project->{'id'});
  250. #Translate plan names to ids
  251. my $plans = $tr->getPlans($project->{'id'}) || [];
  252. #Filter out plans which do not match our filters to prevent a call to getPlanByID
  253. if ($opts->{'plans'}) {
  254. @$plans = grep { my $p = $_; grep { $p->{'name'} eq $_} @{$opts->{'plans'}} } @$plans;
  255. }
  256. #Filter out prior plans
  257. if ($opts->{'plan_ids'}) {
  258. @$plans = grep { my $p = $_; grep { $p->{'id'} eq $_} @{$opts->{'plan_ids'}} } @$plans;
  259. }
  260. $opts->{'runs'} //= [];
  261. foreach my $plan (@$plans) {
  262. $plan = $tr->getPlanByID($plan->{'id'});
  263. my $plan_runs = $tr->getChildRuns($plan);
  264. push(@$runs,@$plan_runs) if $plan_runs;
  265. }
  266. foreach my $run (@$runs) {
  267. next if scalar(@{$opts->{runs}}) && !( grep { $_ eq $run->{'name'} } @{$opts->{'runs'}} );
  268. next if grep { $run->{id} eq $_ } @$prior_runs;
  269. foreach my $case (@cases) {
  270. my $c = $tr->getTestByName($run->{'id'},basename($case));
  271. next unless ref $c eq 'HASH';
  272. $res->{$case} //= [];
  273. $c->{results} = $tr->getTestResults($c->{'id'},$tr->{'global_limit'},0);
  274. #Filter by provided pattern, if any
  275. if ($opts->{'pattern'}) {
  276. my $pattern = $opts->{pattern};
  277. @{$c->{results}} = grep { my $comment = $_->{comment} || ''; $comment =~ m/$pattern/i } @{$c->{results}};
  278. }
  279. push(@{$res->{$case}}, $c) if scalar(@{$c->{results}}); #Make sure they weren't filtered out
  280. }
  281. }
  282. push(@$prior_plans, map {$_->{'id'}} @$plans);
  283. }
  284. @$prior_plans = uniq(@$prior_plans);
  285. return ($res,$prior_plans);
  286. }
  287. 1;
  288. __END__
  289. =head1 SPECIAL THANKS
  290. Thanks to cPanel Inc, for graciously funding the creation of this module.