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() |
ichizok | ae1bd87 | 2022-01-20 14:57:29 +0000 | [diff] [blame] | 17 | call test_override('vterm_title', 1) |
Bram Moolenaar | 606cb8b | 2018-05-03 20:40:20 +0200 | [diff] [blame] | 18 | au TerminalOpen * let b:done = 'yes' |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 19 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | b00fdf6 | 2017-09-21 22:16:21 +0200 | [diff] [blame] | 20 | |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 21 | call assert_equal('t', mode()) |
Bram Moolenaar | b00fdf6 | 2017-09-21 22:16:21 +0200 | [diff] [blame] | 22 | call assert_equal('yes', b:done) |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 23 | call assert_match('%aR[^\n]*running]', execute('ls')) |
Bram Moolenaar | 0751f51 | 2018-03-29 16:37:16 +0200 | [diff] [blame] | 24 | call assert_match('%aR[^\n]*running]', execute('ls R')) |
| 25 | call assert_notmatch('%[^\n]*running]', execute('ls F')) |
| 26 | call assert_notmatch('%[^\n]*running]', execute('ls ?')) |
Bram Moolenaar | 004a678 | 2020-04-11 17:09:31 +0200 | [diff] [blame] | 27 | call assert_fails('set modifiable', 'E946:') |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 28 | |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 29 | call StopShellInTerminal(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 |
ichizok | ae1bd87 | 2022-01-20 14:57:29 +0000 | [diff] [blame] | 41 | call test_override('ALL', 0) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 42 | unlet g:job |
| 43 | endfunc |
| 44 | |
Bram Moolenaar | 00806bc | 2020-11-05 19:36:38 +0100 | [diff] [blame] | 45 | func Test_terminal_no_name() |
| 46 | let buf = Run_shell_in_terminal({}) |
| 47 | call assert_match('^!', bufname(buf)) |
| 48 | 0file |
| 49 | call assert_equal("", bufname(buf)) |
| 50 | call assert_match('\[No Name\]', execute('file')) |
| 51 | call StopShellInTerminal(buf) |
Bram Moolenaar | 00806bc | 2020-11-05 19:36:38 +0100 | [diff] [blame] | 52 | endfunc |
| 53 | |
Bram Moolenaar | 28ed4df | 2019-10-26 16:21:40 +0200 | [diff] [blame] | 54 | func Test_terminal_TerminalWinOpen() |
| 55 | au TerminalWinOpen * let b:done = 'yes' |
| 56 | let buf = Run_shell_in_terminal({}) |
| 57 | call assert_equal('yes', b:done) |
| 58 | call StopShellInTerminal(buf) |
| 59 | " closing window wipes out the terminal buffer with the finished job |
| 60 | close |
| 61 | |
| 62 | if has("unix") |
| 63 | terminal ++hidden ++open sleep 1 |
| 64 | sleep 1 |
| 65 | call assert_fails("echo b:done", 'E121:') |
| 66 | endif |
| 67 | |
| 68 | au! TerminalWinOpen |
| 69 | endfunc |
| 70 | |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 71 | func Test_terminal_make_change() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 72 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 73 | call StopShellInTerminal(buf) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 74 | |
| 75 | setlocal modifiable |
| 76 | exe "normal Axxx\<Esc>" |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 77 | call assert_fails(buf . 'bwipe', 'E89:') |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 78 | undo |
| 79 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 80 | exe buf . 'bwipe' |
| 81 | unlet g:job |
| 82 | endfunc |
| 83 | |
Bram Moolenaar | 5b868a8 | 2019-02-25 06:11:53 +0100 | [diff] [blame] | 84 | func Test_terminal_paste_register() |
| 85 | let @" = "text to paste" |
| 86 | |
| 87 | let buf = Run_shell_in_terminal({}) |
| 88 | " Wait for the shell to display a prompt |
| 89 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
| 90 | |
| 91 | call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt') |
| 92 | call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))}) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 93 | call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())}) |
Bram Moolenaar | 5b868a8 | 2019-02-25 06:11:53 +0100 | [diff] [blame] | 94 | |
| 95 | exe buf . 'bwipe!' |
| 96 | unlet g:job |
| 97 | endfunc |
| 98 | |
Yee Cheng Chin | 4282633 | 2022-10-10 11:46:16 +0100 | [diff] [blame] | 99 | func Test_terminal_unload_buffer() |
| 100 | let buf = Run_shell_in_terminal({}) |
| 101 | call assert_fails(buf . 'bunload', 'E948:') |
| 102 | exe buf . 'bunload!' |
| 103 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
| 104 | call assert_equal("", bufname(buf)) |
| 105 | |
| 106 | unlet g:job |
| 107 | endfunc |
| 108 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 109 | func Test_terminal_wipe_buffer() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 110 | let buf = Run_shell_in_terminal({}) |
Yee Cheng Chin | 15b314f | 2022-10-09 18:53:32 +0100 | [diff] [blame] | 111 | call assert_fails(buf . 'bwipe', 'E948:') |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 112 | exe buf . 'bwipe!' |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 113 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 114 | call assert_equal("", bufname(buf)) |
| 115 | |
| 116 | unlet g:job |
| 117 | endfunc |
| 118 | |
Yee Cheng Chin | 15b314f | 2022-10-09 18:53:32 +0100 | [diff] [blame] | 119 | " Test that using ':confirm bwipe' on terminal works |
| 120 | func Test_terminal_confirm_wipe_buffer() |
| 121 | CheckUnix |
| 122 | CheckNotGui |
| 123 | CheckFeature dialog_con |
| 124 | let buf = Run_shell_in_terminal({}) |
| 125 | call assert_fails(buf . 'bwipe', 'E948:') |
| 126 | call feedkeys('n', 'L') |
| 127 | call assert_fails('confirm ' .. buf .. 'bwipe', 'E517:') |
| 128 | call assert_equal(buf, bufnr()) |
| 129 | call assert_equal(1, &modified) |
| 130 | call feedkeys('y', 'L') |
| 131 | exe 'confirm ' .. buf .. 'bwipe' |
| 132 | call assert_notequal(buf, bufnr()) |
| 133 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
| 134 | call assert_equal("", bufname(buf)) |
| 135 | |
| 136 | unlet g:job |
| 137 | endfunc |
| 138 | |
| 139 | " Test that using :b! will hide the terminal |
| 140 | func Test_terminal_goto_buffer() |
| 141 | let buf_mod = bufnr() |
| 142 | let buf_term = Run_shell_in_terminal({}) |
| 143 | call assert_equal(buf_term, bufnr()) |
| 144 | call assert_fails(buf_mod . 'b', 'E948:') |
| 145 | exe buf_mod . 'b!' |
| 146 | call assert_equal(buf_mod, bufnr()) |
| 147 | call assert_equal('run', job_status(g:job)) |
| 148 | call assert_notequal('', bufname(buf_term)) |
| 149 | exec buf_mod .. 'bwipe!' |
| 150 | exec buf_term .. 'bwipe!' |
| 151 | |
| 152 | unlet g:job |
| 153 | endfunc |
| 154 | |
| 155 | " Test that using ':confirm :b' will kill terminal |
| 156 | func Test_terminal_confirm_goto_buffer() |
| 157 | CheckUnix |
| 158 | CheckNotGui |
| 159 | CheckFeature dialog_con |
| 160 | let buf_mod = bufnr() |
| 161 | let buf_term = Run_shell_in_terminal({}) |
| 162 | call feedkeys('n', 'L') |
| 163 | exe 'confirm ' .. buf_mod .. 'b' |
| 164 | call assert_equal(buf_term, bufnr()) |
| 165 | call feedkeys('y', 'L') |
| 166 | exec 'confirm ' .. buf_mod .. 'b' |
| 167 | call assert_equal(buf_mod, bufnr()) |
| 168 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
| 169 | call assert_equal("", bufname(buf_term)) |
| 170 | exec buf_mod .. 'bwipe!' |
| 171 | |
| 172 | unlet g:job |
| 173 | endfunc |
| 174 | |
| 175 | " Test that using :close! will hide the terminal |
| 176 | func Test_terminal_close_win() |
| 177 | let buf = Run_shell_in_terminal({}) |
| 178 | call assert_equal(buf, bufnr()) |
| 179 | call assert_fails('close', 'E948:') |
| 180 | close! |
| 181 | call assert_notequal(buf, bufnr()) |
| 182 | call assert_equal('run', job_status(g:job)) |
| 183 | call assert_notequal('', bufname(buf)) |
| 184 | exec buf .. 'bwipe!' |
| 185 | |
| 186 | unlet g:job |
| 187 | endfunc |
| 188 | |
| 189 | " Test that using ':confirm close' will kill terminal |
| 190 | func Test_terminal_confirm_close_win() |
| 191 | CheckUnix |
| 192 | CheckNotGui |
| 193 | CheckFeature dialog_con |
| 194 | let buf = Run_shell_in_terminal({}) |
| 195 | call feedkeys('n', 'L') |
| 196 | confirm close |
| 197 | call assert_equal(buf, bufnr()) |
| 198 | call feedkeys('y', 'L') |
| 199 | confirm close |
| 200 | call assert_notequal(buf, bufnr()) |
| 201 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
| 202 | call assert_equal("", bufname(buf)) |
| 203 | |
| 204 | unlet g:job |
| 205 | endfunc |
| 206 | |
| 207 | " Test that using :quit! will kill the terminal |
| 208 | func Test_terminal_quit() |
| 209 | let buf = Run_shell_in_terminal({}) |
| 210 | call assert_equal(buf, bufnr()) |
| 211 | call assert_fails('quit', 'E948:') |
| 212 | quit! |
| 213 | call assert_notequal(buf, bufnr()) |
| 214 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Yee Cheng Chin | 4282633 | 2022-10-10 11:46:16 +0100 | [diff] [blame] | 215 | call assert_equal("", bufname(buf)) |
Yee Cheng Chin | 15b314f | 2022-10-09 18:53:32 +0100 | [diff] [blame] | 216 | |
| 217 | unlet g:job |
| 218 | endfunc |
| 219 | |
| 220 | " Test that using ':confirm quit' will kill terminal |
| 221 | func Test_terminal_confirm_quit() |
| 222 | CheckUnix |
| 223 | CheckNotGui |
| 224 | CheckFeature dialog_con |
| 225 | let buf = Run_shell_in_terminal({}) |
| 226 | call feedkeys('n', 'L') |
| 227 | confirm quit |
| 228 | call assert_equal(buf, bufnr()) |
| 229 | call feedkeys('y', 'L') |
| 230 | confirm quit |
| 231 | call assert_notequal(buf, bufnr()) |
| 232 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
| 233 | |
| 234 | unlet g:job |
| 235 | endfunc |
| 236 | |
| 237 | " Test :q or :next |
| 238 | |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 239 | func Test_terminal_split_quit() |
| 240 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 241 | split |
| 242 | quit! |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 243 | call TermWait(buf) |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 244 | sleep 50m |
| 245 | call assert_equal('run', job_status(g:job)) |
| 246 | |
| 247 | quit! |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 248 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 249 | |
Yee Cheng Chin | 4282633 | 2022-10-10 11:46:16 +0100 | [diff] [blame] | 250 | call assert_equal("", bufname(buf)) |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 251 | unlet g:job |
| 252 | endfunc |
| 253 | |
Bram Moolenaar | c9f8b84 | 2020-11-24 19:36:16 +0100 | [diff] [blame] | 254 | func Test_terminal_hide_buffer_job_running() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 255 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 256 | setlocal bufhidden=hide |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 257 | quit |
| 258 | for nr in range(1, winnr('$')) |
| 259 | call assert_notequal(winbufnr(nr), buf) |
| 260 | endfor |
| 261 | call assert_true(bufloaded(buf)) |
| 262 | call assert_true(buflisted(buf)) |
| 263 | |
| 264 | exe 'split ' . buf . 'buf' |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 265 | call StopShellInTerminal(buf) |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 266 | exe buf . 'bwipe' |
| 267 | |
| 268 | unlet g:job |
| 269 | endfunc |
| 270 | |
Bram Moolenaar | c9f8b84 | 2020-11-24 19:36:16 +0100 | [diff] [blame] | 271 | func Test_terminal_hide_buffer_job_finished() |
| 272 | term echo hello |
| 273 | let buf = bufnr() |
Bram Moolenaar | c9f8b84 | 2020-11-24 19:36:16 +0100 | [diff] [blame] | 274 | call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) |
Yee Cheng Chin | 4282633 | 2022-10-10 11:46:16 +0100 | [diff] [blame] | 275 | |
Bram Moolenaar | c9f8b84 | 2020-11-24 19:36:16 +0100 | [diff] [blame] | 276 | call assert_true(bufloaded(buf)) |
| 277 | call assert_true(buflisted(buf)) |
Yee Cheng Chin | 4282633 | 2022-10-10 11:46:16 +0100 | [diff] [blame] | 278 | |
| 279 | " Test :hide |
| 280 | hide |
| 281 | call assert_true(bufloaded(buf)) |
| 282 | call assert_true(buflisted(buf)) |
| 283 | split |
| 284 | exe buf .. 'buf' |
| 285 | call assert_equal(buf, bufnr()) |
| 286 | |
| 287 | " Test bufhidden, which exercises a different code path |
| 288 | setlocal bufhidden=hide |
Bram Moolenaar | c9f8b84 | 2020-11-24 19:36:16 +0100 | [diff] [blame] | 289 | edit Xasdfasdf |
| 290 | call assert_true(bufloaded(buf)) |
| 291 | call assert_true(buflisted(buf)) |
| 292 | exe buf .. 'buf' |
| 293 | call assert_equal(buf, bufnr()) |
| 294 | setlocal bufhidden= |
Yee Cheng Chin | 4282633 | 2022-10-10 11:46:16 +0100 | [diff] [blame] | 295 | |
Bram Moolenaar | c9f8b84 | 2020-11-24 19:36:16 +0100 | [diff] [blame] | 296 | edit Xasdfasdf |
| 297 | call assert_false(bufloaded(buf)) |
| 298 | call assert_false(buflisted(buf)) |
| 299 | bwipe Xasdfasdf |
| 300 | endfunc |
| 301 | |
Bram Moolenaar | 3ad6953 | 2021-11-19 17:01:08 +0000 | [diff] [blame] | 302 | func Test_terminal_rename_buffer() |
| 303 | let cmd = Get_cat_123_cmd() |
| 304 | let buf = term_start(cmd, {'term_name': 'foo'}) |
| 305 | call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) |
| 306 | call assert_equal('foo', bufname()) |
| 307 | call assert_match('foo.*finished', execute('ls')) |
| 308 | file bar |
| 309 | call assert_equal('bar', bufname()) |
| 310 | call assert_match('bar.*finished', execute('ls')) |
| 311 | exe 'bwipe! ' .. buf |
| 312 | endfunc |
| 313 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 314 | func s:Nasty_exit_cb(job, st) |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 315 | exe g:buf . 'bwipe!' |
| 316 | let g:buf = 0 |
| 317 | endfunc |
| 318 | |
Bram Moolenaar | 9d18961 | 2017-09-09 18:11:00 +0200 | [diff] [blame] | 319 | func Get_cat_123_cmd() |
| 320 | if has('win32') |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 321 | if !has('conpty') |
| 322 | return 'cmd /c "cls && color 2 && echo 123"' |
| 323 | else |
| 324 | " When clearing twice, extra sequence is not output. |
| 325 | return 'cmd /c "cls && cls && color 2 && echo 123"' |
| 326 | endif |
Bram Moolenaar | 9d18961 | 2017-09-09 18:11:00 +0200 | [diff] [blame] | 327 | else |
| 328 | call writefile(["\<Esc>[32m123"], 'Xtext') |
| 329 | return "cat Xtext" |
| 330 | endif |
| 331 | endfunc |
| 332 | |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 333 | func Test_terminal_nasty_cb() |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 334 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 335 | let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')}) |
| 336 | let g:job = term_getjob(g:buf) |
| 337 | |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 338 | call WaitForAssert({-> assert_equal("dead", job_status(g:job))}) |
| 339 | call WaitForAssert({-> assert_equal(0, g:buf)}) |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 340 | unlet g:job |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 341 | unlet g:buf |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 342 | call delete('Xtext') |
| 343 | endfunc |
| 344 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 345 | func Check_123(buf) |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 346 | let l = term_scrape(a:buf, 0) |
| 347 | call assert_true(len(l) == 0) |
| 348 | let l = term_scrape(a:buf, 999) |
| 349 | call assert_true(len(l) == 0) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 350 | let l = a:buf->term_scrape(1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 351 | call assert_true(len(l) > 0) |
| 352 | call assert_equal('1', l[0].chars) |
| 353 | call assert_equal('2', l[1].chars) |
| 354 | call assert_equal('3', l[2].chars) |
| 355 | call assert_equal('#00e000', l[0].fg) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 356 | call assert_equal(0, term_getattr(l[0].attr, 'bold')) |
| 357 | call assert_equal(0, l[0].attr->term_getattr('italic')) |
Bram Moolenaar | 81df635 | 2018-12-22 18:25:30 +0100 | [diff] [blame] | 358 | if has('win32') |
| 359 | " On Windows 'background' always defaults to dark, even though the terminal |
| 360 | " may use a light background. Therefore accept both white and black. |
| 361 | call assert_match('#ffffff\|#000000', l[0].bg) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 362 | else |
Bram Moolenaar | 81df635 | 2018-12-22 18:25:30 +0100 | [diff] [blame] | 363 | if &background == 'light' |
| 364 | call assert_equal('#ffffff', l[0].bg) |
| 365 | else |
| 366 | call assert_equal('#000000', l[0].bg) |
| 367 | endif |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 368 | endif |
| 369 | |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 370 | let l = term_getline(a:buf, -1) |
| 371 | call assert_equal('', l) |
| 372 | let l = term_getline(a:buf, 0) |
| 373 | call assert_equal('', l) |
| 374 | let l = term_getline(a:buf, 999) |
| 375 | call assert_equal('', l) |
Bram Moolenaar | 9c84484 | 2017-08-01 18:41:21 +0200 | [diff] [blame] | 376 | let l = term_getline(a:buf, 1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 377 | call assert_equal('123', l) |
| 378 | endfunc |
| 379 | |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 380 | func Test_terminal_scrape_123() |
| 381 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 382 | let buf = term_start(cmd) |
| 383 | |
| 384 | let termlist = term_list() |
| 385 | call assert_equal(1, len(termlist)) |
| 386 | call assert_equal(buf, termlist[0]) |
| 387 | |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 388 | " Nothing happens with invalid buffer number |
| 389 | call term_wait(1234) |
| 390 | |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 391 | call TermWait(buf) |
Bram Moolenaar | 1783337 | 2017-09-04 22:23:19 +0200 | [diff] [blame] | 392 | " 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] | 393 | " "cls" to happen, after that we have one line with three characters. |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 394 | call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))}) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 395 | call Check_123(buf) |
| 396 | |
| 397 | " Must still work after the job ended. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 398 | let job = term_getjob(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 399 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 400 | call TermWait(buf) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 401 | call Check_123(buf) |
| 402 | |
| 403 | exe buf . 'bwipe' |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 404 | call delete('Xtext') |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 405 | endfunc |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 406 | |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 407 | func Test_terminal_scrape_multibyte() |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 408 | call writefile(["léttまrs"], 'Xtext', 'D') |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 409 | if has('win32') |
Bram Moolenaar | 3678393 | 2017-08-14 23:07:30 +0200 | [diff] [blame] | 410 | " Run cmd with UTF-8 codepage to make the type command print the expected |
| 411 | " multibyte characters. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 412 | let buf = term_start("cmd /K chcp 65001") |
| 413 | call term_sendkeys(buf, "type Xtext\<CR>") |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 414 | eval buf->term_sendkeys("exit\<CR>") |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 415 | let line = 4 |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 416 | else |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 417 | let buf = term_start("cat Xtext") |
| 418 | let line = 1 |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 419 | endif |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 420 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 421 | call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"}) |
| 422 | let l = term_scrape(buf, line) |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 423 | call assert_true(len(l) >= 7) |
| 424 | call assert_equal('l', l[0].chars) |
| 425 | call assert_equal('é', l[1].chars) |
| 426 | call assert_equal(1, l[1].width) |
| 427 | call assert_equal('t', l[2].chars) |
| 428 | call assert_equal('t', l[3].chars) |
| 429 | call assert_equal('ま', l[4].chars) |
| 430 | call assert_equal(2, l[4].width) |
| 431 | call assert_equal('r', l[5].chars) |
| 432 | call assert_equal('s', l[6].chars) |
| 433 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 434 | let job = term_getjob(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 435 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 436 | call TermWait(buf) |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 437 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 438 | exe buf . 'bwipe' |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 439 | endfunc |
| 440 | |
Bram Moolenaar | 8b89614 | 2020-08-02 15:05:05 +0200 | [diff] [blame] | 441 | func Test_terminal_one_column() |
| 442 | " This creates a terminal, displays a double-wide character and makes the |
| 443 | " window one column wide. This used to cause a crash. |
| 444 | let width = &columns |
| 445 | botright vert term |
| 446 | let buf = bufnr('$') |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 447 | call TermWait(buf, 100) |
Bram Moolenaar | 8b89614 | 2020-08-02 15:05:05 +0200 | [diff] [blame] | 448 | exe "set columns=" .. (width / 2) |
| 449 | redraw |
| 450 | call term_sendkeys(buf, "キ") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 451 | call TermWait(buf, 10) |
Bram Moolenaar | 8b89614 | 2020-08-02 15:05:05 +0200 | [diff] [blame] | 452 | exe "set columns=" .. width |
| 453 | exe buf . 'bwipe!' |
| 454 | endfunc |
| 455 | |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 456 | func Test_terminal_scroll() |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 457 | call writefile(range(1, 200), 'Xtext', 'D') |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 458 | if has('win32') |
| 459 | let cmd = 'cmd /c "type Xtext"' |
| 460 | else |
| 461 | let cmd = "cat Xtext" |
| 462 | endif |
| 463 | let buf = term_start(cmd) |
| 464 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 465 | let job = term_getjob(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 466 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 467 | call TermWait(buf) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 468 | |
Bram Moolenaar | bfcfd57 | 2020-03-25 21:27:22 +0100 | [diff] [blame] | 469 | " wait until the scrolling stops |
| 470 | while 1 |
| 471 | let scrolled = buf->term_getscrolled() |
| 472 | sleep 20m |
| 473 | if scrolled == buf->term_getscrolled() |
| 474 | break |
| 475 | endif |
| 476 | endwhile |
| 477 | |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 478 | call assert_equal('1', getline(1)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 479 | call assert_equal('1', term_getline(buf, 1 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 480 | call assert_equal('49', getline(49)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 481 | call assert_equal('49', term_getline(buf, 49 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 482 | call assert_equal('200', getline(200)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 483 | call assert_equal('200', term_getline(buf, 200 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 484 | |
| 485 | exe buf . 'bwipe' |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 486 | endfunc |
| 487 | |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 488 | func Test_terminal_scrollback() |
Bram Moolenaar | 33c5e9f | 2018-05-26 18:58:51 +0200 | [diff] [blame] | 489 | let buf = Run_shell_in_terminal({'term_rows': 15}) |
Bram Moolenaar | 6d150f7 | 2018-04-21 20:03:20 +0200 | [diff] [blame] | 490 | set termwinscroll=100 |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 491 | call writefile(range(150), 'Xtext', 'D') |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 492 | if has('win32') |
| 493 | call term_sendkeys(buf, "type Xtext\<CR>") |
| 494 | else |
| 495 | call term_sendkeys(buf, "cat Xtext\<CR>") |
| 496 | endif |
| 497 | let rows = term_getsize(buf)[0] |
Bram Moolenaar | 6c67219 | 2018-04-15 13:28:42 +0200 | [diff] [blame] | 498 | " 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] | 499 | 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] | 500 | let lines = line('$') |
Bram Moolenaar | ac3e830 | 2018-04-15 13:10:44 +0200 | [diff] [blame] | 501 | call assert_inrange(91, 100, lines) |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 502 | |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 503 | call StopShellInTerminal(buf) |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 504 | exe buf . 'bwipe' |
Bram Moolenaar | 6d150f7 | 2018-04-21 20:03:20 +0200 | [diff] [blame] | 505 | set termwinscroll& |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 506 | endfunc |
| 507 | |
| 508 | func Test_terminal_postponed_scrollback() |
Bram Moolenaar | adbde3f | 2019-09-08 22:57:14 +0200 | [diff] [blame] | 509 | " tail -f only works on Unix |
| 510 | CheckUnix |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 511 | |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 512 | call writefile(range(50), 'Xtext', 'D') |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 513 | call writefile([ |
Bram Moolenaar | 5ff7df5 | 2019-02-15 01:06:13 +0100 | [diff] [blame] | 514 | \ 'set shell=/bin/sh noruler', |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 515 | \ 'terminal', |
Bram Moolenaar | 7e841e3 | 2019-02-15 00:26:14 +0100 | [diff] [blame] | 516 | \ 'sleep 200m', |
Bram Moolenaar | 5ff7df5 | 2019-02-15 01:06:13 +0100 | [diff] [blame] | 517 | \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")', |
| 518 | \ 'sleep 100m', |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 519 | \ 'call feedkeys("\<C-W>N", "xt")', |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 520 | \ ], 'XTest_postponed', 'D') |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 521 | let buf = RunVimInTerminal('-S XTest_postponed', {}) |
| 522 | " Check that the Xtext lines are displayed and in Terminal-Normal mode |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 523 | call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {}) |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 524 | |
| 525 | silent !echo 'one more line' >>Xtext |
Bram Moolenaar | 700dfaa | 2019-04-13 14:21:19 +0200 | [diff] [blame] | 526 | " Screen will not change, move cursor to get a different dump |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 527 | call term_sendkeys(buf, "k") |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 528 | call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {}) |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 529 | |
| 530 | " Back to Terminal-Job mode, text will scroll and show the extra line. |
| 531 | call term_sendkeys(buf, "a") |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 532 | call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {}) |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 533 | |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 534 | " stop "tail -f" |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 535 | call term_sendkeys(buf, "\<C-C>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 536 | call TermWait(buf, 25) |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 537 | " stop shell |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 538 | call term_sendkeys(buf, "exit\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 539 | call TermWait(buf, 50) |
Bram Moolenaar | ddbfe23 | 2020-03-15 20:33:40 +0100 | [diff] [blame] | 540 | " close terminal window |
Bram Moolenaar | 3a05ce6 | 2020-03-11 19:30:01 +0100 | [diff] [blame] | 541 | let tsk_ret = term_sendkeys(buf, ":q\<CR>") |
| 542 | |
| 543 | " check type of term_sendkeys() return value |
| 544 | echo type(tsk_ret) |
| 545 | |
Bram Moolenaar | 29ae223 | 2019-02-14 21:22:01 +0100 | [diff] [blame] | 546 | call StopVimInTerminal(buf) |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 547 | endfunc |
| 548 | |
Bram Moolenaar | 81aa0f5 | 2019-02-14 23:23:19 +0100 | [diff] [blame] | 549 | " Run diff on two dumps with different size. |
| 550 | func Test_terminal_dumpdiff_size() |
| 551 | call assert_equal(1, winnr('$')) |
| 552 | call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump') |
| 553 | call assert_equal(2, winnr('$')) |
| 554 | call assert_match('Test_incsearch_search_01.dump', getline(10)) |
| 555 | call assert_match(' +++++$', getline(11)) |
| 556 | call assert_match('Test_popup_command_01.dump', getline(31)) |
| 557 | call assert_equal(repeat('+', 75), getline(30)) |
| 558 | quit |
| 559 | endfunc |
| 560 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 561 | func Test_terminal_size() |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 562 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 563 | |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 564 | exe 'terminal ++rows=5 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 565 | let size = term_getsize('') |
| 566 | bwipe! |
| 567 | call assert_equal(5, size[0]) |
| 568 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 569 | call term_start(cmd, {'term_rows': 6}) |
| 570 | let size = term_getsize('') |
| 571 | bwipe! |
| 572 | call assert_equal(6, size[0]) |
| 573 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 574 | vsplit |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 575 | exe 'terminal ++rows=5 ++cols=33 ' . cmd |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 576 | call assert_equal([5, 33], ''->term_getsize()) |
Bram Moolenaar | a42d363 | 2018-04-14 17:05:38 +0200 | [diff] [blame] | 577 | |
| 578 | call term_setsize('', 6, 0) |
| 579 | call assert_equal([6, 33], term_getsize('')) |
| 580 | |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 581 | eval ''->term_setsize(0, 35) |
Bram Moolenaar | a42d363 | 2018-04-14 17:05:38 +0200 | [diff] [blame] | 582 | call assert_equal([6, 35], term_getsize('')) |
| 583 | |
| 584 | call term_setsize('', 7, 30) |
| 585 | call assert_equal([7, 30], term_getsize('')) |
| 586 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 587 | bwipe! |
Bram Moolenaar | 6e72cd0 | 2018-04-14 21:31:35 +0200 | [diff] [blame] | 588 | call assert_fails("call term_setsize('', 7, 30)", "E955:") |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 589 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 590 | call term_start(cmd, {'term_rows': 6, 'term_cols': 36}) |
| 591 | let size = term_getsize('') |
| 592 | bwipe! |
| 593 | call assert_equal([6, 36], size) |
| 594 | |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 595 | exe 'vertical terminal ++cols=20 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 596 | let size = term_getsize('') |
| 597 | bwipe! |
| 598 | call assert_equal(20, size[1]) |
| 599 | |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 600 | eval cmd->term_start({'vertical': 1, 'term_cols': 26}) |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 601 | let size = term_getsize('') |
| 602 | bwipe! |
| 603 | call assert_equal(26, size[1]) |
| 604 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 605 | split |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 606 | exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 607 | let size = term_getsize('') |
| 608 | bwipe! |
| 609 | call assert_equal([6, 20], size) |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 610 | |
| 611 | call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27}) |
| 612 | let size = term_getsize('') |
| 613 | bwipe! |
| 614 | call assert_equal([7, 27], size) |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 615 | |
Bram Moolenaar | 5300be6 | 2021-11-13 10:27:40 +0000 | [diff] [blame] | 616 | call assert_fails("call term_start(cmd, {'term_rows': -1})", 'E475:') |
| 617 | call assert_fails("call term_start(cmd, {'term_rows': 1001})", 'E475:') |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 618 | call assert_fails("call term_start(cmd, {'term_rows': 10.0})", 'E805:') |
Bram Moolenaar | 8813739 | 2021-11-12 16:01:15 +0000 | [diff] [blame] | 619 | |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 620 | call delete('Xtext') |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 621 | endfunc |
| 622 | |
Bram Moolenaar | eba13e4 | 2021-02-23 17:47:23 +0100 | [diff] [blame] | 623 | func Test_terminal_zero_height() |
| 624 | split |
| 625 | wincmd j |
| 626 | anoremenu 1.1 WinBar.test : |
| 627 | terminal ++curwin |
| 628 | wincmd k |
| 629 | wincmd _ |
| 630 | redraw |
| 631 | |
| 632 | call term_sendkeys(bufnr(), "exit\r") |
| 633 | bwipe! |
| 634 | endfunc |
| 635 | |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 636 | func Test_terminal_curwin() |
| 637 | let cmd = Get_cat_123_cmd() |
| 638 | call assert_equal(1, winnr('$')) |
| 639 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 640 | split Xdummy |
| 641 | call setline(1, 'dummy') |
| 642 | write |
| 643 | call assert_equal(1, getbufinfo('Xdummy')[0].loaded) |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 644 | exe 'terminal ++curwin ' . cmd |
| 645 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 646 | call assert_equal(0, getbufinfo('Xdummy')[0].loaded) |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 647 | bwipe! |
| 648 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 649 | split Xdummy |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 650 | call term_start(cmd, {'curwin': 1}) |
| 651 | call assert_equal(2, winnr('$')) |
| 652 | bwipe! |
| 653 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 654 | split Xdummy |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 655 | call setline(1, 'change') |
| 656 | call assert_fails('terminal ++curwin ' . cmd, 'E37:') |
| 657 | call assert_equal(2, winnr('$')) |
| 658 | exe 'terminal! ++curwin ' . cmd |
| 659 | call assert_equal(2, winnr('$')) |
| 660 | bwipe! |
| 661 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 662 | split Xdummy |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 663 | call setline(1, 'change') |
| 664 | call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:') |
| 665 | call assert_equal(2, winnr('$')) |
| 666 | bwipe! |
| 667 | |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 668 | split Xdummy |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 669 | bwipe! |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 670 | call delete('Xtext') |
Bram Moolenaar | b100909 | 2020-05-31 16:04:42 +0200 | [diff] [blame] | 671 | call delete('Xdummy') |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 672 | endfunc |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 673 | |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 674 | func s:get_sleep_cmd() |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 675 | if s:python != '' |
| 676 | let cmd = s:python . " test_short_sleep.py" |
Bram Moolenaar | c8523e2 | 2018-06-03 18:22:02 +0200 | [diff] [blame] | 677 | " 500 was not enough for Travis |
| 678 | let waittime = 900 |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 679 | else |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 680 | echo 'This will take five seconds...' |
| 681 | let waittime = 2000 |
| 682 | if has('win32') |
| 683 | let cmd = $windir . '\system32\timeout.exe 1' |
| 684 | else |
| 685 | let cmd = 'sleep 1' |
| 686 | endif |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 687 | endif |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 688 | return [cmd, waittime] |
| 689 | endfunc |
| 690 | |
| 691 | func Test_terminal_finish_open_close() |
| 692 | call assert_equal(1, winnr('$')) |
| 693 | |
| 694 | let [cmd, waittime] = s:get_sleep_cmd() |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 695 | |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 696 | " shell terminal closes automatically |
| 697 | terminal |
| 698 | let buf = bufnr('%') |
| 699 | call assert_equal(2, winnr('$')) |
| 700 | " Wait for the shell to display a prompt |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 701 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 702 | call StopShellInTerminal(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 703 | call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime) |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 704 | |
| 705 | " shell terminal that does not close automatically |
| 706 | terminal ++noclose |
| 707 | let buf = bufnr('%') |
| 708 | call assert_equal(2, winnr('$')) |
| 709 | " Wait for the shell to display a prompt |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 710 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 711 | call StopShellInTerminal(buf) |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 712 | call assert_equal(2, winnr('$')) |
| 713 | quit |
| 714 | call assert_equal(1, winnr('$')) |
| 715 | |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 716 | exe 'terminal ++close ' . cmd |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 717 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 718 | wincmd p |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 719 | call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 720 | |
| 721 | call term_start(cmd, {'term_finish': 'close'}) |
| 722 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 723 | wincmd p |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 724 | call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 725 | call assert_equal(1, winnr('$')) |
| 726 | |
| 727 | exe 'terminal ++open ' . cmd |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 728 | close! |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 729 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 730 | bwipe |
| 731 | |
| 732 | call term_start(cmd, {'term_finish': 'open'}) |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 733 | close! |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 734 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 735 | bwipe |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 736 | |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 737 | exe 'terminal ++hidden ++open ' . cmd |
| 738 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 739 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 740 | bwipe |
| 741 | |
| 742 | call term_start(cmd, {'term_finish': 'open', 'hidden': 1}) |
| 743 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 744 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 745 | bwipe |
Bram Moolenaar | 37c4583 | 2017-08-12 16:01:04 +0200 | [diff] [blame] | 746 | |
| 747 | call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:') |
| 748 | call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:') |
| 749 | call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:') |
| 750 | call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:') |
| 751 | |
Bram Moolenaar | 47c5ea4 | 2020-11-12 15:12:15 +0100 | [diff] [blame] | 752 | call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d | let g:result = "opened the buffer in a window"'}) |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 753 | close! |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 754 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | 37c4583 | 2017-08-12 16:01:04 +0200 | [diff] [blame] | 755 | call assert_equal(4, winheight(0)) |
Bram Moolenaar | 47c5ea4 | 2020-11-12 15:12:15 +0100 | [diff] [blame] | 756 | call assert_equal('opened the buffer in a window', g:result) |
| 757 | unlet g:result |
Bram Moolenaar | 37c4583 | 2017-08-12 16:01:04 +0200 | [diff] [blame] | 758 | bwipe |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 759 | endfunc |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 760 | |
| 761 | func Test_terminal_cwd() |
Bram Moolenaar | 077ff43 | 2019-10-28 00:42:21 +0100 | [diff] [blame] | 762 | if has('win32') |
| 763 | let cmd = 'cmd /c cd' |
| 764 | else |
| 765 | CheckExecutable pwd |
| 766 | let cmd = 'pwd' |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 767 | endif |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 768 | call mkdir('Xtermdir') |
| 769 | let buf = term_start(cmd, {'cwd': 'Xtermdir'}) |
Bram Moolenaar | ee7c8d9 | 2022-09-22 12:57:06 +0100 | [diff] [blame] | 770 | " if the path is very long it may be split over two lines, join them |
| 771 | " together |
| 772 | call WaitForAssert({-> assert_equal('Xtermdir', fnamemodify(getline(1) .. getline(2), ":t"))}) |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 773 | |
| 774 | exe buf . 'bwipe' |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 775 | call delete('Xtermdir', 'rf') |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 776 | endfunc |
| 777 | |
Bram Moolenaar | 839e81e | 2018-10-19 16:53:39 +0200 | [diff] [blame] | 778 | func Test_terminal_cwd_failure() |
| 779 | " Case 1: Provided directory is not actually a directory. Attempt to make |
| 780 | " the file executable as well. |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 781 | call writefile([], 'Xtcfile', 'D') |
Dominique Pelle | 91a874e | 2022-09-03 10:59:32 +0100 | [diff] [blame] | 782 | call setfperm('Xtcfile', 'rwx------') |
| 783 | call assert_fails("call term_start(&shell, {'cwd': 'Xtcfile'})", 'E475:') |
Bram Moolenaar | 839e81e | 2018-10-19 16:53:39 +0200 | [diff] [blame] | 784 | |
| 785 | " Case 2: Directory does not exist. |
| 786 | call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:') |
| 787 | |
| 788 | " Case 3: Directory exists but is not accessible. |
Bram Moolenaar | 0b38f54 | 2018-11-03 21:47:16 +0100 | [diff] [blame] | 789 | " Skip this for root, it will be accessible anyway. |
Bram Moolenaar | 07282f0 | 2019-10-10 16:46:17 +0200 | [diff] [blame] | 790 | if !IsRoot() |
Bram Moolenaar | 0b38f54 | 2018-11-03 21:47:16 +0100 | [diff] [blame] | 791 | call mkdir('XdirNoAccess', '', '0600') |
| 792 | " return early if the directory permissions could not be set properly |
| 793 | if getfperm('XdirNoAccess')[2] == 'x' |
| 794 | call delete('XdirNoAccess', 'rf') |
| 795 | return |
| 796 | endif |
| 797 | call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:') |
| 798 | call delete('XdirNoAccess', 'rf') |
Bram Moolenaar | 839e81e | 2018-10-19 16:53:39 +0200 | [diff] [blame] | 799 | endif |
Bram Moolenaar | 839e81e | 2018-10-19 16:53:39 +0200 | [diff] [blame] | 800 | endfunc |
| 801 | |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 802 | func Test_terminal_servername() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 803 | CheckFeature clientserver |
Bram Moolenaar | d7a137f | 2018-06-12 18:05:24 +0200 | [diff] [blame] | 804 | call s:test_environment("VIM_SERVERNAME", v:servername) |
| 805 | endfunc |
| 806 | |
| 807 | func Test_terminal_version() |
| 808 | call s:test_environment("VIM_TERMINAL", string(v:version)) |
| 809 | endfunc |
| 810 | |
| 811 | func s:test_environment(name, value) |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 812 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 813 | " Wait for the shell to display a prompt |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 814 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 815 | if has('win32') |
Bram Moolenaar | d7a137f | 2018-06-12 18:05:24 +0200 | [diff] [blame] | 816 | call term_sendkeys(buf, "echo %" . a:name . "%\r") |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 817 | else |
Bram Moolenaar | d7a137f | 2018-06-12 18:05:24 +0200 | [diff] [blame] | 818 | call term_sendkeys(buf, "echo $" . a:name . "\r") |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 819 | endif |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 820 | call TermWait(buf) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 821 | call StopShellInTerminal(buf) |
Bram Moolenaar | d7a137f | 2018-06-12 18:05:24 +0200 | [diff] [blame] | 822 | call WaitForAssert({-> assert_equal(a:value, getline(2))}) |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 823 | |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 824 | exe buf . 'bwipe' |
| 825 | unlet buf |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 826 | endfunc |
| 827 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 828 | func Test_terminal_env() |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 829 | let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}}) |
Bram Moolenaar | 51c2368 | 2017-08-14 21:45:00 +0200 | [diff] [blame] | 830 | " Wait for the shell to display a prompt |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 831 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 832 | if has('win32') |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 833 | call term_sendkeys(buf, "echo %TESTENV%\r") |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 834 | else |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 835 | call term_sendkeys(buf, "echo $TESTENV\r") |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 836 | endif |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 837 | eval buf->TermWait() |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 838 | call StopShellInTerminal(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 839 | call WaitForAssert({-> assert_equal('correct', getline(2))}) |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 840 | |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 841 | exe buf . 'bwipe' |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 842 | endfunc |
Bram Moolenaar | 679653e | 2017-08-13 14:13:19 +0200 | [diff] [blame] | 843 | |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 844 | func Test_terminal_list_args() |
| 845 | let buf = term_start([&shell, &shellcmdflag, 'echo "123"']) |
Yee Cheng Chin | 15b314f | 2022-10-09 18:53:32 +0100 | [diff] [blame] | 846 | call assert_fails(buf . 'bwipe', 'E948:') |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 847 | exe buf . 'bwipe!' |
| 848 | call assert_equal("", bufname(buf)) |
| 849 | endfunction |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 850 | |
| 851 | func Test_terminal_noblock() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 852 | let g:test_is_flaky = 1 |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 853 | let buf = term_start(&shell) |
Zdenek Dohnal | dd2dfb3 | 2022-02-23 14:25:17 +0000 | [diff] [blame] | 854 | " Starting a terminal can be slow, esp. on busy CI machines. |
| 855 | let wait_time = 7500 |
Bram Moolenaar | f3710ee | 2020-03-24 12:12:30 +0100 | [diff] [blame] | 856 | let letters = 'abcdefghijklmnopqrstuvwxyz' |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 857 | if has('bsd') || has('mac') || has('sun') |
Bram Moolenaar | d8d85bf | 2017-09-03 18:08:00 +0200 | [diff] [blame] | 858 | " 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] | 859 | " characters at the same time. It's very slow too. |
Bram Moolenaar | d8d85bf | 2017-09-03 18:08:00 +0200 | [diff] [blame] | 860 | let len = 1000 |
Bram Moolenaar | d06dbf3 | 2020-03-24 10:33:00 +0100 | [diff] [blame] | 861 | let wait_time = 15000 |
Bram Moolenaar | f3710ee | 2020-03-24 12:12:30 +0100 | [diff] [blame] | 862 | let letters = 'abcdefghijklm' |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 863 | " NPFS is used in Windows, nonblocking mode does not work properly. |
| 864 | elseif has('win32') |
| 865 | let len = 1 |
Bram Moolenaar | d8d85bf | 2017-09-03 18:08:00 +0200 | [diff] [blame] | 866 | else |
| 867 | let len = 5000 |
| 868 | endif |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 869 | |
Bram Moolenaar | d06dbf3 | 2020-03-24 10:33:00 +0100 | [diff] [blame] | 870 | " Send a lot of text lines, should be buffered properly. |
Bram Moolenaar | f3710ee | 2020-03-24 12:12:30 +0100 | [diff] [blame] | 871 | for c in split(letters, '\zs') |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 872 | call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>") |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 873 | endfor |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 874 | call term_sendkeys(buf, "echo done\<cr>") |
Bram Moolenaar | eef0531 | 2017-08-20 20:21:23 +0200 | [diff] [blame] | 875 | |
| 876 | " On MS-Windows there is an extra empty line below "done". Find "done" in |
| 877 | " the last-but-one or the last-but-two line. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 878 | let lnum = term_getsize(buf)[0] - 1 |
Bram Moolenaar | d06dbf3 | 2020-03-24 10:33:00 +0100 | [diff] [blame] | 879 | 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] | 880 | let line = term_getline(buf, lnum) |
Bram Moolenaar | eef0531 | 2017-08-20 20:21:23 +0200 | [diff] [blame] | 881 | if line !~ 'done' |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 882 | let line = term_getline(buf, lnum - 1) |
Bram Moolenaar | eef0531 | 2017-08-20 20:21:23 +0200 | [diff] [blame] | 883 | endif |
| 884 | call assert_match('done', line) |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 885 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 886 | let g:job = term_getjob(buf) |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 887 | call StopShellInTerminal(buf) |
Bram Moolenaar | d21f8b5 | 2017-08-19 15:40:01 +0200 | [diff] [blame] | 888 | unlet g:job |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 889 | bwipe |
| 890 | endfunc |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 891 | |
| 892 | func Test_terminal_write_stdin() |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 893 | " TODO: enable once writing to stdin works on MS-Windows |
| 894 | CheckNotMSWindows |
| 895 | CheckExecutable wc |
Bram Moolenaar | 62eb239 | 2022-06-15 17:52:44 +0100 | [diff] [blame] | 896 | let g:test_is_flaky = 1 |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 897 | |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 898 | call setline(1, ['one', 'two', 'three']) |
| 899 | %term wc |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 900 | call WaitForAssert({-> assert_match('3', getline("$"))}) |
Bram Moolenaar | 3346cc4 | 2017-09-02 14:54:21 +0200 | [diff] [blame] | 901 | let nrs = split(getline('$')) |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 902 | call assert_equal(['3', '3', '14'], nrs) |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 903 | %bwipe! |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 904 | |
| 905 | call setline(1, ['one', 'two', 'three', 'four']) |
| 906 | 2,3term wc |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 907 | call WaitForAssert({-> assert_match('2', getline("$"))}) |
Bram Moolenaar | 3346cc4 | 2017-09-02 14:54:21 +0200 | [diff] [blame] | 908 | let nrs = split(getline('$')) |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 909 | call assert_equal(['2', '2', '10'], nrs) |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 910 | %bwipe! |
| 911 | endfunc |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 912 | |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 913 | func Test_terminal_eof_arg() |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 914 | call CheckPython(s:python) |
Bram Moolenaar | 62eb239 | 2022-06-15 17:52:44 +0100 | [diff] [blame] | 915 | let g:test_is_flaky = 1 |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 916 | |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 917 | call setline(1, ['print("hello")']) |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 918 | exe '1term ++eof=exit(123) ' .. s:python |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 919 | " MS-Windows echoes the input, Unix doesn't. |
| 920 | if has('win32') |
| 921 | call WaitFor({-> getline('$') =~ 'exit(123)'}) |
| 922 | call assert_equal('hello', getline(line('$') - 1)) |
| 923 | else |
| 924 | call WaitFor({-> getline('$') =~ 'hello'}) |
| 925 | call assert_equal('hello', getline('$')) |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 926 | endif |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 927 | call assert_equal(123, bufnr()->term_getjob()->job_info().exitval) |
| 928 | %bwipe! |
| 929 | endfunc |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 930 | |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 931 | func Test_terminal_eof_arg_win32_ctrl_z() |
| 932 | CheckMSWindows |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 933 | call CheckPython(s:python) |
Bram Moolenaar | 62eb239 | 2022-06-15 17:52:44 +0100 | [diff] [blame] | 934 | let g:test_is_flaky = 1 |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 935 | |
| 936 | call setline(1, ['print("hello")']) |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 937 | exe '1term ++eof=<C-Z> ' .. s:python |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 938 | call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))}) |
| 939 | call assert_match('\^Z', getline(line('$') - 1)) |
| 940 | %bwipe! |
| 941 | endfunc |
| 942 | |
| 943 | func Test_terminal_duplicate_eof_arg() |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 944 | call CheckPython(s:python) |
Bram Moolenaar | 62eb239 | 2022-06-15 17:52:44 +0100 | [diff] [blame] | 945 | let g:test_is_flaky = 1 |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 946 | |
Bram Moolenaar | 62eb239 | 2022-06-15 17:52:44 +0100 | [diff] [blame] | 947 | " Check the last specified ++eof arg is used and does not leak memory. |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 948 | new |
| 949 | call setline(1, ['print("hello")']) |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 950 | exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python |
Bram Moolenaar | 2110927 | 2020-01-30 16:27:20 +0100 | [diff] [blame] | 951 | " MS-Windows echoes the input, Unix doesn't. |
| 952 | if has('win32') |
| 953 | call WaitFor({-> getline('$') =~ 'exit(123)'}) |
| 954 | call assert_equal('hello', getline(line('$') - 1)) |
| 955 | else |
| 956 | call WaitFor({-> getline('$') =~ 'hello'}) |
| 957 | call assert_equal('hello', getline('$')) |
| 958 | endif |
| 959 | call assert_equal(123, bufnr()->term_getjob()->job_info().exitval) |
| 960 | %bwipe! |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 961 | endfunc |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 962 | |
| 963 | func Test_terminal_no_cmd() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 964 | let g:test_is_flaky = 1 |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 965 | let buf = term_start('NONE', {}) |
| 966 | call assert_notequal(0, buf) |
| 967 | |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 968 | let pty = job_info(term_getjob(buf))['tty_out'] |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 969 | call assert_notequal('', pty) |
Bram Moolenaar | cfc1523 | 2019-01-23 22:33:18 +0100 | [diff] [blame] | 970 | if has('gui_running') && !has('win32') |
| 971 | " 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] | 972 | call system('echo "look here" > ' . pty) |
Bram Moolenaar | cfc1523 | 2019-01-23 22:33:18 +0100 | [diff] [blame] | 973 | else |
| 974 | " Otherwise using a job works on all systems. |
| 975 | call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty]) |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 976 | endif |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 977 | call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))}) |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 978 | |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 979 | bwipe! |
| 980 | endfunc |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 981 | |
| 982 | func Test_terminal_special_chars() |
| 983 | " this file name only works on Unix |
Bram Moolenaar | adbde3f | 2019-09-08 22:57:14 +0200 | [diff] [blame] | 984 | CheckUnix |
| 985 | |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 986 | call mkdir('Xdir with spaces', 'R') |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 987 | call writefile(['x'], 'Xdir with spaces/quoted"file') |
| 988 | term ls Xdir\ with\ spaces/quoted\"file |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 989 | call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))}) |
Bram Moolenaar | 95ffd43 | 2020-02-23 13:29:31 +0100 | [diff] [blame] | 990 | " make sure the job has finished |
| 991 | call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))}) |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 992 | |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 993 | bwipe |
| 994 | endfunc |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 995 | |
| 996 | func Test_terminal_wrong_options() |
| 997 | call assert_fails('call term_start(&shell, { |
| 998 | \ "in_io": "file", |
| 999 | \ "in_name": "xxx", |
| 1000 | \ "out_io": "file", |
| 1001 | \ "out_name": "xxx", |
| 1002 | \ "err_io": "file", |
| 1003 | \ "err_name": "xxx" |
| 1004 | \ })', 'E474:') |
| 1005 | call assert_fails('call term_start(&shell, { |
| 1006 | \ "out_buf": bufnr("%") |
| 1007 | \ })', 'E474:') |
| 1008 | call assert_fails('call term_start(&shell, { |
| 1009 | \ "err_buf": bufnr("%") |
| 1010 | \ })', 'E474:') |
| 1011 | endfunc |
| 1012 | |
| 1013 | func Test_terminal_redir_file() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1014 | let g:test_is_flaky = 1 |
Bram Moolenaar | f25329c | 2018-05-06 21:49:32 +0200 | [diff] [blame] | 1015 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 1016 | let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xtrfile'}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1017 | call TermWait(buf) |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 1018 | " ConPTY may precede escape sequence. There are things that are not so. |
| 1019 | if !has('conpty') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 1020 | call WaitForAssert({-> assert_notequal(0, len(readfile("Xtrfile")))}) |
| 1021 | call assert_match('123', readfile('Xtrfile')[0]) |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 1022 | endif |
Bram Moolenaar | f25329c | 2018-05-06 21:49:32 +0200 | [diff] [blame] | 1023 | let g:job = term_getjob(buf) |
| 1024 | call WaitForAssert({-> assert_equal("dead", job_status(g:job))}) |
Bram Moolenaar | c69950a | 2020-07-22 22:23:40 +0200 | [diff] [blame] | 1025 | |
| 1026 | if has('win32') |
| 1027 | " On Windows we cannot delete a file being used by a process. When |
| 1028 | " job_status() returns "dead", the process remains for a short time. |
| 1029 | " Just wait for a moment. |
| 1030 | sleep 50m |
| 1031 | endif |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 1032 | call delete('Xtrfile') |
Bram Moolenaar | f25329c | 2018-05-06 21:49:32 +0200 | [diff] [blame] | 1033 | bwipe |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 1034 | |
| 1035 | if has('unix') |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1036 | call writefile(['one line'], 'Xtrfile', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 1037 | let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xtrfile'}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1038 | call TermWait(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1039 | call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))}) |
Bram Moolenaar | 8b53b79 | 2017-09-05 20:29:25 +0200 | [diff] [blame] | 1040 | let g:job = term_getjob(buf) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1041 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 1042 | bwipe |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 1043 | endif |
| 1044 | endfunc |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 1045 | |
| 1046 | func TerminalTmap(remap) |
| 1047 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | f4a2ed0 | 2021-03-23 16:25:09 +0100 | [diff] [blame] | 1048 | " Wait for the shell to display a prompt |
| 1049 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 1050 | call assert_equal('t', mode()) |
| 1051 | |
| 1052 | if a:remap |
| 1053 | tmap 123 456 |
| 1054 | else |
| 1055 | tnoremap 123 456 |
| 1056 | endif |
Bram Moolenaar | 461fe50 | 2017-12-05 12:30:03 +0100 | [diff] [blame] | 1057 | " don't use abcde, it's an existing command |
| 1058 | tmap 456 abxde |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 1059 | call assert_equal('456', maparg('123', 't')) |
Bram Moolenaar | 461fe50 | 2017-12-05 12:30:03 +0100 | [diff] [blame] | 1060 | call assert_equal('abxde', maparg('456', 't')) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 1061 | call feedkeys("123", 'tx') |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1062 | 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] | 1063 | let lnum = term_getcursor(buf)[0] |
| 1064 | if a:remap |
Bram Moolenaar | 461fe50 | 2017-12-05 12:30:03 +0100 | [diff] [blame] | 1065 | call assert_match('abxde', term_getline(buf, lnum)) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 1066 | else |
| 1067 | call assert_match('456', term_getline(buf, lnum)) |
| 1068 | endif |
| 1069 | |
| 1070 | call term_sendkeys(buf, "\r") |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 1071 | call StopShellInTerminal(buf) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 1072 | |
| 1073 | tunmap 123 |
| 1074 | tunmap 456 |
| 1075 | call assert_equal('', maparg('123', 't')) |
Yegappan Lakshmanan | 1104a6d | 2022-03-31 12:34:15 +0100 | [diff] [blame] | 1076 | exe buf . 'bwipe' |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 1077 | unlet g:job |
| 1078 | endfunc |
| 1079 | |
| 1080 | func Test_terminal_tmap() |
| 1081 | call TerminalTmap(1) |
| 1082 | call TerminalTmap(0) |
| 1083 | endfunc |
Bram Moolenaar | 059db5c | 2017-10-15 22:42:23 +0200 | [diff] [blame] | 1084 | |
| 1085 | func Test_terminal_wall() |
| 1086 | let buf = Run_shell_in_terminal({}) |
| 1087 | wall |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 1088 | call StopShellInTerminal(buf) |
Bram Moolenaar | 059db5c | 2017-10-15 22:42:23 +0200 | [diff] [blame] | 1089 | exe buf . 'bwipe' |
| 1090 | unlet g:job |
| 1091 | endfunc |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1092 | |
Bram Moolenaar | 7a76092 | 2018-02-19 23:10:02 +0100 | [diff] [blame] | 1093 | func Test_terminal_wqall() |
| 1094 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1095 | call assert_fails('wqall', 'E948:') |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 1096 | call StopShellInTerminal(buf) |
Bram Moolenaar | 7a76092 | 2018-02-19 23:10:02 +0100 | [diff] [blame] | 1097 | exe buf . 'bwipe' |
| 1098 | unlet g:job |
| 1099 | endfunc |
| 1100 | |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1101 | func Test_terminal_composing_unicode() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1102 | let g:test_is_flaky = 1 |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1103 | let save_enc = &encoding |
| 1104 | set encoding=utf-8 |
| 1105 | |
| 1106 | if has('win32') |
| 1107 | let cmd = "cmd /K chcp 65001" |
| 1108 | let lnum = [3, 6, 9] |
| 1109 | else |
| 1110 | let cmd = &shell |
| 1111 | let lnum = [1, 3, 5] |
| 1112 | endif |
| 1113 | |
| 1114 | enew |
Bram Moolenaar | c98cdb3 | 2020-09-06 21:13:00 +0200 | [diff] [blame] | 1115 | let buf = term_start(cmd, {'curwin': 1}) |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 1116 | let g:job = term_getjob(buf) |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 1117 | call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1118 | |
Bram Moolenaar | ebe74b7 | 2018-04-21 23:34:43 +0200 | [diff] [blame] | 1119 | if has('win32') |
| 1120 | call assert_equal('cmd', job_info(g:job).cmd[0]) |
| 1121 | else |
| 1122 | call assert_equal(&shell, job_info(g:job).cmd[0]) |
| 1123 | endif |
| 1124 | |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1125 | " ascii + composing |
| 1126 | let txt = "a\u0308bc" |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 1127 | call term_sendkeys(buf, "echo " . txt) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1128 | call TermWait(buf, 25) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1129 | call assert_match("echo " . txt, term_getline(buf, lnum[0])) |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 1130 | call term_sendkeys(buf, "\<cr>") |
| 1131 | call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1132 | let l = term_scrape(buf, lnum[0] + 1) |
| 1133 | call assert_equal("a\u0308", l[0].chars) |
| 1134 | call assert_equal("b", l[1].chars) |
| 1135 | call assert_equal("c", l[2].chars) |
| 1136 | |
Bram Moolenaar | 4549dad | 2021-02-08 21:29:48 +0100 | [diff] [blame] | 1137 | " multibyte + composing: がぎぐげご |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1138 | let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099" |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 1139 | call term_sendkeys(buf, "echo " . txt) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1140 | call TermWait(buf, 25) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1141 | call assert_match("echo " . txt, term_getline(buf, lnum[1])) |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 1142 | call term_sendkeys(buf, "\<cr>") |
| 1143 | call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1144 | let l = term_scrape(buf, lnum[1] + 1) |
| 1145 | call assert_equal("\u304b\u3099", l[0].chars) |
Bram Moolenaar | 4549dad | 2021-02-08 21:29:48 +0100 | [diff] [blame] | 1146 | call assert_equal(2, l[0].width) |
| 1147 | call assert_equal("\u304e", l[1].chars) |
| 1148 | call assert_equal(2, l[1].width) |
| 1149 | call assert_equal("\u304f\u3099", l[2].chars) |
| 1150 | call assert_equal(2, l[2].width) |
| 1151 | call assert_equal("\u3052", l[3].chars) |
| 1152 | call assert_equal(2, l[3].width) |
| 1153 | call assert_equal("\u3053\u3099", l[4].chars) |
| 1154 | call assert_equal(2, l[4].width) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1155 | |
| 1156 | " \u00a0 + composing |
| 1157 | let txt = "abc\u00a0\u0308" |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 1158 | call term_sendkeys(buf, "echo " . txt) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1159 | call TermWait(buf, 25) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1160 | call assert_match("echo " . txt, term_getline(buf, lnum[2])) |
Bram Moolenaar | 41d4299 | 2020-05-03 16:29:50 +0200 | [diff] [blame] | 1161 | call term_sendkeys(buf, "\<cr>") |
| 1162 | call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1163 | let l = term_scrape(buf, lnum[2] + 1) |
| 1164 | call assert_equal("\u00a0\u0308", l[3].chars) |
| 1165 | |
| 1166 | call term_sendkeys(buf, "exit\r") |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1167 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1168 | bwipe! |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 1169 | unlet g:job |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 1170 | let &encoding = save_enc |
| 1171 | endfunc |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 1172 | |
| 1173 | func Test_terminal_aucmd_on_close() |
| 1174 | fun Nop() |
| 1175 | let s:called = 1 |
| 1176 | endfun |
| 1177 | |
| 1178 | aug repro |
| 1179 | au! |
| 1180 | au BufWinLeave * call Nop() |
| 1181 | aug END |
| 1182 | |
| 1183 | let [cmd, waittime] = s:get_sleep_cmd() |
| 1184 | |
| 1185 | call assert_equal(1, winnr('$')) |
| 1186 | new |
| 1187 | call setline(1, ['one', 'two']) |
| 1188 | exe 'term ++close ' . cmd |
| 1189 | wincmd p |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1190 | call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime) |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 1191 | call assert_equal(1, s:called) |
| 1192 | bwipe! |
| 1193 | |
| 1194 | unlet s:called |
| 1195 | au! repro |
| 1196 | delfunc Nop |
| 1197 | endfunc |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 1198 | |
| 1199 | func Test_terminal_term_start_empty_command() |
| 1200 | let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})" |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1201 | call assert_fails(cmd, 'E474:') |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 1202 | let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})" |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1203 | call assert_fails(cmd, 'E474:') |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 1204 | let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})" |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1205 | call assert_fails(cmd, 'E474:') |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 1206 | let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})" |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1207 | call assert_fails(cmd, 'E474:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1208 | let cmd = "call term_start('', {'term_name' : []})" |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1209 | call assert_fails(cmd, 'E730:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1210 | let cmd = "call term_start('', {'term_finish' : 'axby'})" |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1211 | call assert_fails(cmd, 'E475:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1212 | let cmd = "call term_start('', {'eof_chars' : []})" |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1213 | call assert_fails(cmd, 'E730:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1214 | let cmd = "call term_start('', {'term_kill' : []})" |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1215 | call assert_fails(cmd, 'E730:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1216 | let cmd = "call term_start('', {'tty_type' : []})" |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1217 | call assert_fails(cmd, 'E730:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1218 | let cmd = "call term_start('', {'tty_type' : 'abc'})" |
| 1219 | call assert_fails(cmd, 'E475:') |
| 1220 | let cmd = "call term_start('', {'term_highlight' : []})" |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1221 | call assert_fails(cmd, 'E730:') |
Bram Moolenaar | 8720226 | 2020-05-24 17:23:45 +0200 | [diff] [blame] | 1222 | if has('gui') || has('termguicolors') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1223 | let cmd = "call term_start('', {'ansi_colors' : 'abc'})" |
| 1224 | call assert_fails(cmd, 'E475:') |
| 1225 | let cmd = "call term_start('', {'ansi_colors' : [[]]})" |
| 1226 | call assert_fails(cmd, 'E730:') |
| 1227 | let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})" |
Bram Moolenaar | 8720226 | 2020-05-24 17:23:45 +0200 | [diff] [blame] | 1228 | if has('gui_running') || has('termguicolors') |
| 1229 | call assert_fails(cmd, 'E475:') |
| 1230 | else |
| 1231 | call assert_fails(cmd, 'E254:') |
| 1232 | endif |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1233 | endif |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 1234 | endfunc |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1235 | |
| 1236 | func Test_terminal_response_to_control_sequence() |
Bram Moolenaar | adbde3f | 2019-09-08 22:57:14 +0200 | [diff] [blame] | 1237 | CheckUnix |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1238 | |
| 1239 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1240 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1241 | |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 1242 | call term_sendkeys(buf, "cat\<CR>") |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1243 | call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))}) |
Bram Moolenaar | d4a282f | 2018-02-02 18:22:31 +0100 | [diff] [blame] | 1244 | |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 1245 | " Request the cursor position. |
| 1246 | call term_sendkeys(buf, "\x1b[6n\<CR>") |
Bram Moolenaar | d4a282f | 2018-02-02 18:22:31 +0100 | [diff] [blame] | 1247 | |
| 1248 | " Wait for output from tty to display, below an empty line. |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1249 | call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))}) |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1250 | |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 1251 | " End "cat" gently. |
| 1252 | call term_sendkeys(buf, "\<CR>\<C-D>") |
| 1253 | |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 1254 | call StopShellInTerminal(buf) |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 1255 | exe buf . 'bwipe' |
| 1256 | unlet g:job |
| 1257 | endfunc |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1258 | |
Bram Moolenaar | c295858 | 2021-12-14 11:16:31 +0000 | [diff] [blame] | 1259 | " Run this first, it fails when run after other tests. |
| 1260 | func Test_aa_terminal_focus_events() |
Bram Moolenaar | a48d4e4 | 2021-12-08 22:13:38 +0000 | [diff] [blame] | 1261 | CheckNotGui |
| 1262 | CheckUnix |
| 1263 | CheckRunVimInTerminal |
| 1264 | |
| 1265 | let save_term = &term |
| 1266 | let save_ttymouse = &ttymouse |
| 1267 | set term=xterm ttymouse=xterm2 |
| 1268 | |
| 1269 | let lines =<< trim END |
| 1270 | set term=xterm ttymouse=xterm2 |
Bram Moolenaar | e505071 | 2021-12-09 10:51:05 +0000 | [diff] [blame] | 1271 | au FocusLost * call setline(1, 'I am lost') | set nomod |
| 1272 | au FocusGained * call setline(1, 'I am back') | set nomod |
Bram Moolenaar | a48d4e4 | 2021-12-08 22:13:38 +0000 | [diff] [blame] | 1273 | END |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1274 | call writefile(lines, 'XtermFocus', 'D') |
Bram Moolenaar | a48d4e4 | 2021-12-08 22:13:38 +0000 | [diff] [blame] | 1275 | let buf = RunVimInTerminal('-S XtermFocus', #{rows: 6}) |
| 1276 | |
| 1277 | " Send a focus event to ourselves, it should be forwarded to the terminal |
| 1278 | call feedkeys("\<Esc>[O", "Lx!") |
Bram Moolenaar | a48d4e4 | 2021-12-08 22:13:38 +0000 | [diff] [blame] | 1279 | call VerifyScreenDump(buf, 'Test_terminal_focus_1', {}) |
| 1280 | |
| 1281 | call feedkeys("\<Esc>[I", "Lx!") |
Bram Moolenaar | a48d4e4 | 2021-12-08 22:13:38 +0000 | [diff] [blame] | 1282 | call VerifyScreenDump(buf, 'Test_terminal_focus_2', {}) |
| 1283 | |
Bram Moolenaar | e505071 | 2021-12-09 10:51:05 +0000 | [diff] [blame] | 1284 | " check that a command line being edited is redrawn in place |
| 1285 | call term_sendkeys(buf, ":" .. repeat('x', 80)) |
| 1286 | call TermWait(buf) |
| 1287 | call feedkeys("\<Esc>[O", "Lx!") |
Bram Moolenaar | e505071 | 2021-12-09 10:51:05 +0000 | [diff] [blame] | 1288 | call VerifyScreenDump(buf, 'Test_terminal_focus_3', {}) |
| 1289 | call term_sendkeys(buf, "\<Esc>") |
| 1290 | |
Bram Moolenaar | a48d4e4 | 2021-12-08 22:13:38 +0000 | [diff] [blame] | 1291 | call StopVimInTerminal(buf) |
Bram Moolenaar | a48d4e4 | 2021-12-08 22:13:38 +0000 | [diff] [blame] | 1292 | let &term = save_term |
| 1293 | let &ttymouse = save_ttymouse |
| 1294 | endfunc |
| 1295 | |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1296 | " Run Vim, start a terminal in that Vim with the kill argument, |
| 1297 | " :qall works. |
| 1298 | func Run_terminal_qall_kill(line1, line2) |
| 1299 | " 1. Open a terminal window and wait for the prompt to appear |
| 1300 | " 2. set kill using term_setkill() |
| 1301 | " 3. make Vim exit, it will kill the shell |
| 1302 | let after = [ |
| 1303 | \ a:line1, |
| 1304 | \ 'let buf = bufnr("%")', |
| 1305 | \ 'while term_getline(buf, 1) =~ "^\\s*$"', |
| 1306 | \ ' sleep 10m', |
| 1307 | \ 'endwhile', |
| 1308 | \ a:line2, |
| 1309 | \ 'au VimLeavePre * call writefile(["done"], "Xdone")', |
| 1310 | \ 'qall', |
| 1311 | \ ] |
| 1312 | if !RunVim([], after, '') |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1313 | return |
| 1314 | endif |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1315 | call assert_equal("done", readfile("Xdone")[0]) |
| 1316 | call delete("Xdone") |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1317 | endfunc |
| 1318 | |
| 1319 | " Run Vim in a terminal, then start a terminal in that Vim with a kill |
| 1320 | " argument, check that :qall works. |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1321 | func Test_terminal_qall_kill_arg() |
| 1322 | call Run_terminal_qall_kill('term ++kill=kill', '') |
| 1323 | endfunc |
| 1324 | |
| 1325 | " Run Vim, start a terminal in that Vim, set the kill argument with |
| 1326 | " term_setkill(), check that :qall works. |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1327 | func Test_terminal_qall_kill_func() |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 1328 | call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")') |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1329 | endfunc |
| 1330 | |
| 1331 | " Run Vim, start a terminal in that Vim without the kill argument, |
| 1332 | " check that :qall does not exit, :qall! does. |
| 1333 | func Test_terminal_qall_exit() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 1334 | let after =<< trim [CODE] |
| 1335 | term |
| 1336 | let buf = bufnr("%") |
| 1337 | while term_getline(buf, 1) =~ "^\\s*$" |
| 1338 | sleep 10m |
| 1339 | endwhile |
| 1340 | set nomore |
| 1341 | au VimLeavePre * call writefile(["too early"], "Xdone") |
| 1342 | qall |
| 1343 | au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone") |
| 1344 | cquit |
| 1345 | [CODE] |
| 1346 | |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1347 | if !RunVim([], after, '') |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1348 | return |
| 1349 | endif |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 1350 | call assert_equal("done", readfile("Xdone")[0]) |
| 1351 | call delete("Xdone") |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1352 | endfunc |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1353 | |
| 1354 | " Run Vim in a terminal, then start a terminal in that Vim without a kill |
| 1355 | " argument, check that :confirm qall works. |
| 1356 | func Test_terminal_qall_prompt() |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 1357 | CheckRunVimInTerminal |
Bram Moolenaar | e564c70 | 2022-06-14 15:00:28 +0100 | [diff] [blame] | 1358 | |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1359 | let buf = RunVimInTerminal('', {}) |
| 1360 | |
Bram Moolenaar | 99f4b6e | 2022-06-14 19:52:16 +0100 | [diff] [blame] | 1361 | " the shell may set the window title, we don't want that here |
Bram Moolenaar | 377d92a | 2022-06-14 21:22:12 +0100 | [diff] [blame] | 1362 | call term_sendkeys(buf, ":call test_override('vterm_title', 1)\<CR>") |
Bram Moolenaar | 99f4b6e | 2022-06-14 19:52:16 +0100 | [diff] [blame] | 1363 | |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1364 | " Open a terminal window and wait for the prompt to appear |
| 1365 | call term_sendkeys(buf, ":term\<CR>") |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1366 | call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))}) |
| 1367 | call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))}) |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1368 | |
| 1369 | " make Vim exit, it will prompt to kill the shell |
| 1370 | call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>") |
Yee Cheng Chin | 15b314f | 2022-10-09 18:53:32 +0100 | [diff] [blame] | 1371 | call WaitForAssert({-> assert_match('\[Y\]es, (N)o:', term_getline(buf, 20))}) |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1372 | call term_sendkeys(buf, "y") |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1373 | call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 1374 | |
| 1375 | " close the terminal window where Vim was running |
| 1376 | quit |
| 1377 | endfunc |
Bram Moolenaar | b852c3e | 2018-03-11 16:55:36 +0100 | [diff] [blame] | 1378 | |
Bram Moolenaar | 4d14bac | 2019-10-20 21:15:15 +0200 | [diff] [blame] | 1379 | " Run Vim in a terminal, then start a terminal window with a shell and check |
| 1380 | " that Vim exits if it is closed. |
| 1381 | func Test_terminal_exit() |
| 1382 | CheckRunVimInTerminal |
| 1383 | |
| 1384 | let lines =<< trim END |
| 1385 | let winid = win_getid() |
| 1386 | help |
| 1387 | term |
| 1388 | let termid = win_getid() |
| 1389 | call win_gotoid(winid) |
| 1390 | close |
| 1391 | call win_gotoid(termid) |
| 1392 | END |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1393 | call writefile(lines, 'XtermExit', 'D') |
Bram Moolenaar | 4d14bac | 2019-10-20 21:15:15 +0200 | [diff] [blame] | 1394 | let buf = RunVimInTerminal('-S XtermExit', #{rows: 10}) |
| 1395 | let job = term_getjob(buf) |
| 1396 | call WaitForAssert({-> assert_equal("run", job_status(job))}) |
| 1397 | |
| 1398 | " quit the shell, it will make Vim exit |
| 1399 | call term_sendkeys(buf, "exit\<CR>") |
| 1400 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 4d14bac | 2019-10-20 21:15:15 +0200 | [diff] [blame] | 1401 | endfunc |
| 1402 | |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 1403 | func Test_terminal_open_autocmd() |
Bram Moolenaar | b852c3e | 2018-03-11 16:55:36 +0100 | [diff] [blame] | 1404 | augroup repro |
| 1405 | au! |
| 1406 | au TerminalOpen * let s:called += 1 |
| 1407 | augroup END |
| 1408 | |
| 1409 | let s:called = 0 |
| 1410 | |
| 1411 | " Open a terminal window with :terminal |
| 1412 | terminal |
| 1413 | call assert_equal(1, s:called) |
| 1414 | bwipe! |
| 1415 | |
| 1416 | " Open a terminal window with term_start() |
| 1417 | call term_start(&shell) |
| 1418 | call assert_equal(2, s:called) |
| 1419 | bwipe! |
| 1420 | |
| 1421 | " Open a hidden terminal buffer with :terminal |
| 1422 | terminal ++hidden |
| 1423 | call assert_equal(3, s:called) |
| 1424 | for buf in term_list() |
| 1425 | exe buf . "bwipe!" |
| 1426 | endfor |
| 1427 | |
| 1428 | " Open a hidden terminal buffer with term_start() |
| 1429 | let buf = term_start(&shell, {'hidden': 1}) |
| 1430 | call assert_equal(4, s:called) |
| 1431 | exe buf . "bwipe!" |
| 1432 | |
| 1433 | unlet s:called |
| 1434 | au! repro |
Bram Moolenaar | f4d61bc | 2020-11-14 14:22:28 +0100 | [diff] [blame] | 1435 | endfunc |
| 1436 | |
| 1437 | func Test_open_term_from_cmd() |
| 1438 | CheckUnix |
| 1439 | CheckRunVimInTerminal |
| 1440 | |
| 1441 | let lines =<< trim END |
| 1442 | call setline(1, ['a', 'b', 'c']) |
| 1443 | 3 |
| 1444 | set incsearch |
| 1445 | cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR> |
| 1446 | END |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1447 | call writefile(lines, 'Xopenterm', 'D') |
Bram Moolenaar | f4d61bc | 2020-11-14 14:22:28 +0100 | [diff] [blame] | 1448 | let buf = RunVimInTerminal('-S Xopenterm', {}) |
| 1449 | |
| 1450 | " this opens a window, incsearch should not use the old cursor position |
| 1451 | call term_sendkeys(buf, "/\<F3>") |
| 1452 | call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {}) |
| 1453 | call term_sendkeys(buf, "\<Esc>") |
| 1454 | call term_sendkeys(buf, ":q\<CR>") |
| 1455 | |
| 1456 | call StopVimInTerminal(buf) |
Bram Moolenaar | f4d61bc | 2020-11-14 14:22:28 +0100 | [diff] [blame] | 1457 | endfunc |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1458 | |
Bram Moolenaar | 4549dad | 2021-02-08 21:29:48 +0100 | [diff] [blame] | 1459 | func Test_combining_double_width() |
| 1460 | CheckUnix |
| 1461 | CheckRunVimInTerminal |
| 1462 | |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1463 | call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble', 'D') |
Bram Moolenaar | 4549dad | 2021-02-08 21:29:48 +0100 | [diff] [blame] | 1464 | let lines =<< trim END |
| 1465 | call term_start(['/bin/sh', '-c', 'cat Xonedouble']) |
| 1466 | END |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1467 | call writefile(lines, 'Xcombining', 'D') |
Bram Moolenaar | 4549dad | 2021-02-08 21:29:48 +0100 | [diff] [blame] | 1468 | let buf = RunVimInTerminal('-S Xcombining', #{rows: 9}) |
| 1469 | |
| 1470 | " this opens a window, incsearch should not use the old cursor position |
| 1471 | call VerifyScreenDump(buf, 'Test_terminal_combining', {}) |
| 1472 | call term_sendkeys(buf, ":q\<CR>") |
| 1473 | |
| 1474 | call StopVimInTerminal(buf) |
Bram Moolenaar | 4549dad | 2021-02-08 21:29:48 +0100 | [diff] [blame] | 1475 | endfunc |
| 1476 | |
Bram Moolenaar | 0276471 | 2020-11-14 20:21:55 +0100 | [diff] [blame] | 1477 | func Test_terminal_popup_with_cmd() |
| 1478 | " this was crashing |
| 1479 | let buf = term_start(&shell, #{hidden: v:true}) |
| 1480 | let s:winid = popup_create(buf, {}) |
| 1481 | tnoremap <F3> <Cmd>call popup_close(s:winid)<CR> |
| 1482 | call feedkeys("\<F3>", 'xt') |
| 1483 | |
| 1484 | tunmap <F3> |
| 1485 | exe 'bwipe! ' .. buf |
| 1486 | unlet s:winid |
| 1487 | endfunc |
| 1488 | |
Bram Moolenaar | 8adc8d9 | 2020-11-16 20:47:31 +0100 | [diff] [blame] | 1489 | func Test_terminal_popup_bufload() |
| 1490 | let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'}) |
| 1491 | let winid = popup_create(termbuf, {}) |
| 1492 | sleep 50m |
| 1493 | |
| 1494 | let newbuf = bufadd('') |
| 1495 | call bufload(newbuf) |
| 1496 | call setbufline(newbuf, 1, 'foobar') |
| 1497 | |
| 1498 | " must not have switched to another window |
| 1499 | call assert_equal(winid, win_getid()) |
| 1500 | |
Bram Moolenaar | e6329e4 | 2020-11-16 21:10:34 +0100 | [diff] [blame] | 1501 | call StopShellInTerminal(termbuf) |
| 1502 | call WaitFor({-> win_getid() != winid}) |
Bram Moolenaar | 8adc8d9 | 2020-11-16 20:47:31 +0100 | [diff] [blame] | 1503 | exe 'bwipe! ' .. newbuf |
| 1504 | endfunc |
| 1505 | |
Bram Moolenaar | 3194e5b | 2021-12-13 21:59:09 +0000 | [diff] [blame] | 1506 | func Test_terminal_popup_two_windows() |
Bram Moolenaar | 0407d27 | 2021-12-13 22:17:44 +0000 | [diff] [blame] | 1507 | CheckRunVimInTerminal |
Bram Moolenaar | 3194e5b | 2021-12-13 21:59:09 +0000 | [diff] [blame] | 1508 | CheckUnix |
| 1509 | |
| 1510 | " use "sh" instead of "&shell" in the hope it will use a short prompt |
| 1511 | let lines =<< trim END |
| 1512 | let termbuf = term_start('sh', #{hidden: v:true, term_finish: 'close'}) |
| 1513 | exe 'buffer ' .. termbuf |
| 1514 | |
| 1515 | let winid = popup_create(termbuf, #{line: 2, minwidth: 30, border: []}) |
| 1516 | sleep 50m |
| 1517 | |
| 1518 | call term_sendkeys(termbuf, "echo 'test'") |
| 1519 | END |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1520 | call writefile(lines, 'XpopupScript', 'D') |
Bram Moolenaar | 3194e5b | 2021-12-13 21:59:09 +0000 | [diff] [blame] | 1521 | let buf = RunVimInTerminal('-S XpopupScript', {}) |
| 1522 | |
| 1523 | " typed text appears both in normal window and in popup |
| 1524 | call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 1))}) |
| 1525 | call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 3))}) |
| 1526 | |
Bram Moolenaar | 0407d27 | 2021-12-13 22:17:44 +0000 | [diff] [blame] | 1527 | call term_sendkeys(buf, "\<CR>\<CR>exit\<CR>") |
| 1528 | call TermWait(buf) |
| 1529 | call term_sendkeys(buf, ":q\<CR>") |
Bram Moolenaar | 3194e5b | 2021-12-13 21:59:09 +0000 | [diff] [blame] | 1530 | call StopVimInTerminal(buf) |
Bram Moolenaar | 3194e5b | 2021-12-13 21:59:09 +0000 | [diff] [blame] | 1531 | endfunc |
| 1532 | |
Bram Moolenaar | e41decc | 2020-11-14 21:34:59 +0100 | [diff] [blame] | 1533 | func Test_terminal_popup_insert_cmd() |
| 1534 | CheckUnix |
| 1535 | |
| 1536 | inoremap <F3> <Cmd>call StartTermInPopup()<CR> |
| 1537 | func StartTermInPopup() |
Bram Moolenaar | 27f4f6b | 2020-11-16 21:02:28 +0100 | [diff] [blame] | 1538 | call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'}) |
Bram Moolenaar | e41decc | 2020-11-14 21:34:59 +0100 | [diff] [blame] | 1539 | endfunc |
| 1540 | call feedkeys("i\<F3>") |
| 1541 | sleep 10m |
| 1542 | call assert_equal('n', mode()) |
| 1543 | |
| 1544 | call feedkeys("\<C-D>", 'xt') |
Bram Moolenaar | 17ab28d | 2020-11-18 12:24:01 +0100 | [diff] [blame] | 1545 | call WaitFor({-> popup_list() == []}) |
Bram Moolenaar | e41decc | 2020-11-14 21:34:59 +0100 | [diff] [blame] | 1546 | delfunc StartTermInPopup |
| 1547 | iunmap <F3> |
| 1548 | endfunc |
| 1549 | |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1550 | func Check_dump01(off) |
| 1551 | call assert_equal('one two three four five', trim(getline(a:off + 1))) |
| 1552 | call assert_equal('~ Select Word', trim(getline(a:off + 7))) |
Bram Moolenaar | 1834d37 | 2018-03-29 17:40:46 +0200 | [diff] [blame] | 1553 | call assert_equal(':popup PopUp', trim(getline(a:off + 20))) |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1554 | endfunc |
| 1555 | |
Bram Moolenaar | f06b0b6 | 2018-03-29 17:22:24 +0200 | [diff] [blame] | 1556 | func Test_terminal_dumpwrite_composing() |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 1557 | CheckRunVimInTerminal |
Bram Moolenaar | 3194e5b | 2021-12-13 21:59:09 +0000 | [diff] [blame] | 1558 | |
Bram Moolenaar | f06b0b6 | 2018-03-29 17:22:24 +0200 | [diff] [blame] | 1559 | let save_enc = &encoding |
| 1560 | set encoding=utf-8 |
| 1561 | call assert_equal(1, winnr('$')) |
| 1562 | |
| 1563 | let text = " a\u0300 e\u0302 o\u0308" |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1564 | call writefile([text], 'Xcomposing', 'D') |
Bram Moolenaar | 77bfd75 | 2018-04-30 18:03:10 +0200 | [diff] [blame] | 1565 | let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {}) |
Bram Moolenaar | 0e9d1ae | 2018-04-30 14:28:24 +0200 | [diff] [blame] | 1566 | call WaitForAssert({-> assert_match(text, term_getline(buf, 1))}) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 1567 | eval 'Xdump'->term_dumpwrite(buf) |
Bram Moolenaar | f06b0b6 | 2018-03-29 17:22:24 +0200 | [diff] [blame] | 1568 | let dumpline = readfile('Xdump')[0] |
| 1569 | call assert_match('|à| |ê| |ö', dumpline) |
| 1570 | |
| 1571 | call StopVimInTerminal(buf) |
Bram Moolenaar | f06b0b6 | 2018-03-29 17:22:24 +0200 | [diff] [blame] | 1572 | call delete('Xdump') |
| 1573 | let &encoding = save_enc |
| 1574 | endfunc |
| 1575 | |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1576 | " Tests for failures in the term_dumpwrite() function |
| 1577 | func Test_terminal_dumpwrite_errors() |
| 1578 | CheckRunVimInTerminal |
| 1579 | call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:') |
| 1580 | let buf = RunVimInTerminal('', {}) |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 1581 | call TermWait(buf) |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1582 | call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E1206:') |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1583 | call assert_fails("call term_dumpwrite(buf, [])", 'E730:') |
| 1584 | call writefile([], 'Xtest.dump') |
| 1585 | call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:') |
| 1586 | call delete('Xtest.dump') |
| 1587 | call assert_fails("call term_dumpwrite(buf, '')", 'E482:') |
| 1588 | call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:') |
Bram Moolenaar | 98f1671 | 2020-05-22 13:34:01 +0200 | [diff] [blame] | 1589 | call test_garbagecollect_now() |
Bram Moolenaar | a46765a | 2020-11-01 20:58:26 +0100 | [diff] [blame] | 1590 | call StopVimInTerminal(buf, 0) |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 1591 | call TermWait(buf) |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1592 | call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:') |
| 1593 | call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:') |
| 1594 | call assert_equal(0, term_sendkeys(buf, ":q\<CR>")) |
| 1595 | endfunc |
| 1596 | |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1597 | " just testing basic functionality. |
| 1598 | func Test_terminal_dumpload() |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1599 | let curbuf = winbufnr('') |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1600 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1601 | let buf = term_dumpload('dumps/Test_popup_command_01.dump') |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1602 | call assert_equal(2, winnr('$')) |
| 1603 | call assert_equal(20, line('$')) |
| 1604 | call Check_dump01(0) |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1605 | |
| 1606 | " Load another dump in the same window |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 1607 | let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf}) |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1608 | call assert_equal(buf, buf2) |
| 1609 | call assert_notequal('one two three four five', trim(getline(1))) |
| 1610 | |
| 1611 | " Load the first dump again in the same window |
| 1612 | let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf}) |
| 1613 | call assert_equal(buf, buf2) |
| 1614 | call Check_dump01(0) |
| 1615 | |
| 1616 | call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:') |
| 1617 | call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:') |
| 1618 | new |
| 1619 | let closedbuf = winbufnr('') |
| 1620 | quit |
| 1621 | call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:') |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1622 | call assert_fails('call term_dumpload([])', 'E730:') |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1623 | call assert_fails('call term_dumpload("xabcy.dump")', 'E485:') |
Bram Moolenaar | 87abab9 | 2019-06-03 21:14:59 +0200 | [diff] [blame] | 1624 | |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1625 | quit |
| 1626 | endfunc |
| 1627 | |
Bram Moolenaar | 17efc7f | 2019-10-16 18:11:31 +0200 | [diff] [blame] | 1628 | func Test_terminal_dumpload_dump() |
| 1629 | CheckRunVimInTerminal |
| 1630 | |
| 1631 | let lines =<< trim END |
| 1632 | call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12}) |
| 1633 | END |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1634 | call writefile(lines, 'XtermDumpload', 'D') |
Bram Moolenaar | 17efc7f | 2019-10-16 18:11:31 +0200 | [diff] [blame] | 1635 | let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15}) |
| 1636 | call VerifyScreenDump(buf, 'Test_terminal_dumpload', {}) |
| 1637 | |
| 1638 | call StopVimInTerminal(buf) |
Bram Moolenaar | 17efc7f | 2019-10-16 18:11:31 +0200 | [diff] [blame] | 1639 | endfunc |
| 1640 | |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1641 | func Test_terminal_dumpdiff() |
| 1642 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 7ee80f7 | 2019-09-08 20:55:06 +0200 | [diff] [blame] | 1643 | 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] | 1644 | call assert_equal(2, winnr('$')) |
| 1645 | call assert_equal(62, line('$')) |
| 1646 | call Check_dump01(0) |
| 1647 | call Check_dump01(42) |
| 1648 | call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29]) |
| 1649 | quit |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1650 | |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1651 | call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:') |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1652 | call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:') |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1653 | call writefile([], 'X1.dump', 'D') |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 1654 | call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:') |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1655 | endfunc |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1656 | |
Bram Moolenaar | c3ef896 | 2019-02-15 00:16:13 +0100 | [diff] [blame] | 1657 | func Test_terminal_dumpdiff_swap() |
| 1658 | call assert_equal(1, winnr('$')) |
| 1659 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump') |
| 1660 | call assert_equal(2, winnr('$')) |
| 1661 | call assert_equal(62, line('$')) |
| 1662 | call assert_match('Test_popup_command_01.dump', getline(21)) |
| 1663 | call assert_match('Test_popup_command_03.dump', getline(42)) |
| 1664 | call assert_match('Undo', getline(3)) |
| 1665 | call assert_match('three four five', getline(45)) |
| 1666 | |
| 1667 | normal s |
| 1668 | call assert_match('Test_popup_command_03.dump', getline(21)) |
| 1669 | call assert_match('Test_popup_command_01.dump', getline(42)) |
| 1670 | call assert_match('three four five', getline(3)) |
| 1671 | call assert_match('Undo', getline(45)) |
| 1672 | quit |
Bram Moolenaar | 98f1671 | 2020-05-22 13:34:01 +0200 | [diff] [blame] | 1673 | |
| 1674 | " Diff two terminal dump files with different number of rows |
| 1675 | " Swap the diffs |
| 1676 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump') |
| 1677 | call assert_match('Test_popup_command_01.dump', getline(21)) |
| 1678 | call assert_match('Test_winline_rnu.dump', getline(42)) |
| 1679 | normal s |
| 1680 | call assert_match('Test_winline_rnu.dump', getline(6)) |
| 1681 | call assert_match('Test_popup_command_01.dump', getline(27)) |
| 1682 | quit |
Bram Moolenaar | c3ef896 | 2019-02-15 00:16:13 +0100 | [diff] [blame] | 1683 | endfunc |
| 1684 | |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1685 | func Test_terminal_dumpdiff_options() |
| 1686 | set laststatus=0 |
| 1687 | call assert_equal(1, winnr('$')) |
| 1688 | let height = winheight(0) |
| 1689 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33}) |
| 1690 | call assert_equal(2, winnr('$')) |
| 1691 | call assert_equal(height, winheight(winnr())) |
| 1692 | call assert_equal(33, winwidth(winnr())) |
| 1693 | call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%')) |
| 1694 | quit |
| 1695 | |
| 1696 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1697 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'}) |
| 1698 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | e809a4e | 2019-07-04 17:35:05 +0200 | [diff] [blame] | 1699 | call assert_equal(&columns, winwidth(0)) |
| 1700 | call assert_equal(13, winheight(0)) |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1701 | call assert_equal('something else', bufname('%')) |
| 1702 | quit |
| 1703 | |
| 1704 | call assert_equal(1, winnr('$')) |
| 1705 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1}) |
| 1706 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1707 | 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] | 1708 | bwipe |
| 1709 | |
| 1710 | set laststatus& |
| 1711 | endfunc |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 1712 | |
Bram Moolenaar | 1077230 | 2019-01-20 18:25:54 +0100 | [diff] [blame] | 1713 | " When drawing the statusline the cursor position may not have been updated |
| 1714 | " yet. |
| 1715 | " 1. create a terminal, make it show 2 lines |
| 1716 | " 2. 0.5 sec later: leave terminal window, execute "i" |
| 1717 | " 3. 0.5 sec later: clear terminal window, now it's 1 line |
| 1718 | " 4. 0.5 sec later: redraw, including statusline (used to trigger bug) |
| 1719 | " 4. 0.5 sec later: should be done, clean up |
| 1720 | func Test_terminal_statusline() |
Bram Moolenaar | adbde3f | 2019-09-08 22:57:14 +0200 | [diff] [blame] | 1721 | CheckUnix |
Bram Moolenaar | 8103527 | 2021-12-16 18:02:07 +0000 | [diff] [blame] | 1722 | CheckFeature timers |
Bram Moolenaar | adbde3f | 2019-09-08 22:57:14 +0200 | [diff] [blame] | 1723 | |
Bram Moolenaar | 1077230 | 2019-01-20 18:25:54 +0100 | [diff] [blame] | 1724 | set statusline=x |
| 1725 | terminal |
| 1726 | let tbuf = bufnr('') |
| 1727 | call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n") |
| 1728 | call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') }) |
| 1729 | call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') }) |
| 1730 | au BufLeave * if &buftype == 'terminal' | silent! normal i | endif |
| 1731 | |
| 1732 | sleep 2 |
| 1733 | exe tbuf . 'bwipe!' |
| 1734 | au! BufLeave |
| 1735 | set statusline= |
| 1736 | endfunc |
Bram Moolenaar | fa1e90c | 2019-04-06 17:47:40 +0200 | [diff] [blame] | 1737 | |
Bram Moolenaar | 8103527 | 2021-12-16 18:02:07 +0000 | [diff] [blame] | 1738 | func CheckTerminalWindowWorks(buf) |
| 1739 | call WaitForAssert({-> assert_match('!sh \[running\]', term_getline(a:buf, 10))}) |
| 1740 | call term_sendkeys(a:buf, "exit\<CR>") |
| 1741 | call WaitForAssert({-> assert_match('!sh \[finished\]', term_getline(a:buf, 10))}) |
| 1742 | call term_sendkeys(a:buf, ":q\<CR>") |
| 1743 | call WaitForAssert({-> assert_match('^\~', term_getline(a:buf, 10))}) |
| 1744 | endfunc |
| 1745 | |
| 1746 | func Test_start_terminal_from_timer() |
| 1747 | CheckUnix |
| 1748 | CheckFeature timers |
| 1749 | |
| 1750 | " Open a terminal window from a timer, typed text goes to the terminal |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1751 | call writefile(["call timer_start(100, { -> term_start('sh') })"], 'XtimerTerm', 'D') |
Bram Moolenaar | 8103527 | 2021-12-16 18:02:07 +0000 | [diff] [blame] | 1752 | let buf = RunVimInTerminal('-S XtimerTerm', {}) |
| 1753 | call CheckTerminalWindowWorks(buf) |
| 1754 | |
| 1755 | " do the same in Insert mode |
| 1756 | call term_sendkeys(buf, ":call timer_start(200, { -> term_start('sh') })\<CR>a") |
| 1757 | call CheckTerminalWindowWorks(buf) |
| 1758 | |
| 1759 | call StopVimInTerminal(buf) |
Bram Moolenaar | 8103527 | 2021-12-16 18:02:07 +0000 | [diff] [blame] | 1760 | endfunc |
| 1761 | |
Bram Moolenaar | f43e7ac | 2020-09-29 21:23:25 +0200 | [diff] [blame] | 1762 | func Test_terminal_window_focus() |
| 1763 | let winid1 = win_getid() |
| 1764 | terminal |
| 1765 | let winid2 = win_getid() |
| 1766 | call feedkeys("\<C-W>j", 'xt') |
| 1767 | call assert_equal(winid1, win_getid()) |
| 1768 | call feedkeys("\<C-W>k", 'xt') |
| 1769 | call assert_equal(winid2, win_getid()) |
| 1770 | " can use a cursor key here |
| 1771 | call feedkeys("\<C-W>\<Down>", 'xt') |
| 1772 | call assert_equal(winid1, win_getid()) |
| 1773 | call feedkeys("\<C-W>\<Up>", 'xt') |
| 1774 | call assert_equal(winid2, win_getid()) |
| 1775 | |
| 1776 | bwipe! |
| 1777 | endfunc |
| 1778 | |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 1779 | func Api_drop_common(options) |
| 1780 | call assert_equal(1, winnr('$')) |
| 1781 | |
| 1782 | " Use the title termcap entries to output the escape sequence. |
| 1783 | call writefile([ |
| 1784 | \ 'set title', |
| 1785 | \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"', |
| 1786 | \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''', |
| 1787 | \ 'redraw', |
| 1788 | \ "set t_ts=", |
| 1789 | \ ], 'Xscript') |
| 1790 | let buf = RunVimInTerminal('-S Xscript', {}) |
| 1791 | call WaitFor({-> bufnr('Xtextfile') > 0}) |
| 1792 | call assert_equal('Xtextfile', expand('%:t')) |
| 1793 | call assert_true(winnr('$') >= 3) |
| 1794 | return buf |
| 1795 | endfunc |
| 1796 | |
| 1797 | func Test_terminal_api_drop_newwin() |
| 1798 | CheckRunVimInTerminal |
| 1799 | let buf = Api_drop_common('') |
| 1800 | call assert_equal(0, &bin) |
| 1801 | call assert_equal('', &fenc) |
| 1802 | |
| 1803 | call StopVimInTerminal(buf) |
| 1804 | call delete('Xscript') |
| 1805 | bwipe Xtextfile |
| 1806 | endfunc |
| 1807 | |
| 1808 | func Test_terminal_api_drop_newwin_bin() |
| 1809 | CheckRunVimInTerminal |
| 1810 | let buf = Api_drop_common(',{"bin":1}') |
| 1811 | call assert_equal(1, &bin) |
| 1812 | |
| 1813 | call StopVimInTerminal(buf) |
| 1814 | call delete('Xscript') |
| 1815 | bwipe Xtextfile |
| 1816 | endfunc |
| 1817 | |
| 1818 | func Test_terminal_api_drop_newwin_binary() |
| 1819 | CheckRunVimInTerminal |
| 1820 | let buf = Api_drop_common(',{"binary":1}') |
| 1821 | call assert_equal(1, &bin) |
| 1822 | |
| 1823 | call StopVimInTerminal(buf) |
| 1824 | call delete('Xscript') |
| 1825 | bwipe Xtextfile |
| 1826 | endfunc |
| 1827 | |
| 1828 | func Test_terminal_api_drop_newwin_nobin() |
| 1829 | CheckRunVimInTerminal |
| 1830 | set binary |
| 1831 | let buf = Api_drop_common(',{"nobin":1}') |
| 1832 | call assert_equal(0, &bin) |
| 1833 | |
| 1834 | call StopVimInTerminal(buf) |
| 1835 | call delete('Xscript') |
| 1836 | bwipe Xtextfile |
| 1837 | set nobinary |
| 1838 | endfunc |
| 1839 | |
| 1840 | func Test_terminal_api_drop_newwin_nobinary() |
| 1841 | CheckRunVimInTerminal |
| 1842 | set binary |
| 1843 | let buf = Api_drop_common(',{"nobinary":1}') |
| 1844 | call assert_equal(0, &bin) |
| 1845 | |
| 1846 | call StopVimInTerminal(buf) |
| 1847 | call delete('Xscript') |
| 1848 | bwipe Xtextfile |
| 1849 | set nobinary |
| 1850 | endfunc |
| 1851 | |
| 1852 | func Test_terminal_api_drop_newwin_ff() |
| 1853 | CheckRunVimInTerminal |
| 1854 | let buf = Api_drop_common(',{"ff":"dos"}') |
| 1855 | call assert_equal("dos", &ff) |
| 1856 | |
| 1857 | call StopVimInTerminal(buf) |
| 1858 | call delete('Xscript') |
| 1859 | bwipe Xtextfile |
| 1860 | endfunc |
| 1861 | |
| 1862 | func Test_terminal_api_drop_newwin_fileformat() |
| 1863 | CheckRunVimInTerminal |
| 1864 | let buf = Api_drop_common(',{"fileformat":"dos"}') |
| 1865 | call assert_equal("dos", &ff) |
| 1866 | |
| 1867 | call StopVimInTerminal(buf) |
| 1868 | call delete('Xscript') |
| 1869 | bwipe Xtextfile |
| 1870 | endfunc |
| 1871 | |
| 1872 | func Test_terminal_api_drop_newwin_enc() |
| 1873 | CheckRunVimInTerminal |
| 1874 | let buf = Api_drop_common(',{"enc":"utf-16"}') |
| 1875 | call assert_equal("utf-16", &fenc) |
| 1876 | |
| 1877 | call StopVimInTerminal(buf) |
| 1878 | call delete('Xscript') |
| 1879 | bwipe Xtextfile |
| 1880 | endfunc |
| 1881 | |
| 1882 | func Test_terminal_api_drop_newwin_encoding() |
| 1883 | CheckRunVimInTerminal |
| 1884 | let buf = Api_drop_common(',{"encoding":"utf-16"}') |
| 1885 | call assert_equal("utf-16", &fenc) |
| 1886 | |
| 1887 | call StopVimInTerminal(buf) |
| 1888 | call delete('Xscript') |
| 1889 | bwipe Xtextfile |
| 1890 | endfunc |
| 1891 | |
| 1892 | func Test_terminal_api_drop_oldwin() |
| 1893 | CheckRunVimInTerminal |
| 1894 | let firstwinid = win_getid() |
| 1895 | split Xtextfile |
| 1896 | let textfile_winid = win_getid() |
| 1897 | call assert_equal(2, winnr('$')) |
| 1898 | call win_gotoid(firstwinid) |
| 1899 | |
| 1900 | " Use the title termcap entries to output the escape sequence. |
| 1901 | call writefile([ |
| 1902 | \ 'set title', |
| 1903 | \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"', |
| 1904 | \ 'let &titlestring = ''["drop","Xtextfile"]''', |
| 1905 | \ 'redraw', |
| 1906 | \ "set t_ts=", |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 1907 | \ ], 'Xscript', 'D') |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 1908 | let buf = RunVimInTerminal('-S Xscript', {'rows': 10}) |
| 1909 | call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))}) |
| 1910 | call assert_equal(textfile_winid, win_getid()) |
| 1911 | |
| 1912 | call StopVimInTerminal(buf) |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 1913 | bwipe Xtextfile |
| 1914 | endfunc |
| 1915 | |
| 1916 | func Tapi_TryThis(bufnum, arg) |
| 1917 | let g:called_bufnum = a:bufnum |
| 1918 | let g:called_arg = a:arg |
| 1919 | endfunc |
| 1920 | |
| 1921 | func WriteApiCall(funcname) |
| 1922 | " Use the title termcap entries to output the escape sequence. |
| 1923 | call writefile([ |
| 1924 | \ 'set title', |
| 1925 | \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"', |
| 1926 | \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''', |
| 1927 | \ 'redraw', |
| 1928 | \ "set t_ts=", |
| 1929 | \ ], 'Xscript') |
| 1930 | endfunc |
| 1931 | |
| 1932 | func Test_terminal_api_call() |
| 1933 | CheckRunVimInTerminal |
| 1934 | |
| 1935 | unlet! g:called_bufnum |
| 1936 | unlet! g:called_arg |
| 1937 | |
| 1938 | call WriteApiCall('Tapi_TryThis') |
| 1939 | |
| 1940 | " Default |
| 1941 | let buf = RunVimInTerminal('-S Xscript', {}) |
| 1942 | call WaitFor({-> exists('g:called_bufnum')}) |
| 1943 | call assert_equal(buf, g:called_bufnum) |
| 1944 | call assert_equal(['hello', 123], g:called_arg) |
| 1945 | call StopVimInTerminal(buf) |
| 1946 | |
| 1947 | unlet! g:called_bufnum |
| 1948 | unlet! g:called_arg |
| 1949 | |
| 1950 | " Enable explicitly |
| 1951 | let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'}) |
| 1952 | call WaitFor({-> exists('g:called_bufnum')}) |
| 1953 | call assert_equal(buf, g:called_bufnum) |
| 1954 | call assert_equal(['hello', 123], g:called_arg) |
| 1955 | call StopVimInTerminal(buf) |
| 1956 | |
| 1957 | unlet! g:called_bufnum |
| 1958 | unlet! g:called_arg |
| 1959 | |
| 1960 | func! ApiCall_TryThis(bufnum, arg) |
| 1961 | let g:called_bufnum2 = a:bufnum |
| 1962 | let g:called_arg2 = a:arg |
| 1963 | endfunc |
| 1964 | |
| 1965 | call WriteApiCall('ApiCall_TryThis') |
| 1966 | |
| 1967 | " Use prefix match |
| 1968 | let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'}) |
| 1969 | call WaitFor({-> exists('g:called_bufnum2')}) |
| 1970 | call assert_equal(buf, g:called_bufnum2) |
| 1971 | call assert_equal(['hello', 123], g:called_arg2) |
| 1972 | call StopVimInTerminal(buf) |
| 1973 | |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1974 | call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:') |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 1975 | |
| 1976 | unlet! g:called_bufnum2 |
| 1977 | unlet! g:called_arg2 |
| 1978 | |
| 1979 | call delete('Xscript') |
| 1980 | delfunction! ApiCall_TryThis |
| 1981 | unlet! g:called_bufnum2 |
| 1982 | unlet! g:called_arg2 |
| 1983 | endfunc |
| 1984 | |
| 1985 | func Test_terminal_api_call_fails() |
| 1986 | CheckRunVimInTerminal |
| 1987 | |
| 1988 | func! TryThis(bufnum, arg) |
| 1989 | let g:called_bufnum3 = a:bufnum |
| 1990 | let g:called_arg3 = a:arg |
| 1991 | endfunc |
| 1992 | |
| 1993 | call WriteApiCall('TryThis') |
| 1994 | |
| 1995 | unlet! g:called_bufnum3 |
| 1996 | unlet! g:called_arg3 |
| 1997 | |
| 1998 | " Not permitted |
| 1999 | call ch_logfile('Xlog', 'w') |
| 2000 | let buf = RunVimInTerminal('-S Xscript', {'term_api': ''}) |
| 2001 | call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))}) |
| 2002 | call assert_false(exists('g:called_bufnum3')) |
| 2003 | call assert_false(exists('g:called_arg3')) |
| 2004 | call StopVimInTerminal(buf) |
| 2005 | |
| 2006 | " No match |
| 2007 | call ch_logfile('Xlog', 'w') |
| 2008 | let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'}) |
| 2009 | call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'}) |
| 2010 | call assert_false(exists('g:called_bufnum3')) |
| 2011 | call assert_false(exists('g:called_arg3')) |
| 2012 | call StopVimInTerminal(buf) |
| 2013 | |
| 2014 | call delete('Xscript') |
| 2015 | call ch_logfile('') |
| 2016 | call delete('Xlog') |
| 2017 | delfunction! TryThis |
| 2018 | unlet! g:called_bufnum3 |
| 2019 | unlet! g:called_arg3 |
| 2020 | endfunc |
| 2021 | |
| 2022 | let s:caught_e937 = 0 |
| 2023 | |
| 2024 | func Tapi_Delete(bufnum, arg) |
| 2025 | try |
| 2026 | execute 'bdelete!' a:bufnum |
| 2027 | catch /E937:/ |
| 2028 | let s:caught_e937 = 1 |
| 2029 | endtry |
| 2030 | endfunc |
| 2031 | |
| 2032 | func Test_terminal_api_call_fail_delete() |
| 2033 | CheckRunVimInTerminal |
| 2034 | |
| 2035 | call WriteApiCall('Tapi_Delete') |
| 2036 | let buf = RunVimInTerminal('-S Xscript', {}) |
| 2037 | call WaitForAssert({-> assert_equal(1, s:caught_e937)}) |
| 2038 | |
| 2039 | call StopVimInTerminal(buf) |
| 2040 | call delete('Xscript') |
| 2041 | call ch_logfile('', '') |
| 2042 | endfunc |
| 2043 | |
| 2044 | func Test_terminal_setapi_and_call() |
| 2045 | CheckRunVimInTerminal |
| 2046 | |
| 2047 | call WriteApiCall('Tapi_TryThis') |
| 2048 | call ch_logfile('Xlog', 'w') |
| 2049 | |
| 2050 | unlet! g:called_bufnum |
| 2051 | unlet! g:called_arg |
| 2052 | |
| 2053 | let buf = RunVimInTerminal('-S Xscript', {'term_api': ''}) |
| 2054 | call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))}) |
| 2055 | call assert_false(exists('g:called_bufnum')) |
| 2056 | call assert_false(exists('g:called_arg')) |
| 2057 | |
| 2058 | eval buf->term_setapi('Tapi_') |
| 2059 | call term_sendkeys(buf, ":set notitle\<CR>") |
| 2060 | call term_sendkeys(buf, ":source Xscript\<CR>") |
| 2061 | call WaitFor({-> exists('g:called_bufnum')}) |
| 2062 | call assert_equal(buf, g:called_bufnum) |
| 2063 | call assert_equal(['hello', 123], g:called_arg) |
| 2064 | |
| 2065 | call StopVimInTerminal(buf) |
| 2066 | |
| 2067 | call delete('Xscript') |
| 2068 | call ch_logfile('') |
| 2069 | call delete('Xlog') |
| 2070 | unlet! g:called_bufnum |
| 2071 | unlet! g:called_arg |
| 2072 | endfunc |
| 2073 | |
| 2074 | func Test_terminal_api_arg() |
| 2075 | CheckRunVimInTerminal |
| 2076 | |
| 2077 | call WriteApiCall('Tapi_TryThis') |
| 2078 | call ch_logfile('Xlog', 'w') |
| 2079 | |
| 2080 | unlet! g:called_bufnum |
| 2081 | unlet! g:called_arg |
| 2082 | |
| 2083 | execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript' |
| 2084 | let buf = bufnr('%') |
| 2085 | call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))}) |
| 2086 | call assert_false(exists('g:called_bufnum')) |
| 2087 | call assert_false(exists('g:called_arg')) |
| 2088 | |
| 2089 | call StopVimInTerminal(buf) |
| 2090 | |
| 2091 | call ch_logfile('Xlog', 'w') |
| 2092 | |
| 2093 | execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript' |
| 2094 | let buf = bufnr('%') |
| 2095 | call WaitFor({-> exists('g:called_bufnum')}) |
| 2096 | call assert_equal(buf, g:called_bufnum) |
| 2097 | call assert_equal(['hello', 123], g:called_arg) |
| 2098 | |
| 2099 | call StopVimInTerminal(buf) |
| 2100 | |
| 2101 | call delete('Xscript') |
| 2102 | call ch_logfile('') |
| 2103 | call delete('Xlog') |
| 2104 | unlet! g:called_bufnum |
| 2105 | unlet! g:called_arg |
| 2106 | endfunc |
| 2107 | |
| 2108 | func Test_terminal_ansicolors_default() |
| 2109 | CheckFunction term_getansicolors |
| 2110 | |
| 2111 | let colors = [ |
| 2112 | \ '#000000', '#e00000', |
| 2113 | \ '#00e000', '#e0e000', |
| 2114 | \ '#0000e0', '#e000e0', |
| 2115 | \ '#00e0e0', '#e0e0e0', |
| 2116 | \ '#808080', '#ff4040', |
| 2117 | \ '#40ff40', '#ffff40', |
| 2118 | \ '#4040ff', '#ff40ff', |
| 2119 | \ '#40ffff', '#ffffff', |
| 2120 | \] |
| 2121 | |
| 2122 | let buf = Run_shell_in_terminal({}) |
| 2123 | call assert_equal(colors, term_getansicolors(buf)) |
| 2124 | call StopShellInTerminal(buf) |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 2125 | call assert_equal([], term_getansicolors(buf)) |
| 2126 | |
| 2127 | exe buf . 'bwipe' |
| 2128 | endfunc |
| 2129 | |
| 2130 | let s:test_colors = [ |
| 2131 | \ '#616e64', '#0d0a79', |
| 2132 | \ '#6d610d', '#0a7373', |
| 2133 | \ '#690d0a', '#6d696e', |
| 2134 | \ '#0d0a6f', '#616e0d', |
| 2135 | \ '#0a6479', '#6d0d0a', |
| 2136 | \ '#617373', '#0d0a69', |
| 2137 | \ '#6d690d', '#0a6e6f', |
| 2138 | \ '#610d0a', '#6e6479', |
| 2139 | \] |
| 2140 | |
| 2141 | func Test_terminal_ansicolors_global() |
| 2142 | CheckFeature termguicolors |
| 2143 | CheckFunction term_getansicolors |
| 2144 | |
LemonBoy | b2b3acb | 2022-05-20 10:10:34 +0100 | [diff] [blame] | 2145 | if has('vtp') && !has('vcon') && !has('gui_running') |
| 2146 | throw 'Skipped: does not support termguicolors' |
| 2147 | endif |
| 2148 | |
| 2149 | set tgc |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 2150 | let g:terminal_ansi_colors = reverse(copy(s:test_colors)) |
| 2151 | let buf = Run_shell_in_terminal({}) |
| 2152 | call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf)) |
| 2153 | call StopShellInTerminal(buf) |
LemonBoy | b2b3acb | 2022-05-20 10:10:34 +0100 | [diff] [blame] | 2154 | set tgc& |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 2155 | |
| 2156 | exe buf . 'bwipe' |
| 2157 | unlet g:terminal_ansi_colors |
| 2158 | endfunc |
| 2159 | |
| 2160 | func Test_terminal_ansicolors_func() |
| 2161 | CheckFeature termguicolors |
| 2162 | CheckFunction term_getansicolors |
| 2163 | |
LemonBoy | b2b3acb | 2022-05-20 10:10:34 +0100 | [diff] [blame] | 2164 | if has('vtp') && !has('vcon') && !has('gui_running') |
| 2165 | throw 'Skipped: does not support termguicolors' |
| 2166 | endif |
| 2167 | |
| 2168 | set tgc |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 2169 | let g:terminal_ansi_colors = reverse(copy(s:test_colors)) |
| 2170 | let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors}) |
| 2171 | call assert_equal(s:test_colors, term_getansicolors(buf)) |
| 2172 | |
| 2173 | call term_setansicolors(buf, g:terminal_ansi_colors) |
| 2174 | call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors()) |
| 2175 | |
| 2176 | let colors = [ |
| 2177 | \ 'ivory', 'AliceBlue', |
| 2178 | \ 'grey67', 'dark goldenrod', |
| 2179 | \ 'SteelBlue3', 'PaleVioletRed4', |
| 2180 | \ 'MediumPurple2', 'yellow2', |
| 2181 | \ 'RosyBrown3', 'OrangeRed2', |
| 2182 | \ 'white smoke', 'navy blue', |
| 2183 | \ 'grey47', 'gray97', |
| 2184 | \ 'MistyRose2', 'DodgerBlue4', |
| 2185 | \] |
| 2186 | eval buf->term_setansicolors(colors) |
| 2187 | |
| 2188 | let colors[4] = 'Invalid' |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 2189 | call assert_fails('call term_setansicolors(buf, colors)', 'E254:') |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 2190 | call assert_fails('call term_setansicolors(buf, {})', 'E1211:') |
Bram Moolenaar | 2391954 | 2023-05-05 22:12:22 +0100 | [diff] [blame] | 2191 | call assert_fails('call term_setansicolors(buf, [])', 'E475: Invalid value for argument "colors"') |
LemonBoy | b2b3acb | 2022-05-20 10:10:34 +0100 | [diff] [blame] | 2192 | set tgc& |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 2193 | |
| 2194 | call StopShellInTerminal(buf) |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 2195 | call assert_equal(0, term_setansicolors(buf, [])) |
| 2196 | exe buf . 'bwipe' |
| 2197 | endfunc |
| 2198 | |
| 2199 | func Test_terminal_all_ansi_colors() |
| 2200 | CheckRunVimInTerminal |
| 2201 | |
| 2202 | " Use all the ANSI colors. |
| 2203 | call writefile([ |
| 2204 | \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")', |
| 2205 | \ 'hi Tblack ctermfg=0 ctermbg=8', |
| 2206 | \ 'hi Tdarkred ctermfg=1 ctermbg=9', |
| 2207 | \ 'hi Tdarkgreen ctermfg=2 ctermbg=10', |
| 2208 | \ 'hi Tbrown ctermfg=3 ctermbg=11', |
| 2209 | \ 'hi Tdarkblue ctermfg=4 ctermbg=12', |
| 2210 | \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13', |
| 2211 | \ 'hi Tdarkcyan ctermfg=6 ctermbg=14', |
| 2212 | \ 'hi Tlightgrey ctermfg=7 ctermbg=15', |
| 2213 | \ 'hi Tdarkgrey ctermfg=8 ctermbg=0', |
| 2214 | \ 'hi Tred ctermfg=9 ctermbg=1', |
| 2215 | \ 'hi Tgreen ctermfg=10 ctermbg=2', |
| 2216 | \ 'hi Tyellow ctermfg=11 ctermbg=3', |
| 2217 | \ 'hi Tblue ctermfg=12 ctermbg=4', |
| 2218 | \ 'hi Tmagenta ctermfg=13 ctermbg=5', |
| 2219 | \ 'hi Tcyan ctermfg=14 ctermbg=6', |
| 2220 | \ 'hi Twhite ctermfg=15 ctermbg=7', |
| 2221 | \ 'hi TdarkredBold ctermfg=1 cterm=bold', |
| 2222 | \ 'hi TgreenBold ctermfg=10 cterm=bold', |
| 2223 | \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5', |
| 2224 | \ '', |
| 2225 | \ 'call matchadd("Tblack", "A")', |
| 2226 | \ 'call matchadd("Tdarkred", "B")', |
| 2227 | \ 'call matchadd("Tdarkgreen", "C")', |
| 2228 | \ 'call matchadd("Tbrown", "D")', |
| 2229 | \ 'call matchadd("Tdarkblue", "E")', |
| 2230 | \ 'call matchadd("Tdarkmagenta", "F")', |
| 2231 | \ 'call matchadd("Tdarkcyan", "G")', |
| 2232 | \ 'call matchadd("Tlightgrey", "H")', |
| 2233 | \ 'call matchadd("Tdarkgrey", "I")', |
| 2234 | \ 'call matchadd("Tred", "J")', |
| 2235 | \ 'call matchadd("Tgreen", "K")', |
| 2236 | \ 'call matchadd("Tyellow", "L")', |
| 2237 | \ 'call matchadd("Tblue", "M")', |
| 2238 | \ 'call matchadd("Tmagenta", "N")', |
| 2239 | \ 'call matchadd("Tcyan", "O")', |
| 2240 | \ 'call matchadd("Twhite", "P")', |
| 2241 | \ 'call matchadd("TdarkredBold", "X")', |
| 2242 | \ 'call matchadd("TgreenBold", "Y")', |
| 2243 | \ 'call matchadd("TmagentaBold", "Z")', |
| 2244 | \ 'redraw', |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 2245 | \ ], 'Xcolorscript', 'D') |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 2246 | let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10}) |
| 2247 | call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {}) |
| 2248 | |
| 2249 | call term_sendkeys(buf, ":q\<CR>") |
| 2250 | call StopVimInTerminal(buf) |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 2251 | endfunc |
| 2252 | |
Bram Moolenaar | 1e6bbfb | 2021-04-03 13:19:26 +0200 | [diff] [blame] | 2253 | function On_BufFilePost() |
| 2254 | doautocmd <nomodeline> User UserEvent |
| 2255 | endfunction |
| 2256 | |
| 2257 | func Test_terminal_nested_autocmd() |
| 2258 | new |
| 2259 | call setline(1, range(500)) |
| 2260 | $ |
| 2261 | let lastline = line('.') |
| 2262 | |
| 2263 | augroup TermTest |
| 2264 | autocmd BufFilePost * call On_BufFilePost() |
| 2265 | autocmd User UserEvent silent |
| 2266 | augroup END |
| 2267 | |
| 2268 | let cmd = Get_cat_123_cmd() |
| 2269 | let buf = term_start(cmd, #{term_finish: 'close', hidden: 1}) |
| 2270 | call assert_equal(lastline, line('.')) |
| 2271 | |
Bram Moolenaar | 6437475 | 2021-04-03 17:22:29 +0200 | [diff] [blame] | 2272 | let job = term_getjob(buf) |
| 2273 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 1e6bbfb | 2021-04-03 13:19:26 +0200 | [diff] [blame] | 2274 | call delete('Xtext') |
| 2275 | augroup TermTest |
| 2276 | au! |
| 2277 | augroup END |
| 2278 | endfunc |
| 2279 | |
Bram Moolenaar | aeed2a6 | 2021-04-29 20:18:45 +0200 | [diff] [blame] | 2280 | func Test_terminal_adds_jump() |
| 2281 | clearjumps |
| 2282 | call term_start("ls", #{curwin: 1}) |
| 2283 | call assert_equal(1, getjumplist()[0]->len()) |
| 2284 | bwipe! |
| 2285 | endfunc |
| 2286 | |
Bram Moolenaar | eea32af | 2021-11-21 14:51:13 +0000 | [diff] [blame] | 2287 | func Close_cb(ch, ctx) |
| 2288 | call term_wait(a:ctx.bufnr) |
| 2289 | let g:close_done = 'done' |
| 2290 | endfunc |
| 2291 | |
| 2292 | func Test_term_wait_in_close_cb() |
| 2293 | let g:close_done = '' |
| 2294 | let ctx = {} |
| 2295 | let ctx.bufnr = term_start('echo "HELLO WORLD"', |
| 2296 | \ {'close_cb': {ch -> Close_cb(ch, ctx)}}) |
| 2297 | |
| 2298 | call WaitForAssert({-> assert_equal("done", g:close_done)}) |
| 2299 | |
| 2300 | unlet g:close_done |
| 2301 | bwipe! |
| 2302 | endfunc |
| 2303 | |
Shougo Matsushita | 4ccaedf | 2022-10-15 11:48:00 +0100 | [diff] [blame] | 2304 | func Test_term_TextChangedT() |
| 2305 | augroup TermTest |
| 2306 | autocmd TextChangedT * ++once |
| 2307 | \ execute expand('<abuf>') . 'buffer' | |
| 2308 | \ let b:called = 1 | |
| 2309 | \ split | |
| 2310 | \ enew |
| 2311 | augroup END |
| 2312 | |
| 2313 | terminal |
| 2314 | |
| 2315 | let term_buf = bufnr() |
| 2316 | |
| 2317 | let b:called = 0 |
| 2318 | |
| 2319 | call term_sendkeys(term_buf, "aaabbc\r") |
| 2320 | call TermWait(term_buf) |
| 2321 | |
| 2322 | call assert_equal(1, getbufvar(term_buf, 'called')) |
| 2323 | |
| 2324 | " Current buffer will be restored |
| 2325 | call assert_equal(bufnr(), term_buf) |
| 2326 | |
| 2327 | bwipe! |
| 2328 | augroup TermTest |
| 2329 | au! |
| 2330 | augroup END |
| 2331 | endfunc |
| 2332 | |
| 2333 | func Test_term_TextChangedT_close() |
| 2334 | augroup TermTest |
| 2335 | autocmd TextChangedT * ++once split | enew | 1close! |
| 2336 | augroup END |
| 2337 | |
| 2338 | terminal |
| 2339 | |
| 2340 | let term_buf = bufnr() |
| 2341 | |
| 2342 | call term_sendkeys(term_buf, "aaabbc\r") |
| 2343 | call TermWait(term_buf) |
| 2344 | |
| 2345 | " Current buffer will be restored |
| 2346 | call assert_equal(bufnr(), term_buf) |
| 2347 | |
| 2348 | bwipe! |
| 2349 | augroup TermTest |
| 2350 | au! |
| 2351 | augroup END |
| 2352 | endfunc |
Bram Moolenaar | 91689ea | 2020-05-11 22:04:53 +0200 | [diff] [blame] | 2353 | |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 2354 | " vim: shiftwidth=2 sts=2 expandtab |