Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 1 | " Tests for the terminal window. |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 2 | " This is split in two, because it can take a lot of time. |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 3 | " See test_terminal2.vim and test_terminal3.vim for further tests. |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 4 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 5 | source check.vim |
| 6 | CheckFeature terminal |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 7 | |
| 8 | source shared.vim |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 9 | source screendump.vim |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 10 | source mouse.vim |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 11 | source term_util.vim |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 12 | |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 13 | let s:python = PythonProg() |
Bram Moolenaar | ddd3308 | 2019-06-03 23:07:25 +0200 | [diff] [blame] | 14 | let $PROMPT_COMMAND='' |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 15 | |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 16 | func Test_terminal_basic() |
Bram Moolenaar | 606cb8b | 2018-05-03 20:40:20 +0200 | [diff] [blame] | 17 | au TerminalOpen * let b:done = 'yes' |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 18 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | b00fdf6 | 2017-09-21 22:16:21 +0200 | [diff] [blame] | 19 | |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 20 | call assert_equal('t', mode()) |
Bram Moolenaar | b00fdf6 | 2017-09-21 22:16:21 +0200 | [diff] [blame] | 21 | call assert_equal('yes', b:done) |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 22 | call assert_match('%aR[^\n]*running]', execute('ls')) |
Bram Moolenaar | 0751f51 | 2018-03-29 16:37:16 +0200 | [diff] [blame] | 23 | call assert_match('%aR[^\n]*running]', execute('ls R')) |
| 24 | call assert_notmatch('%[^\n]*running]', execute('ls F')) |
| 25 | call assert_notmatch('%[^\n]*running]', execute('ls ?')) |
Bram Moolenaar | 004a678 | 2020-04-11 17:09:31 +0200 | [diff] [blame] | 26 | call assert_fails('set modifiable', 'E946:') |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 27 | |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 28 | call StopShellInTerminal(buf) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 29 | call TermWait(buf) |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 30 | call assert_equal('n', mode()) |
| 31 | call assert_match('%aF[^\n]*finished]', execute('ls')) |
Bram Moolenaar | 0751f51 | 2018-03-29 16:37:16 +0200 | [diff] [blame] | 32 | call assert_match('%aF[^\n]*finished]', execute('ls F')) |
| 33 | call assert_notmatch('%[^\n]*finished]', execute('ls R')) |
| 34 | call assert_notmatch('%[^\n]*finished]', execute('ls ?')) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 35 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 36 | " closing window wipes out the terminal buffer a with finished job |
| 37 | close |
| 38 | call assert_equal("", bufname(buf)) |
| 39 | |
Bram Moolenaar | 606cb8b | 2018-05-03 20:40:20 +0200 | [diff] [blame] | 40 | au! TerminalOpen |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 41 | unlet g:job |
| 42 | endfunc |
| 43 | |
Bram Moolenaar | 28ed4df | 2019-10-26 16:21:40 +0200 | [diff] [blame] | 44 | func Test_terminal_TerminalWinOpen() |
| 45 | au TerminalWinOpen * let b:done = 'yes' |
| 46 | let buf = Run_shell_in_terminal({}) |
| 47 | call assert_equal('yes', b:done) |
| 48 | call StopShellInTerminal(buf) |
| 49 | " closing window wipes out the terminal buffer with the finished job |
| 50 | close |
| 51 | |
| 52 | if has("unix") |
| 53 | terminal ++hidden ++open sleep 1 |
| 54 | sleep 1 |
| 55 | call assert_fails("echo b:done", 'E121:') |
| 56 | endif |
| 57 | |
| 58 | au! TerminalWinOpen |
| 59 | endfunc |
| 60 | |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 61 | func Test_terminal_make_change() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 62 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 63 | call StopShellInTerminal(buf) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 64 | call TermWait(buf) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 65 | |
| 66 | setlocal modifiable |
| 67 | exe "normal Axxx\<Esc>" |
| 68 | call assert_fails(buf . 'bwipe', 'E517') |
| 69 | undo |
| 70 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 71 | exe buf . 'bwipe' |
| 72 | unlet g:job |
| 73 | endfunc |
| 74 | |
Bram Moolenaar | 5b868a8 | 2019-02-25 06:11:53 +0100 | [diff] [blame] | 75 | func Test_terminal_paste_register() |
| 76 | let @" = "text to paste" |
| 77 | |
| 78 | let buf = Run_shell_in_terminal({}) |
| 79 | " Wait for the shell to display a prompt |
| 80 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
| 81 | |
| 82 | call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt') |
| 83 | call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))}) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 84 | call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())}) |
Bram Moolenaar | 5b868a8 | 2019-02-25 06:11:53 +0100 | [diff] [blame] | 85 | |
| 86 | exe buf . 'bwipe!' |
| 87 | unlet g:job |
| 88 | endfunc |
| 89 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 90 | func Test_terminal_wipe_buffer() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 91 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 92 | call assert_fails(buf . 'bwipe', 'E517') |
| 93 | exe buf . 'bwipe!' |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 94 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 95 | call assert_equal("", bufname(buf)) |
| 96 | |
| 97 | unlet g:job |
| 98 | endfunc |
| 99 | |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 100 | func Test_terminal_split_quit() |
| 101 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 102 | call TermWait(buf) |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 103 | split |
| 104 | quit! |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 105 | call TermWait(buf) |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 106 | sleep 50m |
| 107 | call assert_equal('run', job_status(g:job)) |
| 108 | |
| 109 | quit! |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 110 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 111 | |
| 112 | exe buf . 'bwipe' |
| 113 | unlet g:job |
| 114 | endfunc |
| 115 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 116 | func Test_terminal_hide_buffer() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 117 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 118 | setlocal bufhidden=hide |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 119 | quit |
| 120 | for nr in range(1, winnr('$')) |
| 121 | call assert_notequal(winbufnr(nr), buf) |
| 122 | endfor |
| 123 | call assert_true(bufloaded(buf)) |
| 124 | call assert_true(buflisted(buf)) |
| 125 | |
| 126 | exe 'split ' . buf . 'buf' |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 127 | call StopShellInTerminal(buf) |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 128 | exe buf . 'bwipe' |
| 129 | |
| 130 | unlet g:job |
| 131 | endfunc |
| 132 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 133 | func s:Nasty_exit_cb(job, st) |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 134 | exe g:buf . 'bwipe!' |
| 135 | let g:buf = 0 |
| 136 | endfunc |
| 137 | |
Bram Moolenaar | 9d18961 | 2017-09-09 18:11:00 +0200 | [diff] [blame] | 138 | func Get_cat_123_cmd() |
| 139 | if has('win32') |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 140 | if !has('conpty') |
| 141 | return 'cmd /c "cls && color 2 && echo 123"' |
| 142 | else |
| 143 | " When clearing twice, extra sequence is not output. |
| 144 | return 'cmd /c "cls && cls && color 2 && echo 123"' |
| 145 | endif |
Bram Moolenaar | 9d18961 | 2017-09-09 18:11:00 +0200 | [diff] [blame] | 146 | else |
| 147 | call writefile(["\<Esc>[32m123"], 'Xtext') |
| 148 | return "cat Xtext" |
| 149 | endif |
| 150 | endfunc |
| 151 | |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 152 | func Test_terminal_nasty_cb() |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 153 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 154 | let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')}) |
| 155 | let g:job = term_getjob(g:buf) |
| 156 | |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 157 | call WaitForAssert({-> assert_equal("dead", job_status(g:job))}) |
| 158 | call WaitForAssert({-> assert_equal(0, g:buf)}) |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 159 | unlet g:job |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 160 | unlet g:buf |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 161 | call delete('Xtext') |
| 162 | endfunc |
| 163 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 164 | func Check_123(buf) |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 165 | let l = term_scrape(a:buf, 0) |
| 166 | call assert_true(len(l) == 0) |
| 167 | let l = term_scrape(a:buf, 999) |
| 168 | call assert_true(len(l) == 0) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 169 | let l = a:buf->term_scrape(1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 170 | call assert_true(len(l) > 0) |
| 171 | call assert_equal('1', l[0].chars) |
| 172 | call assert_equal('2', l[1].chars) |
| 173 | call assert_equal('3', l[2].chars) |
| 174 | call assert_equal('#00e000', l[0].fg) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 175 | call assert_equal(0, term_getattr(l[0].attr, 'bold')) |
| 176 | call assert_equal(0, l[0].attr->term_getattr('italic')) |
Bram Moolenaar | 81df635 | 2018-12-22 18:25:30 +0100 | [diff] [blame] | 177 | if has('win32') |
| 178 | " On Windows 'background' always defaults to dark, even though the terminal |
| 179 | " may use a light background. Therefore accept both white and black. |
| 180 | call assert_match('#ffffff\|#000000', l[0].bg) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 181 | else |
Bram Moolenaar | 81df635 | 2018-12-22 18:25:30 +0100 | [diff] [blame] | 182 | if &background == 'light' |
| 183 | call assert_equal('#ffffff', l[0].bg) |
| 184 | else |
| 185 | call assert_equal('#000000', l[0].bg) |
| 186 | endif |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 187 | endif |
| 188 | |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 189 | let l = term_getline(a:buf, -1) |
| 190 | call assert_equal('', l) |
| 191 | let l = term_getline(a:buf, 0) |
| 192 | call assert_equal('', l) |
| 193 | let l = term_getline(a:buf, 999) |
| 194 | call assert_equal('', l) |
Bram Moolenaar | 9c84484 | 2017-08-01 18:41:21 +0200 | [diff] [blame] | 195 | let l = term_getline(a:buf, 1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 196 | call assert_equal('123', l) |
| 197 | endfunc |
| 198 | |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 199 | func Test_terminal_scrape_123() |
| 200 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 201 | let buf = term_start(cmd) |
| 202 | |
| 203 | let termlist = term_list() |
| 204 | call assert_equal(1, len(termlist)) |
| 205 | call assert_equal(buf, termlist[0]) |
| 206 | |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 207 | " Nothing happens with invalid buffer number |
| 208 | call term_wait(1234) |
| 209 | |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 210 | call TermWait(buf) |
Bram Moolenaar | 1783337 | 2017-09-04 22:23:19 +0200 | [diff] [blame] | 211 | " On MS-Windows we first get a startup message of two lines, wait for the |
Bram Moolenaar | 1bfdc07 | 2017-09-05 20:19:42 +0200 | [diff] [blame] | 212 | " "cls" to happen, after that we have one line with three characters. |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 213 | call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))}) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 214 | call Check_123(buf) |
| 215 | |
| 216 | " Must still work after the job ended. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 217 | let job = term_getjob(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 218 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 219 | call TermWait(buf) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 220 | call Check_123(buf) |
| 221 | |
| 222 | exe buf . 'bwipe' |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 223 | call delete('Xtext') |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 224 | endfunc |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 225 | |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 226 | func Test_terminal_scrape_multibyte() |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 227 | call writefile(["léttまrs"], 'Xtext') |
| 228 | if has('win32') |
Bram Moolenaar | 3678393 | 2017-08-14 23:07:30 +0200 | [diff] [blame] | 229 | " Run cmd with UTF-8 codepage to make the type command print the expected |
| 230 | " multibyte characters. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 231 | let buf = term_start("cmd /K chcp 65001") |
| 232 | call term_sendkeys(buf, "type Xtext\<CR>") |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 233 | eval buf->term_sendkeys("exit\<CR>") |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 234 | let line = 4 |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 235 | else |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 236 | let buf = term_start("cat Xtext") |
| 237 | let line = 1 |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 238 | endif |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 239 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 240 | call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"}) |
| 241 | let l = term_scrape(buf, line) |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 242 | call assert_true(len(l) >= 7) |
| 243 | call assert_equal('l', l[0].chars) |
| 244 | call assert_equal('é', l[1].chars) |
| 245 | call assert_equal(1, l[1].width) |
| 246 | call assert_equal('t', l[2].chars) |
| 247 | call assert_equal('t', l[3].chars) |
| 248 | call assert_equal('ま', l[4].chars) |
| 249 | call assert_equal(2, l[4].width) |
| 250 | call assert_equal('r', l[5].chars) |
| 251 | call assert_equal('s', l[6].chars) |
| 252 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 253 | let job = term_getjob(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 254 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 255 | call TermWait(buf) |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 256 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 257 | exe buf . 'bwipe' |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 258 | call delete('Xtext') |
| 259 | endfunc |
| 260 | |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 261 | func Test_terminal_scroll() |
| 262 | call writefile(range(1, 200), 'Xtext') |
| 263 | if has('win32') |
| 264 | let cmd = 'cmd /c "type Xtext"' |
| 265 | else |
| 266 | let cmd = "cat Xtext" |
| 267 | endif |
| 268 | let buf = term_start(cmd) |
| 269 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 270 | let job = term_getjob(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 271 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 272 | call TermWait(buf) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 273 | |
Bram Moolenaar | bfcfd57 | 2020-03-25 21:27:22 +0100 | [diff] [blame] | 274 | " wait until the scrolling stops |
| 275 | while 1 |
| 276 | let scrolled = buf->term_getscrolled() |
| 277 | sleep 20m |
| 278 | if scrolled == buf->term_getscrolled() |
| 279 | break |
| 280 | endif |
| 281 | endwhile |
| 282 | |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 283 | call assert_equal('1', getline(1)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 284 | call assert_equal('1', term_getline(buf, 1 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 285 | call assert_equal('49', getline(49)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 286 | call assert_equal('49', term_getline(buf, 49 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 287 | call assert_equal('200', getline(200)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 288 | call assert_equal('200', term_getline(buf, 200 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 289 | |
| 290 | exe buf . 'bwipe' |
| 291 | call delete('Xtext') |
| 292 | endfunc |
| 293 | |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 294 | func Test_terminal_scrollback() |
Bram Moolenaar | 33c5e9f | 2018-05-26 18:58:51 +0200 | [diff] [blame] | 295 | let buf = Run_shell_in_terminal({'term_rows': 15}) |
Bram Moolenaar | 6d150f7 | 2018-04-21 20:03:20 +0200 | [diff] [blame] | 296 | set termwinscroll=100 |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 297 | call writefile(range(150), 'Xtext') |
| 298 | if has('win32') |
| 299 | call term_sendkeys(buf, "type Xtext\<CR>") |
| 300 | else |
| 301 | call term_sendkeys(buf, "cat Xtext\<CR>") |
| 302 | endif |
| 303 | let rows = term_getsize(buf)[0] |
Bram Moolenaar | 6c67219 | 2018-04-15 13:28:42 +0200 | [diff] [blame] | 304 | " On MS-Windows there is an empty line, check both last line and above it. |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 305 | call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))}) |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 306 | let lines = line('$') |
Bram Moolenaar | ac3e830 | 2018-04-15 13:10:44 +0200 | [diff] [blame] | 307 | call assert_inrange(91, 100, lines) |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 308 | |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 309 | call StopShellInTerminal(buf) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 310 | call TermWait(buf) |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 311 | exe buf . 'bwipe' |
Bram Moolenaar | 6d150f7 | 2018-04-21 20:03:20 +0200 | [diff] [blame] | 312 | set termwinscroll& |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 313 | call delete('Xtext') |
| 314 | endfunc |
| 315 | |
| 316 | func Test_terminal_postponed_scrollback() |
Bram Moolenaar | adbde3f | 2019-09-08 22:57:14 +0200 | [diff] [blame] | 317 | " tail -f only works on Unix |
| 318 | CheckUnix |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 319 | |
| 320 | call writefile(range(50), 'Xtext') |
| 321 | call writefile([ |
Bram Moolenaar | 5ff7df5 | 2019-02-15 01:06:13 +0100 | [diff] [blame] | 322 | \ 'set shell=/bin/sh noruler', |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 323 | \ 'terminal', |
Bram Moolenaar | 7e841e3 | 2019-02-15 00:26:14 +0100 | [diff] [blame] | 324 | \ 'sleep 200m', |
Bram Moolenaar | 5ff7df5 | 2019-02-15 01:06:13 +0100 | [diff] [blame] | 325 | \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")', |
| 326 | \ 'sleep 100m', |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 327 | \ 'call feedkeys("\<C-W>N", "xt")', |
| 328 | \ ], 'XTest_postponed') |
| 329 | let buf = RunVimInTerminal('-S XTest_postponed', {}) |
| 330 | " Check that the Xtext lines are displayed and in Terminal-Normal mode |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 331 | call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {}) |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 332 | |
| 333 | silent !echo 'one more line' >>Xtext |
Bram Moolenaar | 700dfaa | 2019-04-13 14:21:19 +0200 | [diff] [blame] | 334 | " Screen will not change, move cursor to get a different dump |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 335 | call term_sendkeys(buf, "k") |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 336 | call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {}) |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 337 | |
| 338 | " Back to Terminal-Job mode, text will scroll and show the extra line. |
| 339 | call term_sendkeys(buf, "a") |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 340 | call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {}) |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 341 | |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 342 | " stop "tail -f" |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 343 | call term_sendkeys(buf, "\<C-C>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 344 | call TermWait(buf, 25) |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 345 | " stop shell |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 346 | call term_sendkeys(buf, "exit\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 347 | call TermWait(buf, 50) |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 348 | " close terminal window |
Bram Moolenaar | 3a05ce6 | 2020-03-11 19:30:01 +0100 | [diff] [blame] | 349 | let tsk_ret = term_sendkeys(buf, ":q\<CR>") |
| 350 | |
| 351 | " check type of term_sendkeys() return value |
| 352 | echo type(tsk_ret) |
| 353 | |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 354 | call StopVimInTerminal(buf) |
| 355 | call delete('XTest_postponed') |
| 356 | call delete('Xtext') |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 357 | endfunc |
| 358 | |
Bram Moolenaar | 81aa0f5 | 2019-02-14 23:23:19 +0100 | [diff] [blame] | 359 | " Run diff on two dumps with different size. |
| 360 | func Test_terminal_dumpdiff_size() |
| 361 | call assert_equal(1, winnr('$')) |
| 362 | call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump') |
| 363 | call assert_equal(2, winnr('$')) |
| 364 | call assert_match('Test_incsearch_search_01.dump', getline(10)) |
| 365 | call assert_match(' +++++$', getline(11)) |
| 366 | call assert_match('Test_popup_command_01.dump', getline(31)) |
| 367 | call assert_equal(repeat('+', 75), getline(30)) |
| 368 | quit |
| 369 | endfunc |
| 370 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 371 | func Test_terminal_size() |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 372 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 373 | |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 374 | exe 'terminal ++rows=5 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 375 | let size = term_getsize('') |
| 376 | bwipe! |
| 377 | call assert_equal(5, size[0]) |
| 378 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 379 | call term_start(cmd, {'term_rows': 6}) |
| 380 | let size = term_getsize('') |
| 381 | bwipe! |
| 382 | call assert_equal(6, size[0]) |
| 383 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 384 | vsplit |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 385 | exe 'terminal ++rows=5 ++cols=33 ' . cmd |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 386 | call assert_equal([5, 33], ''->term_getsize()) |
Bram Moolenaar | a42d363 | 2018-04-14 17:05:38 +0200 | [diff] [blame] | 387 | |
| 388 | call term_setsize('', 6, 0) |
| 389 | call assert_equal([6, 33], term_getsize('')) |
| 390 | |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 391 | eval ''->term_setsize(0, 35) |
Bram Moolenaar | a42d363 | 2018-04-14 17:05:38 +0200 | [diff] [blame] | 392 | call assert_equal([6, 35], term_getsize('')) |
| 393 | |
| 394 | call term_setsize('', 7, 30) |
| 395 | call assert_equal([7, 30], term_getsize('')) |
| 396 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 397 | bwipe! |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 398 | call assert_fails("call term_setsize('', 7, 30)", "E955:") |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 399 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 400 | call term_start(cmd, {'term_rows': 6, 'term_cols': 36}) |
| 401 | let size = term_getsize('') |
| 402 | bwipe! |
| 403 | call assert_equal([6, 36], size) |
| 404 | |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 405 | exe 'vertical terminal ++cols=20 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 406 | let size = term_getsize('') |
| 407 | bwipe! |
| 408 | call assert_equal(20, size[1]) |
| 409 | |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 410 | eval cmd->term_start({'vertical': 1, 'term_cols': 26}) |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 411 | let size = term_getsize('') |
| 412 | bwipe! |
| 413 | call assert_equal(26, size[1]) |
| 414 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 415 | split |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 416 | exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 417 | let size = term_getsize('') |
| 418 | bwipe! |
| 419 | call assert_equal([6, 20], size) |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 420 | |
| 421 | call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27}) |
| 422 | let size = term_getsize('') |
| 423 | bwipe! |
| 424 | call assert_equal([7, 27], size) |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 425 | |
| 426 | call delete('Xtext') |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 427 | endfunc |
| 428 | |
| 429 | func Test_terminal_curwin() |
| 430 | let cmd = Get_cat_123_cmd() |
| 431 | call assert_equal(1, winnr('$')) |
| 432 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 433 | split Xdummy |
| 434 | call setline(1, 'dummy') |
| 435 | write |
| 436 | call assert_equal(1, getbufinfo('Xdummy')[0].loaded) |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 437 | exe 'terminal ++curwin ' . cmd |
| 438 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 439 | call assert_equal(0, getbufinfo('Xdummy')[0].loaded) |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 440 | bwipe! |
| 441 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 442 | split Xdummy |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 443 | call term_start(cmd, {'curwin': 1}) |
| 444 | call assert_equal(2, winnr('$')) |
| 445 | bwipe! |
| 446 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 447 | split Xdummy |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 448 | call setline(1, 'change') |
| 449 | call assert_fails('terminal ++curwin ' . cmd, 'E37:') |
| 450 | call assert_equal(2, winnr('$')) |
| 451 | exe 'terminal! ++curwin ' . cmd |
| 452 | call assert_equal(2, winnr('$')) |
| 453 | bwipe! |
| 454 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 455 | split Xdummy |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 456 | call setline(1, 'change') |
| 457 | call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:') |
| 458 | call assert_equal(2, winnr('$')) |
| 459 | bwipe! |
| 460 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 461 | split Xdummy |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 462 | bwipe! |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 463 | call delete('Xtext') |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 464 | call delete('Xdummy') |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 465 | endfunc |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 466 | |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 467 | func s:get_sleep_cmd() |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 468 | if s:python != '' |
| 469 | let cmd = s:python . " test_short_sleep.py" |
Bram Moolenaar | c8523e2 | 2018-06-03 18:22:02 +0200 | [diff] [blame] | 470 | " 500 was not enough for Travis |
| 471 | let waittime = 900 |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 472 | else |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 473 | echo 'This will take five seconds...' |
| 474 | let waittime = 2000 |
| 475 | if has('win32') |
| 476 | let cmd = $windir . '\system32\timeout.exe 1' |
| 477 | else |
| 478 | let cmd = 'sleep 1' |
| 479 | endif |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 480 | endif |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 481 | return [cmd, waittime] |
| 482 | endfunc |
| 483 | |
| 484 | func Test_terminal_finish_open_close() |
| 485 | call assert_equal(1, winnr('$')) |
| 486 | |
| 487 | let [cmd, waittime] = s:get_sleep_cmd() |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 488 | |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 489 | " shell terminal closes automatically |
| 490 | terminal |
| 491 | let buf = bufnr('%') |
| 492 | call assert_equal(2, winnr('$')) |
| 493 | " Wait for the shell to display a prompt |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 494 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 495 | call StopShellInTerminal(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 496 | call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime) |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 497 | |
| 498 | " shell terminal that does not close automatically |
| 499 | terminal ++noclose |
| 500 | let buf = bufnr('%') |
| 501 | call assert_equal(2, winnr('$')) |
| 502 | " Wait for the shell to display a prompt |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 503 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 504 | call StopShellInTerminal(buf) |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 505 | call assert_equal(2, winnr('$')) |
| 506 | quit |
| 507 | call assert_equal(1, winnr('$')) |
| 508 | |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 509 | exe 'terminal ++close ' . cmd |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 510 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 511 | wincmd p |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 512 | call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 513 | |
| 514 | call term_start(cmd, {'term_finish': 'close'}) |
| 515 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 516 | wincmd p |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 517 | call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 518 | call assert_equal(1, winnr('$')) |
| 519 | |
| 520 | exe 'terminal ++open ' . cmd |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 521 | close! |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 522 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 523 | bwipe |
| 524 | |
| 525 | call term_start(cmd, {'term_finish': 'open'}) |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 526 | close! |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 527 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 528 | bwipe |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 529 | |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 530 | exe 'terminal ++hidden ++open ' . cmd |
| 531 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 532 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 533 | bwipe |
| 534 | |
| 535 | call term_start(cmd, {'term_finish': 'open', 'hidden': 1}) |
| 536 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 537 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 538 | bwipe |
Bram Moolenaar | 37c4583 | 2017-08-12 16:01:04 +0200 | [diff] [blame] | 539 | |
| 540 | call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:') |
| 541 | call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:') |
| 542 | call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:') |
| 543 | call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:') |
| 544 | |
| 545 | call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'}) |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 546 | close! |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 547 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | 37c4583 | 2017-08-12 16:01:04 +0200 | [diff] [blame] | 548 | call assert_equal(4, winheight(0)) |
| 549 | bwipe |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 550 | endfunc |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 551 | |
| 552 | func Test_terminal_cwd() |
Bram Moolenaar | 077ff43 | 2019-10-28 00:42:21 +0100 | [diff] [blame] | 553 | if has('win32') |
| 554 | let cmd = 'cmd /c cd' |
| 555 | else |
| 556 | CheckExecutable pwd |
| 557 | let cmd = 'pwd' |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 558 | endif |
| 559 | call mkdir('Xdir') |
Bram Moolenaar | 077ff43 | 2019-10-28 00:42:21 +0100 | [diff] [blame] | 560 | let buf = term_start(cmd, {'cwd': 'Xdir'}) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 561 | call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))}) |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 562 | |
| 563 | exe buf . 'bwipe' |
| 564 | call delete('Xdir', 'rf') |
| 565 | endfunc |
| 566 | |
Bram Moolenaar | 839e81e | 2018-10-19 16:53:39 +0200 | [diff] [blame] | 567 | func Test_terminal_cwd_failure() |
| 568 | " Case 1: Provided directory is not actually a directory. Attempt to make |
| 569 | " the file executable as well. |
| 570 | call writefile([], 'Xfile') |
| 571 | call setfperm('Xfile', 'rwx------') |
| 572 | call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:') |
| 573 | call delete('Xfile') |
| 574 | |
| 575 | " Case 2: Directory does not exist. |
| 576 | call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:') |
| 577 | |
| 578 | " Case 3: Directory exists but is not accessible. |
Bram Moolenaar | 0b38f54 | 2018-11-03 21:47:16 +0100 | [diff] [blame] | 579 | " Skip this for root, it will be accessible anyway. |
Bram Moolenaar | 07282f0 | 2019-10-10 16:46:17 +0200 | [diff] [blame] | 580 | if !IsRoot() |
Bram Moolenaar | 0b38f54 | 2018-11-03 21:47:16 +0100 | [diff] [blame] | 581 | call mkdir('XdirNoAccess', '', '0600') |
| 582 | " return early if the directory permissions could not be set properly |
| 583 | if getfperm('XdirNoAccess')[2] == 'x' |
| 584 | call delete('XdirNoAccess', 'rf') |
| 585 | return |
| 586 | endif |
| 587 | call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:') |
| 588 | call delete('XdirNoAccess', 'rf') |
Bram Moolenaar | 839e81e | 2018-10-19 16:53:39 +0200 | [diff] [blame] | 589 | endif |
Bram Moolenaar | 839e81e | 2018-10-19 16:53:39 +0200 | [diff] [blame] | 590 | endfunc |
| 591 | |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 592 | func Test_terminal_servername() |
| 593 | if !has('clientserver') |
| 594 | return |
| 595 | endif |
Bram Moolenaar | d7a137f | 2018-06-12 18:05:24 +0200 | [diff] [blame] | 596 | call s:test_environment("VIM_SERVERNAME", v:servername) |
| 597 | endfunc |
| 598 | |
| 599 | func Test_terminal_version() |
| 600 | call s:test_environment("VIM_TERMINAL", string(v:version)) |
| 601 | endfunc |
| 602 | |
| 603 | func s:test_environment(name, value) |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 604 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 605 | " Wait for the shell to display a prompt |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 606 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 607 | if has('win32') |
Bram Moolenaar | d7a137f | 2018-06-12 18:05:24 +0200 | [diff] [blame] | 608 | call term_sendkeys(buf, "echo %" . a:name . "%\r") |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 609 | else |
Bram Moolenaar | d7a137f | 2018-06-12 18:05:24 +0200 | [diff] [blame] | 610 | call term_sendkeys(buf, "echo $" . a:name . "\r") |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 611 | endif |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 612 | call TermWait(buf) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 613 | call StopShellInTerminal(buf) |
Bram Moolenaar | d7a137f | 2018-06-12 18:05:24 +0200 | [diff] [blame] | 614 | call WaitForAssert({-> assert_equal(a:value, getline(2))}) |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 615 | |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 616 | exe buf . 'bwipe' |
| 617 | unlet buf |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 618 | endfunc |
| 619 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 620 | func Test_terminal_env() |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 621 | let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}}) |
Bram Moolenaar | 51c2368 | 2017-08-14 21:45:00 +0200 | [diff] [blame] | 622 | " Wait for the shell to display a prompt |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 623 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 624 | if has('win32') |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 625 | call term_sendkeys(buf, "echo %TESTENV%\r") |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 626 | else |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 627 | call term_sendkeys(buf, "echo $TESTENV\r") |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 628 | endif |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 629 | eval buf->TermWait() |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 630 | call StopShellInTerminal(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 631 | call WaitForAssert({-> assert_equal('correct', getline(2))}) |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 632 | |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 633 | exe buf . 'bwipe' |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 634 | endfunc |
Bram Moolenaar | 679653e | 2017-08-13 14:13:19 +0200 | [diff] [blame] | 635 | |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 636 | func Test_terminal_list_args() |
| 637 | let buf = term_start([&shell, &shellcmdflag, 'echo "123"']) |
| 638 | call assert_fails(buf . 'bwipe', 'E517') |
| 639 | exe buf . 'bwipe!' |
| 640 | call assert_equal("", bufname(buf)) |
| 641 | endfunction |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 642 | |
| 643 | func Test_terminal_noblock() |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 644 | let buf = term_start(&shell) |
Bram Moolenaar | f3710ee | 2020-03-24 12:12:30 +0100 | [diff] [blame] | 645 | let wait_time = 5000 |
| 646 | let letters = 'abcdefghijklmnopqrstuvwxyz' |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 647 | if has('bsd') || has('mac') || has('sun') |
Bram Moolenaar | d8d85bf | 2017-09-03 18:08:00 +0200 | [diff] [blame] | 648 | " The shell or something else has a problem dealing with more than 1000 |
Bram Moolenaar | f3710ee | 2020-03-24 12:12:30 +0100 | [diff] [blame] | 649 | " characters at the same time. It's very slow too. |
Bram Moolenaar | d8d85bf | 2017-09-03 18:08:00 +0200 | [diff] [blame] | 650 | let len = 1000 |
Bram Moolenaar | d06dbf3 | 2020-03-24 10:33:00 +0100 | [diff] [blame] | 651 | let wait_time = 15000 |
Bram Moolenaar | f3710ee | 2020-03-24 12:12:30 +0100 | [diff] [blame] | 652 | let letters = 'abcdefghijklm' |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 653 | " NPFS is used in Windows, nonblocking mode does not work properly. |
| 654 | elseif has('win32') |
| 655 | let len = 1 |
Bram Moolenaar | d8d85bf | 2017-09-03 18:08:00 +0200 | [diff] [blame] | 656 | else |
| 657 | let len = 5000 |
| 658 | endif |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 659 | |
Bram Moolenaar | d06dbf3 | 2020-03-24 10:33:00 +0100 | [diff] [blame] | 660 | " Send a lot of text lines, should be buffered properly. |
Bram Moolenaar | f3710ee | 2020-03-24 12:12:30 +0100 | [diff] [blame] | 661 | for c in split(letters, '\zs') |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 662 | call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>") |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 663 | endfor |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 664 | call term_sendkeys(buf, "echo done\<cr>") |
Bram Moolenaar | eef0531 | 2017-08-20 20:21:23 +0200 | [diff] [blame] | 665 | |
| 666 | " On MS-Windows there is an extra empty line below "done". Find "done" in |
| 667 | " the last-but-one or the last-but-two line. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 668 | let lnum = term_getsize(buf)[0] - 1 |
Bram Moolenaar | d06dbf3 | 2020-03-24 10:33:00 +0100 | [diff] [blame] | 669 | call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time) |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 670 | let line = term_getline(buf, lnum) |
Bram Moolenaar | eef0531 | 2017-08-20 20:21:23 +0200 | [diff] [blame] | 671 | if line !~ 'done' |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 672 | let line = term_getline(buf, lnum - 1) |
Bram Moolenaar | eef0531 | 2017-08-20 20:21:23 +0200 | [diff] [blame] | 673 | endif |
| 674 | call assert_match('done', line) |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 675 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 676 | let g:job = term_getjob(buf) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 677 | call StopShellInTerminal(buf) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 678 | call TermWait(buf) |
Bram Moolenaar | d21f8b5 | 2017-08-19 15:40:01 +0200 | [diff] [blame] | 679 | unlet g:job |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 680 | bwipe |
| 681 | endfunc |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 682 | |
| 683 | func Test_terminal_write_stdin() |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 684 | " TODO: enable once writing to stdin works on MS-Windows |
| 685 | CheckNotMSWindows |
| 686 | CheckExecutable wc |
| 687 | |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 688 | call setline(1, ['one', 'two', 'three']) |
| 689 | %term wc |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 690 | call WaitForAssert({-> assert_match('3', getline("$"))}) |
Bram Moolenaar | 3346cc4 | 2017-09-02 14:54:21 +0200 | [diff] [blame] | 691 | let nrs = split(getline('$')) |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 692 | call assert_equal(['3', '3', '14'], nrs) |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 693 | %bwipe! |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 694 | |
| 695 | call setline(1, ['one', 'two', 'three', 'four']) |
| 696 | 2,3term wc |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 697 | call WaitForAssert({-> assert_match('2', getline("$"))}) |
Bram Moolenaar | 3346cc4 | 2017-09-02 14:54:21 +0200 | [diff] [blame] | 698 | let nrs = split(getline('$')) |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 699 | call assert_equal(['2', '2', '10'], nrs) |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 700 | %bwipe! |
| 701 | endfunc |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 702 | |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 703 | func Test_terminal_eof_arg() |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 704 | call CheckPython(s:python) |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 705 | |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 706 | call setline(1, ['print("hello")']) |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 707 | exe '1term ++eof=exit(123) ' .. s:python |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 708 | " MS-Windows echoes the input, Unix doesn't. |
| 709 | if has('win32') |
| 710 | call WaitFor({-> getline('$') =~ 'exit(123)'}) |
| 711 | call assert_equal('hello', getline(line('$') - 1)) |
| 712 | else |
| 713 | call WaitFor({-> getline('$') =~ 'hello'}) |
| 714 | call assert_equal('hello', getline('$')) |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 715 | endif |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 716 | call assert_equal(123, bufnr()->term_getjob()->job_info().exitval) |
| 717 | %bwipe! |
| 718 | endfunc |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 719 | |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 720 | func Test_terminal_eof_arg_win32_ctrl_z() |
| 721 | CheckMSWindows |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 722 | call CheckPython(s:python) |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 723 | |
| 724 | call setline(1, ['print("hello")']) |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 725 | exe '1term ++eof=<C-Z> ' .. s:python |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 726 | call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))}) |
| 727 | call assert_match('\^Z', getline(line('$') - 1)) |
| 728 | %bwipe! |
| 729 | endfunc |
| 730 | |
| 731 | func Test_terminal_duplicate_eof_arg() |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 732 | call CheckPython(s:python) |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 733 | |
| 734 | " Check the last specified ++eof arg is used and should not memory leak. |
| 735 | new |
| 736 | call setline(1, ['print("hello")']) |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 737 | exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 738 | " MS-Windows echoes the input, Unix doesn't. |
| 739 | if has('win32') |
| 740 | call WaitFor({-> getline('$') =~ 'exit(123)'}) |
| 741 | call assert_equal('hello', getline(line('$') - 1)) |
| 742 | else |
| 743 | call WaitFor({-> getline('$') =~ 'hello'}) |
| 744 | call assert_equal('hello', getline('$')) |
| 745 | endif |
| 746 | call assert_equal(123, bufnr()->term_getjob()->job_info().exitval) |
| 747 | %bwipe! |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 748 | endfunc |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 749 | |
| 750 | func Test_terminal_no_cmd() |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 751 | let buf = term_start('NONE', {}) |
| 752 | call assert_notequal(0, buf) |
| 753 | |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 754 | let pty = job_info(term_getjob(buf))['tty_out'] |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 755 | call assert_notequal('', pty) |
Bram Moolenaar | cfc1523 | 2019-01-23 22:33:18 +0100 | [diff] [blame] | 756 | if has('gui_running') && !has('win32') |
| 757 | " In the GUI job_start() doesn't work, it does not read from the pty. |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 758 | call system('echo "look here" > ' . pty) |
Bram Moolenaar | cfc1523 | 2019-01-23 22:33:18 +0100 | [diff] [blame] | 759 | else |
| 760 | " Otherwise using a job works on all systems. |
| 761 | call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty]) |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 762 | endif |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 763 | call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))}) |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 764 | |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 765 | bwipe! |
| 766 | endfunc |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 767 | |
| 768 | func Test_terminal_special_chars() |
| 769 | " this file name only works on Unix |
Bram Moolenaar | adbde3f | 2019-09-08 22:57:14 +0200 | [diff] [blame] | 770 | CheckUnix |
| 771 | |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 772 | call mkdir('Xdir with spaces') |
| 773 | call writefile(['x'], 'Xdir with spaces/quoted"file') |
| 774 | term ls Xdir\ with\ spaces/quoted\"file |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 775 | call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))}) |
Bram Moolenaar | 95ffd43 | 2020-02-23 13:29:31 +0100 | [diff] [blame] | 776 | " make sure the job has finished |
| 777 | call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))}) |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 778 | |
| 779 | call delete('Xdir with spaces', 'rf') |
| 780 | bwipe |
| 781 | endfunc |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 782 | |
| 783 | func Test_terminal_wrong_options() |
| 784 | call assert_fails('call term_start(&shell, { |
| 785 | \ "in_io": "file", |
| 786 | \ "in_name": "xxx", |
| 787 | \ "out_io": "file", |
| 788 | \ "out_name": "xxx", |
| 789 | \ "err_io": "file", |
| 790 | \ "err_name": "xxx" |
| 791 | \ })', 'E474:') |
| 792 | call assert_fails('call term_start(&shell, { |
| 793 | \ "out_buf": bufnr("%") |
| 794 | \ })', 'E474:') |
| 795 | call assert_fails('call term_start(&shell, { |
| 796 | \ "err_buf": bufnr("%") |
| 797 | \ })', 'E474:') |
| 798 | endfunc |
| 799 | |
| 800 | func Test_terminal_redir_file() |
Bram Moolenaar | f25329c | 2018-05-06 21:49:32 +0200 | [diff] [blame] | 801 | let cmd = Get_cat_123_cmd() |
| 802 | let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 803 | call TermWait(buf) |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 804 | " ConPTY may precede escape sequence. There are things that are not so. |
| 805 | if !has('conpty') |
| 806 | call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))}) |
| 807 | call assert_match('123', readfile('Xfile')[0]) |
| 808 | endif |
Bram Moolenaar | f25329c | 2018-05-06 21:49:32 +0200 | [diff] [blame] | 809 | let g:job = term_getjob(buf) |
| 810 | call WaitForAssert({-> assert_equal("dead", job_status(g:job))}) |
| 811 | call delete('Xfile') |
| 812 | bwipe |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 813 | |
| 814 | if has('unix') |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 815 | call writefile(['one line'], 'Xfile') |
| 816 | let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 817 | call TermWait(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 818 | call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))}) |
Bram Moolenaar | 8b53b79 | 2017-09-05 20:29:25 +0200 | [diff] [blame] | 819 | let g:job = term_getjob(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 820 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 821 | bwipe |
| 822 | call delete('Xfile') |
| 823 | endif |
| 824 | endfunc |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 825 | |
| 826 | func TerminalTmap(remap) |
| 827 | let buf = Run_shell_in_terminal({}) |
| 828 | call assert_equal('t', mode()) |
| 829 | |
| 830 | if a:remap |
| 831 | tmap 123 456 |
| 832 | else |
| 833 | tnoremap 123 456 |
| 834 | endif |
Bram Moolenaar | 461fe50 | 2017-12-05 12:30:03 +0100 | [diff] [blame] | 835 | " don't use abcde, it's an existing command |
| 836 | tmap 456 abxde |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 837 | call assert_equal('456', maparg('123', 't')) |
Bram Moolenaar | 461fe50 | 2017-12-05 12:30:03 +0100 | [diff] [blame] | 838 | call assert_equal('abxde', maparg('456', 't')) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 839 | call feedkeys("123", 'tx') |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 840 | call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))}) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 841 | let lnum = term_getcursor(buf)[0] |
| 842 | if a:remap |
Bram Moolenaar | 461fe50 | 2017-12-05 12:30:03 +0100 | [diff] [blame] | 843 | call assert_match('abxde', term_getline(buf, lnum)) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 844 | else |
| 845 | call assert_match('456', term_getline(buf, lnum)) |
| 846 | endif |
| 847 | |
| 848 | call term_sendkeys(buf, "\r") |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 849 | call StopShellInTerminal(buf) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 850 | call TermWait(buf) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 851 | |
| 852 | tunmap 123 |
| 853 | tunmap 456 |
| 854 | call assert_equal('', maparg('123', 't')) |
| 855 | close |
| 856 | unlet g:job |
| 857 | endfunc |
| 858 | |
| 859 | func Test_terminal_tmap() |
| 860 | call TerminalTmap(1) |
| 861 | call TerminalTmap(0) |
| 862 | endfunc |
Bram Moolenaar | 059db5c | 2017-10-15 22:42:23 +0200 | [diff] [blame] | 863 | |
| 864 | func Test_terminal_wall() |
| 865 | let buf = Run_shell_in_terminal({}) |
| 866 | wall |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 867 | call StopShellInTerminal(buf) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 868 | call TermWait(buf) |
Bram Moolenaar | 059db5c | 2017-10-15 22:42:23 +0200 | [diff] [blame] | 869 | exe buf . 'bwipe' |
| 870 | unlet g:job |
| 871 | endfunc |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 872 | |
Bram Moolenaar | 7a76092 | 2018-02-19 23:10:02 +0100 | [diff] [blame] | 873 | func Test_terminal_wqall() |
| 874 | let buf = Run_shell_in_terminal({}) |
| 875 | call assert_fails('wqall', 'E948') |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 876 | call StopShellInTerminal(buf) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 877 | call TermWait(buf) |
Bram Moolenaar | 7a76092 | 2018-02-19 23:10:02 +0100 | [diff] [blame] | 878 | exe buf . 'bwipe' |
| 879 | unlet g:job |
| 880 | endfunc |
| 881 | |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 882 | func Test_terminal_composing_unicode() |
| 883 | let save_enc = &encoding |
| 884 | set encoding=utf-8 |
| 885 | |
| 886 | if has('win32') |
| 887 | let cmd = "cmd /K chcp 65001" |
| 888 | let lnum = [3, 6, 9] |
| 889 | else |
| 890 | let cmd = &shell |
| 891 | let lnum = [1, 3, 5] |
| 892 | endif |
| 893 | |
| 894 | enew |
| 895 | let buf = term_start(cmd, {'curwin': bufnr('')}) |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 896 | let g:job = term_getjob(buf) |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 897 | call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 898 | |
Bram Moolenaar | ebe74b7 | 2018-04-21 23:34:43 +0200 | [diff] [blame] | 899 | if has('win32') |
| 900 | call assert_equal('cmd', job_info(g:job).cmd[0]) |
| 901 | else |
| 902 | call assert_equal(&shell, job_info(g:job).cmd[0]) |
| 903 | endif |
| 904 | |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 905 | " ascii + composing |
| 906 | let txt = "a\u0308bc" |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 907 | call term_sendkeys(buf, "echo " . txt) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 908 | call TermWait(buf, 25) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 909 | call assert_match("echo " . txt, term_getline(buf, lnum[0])) |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 910 | call term_sendkeys(buf, "\<cr>") |
| 911 | call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 912 | let l = term_scrape(buf, lnum[0] + 1) |
| 913 | call assert_equal("a\u0308", l[0].chars) |
| 914 | call assert_equal("b", l[1].chars) |
| 915 | call assert_equal("c", l[2].chars) |
| 916 | |
| 917 | " multibyte + composing |
| 918 | let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099" |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 919 | call term_sendkeys(buf, "echo " . txt) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 920 | call TermWait(buf, 25) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 921 | call assert_match("echo " . txt, term_getline(buf, lnum[1])) |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 922 | call term_sendkeys(buf, "\<cr>") |
| 923 | call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 924 | let l = term_scrape(buf, lnum[1] + 1) |
| 925 | call assert_equal("\u304b\u3099", l[0].chars) |
Bram Moolenaar | 79ea680 | 2020-05-17 15:09:27 +0200 | [diff] [blame] | 926 | call assert_equal("\u304e", l[2].chars) |
| 927 | call assert_equal("\u304f\u3099", l[3].chars) |
| 928 | call assert_equal("\u3052", l[5].chars) |
| 929 | call assert_equal("\u3053\u3099", l[6].chars) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 930 | |
| 931 | " \u00a0 + composing |
| 932 | let txt = "abc\u00a0\u0308" |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 933 | call term_sendkeys(buf, "echo " . txt) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 934 | call TermWait(buf, 25) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 935 | call assert_match("echo " . txt, term_getline(buf, lnum[2])) |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 936 | call term_sendkeys(buf, "\<cr>") |
| 937 | call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 938 | let l = term_scrape(buf, lnum[2] + 1) |
| 939 | call assert_equal("\u00a0\u0308", l[3].chars) |
| 940 | |
| 941 | call term_sendkeys(buf, "exit\r") |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 942 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 943 | bwipe! |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 944 | unlet g:job |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 945 | let &encoding = save_enc |
| 946 | endfunc |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 947 | |
| 948 | func Test_terminal_aucmd_on_close() |
| 949 | fun Nop() |
| 950 | let s:called = 1 |
| 951 | endfun |
| 952 | |
| 953 | aug repro |
| 954 | au! |
| 955 | au BufWinLeave * call Nop() |
| 956 | aug END |
| 957 | |
| 958 | let [cmd, waittime] = s:get_sleep_cmd() |
| 959 | |
| 960 | call assert_equal(1, winnr('$')) |
| 961 | new |
| 962 | call setline(1, ['one', 'two']) |
| 963 | exe 'term ++close ' . cmd |
| 964 | wincmd p |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 965 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 966 | call assert_equal(1, s:called) |
| 967 | bwipe! |
| 968 | |
| 969 | unlet s:called |
| 970 | au! repro |
| 971 | delfunc Nop |
| 972 | endfunc |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 973 | |
| 974 | func Test_terminal_term_start_empty_command() |
| 975 | let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})" |
| 976 | call assert_fails(cmd, 'E474') |
| 977 | let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})" |
| 978 | call assert_fails(cmd, 'E474') |
| 979 | let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})" |
| 980 | call assert_fails(cmd, 'E474') |
| 981 | let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})" |
| 982 | call assert_fails(cmd, 'E474') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 983 | let cmd = "call term_start('', {'term_name' : []})" |
| 984 | call assert_fails(cmd, 'E475') |
| 985 | let cmd = "call term_start('', {'term_finish' : 'axby'})" |
| 986 | call assert_fails(cmd, 'E475') |
| 987 | let cmd = "call term_start('', {'eof_chars' : []})" |
| 988 | call assert_fails(cmd, 'E475:') |
| 989 | let cmd = "call term_start('', {'term_kill' : []})" |
| 990 | call assert_fails(cmd, 'E475:') |
| 991 | let cmd = "call term_start('', {'tty_type' : []})" |
| 992 | call assert_fails(cmd, 'E475:') |
| 993 | let cmd = "call term_start('', {'tty_type' : 'abc'})" |
| 994 | call assert_fails(cmd, 'E475:') |
| 995 | let cmd = "call term_start('', {'term_highlight' : []})" |
| 996 | call assert_fails(cmd, 'E475:') |
Bram Moolenaar | 8720226 | 2020-05-24 17:23:45 +0200 | [diff] [blame] | 997 | if has('gui') || has('termguicolors') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 998 | let cmd = "call term_start('', {'ansi_colors' : 'abc'})" |
| 999 | call assert_fails(cmd, 'E475:') |
| 1000 | let cmd = "call term_start('', {'ansi_colors' : [[]]})" |
| 1001 | call assert_fails(cmd, 'E730:') |
| 1002 | let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})" |
Bram Moolenaar | 8720226 | 2020-05-24 17:23:45 +0200 | [diff] [blame] | 1003 | if has('gui_running') || has('termguicolors') |
| 1004 | call assert_fails(cmd, 'E475:') |
| 1005 | else |
| 1006 | call assert_fails(cmd, 'E254:') |
| 1007 | endif |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1008 | endif |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 1009 | endfunc |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1010 | |
| 1011 | func Test_terminal_response_to_control_sequence() |
Bram Moolenaar | adbde3f | 2019-09-08 22:57:14 +0200 | [diff] [blame] | 1012 | CheckUnix |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1013 | |
| 1014 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1015 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1016 | |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 1017 | call term_sendkeys(buf, "cat\<CR>") |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1018 | call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))}) |
Bram Moolenaar | d4a282f | 2018-02-02 18:22:31 +0100 | [diff] [blame] | 1019 | |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 1020 | " Request the cursor position. |
| 1021 | call term_sendkeys(buf, "\x1b[6n\<CR>") |
Bram Moolenaar | d4a282f | 2018-02-02 18:22:31 +0100 | [diff] [blame] | 1022 | |
| 1023 | " Wait for output from tty to display, below an empty line. |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1024 | call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))}) |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1025 | |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 1026 | " End "cat" gently. |
| 1027 | call term_sendkeys(buf, "\<CR>\<C-D>") |
| 1028 | |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 1029 | call StopShellInTerminal(buf) |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1030 | exe buf . 'bwipe' |
| 1031 | unlet g:job |
| 1032 | endfunc |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1033 | |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1034 | " Run Vim, start a terminal in that Vim with the kill argument, |
| 1035 | " :qall works. |
| 1036 | func Run_terminal_qall_kill(line1, line2) |
| 1037 | " 1. Open a terminal window and wait for the prompt to appear |
| 1038 | " 2. set kill using term_setkill() |
| 1039 | " 3. make Vim exit, it will kill the shell |
| 1040 | let after = [ |
| 1041 | \ a:line1, |
| 1042 | \ 'let buf = bufnr("%")', |
| 1043 | \ 'while term_getline(buf, 1) =~ "^\\s*$"', |
| 1044 | \ ' sleep 10m', |
| 1045 | \ 'endwhile', |
| 1046 | \ a:line2, |
| 1047 | \ 'au VimLeavePre * call writefile(["done"], "Xdone")', |
| 1048 | \ 'qall', |
| 1049 | \ ] |
| 1050 | if !RunVim([], after, '') |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1051 | return |
| 1052 | endif |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1053 | call assert_equal("done", readfile("Xdone")[0]) |
| 1054 | call delete("Xdone") |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1055 | endfunc |
| 1056 | |
| 1057 | " Run Vim in a terminal, then start a terminal in that Vim with a kill |
| 1058 | " argument, check that :qall works. |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1059 | func Test_terminal_qall_kill_arg() |
| 1060 | call Run_terminal_qall_kill('term ++kill=kill', '') |
| 1061 | endfunc |
| 1062 | |
| 1063 | " Run Vim, start a terminal in that Vim, set the kill argument with |
| 1064 | " term_setkill(), check that :qall works. |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1065 | func Test_terminal_qall_kill_func() |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 1066 | call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")') |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1067 | endfunc |
| 1068 | |
| 1069 | " Run Vim, start a terminal in that Vim without the kill argument, |
| 1070 | " check that :qall does not exit, :qall! does. |
| 1071 | func Test_terminal_qall_exit() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 1072 | let after =<< trim [CODE] |
| 1073 | term |
| 1074 | let buf = bufnr("%") |
| 1075 | while term_getline(buf, 1) =~ "^\\s*$" |
| 1076 | sleep 10m |
| 1077 | endwhile |
| 1078 | set nomore |
| 1079 | au VimLeavePre * call writefile(["too early"], "Xdone") |
| 1080 | qall |
| 1081 | au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone") |
| 1082 | cquit |
| 1083 | [CODE] |
| 1084 | |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1085 | if !RunVim([], after, '') |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1086 | return |
| 1087 | endif |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1088 | call assert_equal("done", readfile("Xdone")[0]) |
| 1089 | call delete("Xdone") |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1090 | endfunc |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1091 | |
| 1092 | " Run Vim in a terminal, then start a terminal in that Vim without a kill |
| 1093 | " argument, check that :confirm qall works. |
| 1094 | func Test_terminal_qall_prompt() |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 1095 | CheckRunVimInTerminal |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1096 | let buf = RunVimInTerminal('', {}) |
| 1097 | |
| 1098 | " Open a terminal window and wait for the prompt to appear |
| 1099 | call term_sendkeys(buf, ":term\<CR>") |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1100 | call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))}) |
| 1101 | call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))}) |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1102 | |
| 1103 | " make Vim exit, it will prompt to kill the shell |
| 1104 | call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>") |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1105 | call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))}) |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1106 | call term_sendkeys(buf, "y") |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1107 | call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1108 | |
| 1109 | " close the terminal window where Vim was running |
| 1110 | quit |
| 1111 | endfunc |
Bram Moolenaar | b852c3e | 2018-03-11 16:55:36 +0100 | [diff] [blame] | 1112 | |
Bram Moolenaar | 4d14bac | 2019-10-20 21:15:15 +0200 | [diff] [blame] | 1113 | " Run Vim in a terminal, then start a terminal window with a shell and check |
| 1114 | " that Vim exits if it is closed. |
| 1115 | func Test_terminal_exit() |
| 1116 | CheckRunVimInTerminal |
| 1117 | |
| 1118 | let lines =<< trim END |
| 1119 | let winid = win_getid() |
| 1120 | help |
| 1121 | term |
| 1122 | let termid = win_getid() |
| 1123 | call win_gotoid(winid) |
| 1124 | close |
| 1125 | call win_gotoid(termid) |
| 1126 | END |
| 1127 | call writefile(lines, 'XtermExit') |
| 1128 | let buf = RunVimInTerminal('-S XtermExit', #{rows: 10}) |
| 1129 | let job = term_getjob(buf) |
| 1130 | call WaitForAssert({-> assert_equal("run", job_status(job))}) |
| 1131 | |
| 1132 | " quit the shell, it will make Vim exit |
| 1133 | call term_sendkeys(buf, "exit\<CR>") |
| 1134 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
| 1135 | |
| 1136 | call delete('XtermExit') |
| 1137 | endfunc |
| 1138 | |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 1139 | func Test_terminal_open_autocmd() |
Bram Moolenaar | b852c3e | 2018-03-11 16:55:36 +0100 | [diff] [blame] | 1140 | augroup repro |
| 1141 | au! |
| 1142 | au TerminalOpen * let s:called += 1 |
| 1143 | augroup END |
| 1144 | |
| 1145 | let s:called = 0 |
| 1146 | |
| 1147 | " Open a terminal window with :terminal |
| 1148 | terminal |
| 1149 | call assert_equal(1, s:called) |
| 1150 | bwipe! |
| 1151 | |
| 1152 | " Open a terminal window with term_start() |
| 1153 | call term_start(&shell) |
| 1154 | call assert_equal(2, s:called) |
| 1155 | bwipe! |
| 1156 | |
| 1157 | " Open a hidden terminal buffer with :terminal |
| 1158 | terminal ++hidden |
| 1159 | call assert_equal(3, s:called) |
| 1160 | for buf in term_list() |
| 1161 | exe buf . "bwipe!" |
| 1162 | endfor |
| 1163 | |
| 1164 | " Open a hidden terminal buffer with term_start() |
| 1165 | let buf = term_start(&shell, {'hidden': 1}) |
| 1166 | call assert_equal(4, s:called) |
| 1167 | exe buf . "bwipe!" |
| 1168 | |
| 1169 | unlet s:called |
| 1170 | au! repro |
| 1171 | endfunction |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1172 | |
| 1173 | func Check_dump01(off) |
| 1174 | call assert_equal('one two three four five', trim(getline(a:off + 1))) |
| 1175 | call assert_equal('~ Select Word', trim(getline(a:off + 7))) |
Bram Moolenaar | 1834d37 | 2018-03-29 17:40:46 +0200 | [diff] [blame] | 1176 | call assert_equal(':popup PopUp', trim(getline(a:off + 20))) |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1177 | endfunc |
| 1178 | |
Bram Moolenaar | f06b0b6 | 2018-03-29 17:22:24 +0200 | [diff] [blame] | 1179 | func Test_terminal_dumpwrite_composing() |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 1180 | CheckRunVimInTerminal |
Bram Moolenaar | f06b0b6 | 2018-03-29 17:22:24 +0200 | [diff] [blame] | 1181 | let save_enc = &encoding |
| 1182 | set encoding=utf-8 |
| 1183 | call assert_equal(1, winnr('$')) |
| 1184 | |
| 1185 | let text = " a\u0300 e\u0302 o\u0308" |
| 1186 | call writefile([text], 'Xcomposing') |
Bram Moolenaar | 77bfd75 | 2018-04-30 18:03:10 +0200 | [diff] [blame] | 1187 | let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {}) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1188 | call WaitForAssert({-> assert_match(text, term_getline(buf, 1))}) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 1189 | eval 'Xdump'->term_dumpwrite(buf) |
Bram Moolenaar | f06b0b6 | 2018-03-29 17:22:24 +0200 | [diff] [blame] | 1190 | let dumpline = readfile('Xdump')[0] |
| 1191 | call assert_match('|à| |ê| |ö', dumpline) |
| 1192 | |
| 1193 | call StopVimInTerminal(buf) |
| 1194 | call delete('Xcomposing') |
| 1195 | call delete('Xdump') |
| 1196 | let &encoding = save_enc |
| 1197 | endfunc |
| 1198 | |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1199 | " Tests for failures in the term_dumpwrite() function |
| 1200 | func Test_terminal_dumpwrite_errors() |
| 1201 | CheckRunVimInTerminal |
| 1202 | call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:') |
| 1203 | let buf = RunVimInTerminal('', {}) |
| 1204 | call term_wait(buf) |
| 1205 | call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:') |
| 1206 | call assert_fails("call term_dumpwrite(buf, [])", 'E730:') |
| 1207 | call writefile([], 'Xtest.dump') |
| 1208 | call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:') |
| 1209 | call delete('Xtest.dump') |
| 1210 | call assert_fails("call term_dumpwrite(buf, '')", 'E482:') |
| 1211 | call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:') |
Bram Moolenaar | 98f1671 | 2020-05-22 13:34:01 +0200 | [diff] [blame] | 1212 | call test_garbagecollect_now() |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1213 | call StopVimInTerminal(buf) |
| 1214 | call term_wait(buf) |
| 1215 | call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:') |
| 1216 | call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:') |
| 1217 | call assert_equal(0, term_sendkeys(buf, ":q\<CR>")) |
| 1218 | endfunc |
| 1219 | |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1220 | " just testing basic functionality. |
| 1221 | func Test_terminal_dumpload() |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1222 | let curbuf = winbufnr('') |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1223 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1224 | let buf = term_dumpload('dumps/Test_popup_command_01.dump') |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1225 | call assert_equal(2, winnr('$')) |
| 1226 | call assert_equal(20, line('$')) |
| 1227 | call Check_dump01(0) |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1228 | |
| 1229 | " Load another dump in the same window |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 1230 | let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf}) |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1231 | call assert_equal(buf, buf2) |
| 1232 | call assert_notequal('one two three four five', trim(getline(1))) |
| 1233 | |
| 1234 | " Load the first dump again in the same window |
| 1235 | let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf}) |
| 1236 | call assert_equal(buf, buf2) |
| 1237 | call Check_dump01(0) |
| 1238 | |
| 1239 | call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:') |
| 1240 | call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:') |
| 1241 | new |
| 1242 | let closedbuf = winbufnr('') |
| 1243 | quit |
| 1244 | call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:') |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1245 | call assert_fails('call term_dumpload([])', 'E474:') |
| 1246 | call assert_fails('call term_dumpload("xabcy.dump")', 'E485:') |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1247 | |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1248 | quit |
| 1249 | endfunc |
| 1250 | |
Bram Moolenaar | 17efc7f | 2019-10-16 18:11:31 +0200 | [diff] [blame] | 1251 | func Test_terminal_dumpload_dump() |
| 1252 | CheckRunVimInTerminal |
| 1253 | |
| 1254 | let lines =<< trim END |
| 1255 | call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12}) |
| 1256 | END |
| 1257 | call writefile(lines, 'XtermDumpload') |
| 1258 | let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15}) |
| 1259 | call VerifyScreenDump(buf, 'Test_terminal_dumpload', {}) |
| 1260 | |
| 1261 | call StopVimInTerminal(buf) |
| 1262 | call delete('XtermDumpload') |
| 1263 | endfunc |
| 1264 | |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1265 | func Test_terminal_dumpdiff() |
| 1266 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 1267 | eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump') |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1268 | call assert_equal(2, winnr('$')) |
| 1269 | call assert_equal(62, line('$')) |
| 1270 | call Check_dump01(0) |
| 1271 | call Check_dump01(42) |
| 1272 | call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29]) |
| 1273 | quit |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1274 | |
| 1275 | call assert_fails('call term_dumpdiff("X1.dump", [])', 'E474:') |
| 1276 | call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:') |
| 1277 | call writefile([], 'X1.dump') |
| 1278 | call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:') |
| 1279 | call delete('X1.dump') |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1280 | endfunc |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1281 | |
Bram Moolenaar | c3ef896 | 2019-02-15 00:16:13 +0100 | [diff] [blame] | 1282 | func Test_terminal_dumpdiff_swap() |
| 1283 | call assert_equal(1, winnr('$')) |
| 1284 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump') |
| 1285 | call assert_equal(2, winnr('$')) |
| 1286 | call assert_equal(62, line('$')) |
| 1287 | call assert_match('Test_popup_command_01.dump', getline(21)) |
| 1288 | call assert_match('Test_popup_command_03.dump', getline(42)) |
| 1289 | call assert_match('Undo', getline(3)) |
| 1290 | call assert_match('three four five', getline(45)) |
| 1291 | |
| 1292 | normal s |
| 1293 | call assert_match('Test_popup_command_03.dump', getline(21)) |
| 1294 | call assert_match('Test_popup_command_01.dump', getline(42)) |
| 1295 | call assert_match('three four five', getline(3)) |
| 1296 | call assert_match('Undo', getline(45)) |
| 1297 | quit |
Bram Moolenaar | 98f1671 | 2020-05-22 13:34:01 +0200 | [diff] [blame] | 1298 | |
| 1299 | " Diff two terminal dump files with different number of rows |
| 1300 | " Swap the diffs |
| 1301 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump') |
| 1302 | call assert_match('Test_popup_command_01.dump', getline(21)) |
| 1303 | call assert_match('Test_winline_rnu.dump', getline(42)) |
| 1304 | normal s |
| 1305 | call assert_match('Test_winline_rnu.dump', getline(6)) |
| 1306 | call assert_match('Test_popup_command_01.dump', getline(27)) |
| 1307 | quit |
Bram Moolenaar | c3ef896 | 2019-02-15 00:16:13 +0100 | [diff] [blame] | 1308 | endfunc |
| 1309 | |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1310 | func Test_terminal_dumpdiff_options() |
| 1311 | set laststatus=0 |
| 1312 | call assert_equal(1, winnr('$')) |
| 1313 | let height = winheight(0) |
| 1314 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33}) |
| 1315 | call assert_equal(2, winnr('$')) |
| 1316 | call assert_equal(height, winheight(winnr())) |
| 1317 | call assert_equal(33, winwidth(winnr())) |
| 1318 | call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%')) |
| 1319 | quit |
| 1320 | |
| 1321 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1322 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'}) |
| 1323 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | e809a4e | 2019-07-04 17:35:05 +0200 | [diff] [blame] | 1324 | call assert_equal(&columns, winwidth(0)) |
| 1325 | call assert_equal(13, winheight(0)) |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1326 | call assert_equal('something else', bufname('%')) |
| 1327 | quit |
| 1328 | |
| 1329 | call assert_equal(1, winnr('$')) |
| 1330 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1}) |
| 1331 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1332 | call assert_fails("call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'bufnr': -1})", 'E475:') |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1333 | bwipe |
| 1334 | |
| 1335 | set laststatus& |
| 1336 | endfunc |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 1337 | |
Bram Moolenaar | 1077230 | 2019-01-20 18:25:54 +0100 | [diff] [blame] | 1338 | " When drawing the statusline the cursor position may not have been updated |
| 1339 | " yet. |
| 1340 | " 1. create a terminal, make it show 2 lines |
| 1341 | " 2. 0.5 sec later: leave terminal window, execute "i" |
| 1342 | " 3. 0.5 sec later: clear terminal window, now it's 1 line |
| 1343 | " 4. 0.5 sec later: redraw, including statusline (used to trigger bug) |
| 1344 | " 4. 0.5 sec later: should be done, clean up |
| 1345 | func Test_terminal_statusline() |
Bram Moolenaar | adbde3f | 2019-09-08 22:57:14 +0200 | [diff] [blame] | 1346 | CheckUnix |
| 1347 | |
Bram Moolenaar | 1077230 | 2019-01-20 18:25:54 +0100 | [diff] [blame] | 1348 | set statusline=x |
| 1349 | terminal |
| 1350 | let tbuf = bufnr('') |
| 1351 | call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n") |
| 1352 | call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') }) |
| 1353 | call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') }) |
| 1354 | au BufLeave * if &buftype == 'terminal' | silent! normal i | endif |
| 1355 | |
| 1356 | sleep 2 |
| 1357 | exe tbuf . 'bwipe!' |
| 1358 | au! BufLeave |
| 1359 | set statusline= |
| 1360 | endfunc |
Bram Moolenaar | fa1e90c | 2019-04-06 17:47:40 +0200 | [diff] [blame] | 1361 | |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 1362 | func Api_drop_common(options) |
| 1363 | call assert_equal(1, winnr('$')) |
| 1364 | |
| 1365 | " Use the title termcap entries to output the escape sequence. |
| 1366 | call writefile([ |
| 1367 | \ 'set title', |
| 1368 | \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"', |
| 1369 | \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''', |
| 1370 | \ 'redraw', |
| 1371 | \ "set t_ts=", |
| 1372 | \ ], 'Xscript') |
| 1373 | let buf = RunVimInTerminal('-S Xscript', {}) |
| 1374 | call WaitFor({-> bufnr('Xtextfile') > 0}) |
| 1375 | call assert_equal('Xtextfile', expand('%:t')) |
| 1376 | call assert_true(winnr('$') >= 3) |
| 1377 | return buf |
| 1378 | endfunc |
| 1379 | |
| 1380 | func Test_terminal_api_drop_newwin() |
| 1381 | CheckRunVimInTerminal |
| 1382 | let buf = Api_drop_common('') |
| 1383 | call assert_equal(0, &bin) |
| 1384 | call assert_equal('', &fenc) |
| 1385 | |
| 1386 | call StopVimInTerminal(buf) |
| 1387 | call delete('Xscript') |
| 1388 | bwipe Xtextfile |
| 1389 | endfunc |
| 1390 | |
| 1391 | func Test_terminal_api_drop_newwin_bin() |
| 1392 | CheckRunVimInTerminal |
| 1393 | let buf = Api_drop_common(',{"bin":1}') |
| 1394 | call assert_equal(1, &bin) |
| 1395 | |
| 1396 | call StopVimInTerminal(buf) |
| 1397 | call delete('Xscript') |
| 1398 | bwipe Xtextfile |
| 1399 | endfunc |
| 1400 | |
| 1401 | func Test_terminal_api_drop_newwin_binary() |
| 1402 | CheckRunVimInTerminal |
| 1403 | let buf = Api_drop_common(',{"binary":1}') |
| 1404 | call assert_equal(1, &bin) |
| 1405 | |
| 1406 | call StopVimInTerminal(buf) |
| 1407 | call delete('Xscript') |
| 1408 | bwipe Xtextfile |
| 1409 | endfunc |
| 1410 | |
| 1411 | func Test_terminal_api_drop_newwin_nobin() |
| 1412 | CheckRunVimInTerminal |
| 1413 | set binary |
| 1414 | let buf = Api_drop_common(',{"nobin":1}') |
| 1415 | call assert_equal(0, &bin) |
| 1416 | |
| 1417 | call StopVimInTerminal(buf) |
| 1418 | call delete('Xscript') |
| 1419 | bwipe Xtextfile |
| 1420 | set nobinary |
| 1421 | endfunc |
| 1422 | |
| 1423 | func Test_terminal_api_drop_newwin_nobinary() |
| 1424 | CheckRunVimInTerminal |
| 1425 | set binary |
| 1426 | let buf = Api_drop_common(',{"nobinary":1}') |
| 1427 | call assert_equal(0, &bin) |
| 1428 | |
| 1429 | call StopVimInTerminal(buf) |
| 1430 | call delete('Xscript') |
| 1431 | bwipe Xtextfile |
| 1432 | set nobinary |
| 1433 | endfunc |
| 1434 | |
| 1435 | func Test_terminal_api_drop_newwin_ff() |
| 1436 | CheckRunVimInTerminal |
| 1437 | let buf = Api_drop_common(',{"ff":"dos"}') |
| 1438 | call assert_equal("dos", &ff) |
| 1439 | |
| 1440 | call StopVimInTerminal(buf) |
| 1441 | call delete('Xscript') |
| 1442 | bwipe Xtextfile |
| 1443 | endfunc |
| 1444 | |
| 1445 | func Test_terminal_api_drop_newwin_fileformat() |
| 1446 | CheckRunVimInTerminal |
| 1447 | let buf = Api_drop_common(',{"fileformat":"dos"}') |
| 1448 | call assert_equal("dos", &ff) |
| 1449 | |
| 1450 | call StopVimInTerminal(buf) |
| 1451 | call delete('Xscript') |
| 1452 | bwipe Xtextfile |
| 1453 | endfunc |
| 1454 | |
| 1455 | func Test_terminal_api_drop_newwin_enc() |
| 1456 | CheckRunVimInTerminal |
| 1457 | let buf = Api_drop_common(',{"enc":"utf-16"}') |
| 1458 | call assert_equal("utf-16", &fenc) |
| 1459 | |
| 1460 | call StopVimInTerminal(buf) |
| 1461 | call delete('Xscript') |
| 1462 | bwipe Xtextfile |
| 1463 | endfunc |
| 1464 | |
| 1465 | func Test_terminal_api_drop_newwin_encoding() |
| 1466 | CheckRunVimInTerminal |
| 1467 | let buf = Api_drop_common(',{"encoding":"utf-16"}') |
| 1468 | call assert_equal("utf-16", &fenc) |
| 1469 | |
| 1470 | call StopVimInTerminal(buf) |
| 1471 | call delete('Xscript') |
| 1472 | bwipe Xtextfile |
| 1473 | endfunc |
| 1474 | |
| 1475 | func Test_terminal_api_drop_oldwin() |
| 1476 | CheckRunVimInTerminal |
| 1477 | let firstwinid = win_getid() |
| 1478 | split Xtextfile |
| 1479 | let textfile_winid = win_getid() |
| 1480 | call assert_equal(2, winnr('$')) |
| 1481 | call win_gotoid(firstwinid) |
| 1482 | |
| 1483 | " Use the title termcap entries to output the escape sequence. |
| 1484 | call writefile([ |
| 1485 | \ 'set title', |
| 1486 | \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"', |
| 1487 | \ 'let &titlestring = ''["drop","Xtextfile"]''', |
| 1488 | \ 'redraw', |
| 1489 | \ "set t_ts=", |
| 1490 | \ ], 'Xscript') |
| 1491 | let buf = RunVimInTerminal('-S Xscript', {'rows': 10}) |
| 1492 | call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))}) |
| 1493 | call assert_equal(textfile_winid, win_getid()) |
| 1494 | |
| 1495 | call StopVimInTerminal(buf) |
| 1496 | call delete('Xscript') |
| 1497 | bwipe Xtextfile |
| 1498 | endfunc |
| 1499 | |
| 1500 | func Tapi_TryThis(bufnum, arg) |
| 1501 | let g:called_bufnum = a:bufnum |
| 1502 | let g:called_arg = a:arg |
| 1503 | endfunc |
| 1504 | |
| 1505 | func WriteApiCall(funcname) |
| 1506 | " Use the title termcap entries to output the escape sequence. |
| 1507 | call writefile([ |
| 1508 | \ 'set title', |
| 1509 | \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"', |
| 1510 | \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''', |
| 1511 | \ 'redraw', |
| 1512 | \ "set t_ts=", |
| 1513 | \ ], 'Xscript') |
| 1514 | endfunc |
| 1515 | |
| 1516 | func Test_terminal_api_call() |
| 1517 | CheckRunVimInTerminal |
| 1518 | |
| 1519 | unlet! g:called_bufnum |
| 1520 | unlet! g:called_arg |
| 1521 | |
| 1522 | call WriteApiCall('Tapi_TryThis') |
| 1523 | |
| 1524 | " Default |
| 1525 | let buf = RunVimInTerminal('-S Xscript', {}) |
| 1526 | call WaitFor({-> exists('g:called_bufnum')}) |
| 1527 | call assert_equal(buf, g:called_bufnum) |
| 1528 | call assert_equal(['hello', 123], g:called_arg) |
| 1529 | call StopVimInTerminal(buf) |
| 1530 | |
| 1531 | unlet! g:called_bufnum |
| 1532 | unlet! g:called_arg |
| 1533 | |
| 1534 | " Enable explicitly |
| 1535 | let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'}) |
| 1536 | call WaitFor({-> exists('g:called_bufnum')}) |
| 1537 | call assert_equal(buf, g:called_bufnum) |
| 1538 | call assert_equal(['hello', 123], g:called_arg) |
| 1539 | call StopVimInTerminal(buf) |
| 1540 | |
| 1541 | unlet! g:called_bufnum |
| 1542 | unlet! g:called_arg |
| 1543 | |
| 1544 | func! ApiCall_TryThis(bufnum, arg) |
| 1545 | let g:called_bufnum2 = a:bufnum |
| 1546 | let g:called_arg2 = a:arg |
| 1547 | endfunc |
| 1548 | |
| 1549 | call WriteApiCall('ApiCall_TryThis') |
| 1550 | |
| 1551 | " Use prefix match |
| 1552 | let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'}) |
| 1553 | call WaitFor({-> exists('g:called_bufnum2')}) |
| 1554 | call assert_equal(buf, g:called_bufnum2) |
| 1555 | call assert_equal(['hello', 123], g:called_arg2) |
| 1556 | call StopVimInTerminal(buf) |
| 1557 | |
| 1558 | call assert_fails("call term_start('ls', {'term_api' : []})", 'E475:') |
| 1559 | |
| 1560 | unlet! g:called_bufnum2 |
| 1561 | unlet! g:called_arg2 |
| 1562 | |
| 1563 | call delete('Xscript') |
| 1564 | delfunction! ApiCall_TryThis |
| 1565 | unlet! g:called_bufnum2 |
| 1566 | unlet! g:called_arg2 |
| 1567 | endfunc |
| 1568 | |
| 1569 | func Test_terminal_api_call_fails() |
| 1570 | CheckRunVimInTerminal |
| 1571 | |
| 1572 | func! TryThis(bufnum, arg) |
| 1573 | let g:called_bufnum3 = a:bufnum |
| 1574 | let g:called_arg3 = a:arg |
| 1575 | endfunc |
| 1576 | |
| 1577 | call WriteApiCall('TryThis') |
| 1578 | |
| 1579 | unlet! g:called_bufnum3 |
| 1580 | unlet! g:called_arg3 |
| 1581 | |
| 1582 | " Not permitted |
| 1583 | call ch_logfile('Xlog', 'w') |
| 1584 | let buf = RunVimInTerminal('-S Xscript', {'term_api': ''}) |
| 1585 | call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))}) |
| 1586 | call assert_false(exists('g:called_bufnum3')) |
| 1587 | call assert_false(exists('g:called_arg3')) |
| 1588 | call StopVimInTerminal(buf) |
| 1589 | |
| 1590 | " No match |
| 1591 | call ch_logfile('Xlog', 'w') |
| 1592 | let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'}) |
| 1593 | call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'}) |
| 1594 | call assert_false(exists('g:called_bufnum3')) |
| 1595 | call assert_false(exists('g:called_arg3')) |
| 1596 | call StopVimInTerminal(buf) |
| 1597 | |
| 1598 | call delete('Xscript') |
| 1599 | call ch_logfile('') |
| 1600 | call delete('Xlog') |
| 1601 | delfunction! TryThis |
| 1602 | unlet! g:called_bufnum3 |
| 1603 | unlet! g:called_arg3 |
| 1604 | endfunc |
| 1605 | |
| 1606 | let s:caught_e937 = 0 |
| 1607 | |
| 1608 | func Tapi_Delete(bufnum, arg) |
| 1609 | try |
| 1610 | execute 'bdelete!' a:bufnum |
| 1611 | catch /E937:/ |
| 1612 | let s:caught_e937 = 1 |
| 1613 | endtry |
| 1614 | endfunc |
| 1615 | |
| 1616 | func Test_terminal_api_call_fail_delete() |
| 1617 | CheckRunVimInTerminal |
| 1618 | |
| 1619 | call WriteApiCall('Tapi_Delete') |
| 1620 | let buf = RunVimInTerminal('-S Xscript', {}) |
| 1621 | call WaitForAssert({-> assert_equal(1, s:caught_e937)}) |
| 1622 | |
| 1623 | call StopVimInTerminal(buf) |
| 1624 | call delete('Xscript') |
| 1625 | call ch_logfile('', '') |
| 1626 | endfunc |
| 1627 | |
| 1628 | func Test_terminal_setapi_and_call() |
| 1629 | CheckRunVimInTerminal |
| 1630 | |
| 1631 | call WriteApiCall('Tapi_TryThis') |
| 1632 | call ch_logfile('Xlog', 'w') |
| 1633 | |
| 1634 | unlet! g:called_bufnum |
| 1635 | unlet! g:called_arg |
| 1636 | |
| 1637 | let buf = RunVimInTerminal('-S Xscript', {'term_api': ''}) |
| 1638 | call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))}) |
| 1639 | call assert_false(exists('g:called_bufnum')) |
| 1640 | call assert_false(exists('g:called_arg')) |
| 1641 | |
| 1642 | eval buf->term_setapi('Tapi_') |
| 1643 | call term_sendkeys(buf, ":set notitle\<CR>") |
| 1644 | call term_sendkeys(buf, ":source Xscript\<CR>") |
| 1645 | call WaitFor({-> exists('g:called_bufnum')}) |
| 1646 | call assert_equal(buf, g:called_bufnum) |
| 1647 | call assert_equal(['hello', 123], g:called_arg) |
| 1648 | |
| 1649 | call StopVimInTerminal(buf) |
| 1650 | |
| 1651 | call delete('Xscript') |
| 1652 | call ch_logfile('') |
| 1653 | call delete('Xlog') |
| 1654 | unlet! g:called_bufnum |
| 1655 | unlet! g:called_arg |
| 1656 | endfunc |
| 1657 | |
| 1658 | func Test_terminal_api_arg() |
| 1659 | CheckRunVimInTerminal |
| 1660 | |
| 1661 | call WriteApiCall('Tapi_TryThis') |
| 1662 | call ch_logfile('Xlog', 'w') |
| 1663 | |
| 1664 | unlet! g:called_bufnum |
| 1665 | unlet! g:called_arg |
| 1666 | |
| 1667 | execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript' |
| 1668 | let buf = bufnr('%') |
| 1669 | call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))}) |
| 1670 | call assert_false(exists('g:called_bufnum')) |
| 1671 | call assert_false(exists('g:called_arg')) |
| 1672 | |
| 1673 | call StopVimInTerminal(buf) |
| 1674 | |
| 1675 | call ch_logfile('Xlog', 'w') |
| 1676 | |
| 1677 | execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript' |
| 1678 | let buf = bufnr('%') |
| 1679 | call WaitFor({-> exists('g:called_bufnum')}) |
| 1680 | call assert_equal(buf, g:called_bufnum) |
| 1681 | call assert_equal(['hello', 123], g:called_arg) |
| 1682 | |
| 1683 | call StopVimInTerminal(buf) |
| 1684 | |
| 1685 | call delete('Xscript') |
| 1686 | call ch_logfile('') |
| 1687 | call delete('Xlog') |
| 1688 | unlet! g:called_bufnum |
| 1689 | unlet! g:called_arg |
| 1690 | endfunc |
| 1691 | |
| 1692 | func Test_terminal_ansicolors_default() |
| 1693 | CheckFunction term_getansicolors |
| 1694 | |
| 1695 | let colors = [ |
| 1696 | \ '#000000', '#e00000', |
| 1697 | \ '#00e000', '#e0e000', |
| 1698 | \ '#0000e0', '#e000e0', |
| 1699 | \ '#00e0e0', '#e0e0e0', |
| 1700 | \ '#808080', '#ff4040', |
| 1701 | \ '#40ff40', '#ffff40', |
| 1702 | \ '#4040ff', '#ff40ff', |
| 1703 | \ '#40ffff', '#ffffff', |
| 1704 | \] |
| 1705 | |
| 1706 | let buf = Run_shell_in_terminal({}) |
| 1707 | call assert_equal(colors, term_getansicolors(buf)) |
| 1708 | call StopShellInTerminal(buf) |
| 1709 | call TermWait(buf) |
| 1710 | call assert_equal([], term_getansicolors(buf)) |
| 1711 | |
| 1712 | exe buf . 'bwipe' |
| 1713 | endfunc |
| 1714 | |
| 1715 | let s:test_colors = [ |
| 1716 | \ '#616e64', '#0d0a79', |
| 1717 | \ '#6d610d', '#0a7373', |
| 1718 | \ '#690d0a', '#6d696e', |
| 1719 | \ '#0d0a6f', '#616e0d', |
| 1720 | \ '#0a6479', '#6d0d0a', |
| 1721 | \ '#617373', '#0d0a69', |
| 1722 | \ '#6d690d', '#0a6e6f', |
| 1723 | \ '#610d0a', '#6e6479', |
| 1724 | \] |
| 1725 | |
| 1726 | func Test_terminal_ansicolors_global() |
| 1727 | CheckFeature termguicolors |
| 1728 | CheckFunction term_getansicolors |
| 1729 | |
| 1730 | let g:terminal_ansi_colors = reverse(copy(s:test_colors)) |
| 1731 | let buf = Run_shell_in_terminal({}) |
| 1732 | call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf)) |
| 1733 | call StopShellInTerminal(buf) |
| 1734 | call TermWait(buf) |
| 1735 | |
| 1736 | exe buf . 'bwipe' |
| 1737 | unlet g:terminal_ansi_colors |
| 1738 | endfunc |
| 1739 | |
| 1740 | func Test_terminal_ansicolors_func() |
| 1741 | CheckFeature termguicolors |
| 1742 | CheckFunction term_getansicolors |
| 1743 | |
| 1744 | let g:terminal_ansi_colors = reverse(copy(s:test_colors)) |
| 1745 | let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors}) |
| 1746 | call assert_equal(s:test_colors, term_getansicolors(buf)) |
| 1747 | |
| 1748 | call term_setansicolors(buf, g:terminal_ansi_colors) |
| 1749 | call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors()) |
| 1750 | |
| 1751 | let colors = [ |
| 1752 | \ 'ivory', 'AliceBlue', |
| 1753 | \ 'grey67', 'dark goldenrod', |
| 1754 | \ 'SteelBlue3', 'PaleVioletRed4', |
| 1755 | \ 'MediumPurple2', 'yellow2', |
| 1756 | \ 'RosyBrown3', 'OrangeRed2', |
| 1757 | \ 'white smoke', 'navy blue', |
| 1758 | \ 'grey47', 'gray97', |
| 1759 | \ 'MistyRose2', 'DodgerBlue4', |
| 1760 | \] |
| 1761 | eval buf->term_setansicolors(colors) |
| 1762 | |
| 1763 | let colors[4] = 'Invalid' |
| 1764 | call assert_fails('call term_setansicolors(buf, colors)', 'E474:') |
| 1765 | call assert_fails('call term_setansicolors(buf, {})', 'E714:') |
| 1766 | |
| 1767 | call StopShellInTerminal(buf) |
| 1768 | call TermWait(buf) |
| 1769 | call assert_equal(0, term_setansicolors(buf, [])) |
| 1770 | exe buf . 'bwipe' |
| 1771 | endfunc |
| 1772 | |
| 1773 | func Test_terminal_all_ansi_colors() |
| 1774 | CheckRunVimInTerminal |
| 1775 | |
| 1776 | " Use all the ANSI colors. |
| 1777 | call writefile([ |
| 1778 | \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")', |
| 1779 | \ 'hi Tblack ctermfg=0 ctermbg=8', |
| 1780 | \ 'hi Tdarkred ctermfg=1 ctermbg=9', |
| 1781 | \ 'hi Tdarkgreen ctermfg=2 ctermbg=10', |
| 1782 | \ 'hi Tbrown ctermfg=3 ctermbg=11', |
| 1783 | \ 'hi Tdarkblue ctermfg=4 ctermbg=12', |
| 1784 | \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13', |
| 1785 | \ 'hi Tdarkcyan ctermfg=6 ctermbg=14', |
| 1786 | \ 'hi Tlightgrey ctermfg=7 ctermbg=15', |
| 1787 | \ 'hi Tdarkgrey ctermfg=8 ctermbg=0', |
| 1788 | \ 'hi Tred ctermfg=9 ctermbg=1', |
| 1789 | \ 'hi Tgreen ctermfg=10 ctermbg=2', |
| 1790 | \ 'hi Tyellow ctermfg=11 ctermbg=3', |
| 1791 | \ 'hi Tblue ctermfg=12 ctermbg=4', |
| 1792 | \ 'hi Tmagenta ctermfg=13 ctermbg=5', |
| 1793 | \ 'hi Tcyan ctermfg=14 ctermbg=6', |
| 1794 | \ 'hi Twhite ctermfg=15 ctermbg=7', |
| 1795 | \ 'hi TdarkredBold ctermfg=1 cterm=bold', |
| 1796 | \ 'hi TgreenBold ctermfg=10 cterm=bold', |
| 1797 | \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5', |
| 1798 | \ '', |
| 1799 | \ 'call matchadd("Tblack", "A")', |
| 1800 | \ 'call matchadd("Tdarkred", "B")', |
| 1801 | \ 'call matchadd("Tdarkgreen", "C")', |
| 1802 | \ 'call matchadd("Tbrown", "D")', |
| 1803 | \ 'call matchadd("Tdarkblue", "E")', |
| 1804 | \ 'call matchadd("Tdarkmagenta", "F")', |
| 1805 | \ 'call matchadd("Tdarkcyan", "G")', |
| 1806 | \ 'call matchadd("Tlightgrey", "H")', |
| 1807 | \ 'call matchadd("Tdarkgrey", "I")', |
| 1808 | \ 'call matchadd("Tred", "J")', |
| 1809 | \ 'call matchadd("Tgreen", "K")', |
| 1810 | \ 'call matchadd("Tyellow", "L")', |
| 1811 | \ 'call matchadd("Tblue", "M")', |
| 1812 | \ 'call matchadd("Tmagenta", "N")', |
| 1813 | \ 'call matchadd("Tcyan", "O")', |
| 1814 | \ 'call matchadd("Twhite", "P")', |
| 1815 | \ 'call matchadd("TdarkredBold", "X")', |
| 1816 | \ 'call matchadd("TgreenBold", "Y")', |
| 1817 | \ 'call matchadd("TmagentaBold", "Z")', |
| 1818 | \ 'redraw', |
| 1819 | \ ], 'Xcolorscript') |
| 1820 | let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10}) |
| 1821 | call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {}) |
| 1822 | |
| 1823 | call term_sendkeys(buf, ":q\<CR>") |
| 1824 | call StopVimInTerminal(buf) |
| 1825 | call delete('Xcolorscript') |
| 1826 | endfunc |
| 1827 | |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1828 | |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1829 | " vim: shiftwidth=2 sts=2 expandtab |