tests: Factor out duplicate log capture code

This commit is contained in:
Richard Hansen 2025-01-10 17:02:40 -05:00
parent d18b1cdb27
commit 009033d476
4 changed files with 24 additions and 36 deletions

View file

@ -160,5 +160,6 @@ EXTRA_DIST += $(handwritten_tests) \
t/lib/ddclient/Test/Fake/HTTPD/other-ca-cert.pem \
t/lib/ddclient/t.pm \
t/lib/ddclient/t/HTTPD.pm \
t/lib/ddclient/t/Logger.pm \
t/lib/ddclient/t/ip.pm \
t/lib/ok.pm

View file

@ -0,0 +1,19 @@
package ddclient::t::Logger;
BEGIN { eval { require 'ddclient'; } or BAIL_OUT($@); }
use parent qw(-norequire ddclient::Logger);
sub new {
my ($class, $parent) = @_;
my $self = $class->SUPER::new(undef, $parent);
$self->{logs} = [];
return $self;
}
sub _log {
my ($self, $args) = @_;
push(@{$self->{logs}}, $args)
if ($args->{label} // '') =~ qr/^(?:WARNING|FATAL|SUCCESS|FAILED)$/;
return $self->SUPER::_log($args);
}
1;

View file

@ -3,6 +3,7 @@ BEGIN { SKIP: { eval { require Test::Warnings; 1; } or skip($@, 1); } }
BEGIN { eval { require JSON::PP; 1; } or plan(skip_all => $@); JSON::PP->import(); }
BEGIN { eval { require 'ddclient'; } or BAIL_OUT($@); }
use ddclient::t::HTTPD;
use ddclient::t::Logger;
httpd_required();
@ -29,23 +30,6 @@ httpd()->run(sub {
return [400, $headers, ['unexpected request: ' . $req->uri()]]
});
{
package Logger;
use parent qw(-norequire ddclient::Logger);
sub new {
my ($class, $parent) = @_;
my $self = $class->SUPER::new(undef, $parent);
$self->{logs} = [];
return $self;
}
sub _log {
my ($self, $args) = @_;
push(@{$self->{logs}}, $args)
if ($args->{label} // '') =~ qr/^(?:WARNING|FATAL|SUCCESS|FAILED)$/;
return $self->SUPER::_log($args);
}
}
my $hostname = httpd()->endpoint();
my @test_cases = (
{
@ -149,7 +133,7 @@ for my $tc (@test_cases) {
diag('==============================================================================');
local $ddclient::globals{debug} = 1;
local $ddclient::globals{verbose} = 1;
my $l = Logger->new($ddclient::_l);
my $l = ddclient::t::Logger->new($ddclient::_l);
local %ddclient::config = %{$tc->{cfg}};
local %ddclient::recap;
{

View file

@ -3,6 +3,7 @@ BEGIN { SKIP: { eval { require Test::Warnings; 1; } or skip($@, 1); } }
use MIME::Base64;
BEGIN { eval { require 'ddclient'; } or BAIL_OUT($@); }
use ddclient::t::HTTPD;
use ddclient::t::Logger;
httpd_required();
@ -18,23 +19,6 @@ httpd()->run(sub {
return undef;
});
{
package Logger;
use parent qw(-norequire ddclient::Logger);
sub new {
my ($class, $parent) = @_;
my $self = $class->SUPER::new(undef, $parent);
$self->{logs} = [];
return $self;
}
sub _log {
my ($self, $args) = @_;
push(@{$self->{logs}}, $args)
if ($args->{label} // '') =~ qr/^(?:WARNING|FATAL|SUCCESS|FAILED)$/;
return $self->SUPER::_log($args);
}
}
my @test_cases = (
{
desc => 'IPv4, single host, good',
@ -246,7 +230,7 @@ for my $tc (@test_cases) {
diag('==============================================================================');
local $ddclient::globals{debug} = 1;
local $ddclient::globals{verbose} = 1;
my $l = Logger->new($ddclient::_l);
my $l = ddclient::t::Logger->new($ddclient::_l);
local %ddclient::config;
local %ddclient::recap;
$ddclient::config{$_} = {