Debian Codegraph @

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

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 use strict;
5 use warnings FATAL => 'all';
6
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10 use Data::Dumper;
11
12 # Do nothing unless Makefile has told us that the build is --with-readline.
13 if (!defined($ENV{with_readline}) || $ENV{with_readline} ne 'yes')
14 {
15 plan skip_all => 'readline is not supported by this build';
16 }
17
18 # Also, skip if user has set environment variable to command that.
19 # This is mainly intended to allow working around some of the more broken
20 # versions of libedit --- some users might find them acceptable even if
21 # they won't pass these tests.
22 if (defined($ENV{SKIP_READLINE_TESTS}))
23 {
24 plan skip_all => 'SKIP_READLINE_TESTS is set';
25 }
26
27 # If we don't have IO::Pty, forget it, because IPC::Run depends on that
28 # to support pty connections
29 eval { require IO::Pty; };
30 if ($@)
31 {
32 plan skip_all => 'IO::Pty is needed to run this test';
33 }
34
35 # start a new server
36 my $node = PostgreSQL::Test::Cluster->new('main');
37 $node->init;
38 $node->start;
39
40 # set up a few database objects
41 $node->safe_psql('postgres',
42 "CREATE TABLE tab1 (c1 int primary key constraint foo not null, c2 text);\n"
43 . "CREATE TABLE mytab123 (f1 int, f2 text);\n"
44 . "CREATE TABLE mytab246 (f1 int, f2 text);\n"
45 . "CREATE TABLE \"mixedName\" (f1 int, f2 text);\n"
46 . "CREATE TYPE enum1 AS ENUM ('foo', 'bar', 'baz', 'BLACK');\n"
47 . "CREATE PUBLICATION some_publication;\n");
48
49 # In a VPATH build, we'll be started in the source directory, but we want
50 # to run in the build directory so that we can use relative paths to
51 # access the tab_comp_dir subdirectory; otherwise the output from filename
52 # completion tests is too variable.
53 if ($ENV{TESTDATADIR})
54 {
55 chdir $ENV{TESTDATADIR}
56 or die "could not chdir to \"$ENV{TESTDATADIR}\": $!";
57 }
58
59 # Create some junk files for filename completion testing.
60 mkdir "tab_comp_dir";
61 my $FH;
62 open $FH, ">", "tab_comp_dir/somefile"
63 or die("could not create file \"tab_comp_dir/somefile\": $!");
64 print $FH "some stuff\n";
65 close $FH;
66 open $FH, ">", "tab_comp_dir/afile123"
67 or die("could not create file \"tab_comp_dir/afile123\": $!");
68 print $FH "more stuff\n";
69 close $FH;
70 open $FH, ">", "tab_comp_dir/afile456"
71 or die("could not create file \"tab_comp_dir/afile456\": $!");
72 print $FH "other stuff\n";
73 close $FH;
74
75 # Arrange to capture, not discard, the interactive session's history output.
76 # Put it in the test log directory, so that buildfarm runs capture the result
77 # for possible debugging purposes.
78 my $historyfile = "${PostgreSQL::Test::Utils::log_path}/010_psql_history.txt";
79
80 # fire up an interactive psql session
81 my $h = $node->interactive_psql('postgres', history_file => $historyfile);
82
83 # Simple test case: type something and see if psql responds as expected
84 sub check_completion
85 {
86 my ($send, $pattern, $annotation) = @_;
87
88 # report test failures from caller location
89 local $Test::Builder::Level = $Test::Builder::Level + 1;
90
91 # restart per-command timer
92 $h->{timeout}->start($PostgreSQL::Test::Utils::timeout_default);
93
94 # send the data to be sent and wait for its result
95 my $out = $h->query_until($pattern, $send);
96 my $okay = ($out =~ $pattern && !$h->{timeout}->is_expired);
97 ok($okay, $annotation);
98 # for debugging, log actual output if it didn't match
99 local $Data::Dumper::Terse = 1;
100 local $Data::Dumper::Useqq = 1;
101 diag 'Actual output was ' . Dumper($out) . "Did not match \"$pattern\"\n"
102 if !$okay;
103 return;
104 }
105
106 # Clear query buffer to start over
107 # (won't work if we are inside a string literal!)
108 sub clear_query
109 {
110 local $Test::Builder::Level = $Test::Builder::Level + 1;
111
112 check_completion("\\r\n", qr/Query buffer reset.*postgres=# /s,
113 "\\r works");
114 return;
115 }
116
117 # Clear current line to start over
118 # (this will work in an incomplete string literal, but it's less desirable
119 # than clear_query because we lose evidence in the history file)
120 sub clear_line
121 {
122 local $Test::Builder::Level = $Test::Builder::Level + 1;
123
124 check_completion("\025\n", qr/postgres=# /, "control-U works");
125 return;
126 }
127
128 # check basic command completion: SEL<tab> produces SELECT<space>
129 check_completion("SEL\t", qr/SELECT /, "complete SEL<tab> to SELECT");
130
131 clear_query();
132
133 # check case variation is honored
134 check_completion("sel\t", qr/select /, "complete sel<tab> to select");
135
136 # check basic table name completion
137 check_completion("* from t\t", qr/\* from tab1 /, "complete t<tab> to tab1");
138
139 clear_query();
140
141 # check table name completion with multiple alternatives
142 # note: readline might print a bell before the completion
143 check_completion(
144 "select * from my\t",
145 qr/select \* from my\a?tab/,
146 "complete my<tab> to mytab when there are multiple choices");
147
148 # some versions of readline/libedit require two tabs here, some only need one
149 check_completion(
150 "\t\t",
151 qr/mytab123 +mytab246/,
152 "offer multiple table choices");
153
154 check_completion("2\t", qr/246 /,
155 "finish completion of one of multiple table choices");
156
157 clear_query();
158
159 # check handling of quoted names
160 check_completion(
161 "select * from \"my\t",
162 qr/select \* from "my\a?tab/,
163 "complete \"my<tab> to \"mytab when there are multiple choices");
164
165 check_completion(
166 "\t\t",
167 qr/"mytab123" +"mytab246"/,
168 "offer multiple quoted table choices");
169
170 check_completion("2\t", qr/246" /,
171 "finish completion of one of multiple quoted table choices");
172
173 clear_query();
174
175 # check handling of mixed-case names
176 check_completion(
177 "select * from \"mi\t",
178 qr/"mixedName" /,
179 "complete a mixed-case name");
180
181 clear_query();
182
183 # check case folding
184 check_completion("select * from TAB\t", qr/tab1 /, "automatically fold case");
185
186 clear_query();
187
188 # check case-sensitive keyword replacement
189 # note: various versions of readline/libedit handle backspacing
190 # differently, so just check that the replacement comes out correctly
191 check_completion("\\DRD\t", qr/drds /, "complete \\DRD<tab> to \\drds");
192
193 clear_query();
194
195 # check completion of a schema-qualified name
196 check_completion("select * from pub\t",
197 qr/public\./, "complete schema when relevant");
198
199 check_completion("tab\t", qr/tab1 /, "complete schema-qualified name");
200
201 clear_query();
202
203 check_completion(
204 "select * from PUBLIC.t\t",
205 qr/public\.tab1 /,
206 "automatically fold case in schema-qualified name");
207
208 clear_query();
209
210 # check interpretation of referenced names
211 check_completion(
212 "alter table tab1 drop constraint t\t",
213 qr/tab1_pkey /,
214 "complete index name for referenced table");
215
216 clear_query();
217
218 check_completion(
219 "alter table TAB1 drop constraint t\t",
220 qr/tab1_pkey /,
221 "complete index name for referenced table, with downcasing");
222
223 clear_query();
224
225 check_completion(
226 "alter table public.\"tab1\" drop constraint t\t",
227 qr/tab1_pkey /,
228 "complete index name for referenced table, with schema and quoting");
229
230 clear_query();
231
232 # check variant where we're completing a qualified name from a refname
233 # (this one also checks successful completion in a multiline command)
234 check_completion(
235 "comment on constraint tab1_pkey \n on public.\t",
236 qr/public\.tab1/,
237 "complete qualified name from object reference");
238
239 clear_query();
240
241 # check filename completion
242 check_completion(
243 "\\lo_import tab_comp_dir/some\t",
244 qr|tab_comp_dir/somefile |,
245 "filename completion with one possibility");
246
247 clear_query();
248
249 # note: readline might print a bell before the completion
250 check_completion(
251 "\\lo_import tab_comp_dir/af\t",
252 qr|tab_comp_dir/af\a?ile|,
253 "filename completion with multiple possibilities");
254
255 # here we are inside a string literal 'afile*', so must use clear_line().
256 clear_line();
257
258 # COPY requires quoting
259 check_completion(
260 "COPY foo FROM tab_comp_dir/some\t",
261 qr|'tab_comp_dir/somefile' |,
262 "quoted filename completion with one possibility");
263
264 clear_query();
265
266 check_completion(
267 "COPY foo FROM tab_comp_dir/af\t",
268 qr|'tab_comp_dir/afile|,
269 "quoted filename completion with multiple possibilities");
270
271 # some versions of readline/libedit require two tabs here, some only need one
272 # also, some will offer the whole path name and some just the file name
273 # the quotes might appear, too
274 check_completion(
275 "\t\t",
276 qr|afile123'? +'?(tab_comp_dir/)?afile456|,
277 "offer multiple file choices");
278
279 clear_line();
280
281 # check enum label completion
282 # some versions of readline/libedit require two tabs here, some only need one
283 # also, some versions will offer quotes, some will not
284 check_completion(
285 "ALTER TYPE enum1 RENAME VALUE 'ba\t\t",
286 qr|'?bar'? +'?baz'?|,
287 "offer multiple enum choices");
288
289 clear_line();
290
291 # enum labels are case sensitive, so this should complete BLACK immediately
292 check_completion(
293 "ALTER TYPE enum1 RENAME VALUE 'B\t",
294 qr|BLACK|,
295 "enum labels are case sensitive");
296
297 clear_line();
298
299 # check timezone name completion
300 check_completion("SET timezone TO am\t",
301 qr|'America/|, "offer partial timezone name");
302
303 check_completion("new_\t", qr|New_York|, "complete partial timezone name");
304
305 clear_line();
306
307 # check completion of a keyword offered in addition to object names;
308 # such a keyword should obey COMP_KEYWORD_CASE
309 foreach (
310 [ 'lower', 'CO', 'column' ],
311 [ 'upper', 'co', 'COLUMN' ],
312 [ 'preserve-lower', 'co', 'column' ],
313 [ 'preserve-upper', 'CO', 'COLUMN' ],)
314 {
315 my ($case, $in, $out) = @$_;
316
317 check_completion(
318 "\\set COMP_KEYWORD_CASE $case\n",
319 qr/postgres=#/,
320 "set completion case to '$case'");
321 check_completion("alter table tab1 rename $in\t\t\t",
322 qr|$out|,
323 "offer keyword $out for input $in<TAB>, COMP_KEYWORD_CASE = $case");
324 clear_query();
325 }
326
327 # alternate path where keyword comes from SchemaQuery
328 check_completion(
329 "DROP TYPE big\t",
330 qr/DROP TYPE bigint /,
331 "offer keyword from SchemaQuery");
332
333 clear_query();
334
335 # check create_command_generator
336 check_completion(
337 "CREATE TY\t",
338 qr/CREATE TYPE /,
339 "check create_command_generator");
340
341 clear_query();
342
343 # check words_after_create infrastructure
344 check_completion(
345 "CREATE TABLE mytab\t\t",
346 qr/mytab123 +mytab246/,
347 "check words_after_create");
348
349 clear_query();
350
351 # check VersionedQuery infrastructure
352 check_completion(
353 "DROP PUBLIC\t \t\t",
354 qr/DROP PUBLICATION\s+some_publication /,
355 "check VersionedQuery");
356
357 clear_query();
358
359 # hits ends_with() and logic for completing in multi-line queries
360 check_completion("analyze (\n\t\t", qr/VERBOSE/,
361 "check ANALYZE (VERBOSE ...");
362
363 clear_query();
364
365 # check completions for GUCs
366 check_completion(
367 "set interval\t\t",
368 qr/intervalstyle TO/,
369 "complete a GUC name");
370 check_completion(" iso\t", qr/iso_8601 /, "complete a GUC enum value");
371
372 clear_query();
373
374 # same, for qualified GUC names
375 check_completion(
376 "DO \$\$begin end\$\$ LANGUAGE plpgsql;\n",
377 qr/postgres=# /,
378 "load plpgsql extension");
379
380 check_completion("set plpg\t", qr/plpg\a?sql\./,
381 "complete prefix of a GUC name");
382 check_completion(
383 "var\t\t",
384 qr/variable_conflict TO/,
385 "complete a qualified GUC name");
386 check_completion(" USE_C\t",
387 qr/use_column/, "complete a qualified GUC enum value");
388
389 clear_query();
390
391 # check completions for psql variables
392 check_completion("\\set VERB\t", qr/VERBOSITY /,
393 "complete a psql variable name");
394 check_completion("def\t", qr/default /, "complete a psql variable value");
395
396 clear_query();
397
398 check_completion(
399 "\\echo :VERB\t",
400 qr/:VERBOSITY /,
401 "complete an interpolated psql variable name");
402
403 clear_query();
404
405 # check completion for psql variable test
406 check_completion(
407 "\\echo :{?VERB\t",
408 qr/:\{\?VERBOSITY} /,
409 "complete a psql variable test");
410
411 clear_query();
412
413 # check no-completions code path
414 check_completion("blarg \t\t", qr//, "check completion failure path");
415
416 clear_query();
417
418 # check COPY FROM with DEFAULT option
419 check_completion(
420 "COPY foo FROM stdin WITH ( DEF\t)",
421 qr/DEFAULT /,
422 "COPY FROM with DEFAULT completion");
423
424 clear_line();
425
426 # send psql an explicit \q to shut it down, else pty won't close properly
427 $h->quit or die "psql returned $?";
428
429 # done
430 $node->stop;
431 done_testing();