group_hosts_by: Use Data::Dumper to make the group signature

This is a bit more robust than manually making the group signature
because it gracefully handles corner cases such as `undef`.
This commit is contained in:
Richard Hansen 2024-05-28 02:58:32 -04:00
parent f4802fc534
commit e60e6e804b
3 changed files with 7 additions and 9 deletions

View file

@ -49,6 +49,7 @@ AC_SUBST([PERL])
# package doesn't depend on all of them, so their availability can't
# be assumed.
m4_foreach_w([_m], [
Data::Dumper
File::Basename
File::Path
File::Temp
@ -63,7 +64,6 @@ m4_foreach_w([_m], [
# then some tests will fail. Only prints a warning if not installed.
m4_foreach_w([_m], [
B
Data::Dumper
File::Spec::Functions
File::Temp
], [AX_PROG_PERL_MODULES([_m], [],

View file

@ -15,6 +15,7 @@ package ddclient;
require v5.10.1;
use strict;
use warnings;
use Data::Dumper;
use File::Basename;
use File::Path qw(make_path);
use File::Temp;
@ -3425,8 +3426,10 @@ sub group_hosts_by {
my %attrs = map({ ($_ => 1) } @$attributes);
my @attrs = sort(keys(%attrs));
my %groups = ();
my $d = Data::Dumper->new([])->Indent(0)->Sortkeys(1)->Terse(1)->Useqq(1);
for my $h (@$hosts) {
my $sig = join(',', map({ sprintf("%s=%s", $_, $config{$h}{$_} // '') } @attrs));
my %cfg = map({ ($_ => $config{$h}{$_}); } grep(exists($config{$h}{$_}), @attrs));
my $sig = $d->Reset()->Values([\%cfg])->Dump();
push @{$groups{$sig}}, $h;
}
return %groups;

View file

@ -60,13 +60,11 @@ my @test_cases = (
desc => 'falsy values',
groupby => [qw(falsy)],
want => [[$h1], [$h2], [$h3]],
todo => 'support for undef not yet added',
},
{
desc => 'set, unset, undef',
groupby => [qw(maybeunset)],
want => [[$h1], [$h2], [$h3]],
todo => 'support for unset and undef not yet added',
},
{
desc => 'missing attribute',
@ -85,11 +83,8 @@ for my $tc (@test_cases) {
}
return @$a <=> @$b;
} map({ [sort(@$_)]; } values(%got)));
TODO: {
local $TODO = $tc->{todo};
is_deeply(\@got, $tc->{want}, $tc->{desc})
or diag(Data::Dumper->Dump([\@got, $tc->{want}], [qw(got want)]));
}
is_deeply(\@got, $tc->{want}, $tc->{desc})
or diag(Data::Dumper->Dump([\@got, $tc->{want}], [qw(got want)]));
}
done_testing();