group_hosts_by: Treat undef as unset for consistency with opt

The `opt` function falls back to global/default if the value is
undefined, even if it is explicitly set to `undef`.  `group_hosts_by`
should behave the same.
This commit is contained in:
Richard Hansen 2024-08-18 01:14:51 -04:00
parent 5a66efe79e
commit 912bc6291a
2 changed files with 7 additions and 4 deletions

View file

@ -3381,7 +3381,7 @@ sub group_hosts_by {
my %groups;
my %cfgs;
for my $h (@$hosts) {
my %cfg = map({ ($_ => $config{$h}{$_}); } grep(exists($config{$h}{$_}), @attrs));
my %cfg = map({ ($_ => $config{$h}{$_}); } grep(defined($config{$h}{$_}), @attrs));
my $sig = repr(\%cfg, Indent => 0);
push(@{$groups{$sig}}, $h);
$cfgs{$sig} = \%cfg;

View file

@ -72,7 +72,9 @@ my @test_cases = (
want => [
{cfg => {falsy => 0}, hosts => [$h1]},
{cfg => {falsy => ''}, hosts => [$h2]},
{cfg => {falsy => undef}, hosts => [$h3]},
# undef intentionally becomes unset because undef always means "fall back to global or
# default".
{cfg => {}, hosts => [$h3]},
],
},
{
@ -80,8 +82,9 @@ my @test_cases = (
groupby => [qw(maybeunset)],
want => [
{cfg => {maybeunset => 'unique'}, hosts => [$h1]},
{cfg => {maybeunset => undef}, hosts => [$h2]},
{cfg => {}, hosts => [$h3]},
# undef intentionally becomes unset because undef always means "fall back to global or
# default".
{cfg => {}, hosts => [$h2, $h3]},
],
},
{