CC: | Mark Grimes <mgrimes [...] cpan.org> |
Subject: | [PATCH] Updates the tests to support more recent Mojolicious versions |
Date: | Mon, 1 Feb 2016 11:10:40 -0500 |
To: | bug-Log-Any-Adapter-Mojo [...] rt.cpan.org |
From: | Mark Grimes <mgrimes [...] cpan.org> |
- is_fatal, etc dropped in Mojolicious v6.0
- use_ok is no longer recommended in tests, just use and let it fail
- Mojolicious v5 replaced the format method with an attribute. The "new"
format has been around since Mojolicious v1.5, we can simplify tests
by dropping support for < 1.53.
- Simplifies capturing log output by redirecting STDERR to a filehandle
pointing to a scalar.
---
t/basic.t | 142 +++++++++++---------------------------------------------------
1 file changed, 24 insertions(+), 118 deletions(-)
diff --git a/t/basic.t b/t/basic.t
index 868f99e..2057058 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -8,43 +8,13 @@ use File::Spec;
use lib join '/', File::Spec->splitdir( dirname(__FILE__) ), '..', 'lib';
-use Data::Dumper;
use Mojolicious;
-use Test::More tests => 29;
-use Test::Mojo;
-
-use File::Temp qw{ tempfile tmpnam };
-
-use_ok 'Log::Any::Adapter::Mojo';
+use Test::More;
+use Log::Any::Adapter::Mojo;
use Log::Any qw($log);
-# From 1.53 on an experimental formatter is added to Mojo::Log and the
-# default log format has been simplified.
-# Since all testcases are based on the old format, we need a custom
-# Logger to simulate old behaviour.
-package MyLog;
-use Mojo::Base 'Mojo::Log';
-
-# overload formatter
-sub format {
- my ( $self, $level, @msgs ) = @_;
- my $msgs = join "\n",
- map { utf8::decode $_ unless utf8::is_utf8 $_; $_ } @msgs;
-
- # Caller
- my ( $pkg, $line ) = ( caller(2) )[ 0, 2 ];
-
- ( $pkg, $line ) = ( caller(3) )[ 0, 2 ]
- if $pkg eq ref $self || $pkg eq 'Mojo::Log';
-
- return '' . localtime(time) . " $level $pkg:$line [$$]: $msgs\n";
-}
-
-package main;
-
-# See comment about formatter above
-my $mojo_log = $Mojolicious::VERSION * 1.0 >= 1.53 ? MyLog->new : Mojo::Log->new;
+my $mojo_log = Mojo::Log->new;
Log::Any->set_adapter( 'Mojo', logger => $mojo_log );
@@ -66,9 +36,11 @@ is( $log->is_info, 1, 'is_info' );
is( $log->is_notice, 1, 'is_notice' );
is( $log->is_warning, 1, 'is_warning' );
is( $log->is_error, 1, 'is_error' );
-is( $log->is_critical, 1, 'is_critical' );
-is( $log->is_alert, 1, 'is_alert' );
-is( $log->is_emergency, 1, 'is_emergency' );
+if( $Mojolicious::VERSION * 1.0 < 6 ){ # is_fatal, etc removed in 6.0
+ is( $log->is_critical, 1, 'is_critical' );
+ is( $log->is_alert, 1, 'is_alert' );
+ is( $log->is_emergency, 1, 'is_emergency' );
+}
# Set level to error only
$ENV{MOJO_LOG_LEVEL} = 'error';
@@ -79,96 +51,30 @@ is( $log->is_info, '', 'is_info' );
is( $log->is_notice, '', 'is_notice' );
is( $log->is_warning, '', 'is_warning' );
is( $log->is_error, 1, 'is_error' );
-is( $log->is_critical, 1, 'is_critical' );
-is( $log->is_alert, 1, 'is_alert' );
-is( $log->is_emergency, 1, 'is_emergency' );
+if( $Mojolicious::VERSION * 1.0 < 6 ){ # is_fatal, etc removed in 6.0
+ is( $log->is_critical, 1, 'is_critical' );
+ is( $log->is_alert, 1, 'is_alert' );
+ is( $log->is_emergency, 1, 'is_emergency' );
+}
# Test log line. Not testing caller
sub _test_log {
my ( $level, $target_level, $msg, $label ) = @_;
- like(
- _capture_stderr(
- sub {
- $log->$level($msg);
- }
- ),
- qr/\A\w{3}\ \w{3}\ +\d{1,2}\ \d{2}:\d{2}:\d{2}\ \d{4}
- \ \Q$target_level\E .*?\ \[\d+\]:\ \Q$msg\E\n\z/xms,
+ my $buffer = '';
+ {
+ open my $handle, '>', \$buffer;
+ local *STDERR = $handle;
+ $log->$level($msg);
+ }
+
+ return like(
+ $buffer,
+ qr/\A\[.*\] \[\Q$target_level\E\] \Q$msg\E\s\z/ms,
$label
);
- return;
-}
-
-# Redirect STDERR to temporary file. Execute function and return
-# captured output.
-sub _capture_stderr {
- my ($func_to_exec) = @_;
-
- my ( $temp_file_handle_stderr, $temp_file_name_stderr )
- = tempfile( UNLINK => 1, EXLOCK => 0 );
-
- open my $old_file_handle_stderr, '>&STDERR'
- or Carp::croak qq{Can't dup STDERR: $!};
-
- open STDERR, '>', $temp_file_name_stderr
- or Carp::croak
- qq{Can't redirect STDERR to $temp_file_name_stderr: $!};
-
- my $store = $|;
-
- select STDERR;
- $| = 1;
-
- $func_to_exec->();
-
- open STDERR, '>&', $old_file_handle_stderr
- or Carp::croak qq{Can't dup old_stderr: $!};
-
- $| = $store;
-
- open my $fh, '<', $temp_file_name_stderr
- or Carp::confess qq{$temp_file_name_stderr: $!};
-
- # Slurp temp. file content.
- my $stderr_output = do {
- local $/;
- <$fh>;
- };
- close $fh;
-
- return $stderr_output;
}
-## Test that correct package and line is displayed in log.
-
-######### Test package
-
-package MyTest;
-use Log::Any qw($log);
-
-sub do_log {
- my ( $class, $msg ) = @_;
- return $log->debug($msg);
-}
-
-######### Test package
-
-package main;
-
-$ENV{MOJO_LOG_LEVEL} = 'debug'; # back to debug level
-
-my $msg = 'asdfjkjkladfjk889234jkljk3rmnvm,m,zxcv,asdfkljfk';
-like(
- _capture_stderr(
- sub {
- MyTest->do_log($msg);
- return;
- }
- ),
- qr/\A\w{3}\ \w{3}\ +\d{1,2}\ \d{2}:\d{2}:\d{2}\ \d{4}\ debug
- \ MyTest:\d+\ \[\d+\]:\ \Q$msg\E\n\z/xms,
- 'Test log package and line'
-);
+done_testing;
1;
--
2.7.0