Find.pm 17 KB

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