Re: pgsql: Add basic TAP tests for psql's tab-completion logic.

[email protected] (Dagfinn Ilmari Mannsåker)
Newsgroups gmane.comp.db.postgresql.devel.general
Message-ID <[email protected]>
Tom Lane <[email protected]> writes:

> Also, while I'm asking for Perl advice: I can see in my editor that
> there's a control-G bell character in that string, but this is far
> from obvious on the web page.  I'd kind of like to get the report
> to escapify control characters so that what comes out is more like
>
> 	# Actual output was "\DRD^G"
> or
> 	# Actual output was "\\DRD\007"
>
> or some such.  Anybody know an easy way to do that in Perl?

I was going to suggest using Test::More's like() function to do the
regex check, but sadly that only escapes things that would break the TAP
stream syntax, not non-printables in general.  The next obvious thing is
Data::Dumper with the 'Useqq' option enabled, which makes it use
double-quoted-string escapes (e.g. "\a" for ^G).

The attaced patch does that, and also bumps $Test::Builder::Level so the
diagnostic references the calling line, and uses diag() instad of
note(), so it shows even in non-verbose mode.

- ilmari
-- 
"A disappointingly low fraction of the human race is,
 at any given time, on fire." - Stig Sandbeck Mathisen
0001-Escape-non-printable-characters-in-psql-tab-completi.patch (text/x-diff, 1.6 KB)
From c4541abc826c40e88f729c5a71e5d06a11295aa2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= <[email protected]>
Date: Fri, 3 Jan 2020 17:07:10 +0000
Subject: [PATCH] Escape non-printable characters in psql tab-completion TAP
 tests

By using Data::Dumper in Useqq mode.

In passing, bump $Test::Builder::Level so the diagnostic references the
calling line, and use diag() instad of note(), so it shows even in
non-verbose mode.
---
 src/bin/psql/t/010_tab_completion.pl | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl
index a02cbd8e47..553288bda7 100644
--- a/src/bin/psql/t/010_tab_completion.pl
+++ b/src/bin/psql/t/010_tab_completion.pl
@@ -5,6 +5,7 @@ use PostgresNode;
 use TestLib;
 use Test::More;
 use IPC::Run qw(pump finish timer);
+use Data::Dumper;
 
 if (!defined($ENV{with_readline}) || $ENV{with_readline} ne 'yes')
 {
@@ -52,6 +53,9 @@ sub check_completion
 {
 	my ($send, $pattern, $annotation) = @_;
 
+	# report test failures from caller location
+	local $Test::Builder::Level = $Test::Builder::Level + 1;
+
 	# reset output collector
 	$out = "";
 	# restart per-command timer
@@ -63,7 +67,9 @@ sub check_completion
 	my $okay = ($out =~ m/$pattern/ && !$timer->is_expired);
 	ok($okay, $annotation);
 	# for debugging, log actual output if it didn't match
-	note 'Actual output was "' . $out . "\"\n" if !$okay;
+	local $Data::Dumper::Terse = 1;
+	local $Data::Dumper::Useqq = 1;
+	diag 'Actual output was ' . Dumper($out) . "\n" if !$okay;
 	return;
 }
 
-- 
2.22.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.