AryaWu/sqlite
0
1# 2025-02-242#3# The author disclaims copyright to this source code. In place of4# 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: shell12#13# Test cases for the command-line shell - focusing on .mode and14# especially control-character escaping and the --escape option.15#16#17 18set testdir [file dirname $argv0]19source $testdir/tester.tcl20set CLI [test_cli_invocation]21 22do_execsql_test shellA-1.0 {23 CREATE TABLE t1(a INT, x TEXT);24 INSERT INTO t1 VALUES25 (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 correctly36# 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^[[0m69}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␛[0m74}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^[[0m79}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␛[0m84}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^[[0m89}90do_test shellA-1.8 {91 file delete -force out.txt92 exec {*}$CLI -noinit test.db {.mode list --escape off} {SELECT x FROM t1 WHERE a=7;} \93 >out.txt94 set fd [open out.txt rb]95 set res [read $fd]96 close $fd97 string trim $res98} "carriage\rreturn"99do_test shellA-1.9 {100 set rc [catch {101 exec {*}$CLI -noinit test.db {.mode test --escape xyz}102 } msg]103 lappend rc $msg104} {1 {argv[3]: .mode test --escape xyz105argv[3]: ^--- unknown mode106argv[3]: Use ".help .mode" for more info}}107do_test shellA-1.10 {108 set rc [catch {109 exec {*}$CLI --noinit --escape abc test.db .q110 } msg]111 lappend rc $msg112} {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: 1143 x: line with ' single quote144 145 a: 2146 x: ␛[31mVT-100 codes␛[0m147 148 a: 6149 x: new150 line151 152 a: 7153 x: carriage␍return154 155 a: 8156 x: last line157}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: 1163 x: line with ' single quote164 165 a: 2166 x: ^[[31mVT-100 codes^[[0m167 168 a: 6169 x: new170 line171 172 a: 7173 x: carriage^Mreturn174 175 a: 8176 x: last line177}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 │ x206═══╪══════════════════════════207 1 │ line with ' single quote208───┼──────────────────────────209 2 │ ^[[31mVT-100 codes^[[0m210───┼──────────────────────────211 6 │ new212 │ line213───┼──────────────────────────214 7 │ carriage^Mreturn215───┼──────────────────────────216 8 │ last line217}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 │ x236═══╪═══════════════════════════════════════════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.txt268 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.txt270 set fd [open out.txt rb]271 set res [read $fd]272 close $fd273 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,'new278line');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) VALUES289 ('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 thirteen299two six ten fourteen300three seven eleven301four eight twelve}302# 3456789 123456789 123456789303 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 thirteen313two six ten fourteen314three seven eleven315four eight twelve316one317two318three319four320five321six322seven323eight324nine325ten326eleven327twelve328thirteen329fourteen}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 thirteen340two six ten fourteen341three seven eleven342four eight twelve343+----------+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_test363 