[Rt-commit] rt branch, 4.0/actually-catch-test-warnings, created. rt-4.0.8-240-g9b38263

Thomas Sibley trs at bestpractical.com
Thu Dec 13 15:36:02 EST 2012


The branch, 4.0/actually-catch-test-warnings has been created
        at  9b38263d269d2ba882934430254f030ceea75f64 (commit)

- Log -----------------------------------------------------------------
commit 9b38263d269d2ba882934430254f030ceea75f64
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Wed Dec 12 17:53:00 2012 -0800

    Prevent RT->InitLogging from blowing away our warning catcher for tests
    
    Every time InitLogging is called from RT::Test, the warning catcher
    needs to be reinstalled.
    
    The normal RT warning handler (which ignores bogus wide character
    warnings from Mason), must be used as a filter for the Test::NoWarnings
    handler so that test files which trigger the bogus warning don't
    mistakenly fail the final "no warnings" test.  An example is
    t/web/user_update.t.
    
    Previous testing of 60068e5, which introduced the warning catcher, was
    performed _before_ I moved the "require Test::NoWarnings" line to the
    top of RT::Test; it was at the end of the import() method during my
    testing (and after all InitLogging calls).  Keeping it at the top is
    necessary, however, for test files which prevent RT::Test::import() from
    running via require or an empty import list.
    
    Warnings emitted by tests should now be caught at all levels, as
    expected.

diff --git a/lib/RT.pm b/lib/RT.pm
index 989e54e..5d18127 100644
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -339,6 +339,11 @@ sub InitSignalHandlers {
             unshift @_, $RT::Logger, qw(level warning message);
             goto &Log::Dispatch::log;
         }
+        # Return value is used only by RT::Test to filter warnings from
+        # reaching the Test::NoWarnings catcher.  If Log::Dispatch::log() ever
+        # starts returning 'IGNORE', we'll need to switch to something more
+        # clever.  I don't expect that to happen.
+        return 'IGNORE';
     };
 
 #When we call die, trap it and log->crit with the value of the die.
diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index f4fc539..c21345d 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -53,8 +53,15 @@ use warnings;
 
 
 use base 'Test::More';
+
+# We use the Test::NoWarnings catching and reporting functionality, but need to
+# wrap it in our own special handler because of the warn handler installed via
+# RT->InitLogging().
 require Test::NoWarnings;
 
+my $Test_NoWarnings_Catcher = $SIG{__WARN__};
+my $check_warnings_in_end   = 1;
+
 use Socket;
 use File::Temp qw(tempfile);
 use File::Path qw(mkpath);
@@ -62,7 +69,6 @@ use File::Spec;
 
 our @EXPORT = qw(is_empty diag parse_mail works fails plan done_testing);
 
-my $check_warnings_in_end = 1;
 my %tmp = (
     directory => undef,
     config    => {
@@ -146,7 +152,7 @@ sub import {
     __reconnect_rt()
         unless $args{nodb};
 
-    RT::InitLogging();
+    __init_logging();
 
     RT->Plugins;
 
@@ -434,7 +440,7 @@ sub bootstrap_db {
     $RT::Handle->InsertSchema;
     $RT::Handle->InsertACL unless $db_type eq 'Oracle';
 
-    RT->InitLogging;
+    __init_logging();
     __reconnect_rt();
 
     $RT::Handle->InsertInitialData
@@ -616,6 +622,28 @@ sub __disconnect_rt {
           if DBIx::SearchBuilder::Record::Cachable->can("FlushCache");
 }
 
+sub __init_logging {
+    my $filter;
+    {
+        # We use local to ensure that the $filter we grab is from InitLogging
+        # and not the handler generated by a previous call to this function
+        # itself.
+        local $SIG{__WARN__};
+        RT::InitLogging();
+        $filter = $SIG{__WARN__};
+    }
+    $SIG{__WARN__} = sub {
+        if ($filter) {
+            my $status = $filter->(@_);
+            if ($status and $status eq 'IGNORE') {
+                return; # pretend the bad dream never happened
+            }
+        }
+        # Avoid reporting this anonymous call frame as the source of the warning.
+        goto &$Test_NoWarnings_Catcher;
+    };
+}
+
 
 =head1 UTILITIES
 

-----------------------------------------------------------------------


More information about the Rt-commit mailing list