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:
parent
f4802fc534
commit
e60e6e804b
3 changed files with 7 additions and 9 deletions
|
@ -49,6 +49,7 @@ AC_SUBST([PERL])
|
||||||
# package doesn't depend on all of them, so their availability can't
|
# package doesn't depend on all of them, so their availability can't
|
||||||
# be assumed.
|
# be assumed.
|
||||||
m4_foreach_w([_m], [
|
m4_foreach_w([_m], [
|
||||||
|
Data::Dumper
|
||||||
File::Basename
|
File::Basename
|
||||||
File::Path
|
File::Path
|
||||||
File::Temp
|
File::Temp
|
||||||
|
@ -63,7 +64,6 @@ m4_foreach_w([_m], [
|
||||||
# then some tests will fail. Only prints a warning if not installed.
|
# then some tests will fail. Only prints a warning if not installed.
|
||||||
m4_foreach_w([_m], [
|
m4_foreach_w([_m], [
|
||||||
B
|
B
|
||||||
Data::Dumper
|
|
||||||
File::Spec::Functions
|
File::Spec::Functions
|
||||||
File::Temp
|
File::Temp
|
||||||
], [AX_PROG_PERL_MODULES([_m], [],
|
], [AX_PROG_PERL_MODULES([_m], [],
|
||||||
|
|
|
@ -15,6 +15,7 @@ package ddclient;
|
||||||
require v5.10.1;
|
require v5.10.1;
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
use Data::Dumper;
|
||||||
use File::Basename;
|
use File::Basename;
|
||||||
use File::Path qw(make_path);
|
use File::Path qw(make_path);
|
||||||
use File::Temp;
|
use File::Temp;
|
||||||
|
@ -3425,8 +3426,10 @@ sub group_hosts_by {
|
||||||
my %attrs = map({ ($_ => 1) } @$attributes);
|
my %attrs = map({ ($_ => 1) } @$attributes);
|
||||||
my @attrs = sort(keys(%attrs));
|
my @attrs = sort(keys(%attrs));
|
||||||
my %groups = ();
|
my %groups = ();
|
||||||
|
my $d = Data::Dumper->new([])->Indent(0)->Sortkeys(1)->Terse(1)->Useqq(1);
|
||||||
for my $h (@$hosts) {
|
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;
|
push @{$groups{$sig}}, $h;
|
||||||
}
|
}
|
||||||
return %groups;
|
return %groups;
|
||||||
|
|
|
@ -60,13 +60,11 @@ my @test_cases = (
|
||||||
desc => 'falsy values',
|
desc => 'falsy values',
|
||||||
groupby => [qw(falsy)],
|
groupby => [qw(falsy)],
|
||||||
want => [[$h1], [$h2], [$h3]],
|
want => [[$h1], [$h2], [$h3]],
|
||||||
todo => 'support for undef not yet added',
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc => 'set, unset, undef',
|
desc => 'set, unset, undef',
|
||||||
groupby => [qw(maybeunset)],
|
groupby => [qw(maybeunset)],
|
||||||
want => [[$h1], [$h2], [$h3]],
|
want => [[$h1], [$h2], [$h3]],
|
||||||
todo => 'support for unset and undef not yet added',
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc => 'missing attribute',
|
desc => 'missing attribute',
|
||||||
|
@ -85,11 +83,8 @@ for my $tc (@test_cases) {
|
||||||
}
|
}
|
||||||
return @$a <=> @$b;
|
return @$a <=> @$b;
|
||||||
} map({ [sort(@$_)]; } values(%got)));
|
} map({ [sort(@$_)]; } values(%got)));
|
||||||
TODO: {
|
|
||||||
local $TODO = $tc->{todo};
|
|
||||||
is_deeply(\@got, $tc->{want}, $tc->{desc})
|
is_deeply(\@got, $tc->{want}, $tc->{desc})
|
||||||
or diag(Data::Dumper->Dump([\@got, $tc->{want}], [qw(got want)]));
|
or diag(Data::Dumper->Dump([\@got, $tc->{want}], [qw(got want)]));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
done_testing();
|
done_testing();
|
||||||
|
|
Loading…
Reference in a new issue