Debian Codegraph @

test/shellA.test

raw
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
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
20 set CLI [test_cli_invocation]
21
22 do_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 #
38 do_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 #
65 do_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 }
70 do_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 }
75 do_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 }
80 do_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 }
85 do_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 }
90 do_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"
99 do_test shellA-1.9 {
100 set rc [catch {
101 exec {*}$CLI -noinit test.db {.mode test --escape xyz}
102 } msg]
103 lappend rc $msg
104 } {1 {argv[3]: .mode test --escape xyz
105 argv[3]: ^--- unknown mode
106 argv[3]: Use ".help .mode" for more info}}
107 do_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 #
116 do_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 } {
119 1,'line with '' single quote'
120 2,unistr('\u001b[31mVT-100 codes\u001b[0m')
121 6,unistr('new\u000aline')
122 7,unistr('carriage\u000dreturn')
123 8,'last line'
124 }
125 do_test shellA-2.2 {
126 exec {*}$CLI -noinit test.db --quote {.mode -v}
127 } {/*.mode quote* --escape auto*/}
128 do_test shellA-2.3 {
129 exec {*}$CLI -noinit test.db --quote --escape SYMBOL {.mode}
130 } {.mode quote --escape symbol}
131 do_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 #
138 do_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 }
158 do_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 #
181 do_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 }
200 do_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 }
218 do_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 }
231 do_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 #
246 do_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 } {
250 INSERT INTO t1 VALUES(1,'line with '' single quote');
251 INSERT INTO t1 VALUES(2,unistr('\u001b[31mVT-100 codes\u001b[0m'));
252 INSERT INTO t1 VALUES(6,unistr('new\u000aline'));
253 INSERT INTO t1 VALUES(7,unistr('carriage\u000dreturn'));
254 INSERT INTO t1 VALUES(8,'last line');
255 }
256 do_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 } {
260 INSERT INTO t1 VALUES(1,'line with '' single quote');
261 INSERT INTO t1 VALUES(2,unistr('\u001b[31mVT-100 codes\u001b[0m'));
262 INSERT INTO t1 VALUES(6,unistr('new\u000aline'));
263 INSERT INTO t1 VALUES(7,unistr('carriage\u000dreturn'));
264 INSERT INTO t1 VALUES(8,'last line');
265 }
266 do_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 } "
275 INSERT INTO t1 VALUES(1,'line with '' single quote');
276 INSERT INTO t1 VALUES(2,'\033\13331mVT-100 codes\033\1330m');
277 INSERT INTO t1 VALUES(6,'new
278 line');
279 INSERT INTO t1 VALUES(7,'carriage\rreturn');
280 INSERT INTO t1 VALUES(8,'last line');
281 "
282
283 # ".mode split"
284 #
285 do_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 } {
298 one five nine thirteen
299 two six ten fourteen
300 three seven eleven
301 four eight twelve}
302 # 3456789 123456789 123456789
303
304 do_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 } {
312 one five nine thirteen
313 two six ten fourteen
314 three seven eleven
315 four eight twelve
316 one
317 two
318 three
319 four
320 five
321 six
322 seven
323 eight
324 nine
325 ten
326 eleven
327 twelve
328 thirteen
329 fourteen}
330
331 do_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 } {
339 one five nine thirteen
340 two six ten fourteen
341 three seven eleven
342 four 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
362 finish_test