瀏覽代碼

Fix #106 & Fix #109. Optimize testrail-results.

Also fix minor issue where plan results were not counted
in the case where both plan and non plan runs were present.
George S. Baugh 10 年之前
父節點
當前提交
f9dabc6943
共有 6 個文件被更改,包括 42 次插入13 次删除
  1. 3 0
      Changes
  2. 13 4
      bin/testrail-results
  3. 1 1
      dist.ini
  4. 23 7
      lib/TestRail/Utils/Find.pm
  5. 0 0
      t/data/faketest_cache.json
  6. 2 1
      t/testrail-results.t

+ 3 - 0
Changes

@@ -1,5 +1,8 @@
 Revision history for Perl module TestRail::API
 
+0.038 2016-08-24 TEODESIAN
+    - Optimize TestRail::Utils::Find::getResults and testrail-results
+
 0.037 2016-08-10 TEODESIAN
     - Fix incorrect POD for TestRail::API::createRunInPlan
     - Add testrail-results binary and TestRail::Utils::Find::getResults.

+ 13 - 4
bin/testrail-results

@@ -99,6 +99,7 @@ use Getopt::Long qw{GetOptionsFromArray};
 use File::HomeDir qw{my_home};
 use JSON::MaybeXS ();
 use Statistics::Descriptive;
