Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 384f1ab

Browse files
committed
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
1 parent 40dfac4 commit 384f1ab

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/bin/psql/t/001_basic.pl

+45-7
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,46 @@
1515
my ($stdout, $stderr);
1616
my $result;
1717

18+
# Execute a psql command and check its result patterns.
19+
sub psql_like
20+
{
21+
local $Test::Builder::Level = $Test::Builder::Level + 1;
22+
23+
my $node = shift;
24+
my $test_name = shift;
25+
my $query = shift;
26+
my $expected_stdout = shift;
27+
my $expected_stderr = shift;
28+
29+
die "cannot specify both expected stdout and stderr here"
30+
if (defined($expected_stdout) && defined($expected_stderr));
31+
32+
# Use the context of a WAL sender, some of the tests rely on that.
33+
my ($ret, $stdout, $stderr) = $node->psql(
34+
'postgres', $query,
35+
on_error_die => 0,
36+
replication => 'database');
37+
38+
if (defined($expected_stdout))
39+
{
40+
is($ret, 0, "$test_name: expected result code");
41+
is($stderr, '', "$test_name: no stderr");
42+
like($stdout, $expected_stdout, "$test_name: stdout matches");
43+
}
44+
if (defined($expected_stderr))
45+
{
46+
isnt($ret, 0, "$test_name: expected result code");
47+
like($stderr, $expected_stderr, "$test_name: stderr matches");
48+
}
49+
50+
return;
51+
}
52+
1853
# test --help=foo, analogous to program_help_ok()
1954
foreach my $arg (qw(commands variables))
2055
{
21-
$result = IPC::Run::run [ 'psql', "--help=$arg" ], '>', \$stdout, '2>', \$stderr;
56+
$result = IPC::Run::run [ 'psql', "--help=$arg" ], '>', \$stdout, '2>',
57+
\$stderr;
2258
ok($result, "psql --help=$arg exit code 0");
2359
isnt($stdout, '', "psql --help=$arg goes to stdout");
2460
is($stderr, '', "psql --help=$arg nothing to stderr");
@@ -34,11 +70,13 @@
3470
});
3571
$node->start;
3672

37-
$node->command_like([ 'psql', '-c', '\copyright' ], qr/Copyright/, '\copyright');
38-
$node->command_like([ 'psql', '-c', '\help' ], qr/ALTER/, '\help without arguments');
39-
$node->command_like([ 'psql', '-c', '\help SELECT' ], qr/SELECT/, '\help');
40-
73+
psql_like($node, '\copyright', '\copyright', qr/Copyright/, undef);
74+
psql_like($node, '\help without arguments', '\help', qr/ALTER/, undef);
75+
psql_like($node, '\help with argument', '\help SELECT', qr/SELECT/, undef);
4176

4277
# Test clean handling of unsupported replication command responses
43-
$node->command_fails_like([ 'psql', '-d', 'replication=database', '-c', 'START_REPLICATION 0/0' ],
44-
qr/^unexpected PQresultStatus: 8$/, 'handling of unexpected PQresultStatus');
78+
psql_like(
79+
$node,
80+
'handling of unexpected PQresultStatus',
81+
'START_REPLICATION 0/0',
82+
undef, qr/unexpected PQresultStatus: 8$/);

0 commit comments

Comments
 (0)