Improve psql tab-completion tests.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 2 Feb 2022 21:08:59 +0000 (16:08 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 2 Feb 2022 21:09:03 +0000 (16:09 -0500)
Fix up recently-added test cases in 010_tab_completion.pl
so that they pass with the rather seriously broken libedit
found in Debian 10 (Buster).

Also, add a few more test cases to improve code coverage.
The total line coverage still looks pretty awful, because
we exercise only a few paths of the giant if-else chain in
psql_completion().  However, this now covers almost all of
the code that isn't in one of those if-blocks.

Discussion: https://postgr.es/m/960764.1643751011@sss.pgh.pa.us

src/bin/psql/t/010_tab_completion.pl

index 7a265e067633917f9da4c4ead152f12216a9f9c8..569dd442e8ad0acf0d6f91acc6469b519cd977e9 100644 (file)
@@ -44,7 +44,8 @@ $node->safe_psql('postgres',
      . "CREATE TABLE mytab123 (f1 int, f2 text);\n"
      . "CREATE TABLE mytab246 (f1 int, f2 text);\n"
      . "CREATE TABLE \"mixedName\" (f1 int, f2 text);\n"
-     . "CREATE TYPE enum1 AS ENUM ('foo', 'bar', 'baz', 'BLACK');\n");
+     . "CREATE TYPE enum1 AS ENUM ('foo', 'bar', 'baz', 'BLACK');\n"
+     . "CREATE PUBLICATION some_publication;\n");
 
 # Developers would not appreciate this test adding a bunch of junk to
 # their ~/.psql_history, so be sure to redirect history into a temp file.
@@ -131,7 +132,8 @@ sub clear_query
 {
    local $Test::Builder::Level = $Test::Builder::Level + 1;
 
-   check_completion("\\r\n", qr/postgres=# /, "\\r works");
+   check_completion("\\r\n", qr/Query buffer reset.*postgres=# /s,
+       "\\r works");
    return;
 }
 
@@ -188,18 +190,26 @@ check_completion(
    qr/"mytab123" +"mytab246"/,
    "offer multiple quoted table choices");
 
-check_completion("2\t", qr/246" /,
+# note: broken versions of libedit want to backslash the closing quote;
+# not much we can do about that
+check_completion("2\t", qr/246\\?" /,
    "finish completion of one of multiple quoted table choices");
 
-clear_query();
+# note: broken versions of libedit may leave us in a state where psql
+# thinks there's an unclosed double quote, so that we have to use
+# clear_line not clear_query here
+clear_line();
 
 # check handling of mixed-case names
+# note: broken versions of libedit want to backslash the closing quote;
+# not much we can do about that
 check_completion(
    "select * from \"mi\t",
-   qr/"mixedName"/,
+   qr/"mixedName\\?" /,
    "complete a mixed-case name");
 
-clear_query();
+# as above, must use clear_line not clear_query here
+clear_line();
 
 # check case folding
 check_completion(
@@ -214,7 +224,8 @@ clear_query();
 # differently, so just check that the replacement comes out correctly
 check_completion("\\DRD\t", qr/drds /, "complete \\DRD<tab> to \\drds");
 
-clear_query();
+# broken versions of libedit require clear_line not clear_query here
+clear_line();
 
 # check completion of a schema-qualified name
 check_completion(
@@ -258,6 +269,15 @@ check_completion(
 
 clear_query();
 
+# check variant where we're completing a qualified name from a refname
+# (this one also checks successful completion in a multiline command)
+check_completion(
+   "comment on constraint tab1_pkey \n on public.\t",
+   qr/public\.tab1/,
+   "complete qualified name from object reference");
+
+clear_query();
+
 # check filename completion
 check_completion(
    "\\lo_import tmp_check/some\t",
@@ -272,7 +292,8 @@ check_completion(
    qr|tmp_check/af\a?ile|,
    "filename completion with multiple possibilities");
 
-clear_query();
+# broken versions of libedit require clear_line not clear_query here
+clear_line();
 
 # COPY requires quoting
 # note: broken versions of libedit want to backslash the closing quote;
@@ -342,6 +363,72 @@ foreach (
    clear_query();
 }
 
+# alternate path where keyword comes from SchemaQuery
+check_completion(
+   "DROP TYPE big\t",
+   qr/DROP TYPE bigint /,
+   "offer keyword from SchemaQuery");
+
+clear_query();
+
+# check create_command_generator
+check_completion(
+   "CREATE TY\t",
+   qr/CREATE TYPE /,
+   "check create_command_generator");
+
+clear_query();
+
+# check words_after_create infrastructure
+check_completion(
+   "CREATE TABLE mytab\t\t",
+   qr/mytab123 +mytab246/,
+   "check words_after_create");
+
+clear_query();
+
+# check VersionedQuery infrastructure
+check_completion(
+   "DROP PUBLIC\t \t\t",
+   qr/DROP PUBLICATION\s+some_publication /,
+   "check VersionedQuery");
+
+clear_query();
+
+# hits ends_with() and logic for completing in multi-line queries
+check_completion("analyze (\n\t\t", qr/VERBOSE/,
+   "check ANALYZE (VERBOSE ...");
+
+clear_query();
+
+# check completions for GUCs
+check_completion(
+   "set interval\t\t",
+   qr/intervalstyle TO/,
+   "complete a GUC name");
+check_completion(" iso\t", qr/iso_8601 /, "complete a GUC enum value");
+
+clear_query();
+
+# check completions for psql variables
+check_completion("\\set VERB\t", qr/VERBOSITY /,
+   "complete a psql variable name");
+check_completion("def\t", qr/default /, "complete a psql variable value");
+
+clear_query();
+
+check_completion(
+   "\\echo :VERB\t",
+   qr/:VERBOSITY /,
+   "complete an interpolated psql variable name");
+
+clear_query();
+
+# check no-completions code path
+check_completion("blarg \t\t", qr//, "check completion failure path");
+
+clear_query();
+
 # send psql an explicit \q to shut it down, else pty won't close properly
 $timer->start(5);
 $in .= "\\q\n";