You are viewing a plain text version of this content. The canonical link for it is here.
Posted to test-commits@perl.apache.org by to...@apache.org on 2010/12/27 14:09:55 UTC

svn commit: r1053063 - in /perl/Apache-Test/trunk: Changes lib/Apache/Test.pm t/import.t

Author: torsten
Date: Mon Dec 27 13:09:54 2010
New Revision: 1053063

URL: http://svn.apache.org/viewvc?rev=1053063&view=rev
Log:
The current implementation of AT->import is buggy in many aspects.
For example, import() is called at compile time and plan() at run
time. AT uses a set of global variables to pass state from import() to
plan() (@testmore and $real_plan). Now, suppose a set of test modules
that use a mix of -withtestmore and without it is loaded via
PerlModule or similar. import() is called when they are loaded but
plan() only when the handler is called. What is the state of @testmore
and $real_plan at plan() time?

This is also the reason why the 0Xtestmore tests fail sometimes. If
the wrong interpreter is hit by the request the wrong plan() is
called.

This a rewrite of AT::import plus a few tweaks in other functions.
It introduces %wtm to memorize which caller uses Test::More and which
Test. If a module calls

  use Apache::Test;                     # without -withtestmore
  ...
  use Apache::Test qw/-withtestmore/;   # and later with test more

a warning is issued and the -withtestmore switch is ignored. Once
Test.pm always Test.pm.

The other way around is allowed to export more functions for example:

  use Apache::Test qw/-withtestmore is_deeply/;
  ...
  use Apache::Test qw/plan/;

But still once -withtestmore always -withtestmore (for the namespace).

Further, the code does not import all default symbols from Test and
Test::More into the Apache::Test namespace. It imports ok() and skip()
from Test.pm but only because Apache::TestHandler (and perhaps some
other modules) calls them as Apache::Test::ok. Instead it uses string
eval to export directly from Test.pm or Test::More into the calling
namespace.

But that causes a problem, import() has to decide which functions to
export from Apache::Test namespace and which from Test.pm or
Test::More. Both modules inherit from Exporter and Exporter allows for
quite complex import syntax (e.g. /symbol/ or !/sym.$/). I didn't want
to copy&paste that logic from Exporter. So, import() ignores those
more exotic specifications and issues a warning.

Further, ":withtestmore" is gone. A warning is issued if it is used.

There is one more subtle case. What if someone uses:

  use Apache::Test ();

That way the import() function is skipped. So, the first occasion code
from Apache::Test is called is when

  Apache::Test::plan tests=>19;

is executed. The new AT version takes this as if import was called as

  use Apache::Test qw/!:DEFAULT/;

meaning without -withtestmore. But a warning is issued that there is a
plan() call without a previous use(). That also means that the
following will be an error:

  package my::test;
  require Apache::Test;
  Apache::Test::plan tests=>19;
  ...

  package my::test;
  use Apache::Test qw/-withtestmore/;

The -wtm option will be ignored due to the plan() call above that
nails the package my::test to Test.pm.

Added:
    perl/Apache-Test/trunk/t/import.t
Modified:
    perl/Apache-Test/trunk/Changes
    perl/Apache-Test/trunk/lib/Apache/Test.pm

Modified: perl/Apache-Test/trunk/Changes
URL: http://svn.apache.org/viewvc/perl/Apache-Test/trunk/Changes?rev=1053063&r1=1053062&r2=1053063&view=diff
==============================================================================
--- perl/Apache-Test/trunk/Changes (original)
+++ perl/Apache-Test/trunk/Changes Mon Dec 27 13:09:54 2010
@@ -8,6 +8,8 @@ Changes - Apache::Test change logfile
 
 =item 1.35-dev
 
+Make -withtestmore a per-package option (make it behave sane).
+[Torsten Foertsch]
 
 =item 1.34 December 18, 2010
 

