Make PostgreSQL::Test::Cluster::config_data more flexible
authorAndrew Dunstan <andrew@dunslane.net>
Sun, 13 Nov 2022 13:45:14 +0000 (08:45 -0500)
committerAndrew Dunstan <andrew@dunslane.net>
Sun, 13 Nov 2022 14:00:38 +0000 (09:00 -0500)
Currently this only allows for one argument, which must be present, and
always returns a single string. With this change the following now all
work:

  $all_config = $node->config_data;
  %config_map = ($node->config_data);
  $incdir = $node->config_data('--include-dir');
  ($incdir, $sharedir) = $node->config_data(
      qw(--include-dir --share-dir));

Backpatch to release 15 where this was introduced.

Discussion: https://postgr.es/m/73eea68e-3b6f-5f63-6024-25ed26b52016@dunslane.net

Reviewed by Tom Lane, Alvaro Herrera, Michael Paquier.

src/test/perl/PostgreSQL/Test/Cluster.pm

index d1017b746fc81ab736dc95d189e11edb14cb92cc..c7531c5efb477cb77d09d786fc48a53f9f850ced 100644 (file)
@@ -26,6 +26,14 @@ PostgreSQL::Test::Cluster - class representing PostgreSQL server instance
   # Modify or delete an existing setting
   $node->adjust_conf('postgresql.conf', 'max_wal_senders', '10');
 
+  # get pg_config settings
+  # all the settings in one string
+  $pgconfig = $node->config_data;
+  # all the settings as a map
+  %config_map = ($node->config_data);
+  # specified settings
+  ($incdir, $sharedir) = $node->config_data(qw(--includedir --sharedir));
+
   # run a query with psql, like:
   #   echo 'SELECT 1' | psql -qAXt postgres -v ON_ERROR_STOP=1
   $psql_stdout = $node->safe_psql('postgres', 'SELECT 1');
@@ -345,27 +353,46 @@ sub pg_version
 
 =pod
 
-=item $node->config_data($option)
+=item $node->config_data( option ...)
+
+Return configuration data from pg_config, using options (if supplied).
+The options will be things like '--sharedir'.
 
-Return a string holding configuration data from pg_config, with $option
-being the option switch used with the pg_config command.
+If no options are supplied, return a string in scalar context or a map in
+array context.
+
+If options are supplied, return the list of values.
 
 =cut
 
 sub config_data
 {
-   my ($self, $option) = @_;
+   my ($self, @options) = @_;
    local %ENV = $self->_get_env();
 
    my ($stdout, $stderr);
    my $result =
-     IPC::Run::run [ $self->installed_command('pg_config'), $option ],
+     IPC::Run::run [ $self->installed_command('pg_config'), @options ],
      '>', \$stdout, '2>', \$stderr
      or die "could not execute pg_config";
+   # standardize line endings
+   $stdout =~ s/\r(?=\n)//g;
+   # no options, scalar context: just hand back the output
+   return $stdout unless (wantarray || @options);
    chomp($stdout);
-   $stdout =~ s/\r$//;
-
-   return $stdout;
+   # exactly one option: hand back the output (minus LF)
+   return $stdout if (@options == 1);
+   my @lines = split(/\n/, $stdout);
+   # more than one option: hand back the list of values;
+   return @lines if (@options);
+   # no options, array context: return a map
+   my @map;
+   foreach my $line (@lines)
+   {
+       my ($k,$v) = split (/ = /,$line,2);
+       push(@map, $k, $v);
+   }
+   return @map;
 }
 
 =pod