[tapx-dev] [commit] [99] Add the ability to match test files against regexes.

ovid at hexten.net ovid at hexten.net
Sat Mar 10 12:12:42 GMT 2007


Revision: 99
Author:   ovid
Date:     2007-03-10 12:12:42 +0000 (Sat, 10 Mar 2007)

Log Message:
-----------
Add the ability to match test files against regexes.  While the feature is
needed and it works well, I hate the YAML file format.  I think it's going to
confuse people.  I haven't done the documentation as well as I could, either,
since I suspect this bit might change.

Modified Paths:
--------------
    trunk/Changes
    trunk/examples/my_execrc
    trunk/lib/TAPx/Harness.pm
    trunk/t/120-harness.t
    trunk/t/data/execrc

Modified: trunk/Changes
===================================================================
--- trunk/Changes	2007-03-10 11:17:09 UTC (rev 98)
+++ trunk/Changes	2007-03-10 12:12:42 UTC (rev 99)
@@ -1,5 +1,8 @@
 Revision history for TAP-Parser
 
+0.51
+    - 'execrc' file now allows 'regex' matches for tests.
+
 0.50_07 5 March 2007
     - Fixed bug where we erroneously checked the test number instead of number
       of tests run to determine if we've run more tests than we planned.

Modified: trunk/examples/my_execrc
===================================================================
--- trunk/examples/my_execrc	2007-03-10 11:17:09 UTC (rev 98)
+++ trunk/examples/my_execrc	2007-03-10 12:12:42 UTC (rev 99)
@@ -1,24 +1,18 @@
 ---
-tests:
-# this is the default for all files
+exact:
+  # whoops!  We have a ruby test here!
   -
-    - /usr/bin/perl
-    - -wT
-    - *
-
-# whoops!  We have a ruby test here!
-  -
     - /usr/bin/ruby
     - t/ruby.t
-
-# let's test some web pages
-  -
+regex:
+ -
     - /usr/bin/perl
     - -w
     - bin/test_html.pl
-    - http://www.google.com/
+    - ^https?:.*
+
+default:
   -
     - /usr/bin/perl
-    - -w
-    - bin/test_html.pl
-    - http://www.yahoo.com/
+    - -wT
+

