Fix portability issues in new TAP tests of psql

The tests added by c0280bc and d9ddc50 in 001_basic.pl have introduced
commands calling directly psql, making them sensitive to the
environment.  One issue was that those commands forgot -X to not use a
local .psqlrc, causing all those tests to fail if psql cannot properly
parse this file.

TAP tests should be designed so as they run in an isolated fashion,
without any dependency on the environment where they are run.  As
PostgresNode::psql gives already all the facilities those new tests
need, switch to that instead of calling plain psql commands where
interactions with a backend are needed.  The test is slightly refactored
to be able to check after the expected patterns of stdout and stderr,
keeping the same amount of coverage as previously.

Reported-by: Peter Geoghegan
Discussion: https://postgr.es/m/CAH2-Wzn8ftvcDPwomn+y04JJzbT=TG7TN=QsmSEATUOW-ZuvQQ@mail.gmail.com
This commit is contained in:
Michael Paquier 2021-10-18 09:51:21 +09:00
parent 40dfac4fc4
commit 384f1abdb9
1 changed files with 45 additions and 7 deletions

View File

@ -15,10 +15,46 @@ program_options_handling_ok('psql');
my ($stdout, $stderr);
my $result;
# Execute a psql command and check its result patterns.
sub psql_like
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $node = shift;
my $test_name = shift;
my $query = shift;
my $expected_stdout = shift;
my $expected_stderr = shift;
die "cannot specify both expected stdout and stderr here"
if (defined($expected_stdout) && defined($expected_stderr));
# Use the context of a WAL sender, some of the tests rely on that.
my ($ret, $stdout, $stderr) = $node->psql(
'postgres', $query,
on_error_die => 0,
replication => 'database');
if (defined($expected_stdout))
{
is($ret, 0, "$test_name: expected result code");
is($stderr, '', "$test_name: no stderr");
like($stdout, $expected_stdout, "$test_name: stdout matches");
}
if (defined($expected_stderr))
{
isnt($ret, 0, "$test_name: expected result code");
like($stderr, $expected_stderr, "$test_name: stderr matches");
}
return;
}
# test --help=foo, analogous to program_help_ok()
foreach my $arg (qw(commands variables))
{
$result = IPC::Run::run [ 'psql', "--help=$arg" ], '>', \$stdout, '2>', \$stderr;
$result = IPC::Run::run [ 'psql', "--help=$arg" ], '>', \$stdout, '2>',
\$stderr;
ok($result, "psql --help=$arg exit code 0");
isnt($stdout, '', "psql --help=$arg goes to stdout");
is($stderr, '', "psql --help=$arg nothing to stderr");
@ -34,11 +70,13 @@ max_wal_senders = 4
});
$node->start;
$node->command_like([ 'psql', '-c', '\copyright' ], qr/Copyright/, '\copyright');
$node->command_like([ 'psql', '-c', '\help' ], qr/ALTER/, '\help without arguments');
$node->command_like([ 'psql', '-c', '\help SELECT' ], qr/SELECT/, '\help');
psql_like($node, '\copyright', '\copyright', qr/Copyright/, undef);
psql_like($node, '\help without arguments', '\help', qr/ALTER/, undef);
psql_like($node, '\help with argument', '\help SELECT', qr/SELECT/, undef);
# Test clean handling of unsupported replication command responses
$node->command_fails_like([ 'psql', '-d', 'replication=database', '-c', 'START_REPLICATION 0/0' ],
qr/^unexpected PQresultStatus: 8$/, 'handling of unexpected PQresultStatus');
psql_like(
$node,
'handling of unexpected PQresultStatus',
'START_REPLICATION 0/0',
undef, qr/unexpected PQresultStatus: 8$/);