Log-Mini/t/03_LoggerFILE.t

87 lines
1.9 KiB
Perl
Raw Permalink Normal View History

2019-02-21 22:30:49 +03:00
use lib 'lib';
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use File::Temp;
2023-03-17 18:29:38 +03:00
use Log::Mini::Logger::FILE;
2019-02-21 22:30:49 +03:00
2019-03-19 14:57:41 +03:00
2019-02-21 22:30:49 +03:00
subtest 'creates correct object' => sub {
2023-03-17 18:29:38 +03:00
isa_ok(Log::Mini::Logger::FILE->new, 'Log::Mini::Logger::FILE');
2019-02-21 22:30:49 +03:00
};
subtest 'prints to file' => sub {
2023-03-17 21:53:33 +03:00
for my $level (qw/error warn info debug trace/) {
2019-02-21 22:30:49 +03:00
my $file = File::Temp->new;
2023-03-17 21:53:33 +03:00
my $log = _build_logger(file => $file->filename, level => $level);
2019-02-21 22:30:49 +03:00
$log->$level('message');
undef $log;
my $content = _slurp($file);
ok $content;
}
};
subtest 'prints to file synced' => sub {
my $file = File::Temp->new;
my $log = _build_logger(file => $file->filename, synced => 1);
2023-03-17 21:53:33 +03:00
for my $level (qw/error warn info debug trace /) {
$log->set_level($level);
2019-02-21 22:30:49 +03:00
$log->$level('message');
my $content = _slurp($file);
ok $content;
}
};
subtest 'prints to stderr with \n' => sub {
2023-03-17 21:53:33 +03:00
for my $level (qw/error warn info debug trace/) {
2019-02-21 22:30:49 +03:00
my $file = File::Temp->new;
2023-03-17 21:53:33 +03:00
my $log = _build_logger(file => $file->filename, level => $level);
2019-02-21 22:30:49 +03:00
$log->$level('message');
undef $log;
my $content = _slurp($file);
like $content, qr/\n$/;
}
};
2019-03-19 14:57:41 +03:00
subtest 'prints sprintf formatted line' => sub {
2023-03-17 21:53:33 +03:00
for my $level (qw/error warn info debug trace/) {
2019-03-19 14:57:41 +03:00
my $file = File::Temp->new;
2023-03-17 21:53:33 +03:00
my $log = _build_logger(file => $file->filename, level => $level);
2019-03-19 14:57:41 +03:00
$log->$level('message %s', 'formatted');
undef $log;
my $content = _slurp($file);
like $content,
qr/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d.\d{3} \[$level\] message formatted$/;
}
};
2019-02-21 22:30:49 +03:00
sub _slurp {
my $file = shift;
my $content = do { local $/; open my $fh, '<', $file->filename or die $!; <$fh> };
return $content;
}
sub _build_logger {
2023-03-17 18:29:38 +03:00
my $logger = Log::Mini::Logger::FILE->new(@_);
2023-03-17 21:53:33 +03:00
2019-02-21 22:30:49 +03:00
return $logger;
}
done_testing;