tests: Option to select which log messages to capture

This commit is contained in:
Richard Hansen 2025-01-10 17:04:38 -05:00
parent 009033d476
commit 115f23dead
3 changed files with 25 additions and 5 deletions

View file

@ -2,18 +2,38 @@ package ddclient::t::Logger;
BEGIN { eval { require 'ddclient'; } or BAIL_OUT($@); } BEGIN { eval { require 'ddclient'; } or BAIL_OUT($@); }
use parent qw(-norequire ddclient::Logger); use parent qw(-norequire ddclient::Logger);
{
package ddclient::t::LoggerAbort;
use overload '""' => qw(stringify);
sub new {
my ($class, %args) = @_;
return bless(\%args, $class);
}
sub stringify {
return 'logged a FATAL message';
}
}
sub new { sub new {
my ($class, $parent) = @_; my ($class, $parent, $labelre) = @_;
my $self = $class->SUPER::new(undef, $parent); my $self = $class->SUPER::new(undef, $parent);
$self->{logs} = []; $self->{logs} = [];
$self->{_labelre} = $labelre;
return $self; return $self;
} }
sub _log { sub _log {
my ($self, $args) = @_; my ($self, $args) = @_;
push(@{$self->{logs}}, $args) my $lre = $self->{_labelre};
if ($args->{label} // '') =~ qr/^(?:WARNING|FATAL|SUCCESS|FAILED)$/; my $lbl = $args->{label};
push(@{$self->{logs}}, $args) if !defined($lre) || (defined($lbl) && $lbl =~ $lre);
return $self->SUPER::_log($args); return $self->SUPER::_log($args);
} }
sub _abort {
my ($self) = @_;
push(@{$self->{logs}}, 'aborted');
die(ddclient::t::LoggerAbort->new());
}
1; 1;

View file

@ -133,7 +133,7 @@ for my $tc (@test_cases) {
diag('=============================================================================='); diag('==============================================================================');
local $ddclient::globals{debug} = 1; local $ddclient::globals{debug} = 1;
local $ddclient::globals{verbose} = 1; local $ddclient::globals{verbose} = 1;
my $l = ddclient::t::Logger->new($ddclient::_l); my $l = ddclient::t::Logger->new($ddclient::_l, qr/^(?:WARNING|FATAL|SUCCESS|FAILED)$/);
local %ddclient::config = %{$tc->{cfg}}; local %ddclient::config = %{$tc->{cfg}};
local %ddclient::recap; local %ddclient::recap;
{ {

View file

@ -230,7 +230,7 @@ for my $tc (@test_cases) {
diag('=============================================================================='); diag('==============================================================================');
local $ddclient::globals{debug} = 1; local $ddclient::globals{debug} = 1;
local $ddclient::globals{verbose} = 1; local $ddclient::globals{verbose} = 1;
my $l = ddclient::t::Logger->new($ddclient::_l); my $l = ddclient::t::Logger->new($ddclient::_l, qr/^(?:WARNING|FATAL|SUCCESS|FAILED)$/);
local %ddclient::config; local %ddclient::config;
local %ddclient::recap; local %ddclient::recap;
$ddclient::config{$_} = { $ddclient::config{$_} = {