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

From ilmari@ilmari.org (Dagfinn Ilmari Mannsåker)
Subject Re: pgsql: Add basic TAP tests for psql's tab-completion logic.
Date
Msg-id 87h81a10qo.fsf@wibble.ilmari.org
Whole thread Raw
In response to Re: pgsql: Add basic TAP tests for psql's tab-completion logic.  (ilmari@ilmari.org (Dagfinn Ilmari Mannsåker))
List pgsql-hackers
ilmari@ilmari.org (Dagfinn Ilmari Mannsåker) writes:

> Tom Lane <tgl@sss.pgh.pa.us> writes:
>
>> Cool, I'll go commit a fix along those lines.  Thanks for tracing
>> this down!
>
> Here's one final style cleanup for the TAP test.
>
> - use like() for the banner test
> - pass the regexes around as qr// objects, so they can be
>   syntax-highlighted properly, and don't need regex
>   metacharacter-escaping backslashes doubled.
> - include the regex that didn't match in the diagnostic

This time with the actual attachment...

- ilmari
-- 
- Twitter seems more influential [than blogs] in the 'gets reported in
  the mainstream press' sense at least.               - Matt McLeod
- That'd be because the content of a tweet is easier to condense down
  to a mainstream media article.                      - Calle Dybedahl
From 0307bdea0f95e47e9ed7cf9678c12d568006d772 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= <ilmari@ilmari.org>
Date: Sun, 5 Jan 2020 13:20:10 +0000
Subject: [PATCH] Use qr// for passed-in regexes in tab-completion TAP test

This lets editors syntax-highlight them as regexes, not just plain
strings, and avoids having to double backslashes when escaping regex
metacharacters like *.

Also include the pattern that didn't match in the failure diagnostic,
and use like() for the startup banner test.
---
 src/bin/psql/t/010_tab_completion.pl | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl
index 9cfd7ec79c..bff954de24 100644
--- a/src/bin/psql/t/010_tab_completion.pl
+++ b/src/bin/psql/t/010_tab_completion.pl
@@ -58,7 +58,7 @@ my $timer = timer(5);
 
 my $h = $node->interactive_psql('postgres', \$in, \$out, $timer);
 
-ok($out =~ /psql/, "print startup banner");
+like($out, qr/psql/, "print startup banner");
 
 # Simple test case: type something and see if psql responds as expected
 sub check_completion
@@ -75,13 +75,14 @@ sub check_completion
     # send the data to be sent
     $in .= $send;
     # wait ...
-    pump $h until ($out =~ m/$pattern/ || $timer->is_expired);
-    my $okay = ($out =~ m/$pattern/ && !$timer->is_expired);
+    pump $h until ($out =~ $pattern || $timer->is_expired);
+    my $okay = ($out =~ $pattern && !$timer->is_expired);
     ok($okay, $annotation);
     # for debugging, log actual output if it didn't match
     local $Data::Dumper::Terse = 1;
     local $Data::Dumper::Useqq = 1;
-    diag 'Actual output was ' . Dumper($out) . "\n" if !$okay;
+    diag 'Actual output was ' . Dumper($out) .
+        "Did not match $pattern\n" if !$okay;
     return;
 }
 
@@ -89,20 +90,20 @@ sub check_completion
 # (won't work if we are inside a string literal!)
 sub clear_query
 {
-    check_completion("\\r\n", "postgres=# ", "\\r works");
+    check_completion("\\r\n", qr/postgres=# /, "\\r works");
     return;
 }
 
 # check basic command completion: SEL<tab> produces SELECT<space>
-check_completion("SEL\t", "SELECT ", "complete SEL<tab> to SELECT");
+check_completion("SEL\t", qr/SELECT /, "complete SEL<tab> to SELECT");
 
 clear_query();
 
 # check case variation is honored
-check_completion("sel\t", "select ", "complete sel<tab> to select");
+check_completion("sel\t", qr/select /, "complete sel<tab> to select");
 
 # check basic table name completion
-check_completion("* from t\t", "\\* from tab1 ", "complete t<tab> to tab1");
+check_completion("* from t\t", qr/\* from tab1 /, "complete t<tab> to tab1");
 
 clear_query();
 
@@ -110,14 +111,14 @@ clear_query();
 # note: readline might print a bell before the completion
 check_completion(
     "select * from my\t",
-    "select \\* from my\a?tab",
+    qr/select \* from my\a?tab/,
     "complete my<tab> to mytab when there are multiple choices");
 
 # some versions of readline/libedit require two tabs here, some only need one
-check_completion("\t\t", "mytab123 +mytab246",
+check_completion("\t\t", qr/mytab123 +mytab246/,
     "offer multiple table choices");
 
-check_completion("2\t", "246 ",
+check_completion("2\t", qr/246 /,
     "finish completion of one of multiple table choices");
 
 clear_query();
@@ -125,7 +126,7 @@ clear_query();
 # check case-sensitive keyword replacement
 # note: various versions of readline/libedit handle backspacing
 # differently, so just check that the replacement comes out correctly
-check_completion("\\DRD\t", "drds ", "complete \\DRD<tab> to \\drds");
+check_completion("\\DRD\t", qr/\\drds /, "complete \\DRD<tab> to \\drds");
 
 clear_query();
 
-- 
2.22.0


pgsql-hackers by date:

Previous
From: ilmari@ilmari.org (Dagfinn Ilmari Mannsåker)
Date:
Subject: Re: pgsql: Add basic TAP tests for psql's tab-completion logic.
Next
From: Tomas Vondra
Date:
Subject: Re: parallel vacuum options/syntax