Improve some psql test code

Split psql_like() into two functions psql_like() and psql_fails_like()
and make them mirror the existing command_like() and
command_fails_like() more closely.  In particular, follow the
universal convention that the test name is the last argument.

Discussion: https://www.postgresql.org/message-id/3199e176-424e-1bef-f180-c1548466c2da@enterprisedb.com
This commit is contained in:
Peter Eisentraut 2022-02-22 13:42:38 +01:00
parent 667726fbe5
commit fbee60f6a4
1 changed files with 29 additions and 30 deletions

View File

@ -12,40 +12,36 @@ program_help_ok('psql');
program_version_ok('psql');
program_options_handling_ok('psql');
my ($stdout, $stderr);
my $result;
# Execute a psql command and check its result patterns.
# Execute a psql command and check its output.
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;
my ($node, $sql, $expected_stdout, $test_name) = @_;
die "cannot specify both expected stdout and stderr here"
if (defined($expected_stdout) && defined($expected_stderr));
my ($ret, $stdout, $stderr) = $node->psql('postgres', $sql);
is($ret, 0, "$test_name: exit code 0");
is($stderr, '', "$test_name: no stderr");
like($stdout, $expected_stdout, "$test_name: matches");
return;
}
# Execute a psql command and check that it fails and check the stderr.
sub psql_fails_like
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($node, $sql, $expected_stderr, $test_name) = @_;
# 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,
'postgres', $sql,
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");
}
isnt($ret, 0, "$test_name: exit code not 0");
like($stderr, $expected_stderr, "$test_name: matches");
return;
}
@ -53,6 +49,9 @@ sub psql_like
# test --help=foo, analogous to program_help_ok()
foreach my $arg (qw(commands variables))
{
my ($stdout, $stderr);
my $result;
$result = IPC::Run::run [ 'psql', "--help=$arg" ], '>', \$stdout, '2>',
\$stderr;
ok($result, "psql --help=$arg exit code 0");
@ -70,15 +69,15 @@ max_wal_senders = 4
});
$node->start;
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);
psql_like($node, '\copyright', qr/Copyright/, '\copyright');
psql_like($node, '\help', qr/ALTER/, '\help without arguments');
psql_like($node, '\help SELECT', qr/SELECT/, '\help with argument');
# Test clean handling of unsupported replication command responses
psql_like(
psql_fails_like(
$node,
'handling of unexpected PQresultStatus',
'START_REPLICATION 0/0',
undef, qr/unexpected PQresultStatus: 8$/);
qr/unexpected PQresultStatus: 8$/,
'handling of unexpected PQresultStatus');
done_testing();