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