From 009033d4765aecdc7629d8f478651da42a2ff5de Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 10 Jan 2025 17:02:40 -0500 Subject: [PATCH] tests: Factor out duplicate log capture code --- Makefile.am | 1 + t/lib/ddclient/t/Logger.pm | 19 +++++++++++++++++++ t/protocol_directnic.pl | 20 ++------------------ t/protocol_dyndns2.pl | 20 ++------------------ 4 files changed, 24 insertions(+), 36 deletions(-) create mode 100644 t/lib/ddclient/t/Logger.pm diff --git a/Makefile.am b/Makefile.am index 6a0f32a..fcb38a4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/t/lib/ddclient/t/Logger.pm b/t/lib/ddclient/t/Logger.pm new file mode 100644 index 0000000..f4a1f30 --- /dev/null +++ b/t/lib/ddclient/t/Logger.pm @@ -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; diff --git a/t/protocol_directnic.pl b/t/protocol_directnic.pl index 92b245a..967e182 100644 --- a/t/protocol_directnic.pl +++ b/t/protocol_directnic.pl @@ -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; { diff --git a/t/protocol_dyndns2.pl b/t/protocol_dyndns2.pl index b3130e0..749eddc 100644 --- a/t/protocol_dyndns2.pl +++ b/t/protocol_dyndns2.pl @@ -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{$_} = {