interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / shellA.test

362 lines · 11350 bytes Raw Blame History
1# 2025-02-24
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11# TESTRUNNER: shell
12#
13# Test cases for the command-line shell - focusing on .mode and
14# especially control-character escaping and the --escape option.
15#
16#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20set CLI [test_cli_invocation]
21
22do_execsql_test shellA-1.0 {
23 CREATE TABLE t1(a INT, x TEXT);
24 INSERT INTO t1 VALUES
25 (1, 'line with '' single quote'),
26 (2, concat(char(0x1b),'[31mVT-100 codes',char(0x1b),'[0m')),
27 (3, NULL),
28 (4, 1234),
29 (5, 568.25),
30 (6, unistr('new\u000aline')),
31 (7, unistr('carriage\u000dreturn')),
32 (8, 'last line');
33} {}
34
35# Initial verification that the database created correctly
36# and that our calls to the CLI are working.
37#
38do_test_with_ansi_output shellA-1.2 {
39 exec {*}$CLI -noinit test.db {.mode box -quote off --escape symbol} {SELECT * FROM t1;}
40} {
41╭───┬──────────────────────────╮
42│ a │ x │
43╞═══╪══════════════════════════╡
44│ 1 │ line with ' single quote │
45├───┼──────────────────────────┤
46│ 2 │ ␛[31mVT-100 codes␛[0m │
47├───┼──────────────────────────┤
48│ 3 │ │
49├───┼──────────────────────────┤
50│ 4 │ 1234 │
51├───┼──────────────────────────┤
52│ 5 │ 568.25 │
53├───┼──────────────────────────┤
54│ 6 │ new │
55│ │ line │
56├───┼──────────────────────────┤
57│ 7 │ carriage␍return │
58├───┼──────────────────────────┤
59│ 8 │ last line │
60╰───┴──────────────────────────╯
61}
62
63# ".mode list"
64#
65do_test shellA-1.3 {
66 exec {*}$CLI -noinit -list test.db {SELECT x FROM t1 WHERE a=2;}
67} {
68^[[31mVT-100 codes^[[0m
69}
70do_test_with_ansi_output shellA-1.4 {
71 exec {*}$CLI -noinit -list test.db --escape symbol {SELECT x FROM t1 WHERE a=2;}
72} {
73␛[31mVT-100 codes␛[0m
74}
75do_test shellA-1.5 {
76 exec {*}$CLI -noinit -list test.db --escape ascii {SELECT x FROM t1 WHERE a=2;}
77} {
78^[[31mVT-100 codes^[[0m
79}
80do_test_with_ansi_output shellA-1.6 {
81 exec {*}$CLI -noinit test.db {.mode list --escape symbol} {SELECT x FROM t1 WHERE a=2;}
82} {
83␛[31mVT-100 codes␛[0m
84}
85do_test shellA-1.7 {
86 exec {*}$CLI -noinit test.db {.mode list --escape ascii} {SELECT x FROM t1 WHERE a=2;}
87} {
88^[[31mVT-100 codes^[[0m
89}
90do_test shellA-1.8 {
91 file delete -force out.txt
92 exec {*}$CLI -noinit test.db {.mode list --escape off} {SELECT x FROM t1 WHERE a=7;} \
93 >out.txt
94 set fd [open out.txt rb]
95 set res [read $fd]
96 close $fd
97 string trim $res
98} "carriage\rreturn"
99do_test shellA-1.9 {
100 set rc [catch {
101 exec {*}$CLI -noinit test.db {.mode nosuchmode --escape xyz}
102 } msg]
103 lappend rc $msg
104} {1 {argv[3]: .mode nosuchmode --escape xyz
105argv[3]: ^--- unknown mode
106argv[3]: Use ".help .mode" for more info}}
107do_test shellA-1.10 {
108 set rc [catch {
109 exec {*}$CLI --noinit --escape abc test.db .q
110 } msg]
111 lappend rc $msg
112} {1 {unknown control character escape mode "abc" - choices: auto off ascii symbol}}
113
114# ".mode quote"
115#
116do_test shellA-2.1 {
117 exec {*}$CLI -noinit test.db --quote {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
118} {
1191,'line with '' single quote'
1202,unistr('\u001b[31mVT-100 codes\u001b[0m')
1216,unistr('new\u000aline')
1227,unistr('carriage\u000dreturn')
1238,'last line'
124}
125do_test shellA-2.2 {
126 exec {*}$CLI -noinit test.db --quote {.mode -v}
127} {/*.mode quote* --escape auto*/}
128do_test shellA-2.3 {
129 exec {*}$CLI -noinit test.db --quote --escape SYMBOL {.mode}
130} {.mode quote --escape symbol}
131do_test shellA-2.4 {
132 exec {*}$CLI -noinit test.db --quote --escape OFF {.mode}
133} {.mode quote --escape off}
134
135
136# ".mode line"
137#
138do_test_with_ansi_output shellA-3.1 {
139 exec {*}$CLI -noinit test.db --line --escape symbol \
140 {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
141} {
142 a: 1
143 x: line with ' single quote
144
145 a: 2
146 x: ␛[31mVT-100 codes␛[0m
147
148 a: 6
149 x: new
150 line
151
152 a: 7
153 x: carriage␍return
154
155 a: 8
156 x: last line
157}
158do_test shellA-3.2 {
159 exec {*}$CLI -noinit test.db --line --escape ascii \
160 {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
161} {
162 a: 1
163 x: line with ' single quote
164
165 a: 2
166 x: ^[[31mVT-100 codes^[[0m
167
168 a: 6
169 x: new
170 line
171
172 a: 7
173 x: carriage^Mreturn
174
175 a: 8
176 x: last line
177}
178
179# ".mode box"
180#
181do_test_with_ansi_output shellA-4.1 {
182 exec {*}$CLI -noinit test.db --box --escape ascii \
183 {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
184} {
185╭───┬──────────────────────────╮
186│ a │ x │
187╞═══╪══════════════════════════╡
188│ 1 │ line with ' single quote │
189├───┼──────────────────────────┤
190│ 2 │ ^[[31mVT-100 codes^[[0m │
191├───┼──────────────────────────┤
192│ 6 │ new │
193│ │ line │
194├───┼──────────────────────────┤
195│ 7 │ carriage^Mreturn │
196├───┼──────────────────────────┤
197│ 8 │ last line │
198╰───┴──────────────────────────╯
199}
200do_test_with_ansi_output shellA-4.1b {
201 exec {*}$CLI -noinit test.db --box --escape ascii \
202 {.mode -border off} \
203 {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
204} {
205 a │ x
206═══╪══════════════════════════
207 1 │ line with ' single quote
208───┼──────────────────────────
209 2 │ ^[[31mVT-100 codes^[[0m
210───┼──────────────────────────
211 6 │ new
212 │ line
213───┼──────────────────────────
214 7 │ carriage^Mreturn
215───┼──────────────────────────
216 8 │ last line
217}
218do_test_with_ansi_output shellA-4.2 {
219 exec {*}$CLI -noinit test.db {.mode qbox} {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
220} {
221╭───┬───────────────────────────────────────────╮
222│ a │ x │
223╞═══╪═══════════════════════════════════════════╡
224│ 1 │ 'line with '' single quote' │
225│ 2 │ unistr('\u001b[31mVT-100 codes\u001b[0m') │
226│ 6 │ unistr('new\u000aline') │
227│ 7 │ unistr('carriage\u000dreturn') │
228│ 8 │ 'last line' │
229╰───┴───────────────────────────────────────────╯
230}
231do_test_with_ansi_output shellA-4.2b {
232 exec {*}$CLI -noinit test.db {.mode qbox -border off} \
233 {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
234} {
235 a │ x
236═══╪═══════════════════════════════════════════
237 1 │ 'line with '' single quote'
238 2 │ unistr('\u001b[31mVT-100 codes\u001b[0m')
239 6 │ unistr('new\u000aline')
240 7 │ unistr('carriage\u000dreturn')
241 8 │ 'last line'
242}
243
244# ".mode insert"
245#
246do_test shellA-5.1 {
247 exec {*}$CLI -noinit test.db {.mode insert t1 --escape ascii} \
248 {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
249} {
250INSERT INTO t1 VALUES(1,'line with '' single quote');
251INSERT INTO t1 VALUES(2,unistr('\u001b[31mVT-100 codes\u001b[0m'));
252INSERT INTO t1 VALUES(6,unistr('new\u000aline'));
253INSERT INTO t1 VALUES(7,unistr('carriage\u000dreturn'));
254INSERT INTO t1 VALUES(8,'last line');
255}
256do_test shellA-5.2 {
257 exec {*}$CLI -noinit test.db {.mode insert t1 --escape symbol} \
258 {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
259} {
260INSERT INTO t1 VALUES(1,'line with '' single quote');
261INSERT INTO t1 VALUES(2,unistr('\u001b[31mVT-100 codes\u001b[0m'));
262INSERT INTO t1 VALUES(6,unistr('new\u000aline'));
263INSERT INTO t1 VALUES(7,unistr('carriage\u000dreturn'));
264INSERT INTO t1 VALUES(8,'last line');
265}
266do_test shellA-5.3 {
267 file delete -force out.txt
268 exec {*}$CLI -noinit test.db {.mode insert t1 --escape off} \
269 {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} >out.txt
270 set fd [open out.txt rb]
271 set res [read $fd]
272 close $fd
273 string trim [string map [list \r\n \n] $res]
274} "
275INSERT INTO t1 VALUES(1,'line with '' single quote');
276INSERT INTO t1 VALUES(2,'\033\13331mVT-100 codes\033\1330m');
277INSERT INTO t1 VALUES(6,'new
278line');
279INSERT INTO t1 VALUES(7,'carriage\rreturn');
280INSERT INTO t1 VALUES(8,'last line');
281"
282
283# ".mode split"
284#
285do_test shellA-6.1 {
286 db eval {
287 CREATE TABLE t2(x);
288 INSERT INTO t2(x) VALUES
289 ('one'), ('two'), ('three'), ('four'), ('five'),
290 ('six'), ('seven'), ('eight'), ('nine'), ('ten'),
291 ('eleven'), ('twelve'), ('thirteen'), ('fourteen');
292 }
293 exec {*}$CLI -noinit test.db \
294 {.print} \
295 {.mode split -screenwidth 30} \
296 {SELECT x FROM t2}
297} {
298one five nine thirteen
299two six ten fourteen
300three seven eleven
301four eight twelve}
302# 3456789 123456789 123456789
303
304do_test shellA-6.2 {
305 exec {*}$CLI -noinit test.db \
306 {.print} \
307 {.mode split -screenwidth 30} \
308 {SELECT x FROM t2} \
309 {.mode column -titles off} \
310 {SELECT x FROM t2}
311} {
312one five nine thirteen
313two six ten fourteen
314three seven eleven
315four eight twelve
316one
317two
318three
319four
320five
321six
322seven
323eight
324nine
325ten
326eleven
327twelve
328thirteen
329fourteen}
330
331do_test shellA-6.3 {
332 exec {*}$CLI -noinit test.db \
333 {.print} \
334 {.mode table} \
335 {.mode --once split -screenwidth 30} \
336 {SELECT x FROM t2} \
337 {SELECT x FROM t2}
338} {
339one five nine thirteen
340two six ten fourteen
341three seven eleven
342four eight twelve
343+----------+
344| x |
345+----------+
346| one |
347| two |
348| three |
349| four |
350| five |
351| six |
352| seven |
353| eight |
354| nine |
355| ten |
356| eleven |
357| twelve |
358| thirteen |
359| fourteen |
360+----------+}
361
362finish_test