Find.pm 15 KB

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