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