+use List::MoreUtils qw{uniq};
 
 if (!caller()) {
     my ($out,$code) = run('args' => \@ARGV);
@@ -142,6 +143,7 @@ sub run {
     my $tr = TestRail::Utils::getHandle($opts);
     my $prior_search;
     my $prior_runs = [];
+    my $prior_plans = [];
     if ($opts->{'cachefile'}) {
         my $raw_text = '';
         open(my $fh, '<', $opts->{'cachefile'}) or die "Could not open $opts->{cachefile}";
@@ -152,10 +154,15 @@ sub run {
         $prior_search = JSON::MaybeXS::decode_json($raw_text);
         foreach my $key (keys(%$prior_search)) {
             push(@$prior_runs,@{$prior_search->{$key}->{'seen_runs'}});
+            push(@$prior_plans,@{$prior_search->{$key}->{'seen_plans'}});
         }
+        $opts->{'plan_ids'} = $prior_plans;
     }
 
-    my $res = TestRail::Utils::Find::getResults($tr,$opts,$prior_runs,@{$params{'args'}});
+    my ($res,$seen_plans) = TestRail::Utils::Find::getResults($tr,$opts,$prior_runs,@{$params{'args'}});
+
+    #Make sure subsequent runs keep ignoring the prior plans
+    push(@$seen_plans,@$prior_plans);
 
     my $statuses = $tr->getPossibleTestStatuses();
     my %status_map;
@@ -170,7 +177,7 @@ sub run {
         my $avg_elapsed = 0;
         my $median_runtime = 0;
         my $elapsetotals = [];
-        my $seen_runs    = [];
+        my $seen_runs    = $prior_runs;
 
         foreach my $casedef (@{$res->{$case}}) {
             push(@$seen_runs, $casedef->{run_id});
@@ -181,14 +188,16 @@ sub run {
                 push(@$elapsetotals,_elapsed2secs($result->{'elapsed'}));
             }
         }
+        @$seen_runs = uniq(@$seen_runs);
 
         my $pattern_output = '';
         $out_json->{$case}->{search_string} = $opts->{'pattern'};
         $pattern_output = " using search string '$opts->{pattern}'" if $opts->{'pattern'};
 
         $out .= "$case was present in $num_runs runs$pattern_output.\n";
-        $out_json->{$case}->{'num_runs'}  = $num_runs;
-        $out_json->{$case}->{'seen_runs'} = $seen_runs;
+        $out_json->{$case}->{'num_runs'}   = $num_runs;
+        $out_json->{$case}->{'seen_runs'}  = $seen_runs;
+        $out_json->{$case}->{'seen_plans'} = $seen_plans;
 
         #Collect time statistics
         my $timestats = Statistics::Descriptive::Full->new();

+ 1 - 1
dist.ini

@@ -1,6 +1,6 @@
 name = TestRail-API
 main_module = lib/TestRail/API.pm
-version = 0.037
+version = 0.038
 author = George S. Baugh <teodesian@cpan.org>
 license = Perl_5
 copyright_holder = George S. Baugh

+ 23 - 7
lib/TestRail/Utils/Find.pm

@@ -8,6 +8,7 @@ use warnings;
 
 use Carp qw{confess cluck};
 use Scalar::Util qw{blessed};
+use List::MoreUtils qw{uniq};
 
 use File::Find;
 use Cwd qw{abs_path};
@@ -320,6 +321,8 @@ Get results for tests by name, filtered by the provided options, and skipping an
 
 Probably should have called this findResults, but we all prefer to get results right?
 
+Returns ARRAYREF of results, and an ARRAYREF of seen plan IDs
+
 =cut
 
 sub getResults {
@@ -327,6 +330,9 @@ sub getResults {
     my $res = {};
     my $projects = $tr->getProjects();
 
+
+    my $prior_plans = [];
+
     #TODO obey status filtering
     #TODO obey result notes text grepping
     foreach my $project (@$projects) {
@@ -335,21 +341,26 @@ sub getResults {
 
         #Translate plan names to ids
         my $plans = $tr->getPlans($project->{'id'}) || [];
+
+        #Filter out plans which do not match our filters to prevent a call to getPlanByID
+        if ($opts->{'plans'}) {
+            @$plans = grep { my $p = $_; grep { $p->{'name'} eq $_} @{$opts->{'plans'}} } @$plans;
+        }
+
+        #Filter out prior plans
+        if ($opts->{'plan_ids'}) {
+            @$plans = grep { my $p = $_; grep { $p->{'id'} eq $_} @{$opts->{'plan_ids'}} } @$plans;
+        }
+
         $opts->{'runs'} //= [];
-        my $plan_filters = [];
         foreach my $plan (@$plans) {
             $plan = $tr->getPlanByID($plan->{'id'});
             my $plan_runs = $tr->getChildRuns($plan);
             push(@$runs,@$plan_runs) if $plan_runs;
         }
 
-        if ($opts->{'plans'}) {
-            @$plan_filters = map { $_->{'id'} } grep { my $p = $_; grep { $p->{'name'} eq $_} @{$opts->{'plans'}} } @$plans;
-        }
-
         foreach my $run (@$runs) {
             next if scalar(@{$opts->{runs}}) && !( grep { $_ eq $run->{'name'} } @{$opts->{'runs'}} );
-            next if scalar(@$plan_filters) && !( grep { $run->{'plan_id'} ? $_ eq $run->{'plan_id'} : undef } @$plan_filters );
             next if grep { $run->{id} eq $_ } @$prior_runs;
             foreach my $case (@cases) {
                 my $c = $tr->getTestByName($run->{'id'},basename($case));
@@ -367,8 +378,13 @@ sub getResults {
                 push(@{$res->{$case}}, $c) if scalar(@{$c->{results}}); #Make sure they weren't filtered out
             }
         }
+
+        push(@$prior_plans, map {$_->{'id'}} @$plans);
     }
-    return $res;
+
+    @$prior_plans = uniq(@$prior_plans);
+
+    return ($res,$prior_plans);
 }
 
 1;

文件差異過大導致無法顯示
+ 0 - 0
t/data/faketest_cache.json


+ 2 - 1
t/testrail-results.t

@@ -11,6 +11,7 @@ use Test::LWP::UserAgent::TestRailMock;
 
 use Test::More 'tests' => 22;
 use Capture::Tiny qw{capture_merged};
+use List::MoreUtils qw{uniq};
 
 no warnings qw{redefine once};
 *TestRail::API::getTests = sub {
@@ -73,7 +74,7 @@ like($out,qr/fake\.test was present in 10 runs/,"Gets correct # of runs with tes
 push(@args,'mah dubz plan', 't/fake.test');
 ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
 is($code, 0, "Exit code OK looking for results of fake.test");
-like($out,qr/fake\.test was present in 258 runs/,"Gets correct # of runs with test inside it when filtering by plan name");
+like($out,qr/fake\.test was present in 259 runs/,"Gets correct # of runs with test inside it when filtering by plan name");
 
 #check run filters
 @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --run FinalRun t/fake.test};

部分文件因文件數量過多而無法顯示