Modified: perl/Apache-Test/trunk/lib/Apache/Test.pm
URL: http://svn.apache.org/viewvc/perl/Apache-Test/trunk/lib/Apache/Test.pm?rev=1053063&r1=1053062&r2=1053063&view=diff
==============================================================================
--- perl/Apache-Test/trunk/lib/Apache/Test.pm (original)
+++ perl/Apache-Test/trunk/lib/Apache/Test.pm Mon Dec 27 13:09:54 2010
@@ -21,6 +21,7 @@ use warnings FATAL => 'all';
 use Exporter ();
 use Config;
 use Apache::TestConfig ();
+use Test qw/ok skip/;
 
 BEGIN {
     # Apache::Test loads a bunch of mp2 stuff while getting itself
@@ -47,14 +48,9 @@ my @need = qw(need_lwp need_http11 need_
 my @have = map { (my $need = $_) =~ s/need/have/; $need } @need;
 
 @ISA = qw(Exporter);
-@EXPORT = (qw(ok skip sok plan skip_reason under_construction need),
+@EXPORT = (qw(sok plan skip_reason under_construction need),
            @need, @have);
 
-# everything but ok(), skip(), and plan() - Test::More provides these
-my @test_more_exports = grep { ! /^(ok|skip|plan)$/ } @EXPORT;
-
-%EXPORT_TAGS = (withtestmore => \@test_more_exports);
-
 %SubTests = ();
 @SkipReasons = ();
 
@@ -62,48 +58,79 @@ if (my $subtests = $ENV{HTTPD_TEST_SUBTE
     %SubTests = map { $_, 1 } split /\s+/, $subtests;
 }
 
-my $Config;
-my $real_plan;
-my @testmore;
+sub cp {
+    my @l;
+    for( my $i=1; (@l=caller $i)[0] eq __PACKAGE__; $i++ ) {};
+    return wantarray ? @l : $l[0];
+}
 
+my $Config;
+my %wtm;
 sub import {
-    my $class = shift;
-
-    # once Test::More always Test::More until plan() is called
-    if (($_[0] and $_[0] =~ m/^-withtestmore/) || @testmore) {
-        # special hoops for Test::More support
-
-        $real_plan = eval {
-
-            require Test::More;
-
-            no warnings qw(numeric);
-            Test::Builder->VERSION('0.18_01');
-
-            # required for Test::More::import() and Apache::Test::plan()
-            # if we don't do this, Test::More exports plan() anyway
-            # and we get collisions.  go figure.
-            @testmore = (import => [qw(!plan)]);
-
-            Test::More->import(@testmore);
-
-            \&Test::More::plan;
-        } or die "-withtestmore error: $@";
-
-        # clean up arguments to export_to_level
-        shift;
-        @EXPORT = (@test_more_exports, @Test::More::EXPORT);
-    }
-    else {
-        # the default - Test.pm support
-
-        require Test;
-        Test->import(qw(ok skip));
-        @testmore = ();               # reset, just in case.
-        $real_plan = \&Test::plan;
+    my $class=$_[0];
+    my $wtm=0;
+    my @base_exp;
+    my @exp;
+    my %my_exports;
+    undef @my_exports{@EXPORT};
+
+    my ($caller,$f,$l)=cp;
+
+    for( my $i=1; $i<@_; $i++ ) {
+	if( $_[$i] eq '-withtestmore' ) {
+	    $wtm=1;
+	}
+	elsif( $_[$i] eq ':DEFAULT' ) {
+	    push @exp, $_[$i];
+	    push @base_exp, $_[$i];
+	}
+	elsif( $_[$i] eq '!:DEFAULT' ) {
+	    push @exp, $_[$i];
+	    push @base_exp, $_[$i];
+	}
+	elsif( $_[$i]=~m@^[:/!]@ ) {
+	    warn("Ignoring import spec $_[$i] ".
+		 "at $f line $l\n")
+	}
+	elsif( exists $my_exports{$_[$i]} ) {
+	    push @exp, $_[$i];
+	}
+	else {
+	    push @base_exp, $_[$i];
+	}
+    }
+    if (!@exp and @base_exp) {
+	@exp=('!:DEFAULT');
+    }
+    elsif (@exp and !@base_exp) {
+	@base_exp=('!:DEFAULT');
+    }
+
+    $wtm{$caller}=[$wtm,$f,$l] unless exists $wtm{$caller};
+
+    warn("Ignoring -withtestmore due to a previous call ".
+	 "($wtm{$caller}->[1]:$wtm{$caller}->[2]) without it ".
+	 "at $f line $l\n")
+	if $wtm{$caller}->[0]==0 and $wtm==1;
+
+    $class->export_to_level(1, $class, @exp);
+
+    push @base_exp, '!plan';
+    if( $wtm{$caller}->[0] ) {	# -withtestmore
+	eval <<"EVAL"
+package $caller;
+#line $l $f
+use Test::More import=>\\\@base_exp;
+EVAL
+    }
+    else {			# -withouttestmore
+	eval <<"EVAL";
+package $caller;
+#line $l $f
+use Test \@base_exp;
+EVAL
     }
-
-    $class->export_to_level(1, undef, @_ ? @_ : @EXPORT);
+    die $@ if $@;
 }
 
 sub config {
@@ -144,7 +171,10 @@ EOE
 
 #so Perl's Test.pm can be run inside mod_perl
 sub test_pm_refresh {
-    if (@testmore) {
+    my ($caller,$f,$l)=cp;
+
+    if (exists $wtm{$caller} and $wtm{$caller}->[0]==1) { # -withtestmore
+	require Test::Builder;
         my $builder = Test::Builder->new;
 
         $builder->reset;
@@ -159,11 +189,23 @@ sub test_pm_refresh {
         # later user reports.
         $builder->failure_output(\*STDOUT);
     }
-    else {
-        $Test::TESTOUT = \*STDOUT;
-        $Test::planned = 0;
-        $Test::ntest = 1;
-        %Test::todo = ();
+    else {                                                # -withouttestmore
+	unless (exists $wtm{$caller}) {
+	    warn "You forgot to 'use Apache::Test' in package $caller\n";
+	    $wtm{$caller}=[0,$f,$l];
+	}
+	if (defined &Test::_reset_globals) {
+	    Test::_reset_globals();
+	    # Test.pm uses $TESTOUT=*STDOUT{IO}. We cannot do that
+	    # due to the way SetupEnv works.
+	    $Test::TESTOUT = \*STDOUT;
+	}
+	else {
+	    $Test::TESTOUT = \*STDOUT;
+	    $Test::planned = 0;
+	    $Test::ntest = 1;
+	    %Test::todo = ();
+	}
     }
 }
 
@@ -186,34 +228,6 @@ sub init_test_pm {
     $r->content_type('text/plain');
 }
 
-sub need_http11 {
-    require Apache::TestRequest;
-    if (Apache::TestRequest::install_http11()) {
-        return 1;
-    }
-    else {
-        push @SkipReasons,
-           "LWP version 5.60+ required for HTTP/1.1 support";
-        return 0;
-    }
-}
-
-sub need_ssl {
-    my $vars = vars();
-    need_module([$vars->{ssl_module_name}, 'Net::SSL']);
-}
-
-sub need_lwp {
-    require Apache::TestRequest;
-    if (Apache::TestRequest::has_lwp()) {
-        return 1;
-    }
-    else {
-        push @SkipReasons, "libwww-perl is not installed";
-        return 0;
-    }
-}
-
 sub plan {
     init_test_pm(shift) if ref $_[0];
     test_pm_refresh();
@@ -254,12 +268,51 @@ sub plan {
     }
     @SkipReasons = (); # reset
 
-    $real_plan->(@_, @testmore);
+    my ($caller,$f,$l)=cp;
+
+    if (exists $wtm{$caller} and $wtm{$caller}->[0]==1) { # -withtestmore
+	Test::More::plan(@_);
+    }
+    else {                                                # -withouttestmore
+	unless (exists $wtm{$caller}) {
+	    warn "You forgot to 'use Apache::Test' in package $caller\n";
+	    $wtm{$caller}=[0,$f,$l];
+	}
+	Test::plan(@_);
+    }
 
     # add to Test.pm verbose output
     print "# Using Apache/Test.pm version $VERSION\n";
 }
 
+sub need_http11 {
+    require Apache::TestRequest;
+    if (Apache::TestRequest::install_http11()) {
+        return 1;
+    }
+    else {
+        push @SkipReasons,
+           "LWP version 5.60+ required for HTTP/1.1 support";
+        return 0;
+    }
+}
+
+sub need_ssl {
+    my $vars = vars();
+    need_module([$vars->{ssl_module_name}, 'Net::SSL']);
+}
+
+sub need_lwp {
+    require Apache::TestRequest;
+    if (Apache::TestRequest::has_lwp()) {
+        return 1;
+    }
+    else {
+        push @SkipReasons, "libwww-perl is not installed";
+        return 0;
+    }
+}
+
 sub need {
     my $need_all = 1;
     for my $cond (@_) {

Added: perl/Apache-Test/trunk/t/import.t
URL: http://svn.apache.org/viewvc/perl/Apache-Test/trunk/t/import.t?rev=1053063&view=auto
==============================================================================
--- perl/Apache-Test/trunk/t/import.t (added)
+++ perl/Apache-Test/trunk/t/import.t Mon Dec 27 13:09:54 2010
@@ -0,0 +1,137 @@
+#!perl
+
+use strict;
+use warnings FATAL=>'all';
+use Test ();
+
+Test::plan tests=>47;
+
+sub t {
+    my $p=$_[0];
+    no strict 'refs';
+    Test::ok defined &{$p."::ok"} && \&{$p."::ok"}==\&Test::ok,
+	    1, "$p - ok";
+    Test::ok defined &{$p."::need"} && \&{$p."::need"}==\&Apache::Test::need,
+	    1, "$p - need";
+    Test::ok defined &{$p."::plan"} && \&{$p."::plan"}==\&Apache::Test::plan,
+	    1, "$p - plan";
+}
+
+sub tm {
+    my $p=$_[0];
+    no strict 'refs';
+    Test::ok defined &{$p."::ok"} && \&{$p."::ok"}==\&Test::More::ok,
+	    1, "$p - ok";
+    Test::ok defined &{$p."::need"} && \&{$p."::need"}==\&Apache::Test::need,
+	    1, "$p - need";
+    Test::ok defined &{$p."::plan"} && \&{$p."::plan"}==\&Apache::Test::plan,
+	    1, "$p - plan";
+}
+
+{package X0; use Apache::Test;}
+{package Y0; use Apache::Test qw/-withtestmore/;}
+
+t  'X0';
+tm 'Y0';
+
+{package X1; use Apache::Test qw/:DEFAULT/;}
+{package Y1; use Apache::Test qw/-withtestmore :DEFAULT/;}
+
+t  'X1';
+tm 'Y1';
+
+{package X2; use Apache::Test qw/!:DEFAULT/;}
+{package Y2; use Apache::Test qw/-withtestmore !:DEFAULT/;}
+
+Test::ok !defined &X2::ok, 1,   '!defined &X2::ok';
+Test::ok !defined &X2::need, 1, '!defined &X2::need';
+Test::ok !defined &X2::plan, 1, '!defined &X2::plan';
+Test::ok !defined &Y2::ok, 1,   '!defined &Y2::ok';
+Test::ok !defined &Y2::need, 1, '!defined &Y2::need';
+Test::ok !defined &Y2::plan, 1, '!defined &Y2::plan';
+
+{package X3; use Apache::Test qw/plan/;}
+{package Y3; use Apache::Test qw/-withtestmore plan/;}
+
+Test::ok !defined &X3::ok, 1,   '!defined &X3::ok';
+Test::ok !defined &X3::need, 1, '!defined &X3::need';
+Test::ok defined &X3::plan && \&X3::plan==\&Apache::Test::plan, 1, "X3 - plan";
+Test::ok !defined &Y3::ok, 1,   '!defined &Y3::ok';
+Test::ok !defined &Y3::need, 1, '!defined &Y3::need';
+Test::ok defined &Y3::plan && \&Y3::plan==\&Apache::Test::plan, 1, "Y3 - plan";
+
+{package X4; use Apache::Test qw/need/;}
+{package Y4; use Apache::Test qw/-withtestmore need/;}
+
+Test::ok !defined &X4::ok, 1,   '!defined &X4::ok';
+Test::ok defined &X4::need && \&X4::need==\&Apache::Test::need, 1, "X4 - need";
+Test::ok !defined &X4::plan, 1, '!defined &X4::plan';
+Test::ok !defined &Y4::ok, 1,   '!defined &Y4::ok';
+Test::ok defined &Y4::need && \&Y4::need==\&Apache::Test::need, 1, "Y4 - need";
+Test::ok !defined &Y4::plan, 1, '!defined &Y4::plan';
+
+{package X5; use Apache::Test qw/ok/;}
+{package Y5; use Apache::Test qw/-withtestmore ok/;}
+
+Test::ok defined &X5::ok && \&X5::ok==\&Test::ok, 1, "X5 - ok";
+Test::ok !defined &X5::need, 1, '!defined &X5::need';
+Test::ok !defined &X5::plan, 1, '!defined &X5::plan';
+Test::ok defined &Y5::ok && \&Y5::ok==\&Test::More::ok, 1, "Y5 - ok";
+Test::ok !defined &Y5::need, 1, '!defined &Y5::need';
+Test::ok !defined &Y5::plan, 1, '!defined &Y5::plan';
+
+{package X6; use Apache::Test qw/ok need/;}
+{package Y6; use Apache::Test qw/-withtestmore ok need/;}
+
+Test::ok defined &X6::ok && \&X6::ok==\&Test::ok, 1, "X6 - ok";
+Test::ok defined &X6::need && \&X6::need==\&Apache::Test::need, 1, "X6 - need";
+Test::ok !defined &X6::plan, 1, '!defined &X6::plan';
+Test::ok defined &Y6::ok && \&Y6::ok==\&Test::More::ok, 1, "Y6 - ok";
+Test::ok defined &Y6::need && \&Y6::need==\&Apache::Test::need, 1, "Y6 - need";
+Test::ok !defined &Y6::plan, 1, '!defined &Y6::plan';
+
+my $warning;
+{
+    local $SIG{__WARN__}=sub {$warning=join '', @_};
+    eval <<'EVAL';
+package Z0;
+use Apache::Test qw/:withtestmore/;
+EVAL
+}
+Test::ok $warning, qr/^Ignoring import spec :withtestmore at/,
+    "Ignore import warning";
+
+undef $warning;
+{
+    local $SIG{__WARN__}=sub {$warning=join '', @_};
+    eval <<'EVAL';
+package X0;
+use Apache::Test qw/-withtestmore/;
+EVAL
+}
+Test::ok $warning, qr/^Ignoring -withtestmore due to a previous call /,
+    "Ignore -withtestmore warning";
+
+use Config ();
+my $pio=!$Config::Config{useperlio};
+my $output;
+Test::skip $pio, sub {
+    my @res;
+    {
+	local $Test::ntest=-19;
+	local $Test::planned=-42;
+	package Y2;	       # uses Apache::Test qw/-withtestmore !:DEFAULT/
+			       # so nothing is exported
+
+	local *STDOUT;
+	open STDOUT, '>', \$output;
+	Apache::Test::plan tests=>17;
+	Test::More::isnt "hugo", "erwin", "hugo is not erwin";
+	@res=($Test::ntest, $Test::planned);
+	Test::Builder->new->reset;
+    }
+    return "@res";
+}, '-19 -42', '$Test::ntest, $Test::planned did not change';
+
+Test::skip $pio, $output, qr/^1\.\.17$/m, 'planned';
+Test::skip $pio, $output, qr/^ok 1 - hugo is not erwin$/m, 'Test::More::isnt';