From 6e75c1bec73f2128b94131305e6a37b97257f7c3 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 22 Feb 2022 13:42:38 +0100 Subject: [PATCH 1/2] 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. --- src/bin/psql/t/001_basic.pl | 59 ++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/bin/psql/t/001_basic.pl b/src/bin/psql/t/001_basic.pl index ba3dd846ba..f416e0ab5e 100644 --- a/src/bin/psql/t/001_basic.pl +++ b/src/bin/psql/t/001_basic.pl @@ -12,40 +12,36 @@ 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) = @_; + + 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; - die "cannot specify both expected stdout and stderr here" - if (defined($expected_stdout) && defined($expected_stderr)); + 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 @@ sub psql_like }); $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(); -- 2.35.1