Modified: trunk/lib/TAPx/Harness.pm
===================================================================
--- trunk/lib/TAPx/Harness.pm	2007-03-10 11:17:09 UTC (rev 98)
+++ trunk/lib/TAPx/Harness.pm	2007-03-10 12:12:42 UTC (rev 99)
@@ -262,23 +262,32 @@
 
 sub _read_execrc {
     my $self = shift;
-    $self->_execrc( {} );
+    $self->_execrc( { exact => {}, regex => {} } );
     my $execrc = $self->execrc or return $self;
     my $data   = TAPx::Parser::YAML->read($execrc);
-    my $tests  = $data->[0]{tests};
 
     my %exec_for;
-    foreach my $exec (@$tests) {
-        if ( '*' eq $exec->[-1] ) {
-            pop @$exec;
-
-            # don't override command line
-            $self->exec($exec) unless $self->exec;
+    foreach my $type ( qw{ exact regex } ) {
+        foreach my $exec (@{ $data->[0]{$type} }) {
+            if ( 'regex' eq $type ) {
+                eval { qr/$exec/ };
+                if ( my $error = $@ ) {
+                    warn "Can't use execrc item ($exec) as a regex: $error";
+                    next;
+                }
+            }
+            my $test = $exec->[-1];
+            $exec_for{ $type }{ $test } = $exec;
         }
-        else {
-            $exec_for{ $exec->[-1] } = $exec;
-        }
     }
+
+    if ( my $exec = $data->[0]{default} ) {
+         $exec = $exec->[0];
+
+        # don't override command line
+        $self->exec($exec) unless $self->exec;
+    }
+
     $self->_execrc( \%exec_for );
     return $self;
 }
@@ -686,12 +695,20 @@
     my $execrc = $self->_execrc;
     
     my $executable;
-    if ( my $exec = $execrc->{$test} ) {
+    if ( my $exec = $execrc->{exact}{$test} ) {
         $executable = $exec;
     }
-    elsif ( $exec = $self->exec ) {
-        $executable = [ @$exec, $test ];
+    else {
+         foreach my $regex ( keys %{ $execrc->{regex} } ) {
+             if ( $test =~ qr/$regex/ ) {
+                 $executable = $execrc->{regex}{$regex};
+                 $executable->[-1] = $test;
+             }
+         }
     }
+    if ( my $exec = $self->exec ) {
+        $executable ||= [ @$exec, $test ];
+    }
     return $executable;
 }
 
@@ -862,38 +879,54 @@
 
 Sometimes you want to use different executables to run different tests.  If
 that's the case, you'll need to create an C<execrc> file.  This file should be
-a YAML file.  This should be representative a hash with one key, C<tests>,
-whose value is an array of array references.  Each terminating array reference
-should be a list of the exact arguments which eventually get executed.
+a YAML file.  This should be representating a hash with (at present) three
+keys, C<exact>, C<regex>, and C<default>.
 
  ---
- tests:
- # this is the default for all files
+ exact: 
+   # whoops!  We have a ruby test here!
    -
-     - /usr/bin/perl
-     - -wT
-     - *
- 
- # whoops!  We have a ruby test here!
-   -
      - /usr/bin/ruby
      - t/ruby.t
- 
- # let's test some web pages
+ regex:
+   # let's test some web pages
    -
      - /usr/bin/perl
      - -w
      - bin/test_html.pl
-     - http://www.google.com/
+     - ^https?://
+ default:
    -
      - /usr/bin/perl
-     - -w
-     - bin/test_html.pl
-     - http://www.yahoo.com/
+     - -wT
+  
+=over 4
 
-If the terminating element in an array is '*', then the rest of the array are
-the default arguments used to run any test.
+=item * C<exact>
 
+The C<exact> key should be an array reference with each element being an array
+reference whose items are an exact list of what need to be passed to the shell
+to execute the test.  So for the 'exact' item of C<t/ruby.t> above, we attempt
+to execute in the shell:
+
+ /usr/bin/ruby t/ruby.t
+
+=item * C<regex>
+
+This is the same as C<exact>, except that the final element in each array
+reference should be a Perl regular expression.  For the C<^https?://> regular
+expression above, when the harness sees a test for C<http://www.example.com>,
+it will pass the following to the shell:
+
+ /usr/bin/perl -w bin/test_html.pl http://www.example.com/
+
+=item * C<default>
+
+Any item which the harness does not match to another C<execrc> entry will
+automatically be executed with the C<default>.
+
+=back
+
 Blank lines are allowed.  Lines beginning with a '#' are comments (the '#' may
 have spaces in front of it).
 

Modified: trunk/t/120-harness.t
===================================================================
--- trunk/t/120-harness.t	2007-03-10 11:17:09 UTC (rev 98)
+++ trunk/t/120-harness.t	2007-03-10 12:12:42 UTC (rev 99)
@@ -4,7 +4,7 @@
 
 use lib 'lib';
 
-use Test::More tests => 130;
+use Test::More tests => 132;
 
 # these tests cannot be run from the t/ directory due to checking for the
 # existence of execrc
@@ -35,6 +35,7 @@
 ok $ENV{HARNESS_VERSION}, 'HARNESS_VERSION env variable should be set';
 
 foreach my $HARNESS (qw<TAPx::Harness TAPx::Harness::Color>) {
+#foreach my $HARNESS ( () ) {   # XXX
     can_ok $HARNESS, 'new';
 
     eval { $HARNESS->new( { no_such_key => 1 } ) };
@@ -325,9 +326,10 @@
 
     # make sure execrc parsing is solid (internals test)
     my $harness = TAPx::Harness->new;
-    ok !$harness->exec, 'exec() should not be set with an empty harness';
+    ok !$harness->exec, 'exec() should not be set when the harness is new';
     my %execrc = %{ $harness->_execrc };
-    ok !%execrc, '... nor should execrc';
+    is_deeply \%execrc, { exact => {}, regex => {} },
+         '... nor should execrc';
 
     can_ok $harness, '_read_execrc';
     $harness->execrc('t/data/execrc');
@@ -353,6 +355,17 @@
         'http://www.google.com/',
       ],
       '... even if we match something which is not a file';
+    is_deeply $harness->_get_executable('t/some_customer.t'),
+      [ '/usr/bin/perl',
+        '-w',
+        't/some_customer.t'
+      ],
+      '... and regexes should work for specifying tests';
+    is_deeply $harness->_get_executable('t/customer.t'),
+      [ '/usr/bin/perl',
+        't/customer.t'
+      ],
+      '... but an exact match will override a regex test';
 }
 
 sub trim {

Modified: trunk/t/data/execrc
===================================================================
--- trunk/t/data/execrc	2007-03-10 11:17:09 UTC (rev 98)
+++ trunk/t/data/execrc	2007-03-10 12:12:42 UTC (rev 99)
@@ -1,17 +1,11 @@
 ---
-tests:
-# this is the default for all files
+# exact matches take precedence
+exact:
+  # whoops!  We have a ruby test here!
   -
-    - /usr/bin/perl
-    - -wT
-    - *
-
-# whoops!  We have a ruby test here!
-  -
     - /usr/bin/ruby
     - t/ruby.t
-
-# let's test some web pages
+  # let's test some web pages
   -
     - /usr/bin/perl
     - -w
@@ -22,3 +16,18 @@
     - -w
     - bin/test_html.pl
     - http://www.yahoo.com/
+  # override the regex
+  -
+    - /usr/bin/perl
+    - t/customer.t
+# regex matches are after exact matches
+regex:
+  -
+    - /usr/bin/perl
+    - -w
+    - .*customer.*
+# followed by the default
+default:
+  -
+    - /usr/bin/perl
+    - -wT




More information about the tapx-dev mailing list