github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cli/interactive_tests/test_local_cmds.tcl (about)

     1  #! /usr/bin/env expect -f
     2  
     3  source [file join [file dirname $argv0] common.tcl]
     4  
     5  start_server $argv
     6  
     7  spawn $argv sql
     8  eexpect root@
     9  
    10  start_test "Check that times are displayed by default on interactive terminals."
    11  send "select 1;\r"
    12  eexpect "(1 row)"
    13  eexpect "Time:"
    14  eexpect root@
    15  end_test
    16  
    17  start_test "Check that \\? prints the help text."
    18  send "\\?\r"
    19  eexpect "You are using"
    20  eexpect "More documentation"
    21  eexpect root@
    22  end_test
    23  
    24  start_test "Check that \\! invokes external commands."
    25  send "\\! echo -n he; echo llo\r"
    26  eexpect "hello"
    27  eexpect "root@"
    28  end_test
    29  
    30  start_test "Check that \\q terminates the client."
    31  send "\\q\r"
    32  eexpect eof
    33  spawn $argv sql --format=tsv
    34  eexpect root@
    35  end_test
    36  
    37  start_test "Check that quit terminates the client."
    38  send "quit\r"
    39  eexpect eof
    40  spawn $argv sql --format=tsv
    41  eexpect root@
    42  end_test
    43  
    44  start_test "Check that quit does not terminate the client in the middle of a statement."
    45  send "select\rquit\r;\r"
    46  eexpect "column \"quit\" does not exist"
    47  eexpect root@
    48  end_test
    49  
    50  start_test "Check that exit terminates the client."
    51  send "exit\r"
    52  eexpect eof
    53  spawn $argv sql --format=tsv
    54  eexpect root@
    55  end_test
    56  
    57  start_test "Check that \\| reads statements."
    58  send "\\| echo 'select '; echo '38 + 4;'\r"
    59  eexpect 42
    60  eexpect root@
    61  end_test
    62  
    63  start_test "Check that \\| does not execute upon encountering an error."
    64  send "\\| echo 'create database dontcreate;'; exit 1\r"
    65  eexpect "error in external command"
    66  eexpect root@
    67  send "drop database dontcreate;\r"
    68  eexpect "database * does not exist"
    69  eexpect root@
    70  end_test
    71  
    72  start_test "Check that a buit-in command in between tokens of a statement is processed locally."
    73  send "select\r"
    74  eexpect " ->"
    75  
    76  send "\\?\r"
    77  eexpect " ->"
    78  
    79  send "1;\r"
    80  expect "Time"
    81  eexpect root@
    82  end_test
    83  
    84  start_test "Check that \\set without argument prints the current options"
    85  send "\\set\r"
    86  eexpect "display_format\ttsv"
    87  eexpect root@
    88  end_test
    89  
    90  start_test "Check that \\set display_format properly errors out"
    91  send "\\set display_format blabla\r"
    92  eexpect "invalid table display format"
    93  # check we don't see a stray "cannot change option during multi-line editing" tacked at the end
    94  eexpect "html, raw)\r\n"
    95  eexpect root@
    96  end_test
    97  
    98  start_test "Check that \\set can change the display format"
    99  send "\\set display_format csv\r\\set\r"
   100  eexpect "display_format,csv"
   101  eexpect root@
   102  
   103  send "\\set display_format=tsv\r\\set\r"
   104  eexpect "display_format\ttsv"
   105  eexpect root@
   106  end_test
   107  
   108  start_test "Check various ways to set a boolean flag."
   109  send "\\set show_times=false\r\\set\r"
   110  eexpect "show_times\tfalse"
   111  eexpect root@
   112  send "\\set show_times=true\r\\set\r"
   113  eexpect "show_times\ttrue"
   114  eexpect root@
   115  send "\\set show_times=0\r\\set\r"
   116  eexpect "show_times\tfalse"
   117  eexpect root@
   118  send "\\set show_times=1\r\\set\r"
   119  eexpect "show_times\ttrue"
   120  eexpect root@
   121  send "\\set show_times=off\r\\set\r"
   122  eexpect "show_times\tfalse"
   123  eexpect root@
   124  send "\\set show_times=on\r\\set\r"
   125  eexpect "show_times\ttrue"
   126  eexpect root@
   127  send "\\set show_times=blah\r"
   128  eexpect "invalid syntax"
   129  eexpect root@
   130  end_test
   131  
   132  
   133  start_test "Check that a built-in command in the middle of a token (eg a string) is processed locally."
   134  send "select 'hello\r"
   135  eexpect " ->"
   136  send "\\?\r"
   137  eexpect " ->"
   138  send "world' as woo;\r"
   139  eexpect "hello\r\nworld"
   140  eexpect "Time"
   141  eexpect root@
   142  end_test
   143  
   144  start_test "Check that \\set can change the display of query times"
   145  # check the override
   146  send "\\unset show_times\r\\set\r"
   147  eexpect "show_times\tfalse"
   148  eexpect root@
   149  send "select 1 as woo;\r"
   150  eexpect "woo\r\n1\r\n"
   151  expect {
   152      "Time:" {
   153  	report "unexpected Time"
   154  	exit 1
   155      }
   156      root@ {}
   157  }
   158  eexpect "/defaultdb> "
   159  # restore
   160  send "\\set show_times\r"
   161  end_test
   162  
   163  start_test "Check that \\h with invalid commands print a reminder."
   164  send "\\h invalid\r"
   165  eexpect "no help available"
   166  eexpect "Try"
   167  expect "with no argument"
   168  eexpect root@
   169  end_test
   170  
   171  start_test "Check that \\h with no argument prints a full list."
   172  send "\\h\r"
   173  eexpect "TRUNCATE"
   174  eexpect "SHOW"
   175  eexpect "ROLLBACK"
   176  eexpect root@
   177  end_test
   178  
   179  start_test "Check that \\h with a known statement prints details."
   180  send "\\h select\r"
   181  eexpect "Command:"
   182  eexpect "SELECT"
   183  eexpect "data manipulation"
   184  eexpect "FROM"
   185  eexpect "ORDER BY"
   186  eexpect "See also"
   187  eexpect root@
   188  end_test
   189  
   190  start_test "Check that \\h with a documented clause name prints details."
   191  send "\\h <source>\r"
   192  eexpect "Command:"
   193  eexpect "<SOURCE>"
   194  eexpect "data manipulation"
   195  eexpect "JOIN"
   196  eexpect "SHOW"
   197  eexpect "See also"
   198  eexpect root@
   199  end_test
   200  
   201  start_test "Check that \\hf without argument prints a list."
   202  send "\\hf\r"
   203  eexpect "abs"
   204  eexpect "count"
   205  eexpect "round"
   206  eexpect root@
   207  end_test
   208  
   209  start_test "Check that \\hf with a valid function name prints that functions help."
   210  send "\\hf version\r"
   211  eexpect "Category:"
   212  eexpect "Signature"
   213  eexpect root@
   214  end_test
   215  
   216  start_test "Check that \\hf with an invalid function name prints an error."
   217  send "\\hf invalid\r"
   218  eexpect "no help available"
   219  eexpect "Try"
   220  eexpect "with no argument"
   221  eexpect root@
   222  end_test
   223  
   224  start_test "Check that commands are also recognized with a final semicolon."
   225  send "\\set;\r"
   226  eexpect "display_format"
   227  eexpect root@
   228  send "\\h select;\r"
   229  eexpect SELECT
   230  eexpect Description
   231  eexpect root@
   232  end_test
   233  
   234  # Finally terminate with Ctrl+C.
   235  interrupt
   236  eexpect eof
   237  
   238  spawn /bin/bash
   239  send "PS1=':''/# '\r"
   240  eexpect ":/# "
   241  
   242  start_test "Check that non-interactive built-in commands are only accepted at the start of a statement."
   243  send "(echo '\\set check_syntax'; echo 'select '; echo '\\help'; echo ';') | $argv sql\r"
   244  eexpect "statement ignored"
   245  eexpect ":/# "
   246  
   247  send "(echo '\\unset check_syntax'; echo 'select '; echo '\\help'; echo '1;') | $argv sql\r"
   248  eexpect "ERROR: at or near"
   249  eexpect "syntax error"
   250  eexpect ":/# "
   251  end_test
   252  
   253  start_test "Check that client-side options can be overridden with set"
   254  
   255  # First establish a baseline with all the defaults.
   256  send "$argv demo --empty\r"
   257  eexpect root@
   258  send "\\set display_format csv\r"
   259  send "\\set\r"
   260  eexpect "auto_trace,off"
   261  eexpect "check_syntax,true"
   262  eexpect "echo,false"
   263  eexpect "errexit,false"
   264  eexpect "prompt1,%n@"
   265  eexpect "show_times,true"
   266  eexpect root@
   267  interrupt
   268  eexpect ":/# "
   269  
   270  # Then verify that the defaults can be overridden.
   271  send "$argv demo --empty --set=auto_trace=on --set=check_syntax=false --set=echo=true --set=errexit=true --set=prompt1=%n@haa --set=show_times=false\r"
   272  eexpect root@
   273  send "\\set display_format csv\r"
   274  send "\\set\r"
   275  eexpect "auto_trace,\"on"
   276  eexpect "check_syntax,false"
   277  eexpect "echo,true"
   278  eexpect "errexit,true"
   279  eexpect "prompt1,%n@haa"
   280  eexpect "show_times,false"
   281  eexpect root@
   282  interrupt
   283  eexpect ":/# "
   284  
   285  end_test
   286  
   287  
   288  send "exit 0\r"
   289  eexpect eof
   290  
   291  stop_server $argv