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