mirror of
https://github.com/postgres/postgres.git
synced 2025-08-19 23:22:23 +03:00
Add basic TAP tests for psql's tab-completion logic.
Up to now, psql's tab-complete.c has had exactly no regression test coverage. This patch is an experimental attempt to add some. This needs Perl's IO::Pty module, which isn't installed everywhere, so the test script just skips all tests if that's not present. There may be other portability gotchas too, so I await buildfarm results with interest. So far this just covers a few very basic keyword-completion and query-driven-completion scenarios, which should be enough to let us get a feel for whether this is practical at all from a portability standpoint. If it is, there's lots more that can be done. Discussion: https://postgr.es/m/10967.1577562752@sss.pgh.pa.us
This commit is contained in:
@@ -1534,6 +1534,73 @@ sub psql
|
||||
|
||||
=pod
|
||||
|
||||
=item $node->interactive_psql($dbname, \$stdin, \$stdout, $timer, %params) => harness
|
||||
|
||||
Invoke B<psql> on B<$dbname> and return an IPC::Run harness object,
|
||||
which the caller may use to send interactive input to B<psql>.
|
||||
The process's stdin is sourced from the $stdin scalar reference,
|
||||
and its stdout and stderr go to the $stdout scalar reference.
|
||||
ptys are used so that psql thinks it's being called interactively.
|
||||
|
||||
The specified timer object is attached to the harness, as well.
|
||||
It's caller's responsibility to select the timeout length, and to
|
||||
restart the timer after each command if the timeout is per-command.
|
||||
|
||||
psql is invoked in tuples-only unaligned mode with reading of B<.psqlrc>
|
||||
disabled. That may be overridden by passing extra psql parameters.
|
||||
|
||||
Dies on failure to invoke psql, or if psql fails to connect.
|
||||
Errors occurring later are the caller's problem.
|
||||
|
||||
Be sure to "finish" the harness when done with it.
|
||||
|
||||
The only extra parameter currently accepted is
|
||||
|
||||
=over
|
||||
|
||||
=item extra_params => ['--single-transaction']
|
||||
|
||||
If given, it must be an array reference containing additional parameters to B<psql>.
|
||||
|
||||
=back
|
||||
|
||||
This requires IO::Pty in addition to IPC::Run.
|
||||
|
||||
=cut
|
||||
|
||||
sub interactive_psql
|
||||
{
|
||||
my ($self, $dbname, $stdin, $stdout, $timer, %params) = @_;
|
||||
|
||||
my @psql_params = ('psql', '-XAt', '-d', $self->connstr($dbname));
|
||||
|
||||
push @psql_params, @{ $params{extra_params} }
|
||||
if defined $params{extra_params};
|
||||
|
||||
# Ensure there is no data waiting to be sent:
|
||||
$$stdin = "" if ref($stdin);
|
||||
# IPC::Run would otherwise append to existing contents:
|
||||
$$stdout = "" if ref($stdout);
|
||||
|
||||
my $harness = IPC::Run::start \@psql_params,
|
||||
'<pty<', $stdin, '>pty>', $stdout, $timer;
|
||||
|
||||
# Pump until we see psql's help banner. This ensures that callers
|
||||
# won't write anything to the pty before it's ready, avoiding an
|
||||
# implementation issue in IPC::Run. Also, it means that psql
|
||||
# connection failures are caught here, relieving callers of
|
||||
# the need to handle those. (Right now, we have no particularly
|
||||
# good handling for errors anyway, but that might be added later.)
|
||||
pump $harness
|
||||
until $$stdout =~ /Type "help" for help/ || $timer->is_expired;
|
||||
|
||||
die "psql startup timed out" if $timer->is_expired;
|
||||
|
||||
return $harness;
|
||||
}
|
||||
|
||||
=pod
|
||||
|
||||
=item $node->poll_query_until($dbname, $query [, $expected ])
|
||||
|
||||
Run B<$query> repeatedly, until it returns the B<$expected> result
|
||||
|
Reference in New Issue
Block a user