Ver código fonte

Fix #113: TestRail::Utils::Find::FindTests accepts custom finder sub

George S. Baugh 9 anos atrás
pai
commit
a08652f9d7
3 arquivos alterados com 16 adições e 5 exclusões
  1. 2 1
      Changes
  2. 10 3
      lib/TestRail/Utils/Find.pm
  3. 4 1
      t/TestRail-Utils-Find.t

+ 2 - 1
Changes

@@ -1,11 +1,12 @@
 Revision history for Perl module TestRail::API
 
-0.038 2016-12-12 TEODESIAN
+0.038 2017-01-23 TEODESIAN
     - Optimize TestRail::Utils::Find::getResults and testrail-results
     - Add ability to follow POST redirects
     - Don't print stack traces during constructor errors, this can leak auth info into logs
     - Don't override filename in TAP Parser if we already have it
     - Fix issue where non-standard status overrides were not possible
+    - Add finder callback to TestRail::Utils::FindTests
 
 0.037 2016-08-10 TEODESIAN
     - Fix incorrect POD for TestRail::API::createRunInPlan

+ 10 - 3
lib/TestRail/Utils/Find.pm

@@ -179,6 +179,8 @@ Given an ARRAY of tests, find tests meeting your criteria (or not) in the specif
 
 =item STRING C<EXTENSION> (optional) - Only return files ending with the provided text (e.g. .t, .test, .pl, .pm)
 
+=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.
+
 =back
 
 =item ARRAY C<CASES> - Array of cases to translate to pathnames based on above options.
@@ -205,10 +207,15 @@ sub findTests {
         my @tmpArr = ();
         my $dir = ($opts->{'match'} || $opts->{'orphans'}) ? ($opts->{'match'} || $opts->{'orphans'}) : $opts->{'no-match'};
         confess "No such directory '$dir'" if ! -d $dir;
-        if (!$opts->{'no-recurse'}) {
-            File::Find::find( sub { push(@realtests,$File::Find::name) if -f && m/\Q$ext\E$/ }, $dir );
+
+        if (ref($opts->{finder}) eq 'CODE') {
+            @realtests = $opts->{finder}->($dir,$ext)
         } else {
-            @realtests = glob("$dir/*$ext");
+            if (!$opts->{'no-recurse'}) {
+                File::Find::find( sub { push(@realtests,$File::Find::name) if -f && m/\Q$ext\E$/ }, $dir );
+            } else {
+                @realtests = glob("$dir/*$ext");
+            }
         }
         foreach my $case (@cases) {
             foreach my $path (@realtests) {

+ 4 - 1
t/TestRail-Utils-Find.t

@@ -4,7 +4,7 @@ use warnings;
 use FindBin;
 use lib "$FindBin::Bin/lib";
 
-use Test::More 'tests' => 52;
+use Test::More 'tests' => 53;
 use Test::Fatal;
 use Test::Deep;
 use File::Basename qw{dirname};
@@ -174,6 +174,9 @@ delete $opts->{'match'};
 @tests = TestRail::Utils::Find::findTests($opts,@$cases);
 is(scalar(@tests),0,"Correct number of cases shown (match, plan run, failed)");
 
+#Test FindTests' finder sub
+like(exception { TestRail::Utils::Find::findTests({'match' => '.', 'finder' => sub { return die('got here') } }) }, qr/got here/i, "FindTests callback can fire correctly");
+
 $opts = {
     'project' => 'TestProject',
     'testsuite' => 'HAMBURGER-IZE HUMANITY',