Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 1 | " Tests for the terminal window. |
| 2 | |
| 3 | if !exists('*term_start') |
| 4 | finish |
| 5 | endif |
| 6 | |
| 7 | source shared.vim |
| 8 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 9 | " Open a terminal with a shell, assign the job to g:job and return the buffer |
| 10 | " number. |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 11 | func Run_shell_in_terminal(options) |
| 12 | let buf = term_start(&shell, a:options) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 13 | |
| 14 | let termlist = term_list() |
| 15 | call assert_equal(1, len(termlist)) |
| 16 | call assert_equal(buf, termlist[0]) |
| 17 | |
| 18 | let g:job = term_getjob(buf) |
| 19 | call assert_equal(v:t_job, type(g:job)) |
| 20 | |
Bram Moolenaar | 35422f4 | 2017-08-05 16:33:56 +0200 | [diff] [blame] | 21 | let string = string({'job': term_getjob(buf)}) |
| 22 | call assert_match("{'job': 'process \\d\\+ run'}", string) |
| 23 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 24 | return buf |
| 25 | endfunc |
| 26 | |
| 27 | " Stops the shell started by Run_shell_in_terminal(). |
| 28 | func Stop_shell_in_terminal(buf) |
| 29 | call term_sendkeys(a:buf, "exit\r") |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 30 | call WaitFor('job_status(g:job) == "dead"') |
| 31 | call assert_equal('dead', job_status(g:job)) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 32 | endfunc |
| 33 | |
| 34 | func Test_terminal_basic() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 35 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 7c9aec4 | 2017-08-03 13:51:25 +0200 | [diff] [blame] | 36 | if has("unix") |
| 37 | call assert_match("^/dev/", job_info(g:job).tty) |
| 38 | call assert_match("^/dev/", term_gettty('')) |
| 39 | else |
Bram Moolenaar | 5be8dd0 | 2017-08-03 20:52:19 +0200 | [diff] [blame] | 40 | call assert_match("^winpty://", job_info(g:job).tty) |
| 41 | call assert_match("^winpty://", term_gettty('')) |
Bram Moolenaar | 7c9aec4 | 2017-08-03 13:51:25 +0200 | [diff] [blame] | 42 | endif |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 43 | call Stop_shell_in_terminal(buf) |
| 44 | call term_wait(buf) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 45 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 46 | " closing window wipes out the terminal buffer a with finished job |
| 47 | close |
| 48 | call assert_equal("", bufname(buf)) |
| 49 | |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 50 | unlet g:job |
| 51 | endfunc |
| 52 | |
| 53 | func Test_terminal_make_change() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 54 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 55 | call Stop_shell_in_terminal(buf) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 56 | call term_wait(buf) |
| 57 | |
| 58 | setlocal modifiable |
| 59 | exe "normal Axxx\<Esc>" |
| 60 | call assert_fails(buf . 'bwipe', 'E517') |
| 61 | undo |
| 62 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 63 | exe buf . 'bwipe' |
| 64 | unlet g:job |
| 65 | endfunc |
| 66 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 67 | func Test_terminal_wipe_buffer() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 68 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 69 | call assert_fails(buf . 'bwipe', 'E517') |
| 70 | exe buf . 'bwipe!' |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 71 | call WaitFor('job_status(g:job) == "dead"') |
| 72 | call assert_equal('dead', job_status(g:job)) |
| 73 | call assert_equal("", bufname(buf)) |
| 74 | |
| 75 | unlet g:job |
| 76 | endfunc |
| 77 | |
| 78 | func Test_terminal_hide_buffer() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 79 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 80 | quit |
| 81 | for nr in range(1, winnr('$')) |
| 82 | call assert_notequal(winbufnr(nr), buf) |
| 83 | endfor |
| 84 | call assert_true(bufloaded(buf)) |
| 85 | call assert_true(buflisted(buf)) |
| 86 | |
| 87 | exe 'split ' . buf . 'buf' |
| 88 | call Stop_shell_in_terminal(buf) |
| 89 | exe buf . 'bwipe' |
| 90 | |
| 91 | unlet g:job |
| 92 | endfunc |
| 93 | |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 94 | func! s:Nasty_exit_cb(job, st) |
| 95 | exe g:buf . 'bwipe!' |
| 96 | let g:buf = 0 |
| 97 | endfunc |
| 98 | |
| 99 | func Test_terminal_nasty_cb() |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 100 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 101 | let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')}) |
| 102 | let g:job = term_getjob(g:buf) |
| 103 | |
| 104 | call WaitFor('job_status(g:job) == "dead"') |
| 105 | call WaitFor('g:buf == 0') |
| 106 | unlet g:buf |
| 107 | unlet g:job |
| 108 | call delete('Xtext') |
| 109 | endfunc |
| 110 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 111 | func Check_123(buf) |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 112 | let l = term_scrape(a:buf, 0) |
| 113 | call assert_true(len(l) == 0) |
| 114 | let l = term_scrape(a:buf, 999) |
| 115 | call assert_true(len(l) == 0) |
Bram Moolenaar | 9c84484 | 2017-08-01 18:41:21 +0200 | [diff] [blame] | 116 | let l = term_scrape(a:buf, 1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 117 | call assert_true(len(l) > 0) |
| 118 | call assert_equal('1', l[0].chars) |
| 119 | call assert_equal('2', l[1].chars) |
| 120 | call assert_equal('3', l[2].chars) |
| 121 | call assert_equal('#00e000', l[0].fg) |
| 122 | if &background == 'light' |
| 123 | call assert_equal('#ffffff', l[0].bg) |
| 124 | else |
| 125 | call assert_equal('#000000', l[0].bg) |
| 126 | endif |
| 127 | |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 128 | let l = term_getline(a:buf, -1) |
| 129 | call assert_equal('', l) |
| 130 | let l = term_getline(a:buf, 0) |
| 131 | call assert_equal('', l) |
| 132 | let l = term_getline(a:buf, 999) |
| 133 | call assert_equal('', l) |
Bram Moolenaar | 9c84484 | 2017-08-01 18:41:21 +0200 | [diff] [blame] | 134 | let l = term_getline(a:buf, 1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 135 | call assert_equal('123', l) |
| 136 | endfunc |
| 137 | |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 138 | func Get_cat_123_cmd() |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 139 | if has('win32') |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 140 | return 'cmd /c "cls && color 2 && echo 123"' |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 141 | else |
| 142 | call writefile(["\<Esc>[32m123"], 'Xtext') |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 143 | return "cat Xtext" |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 144 | endif |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 145 | endfunc |
| 146 | |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 147 | func Test_terminal_scrape_123() |
| 148 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 149 | let buf = term_start(cmd) |
| 150 | |
| 151 | let termlist = term_list() |
| 152 | call assert_equal(1, len(termlist)) |
| 153 | call assert_equal(buf, termlist[0]) |
| 154 | |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 155 | " Nothing happens with invalid buffer number |
| 156 | call term_wait(1234) |
| 157 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 158 | call term_wait(buf) |
Bram Moolenaar | 620d064 | 2017-08-03 21:08:05 +0200 | [diff] [blame] | 159 | if has('win32') |
| 160 | " TODO: this should not be needed |
| 161 | sleep 100m |
| 162 | endif |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 163 | call Check_123(buf) |
| 164 | |
| 165 | " Must still work after the job ended. |
| 166 | let g:job = term_getjob(buf) |
| 167 | call WaitFor('job_status(g:job) == "dead"') |
| 168 | call term_wait(buf) |
| 169 | call Check_123(buf) |
| 170 | |
| 171 | exe buf . 'bwipe' |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 172 | call delete('Xtext') |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 173 | endfunc |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 174 | |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 175 | func Test_terminal_scrape_multibyte() |
| 176 | if !has('multi_byte') |
| 177 | return |
| 178 | endif |
| 179 | call writefile(["léttまrs"], 'Xtext') |
| 180 | if has('win32') |
| 181 | let cmd = 'cmd /c "type Xtext"' |
| 182 | else |
| 183 | let cmd = "cat Xtext" |
| 184 | endif |
| 185 | let buf = term_start(cmd) |
| 186 | |
| 187 | call term_wait(buf) |
| 188 | if has('win32') |
| 189 | " TODO: this should not be needed |
| 190 | sleep 100m |
| 191 | endif |
| 192 | |
| 193 | let l = term_scrape(buf, 1) |
| 194 | call assert_true(len(l) >= 7) |
| 195 | call assert_equal('l', l[0].chars) |
| 196 | call assert_equal('é', l[1].chars) |
| 197 | call assert_equal(1, l[1].width) |
| 198 | call assert_equal('t', l[2].chars) |
| 199 | call assert_equal('t', l[3].chars) |
| 200 | call assert_equal('ま', l[4].chars) |
| 201 | call assert_equal(2, l[4].width) |
| 202 | call assert_equal('r', l[5].chars) |
| 203 | call assert_equal('s', l[6].chars) |
| 204 | |
| 205 | let g:job = term_getjob(buf) |
| 206 | call WaitFor('job_status(g:job) == "dead"') |
| 207 | call term_wait(buf) |
| 208 | |
| 209 | exe buf . 'bwipe' |
| 210 | call delete('Xtext') |
| 211 | endfunc |
| 212 | |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 213 | func Test_terminal_scroll() |
| 214 | call writefile(range(1, 200), 'Xtext') |
| 215 | if has('win32') |
| 216 | let cmd = 'cmd /c "type Xtext"' |
| 217 | else |
| 218 | let cmd = "cat Xtext" |
| 219 | endif |
| 220 | let buf = term_start(cmd) |
| 221 | |
| 222 | let g:job = term_getjob(buf) |
| 223 | call WaitFor('job_status(g:job) == "dead"') |
| 224 | call term_wait(buf) |
| 225 | if has('win32') |
| 226 | " TODO: this should not be needed |
| 227 | sleep 100m |
| 228 | endif |
| 229 | |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 230 | let scrolled = term_getscrolled(buf) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 231 | call assert_equal('1', getline(1)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 232 | call assert_equal('1', term_getline(buf, 1 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 233 | call assert_equal('49', getline(49)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 234 | call assert_equal('49', term_getline(buf, 49 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 235 | call assert_equal('200', getline(200)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 236 | call assert_equal('200', term_getline(buf, 200 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 237 | |
| 238 | exe buf . 'bwipe' |
| 239 | call delete('Xtext') |
| 240 | endfunc |
| 241 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 242 | func Test_terminal_size() |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 243 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 244 | |
| 245 | exe '5terminal ' . cmd |
| 246 | let size = term_getsize('') |
| 247 | bwipe! |
| 248 | call assert_equal(5, size[0]) |
| 249 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 250 | call term_start(cmd, {'term_rows': 6}) |
| 251 | let size = term_getsize('') |
| 252 | bwipe! |
| 253 | call assert_equal(6, size[0]) |
| 254 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 255 | vsplit |
| 256 | exe '5,33terminal ' . cmd |
| 257 | let size = term_getsize('') |
| 258 | bwipe! |
| 259 | call assert_equal([5, 33], size) |
| 260 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 261 | call term_start(cmd, {'term_rows': 6, 'term_cols': 36}) |
| 262 | let size = term_getsize('') |
| 263 | bwipe! |
| 264 | call assert_equal([6, 36], size) |
| 265 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 266 | exe 'vertical 20terminal ' . cmd |
| 267 | let size = term_getsize('') |
| 268 | bwipe! |
| 269 | call assert_equal(20, size[1]) |
| 270 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 271 | call term_start(cmd, {'vertical': 1, 'term_cols': 26}) |
| 272 | let size = term_getsize('') |
| 273 | bwipe! |
| 274 | call assert_equal(26, size[1]) |
| 275 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 276 | split |
| 277 | exe 'vertical 6,20terminal ' . cmd |
| 278 | let size = term_getsize('') |
| 279 | bwipe! |
| 280 | call assert_equal([6, 20], size) |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 281 | |
| 282 | call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27}) |
| 283 | let size = term_getsize('') |
| 284 | bwipe! |
| 285 | call assert_equal([7, 27], size) |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame^] | 286 | endfunc |
| 287 | |
| 288 | func Test_terminal_curwin() |
| 289 | let cmd = Get_cat_123_cmd() |
| 290 | call assert_equal(1, winnr('$')) |
| 291 | |
| 292 | split dummy |
| 293 | exe 'terminal ++curwin ' . cmd |
| 294 | call assert_equal(2, winnr('$')) |
| 295 | bwipe! |
| 296 | |
| 297 | split dummy |
| 298 | call term_start(cmd, {'curwin': 1}) |
| 299 | call assert_equal(2, winnr('$')) |
| 300 | bwipe! |
| 301 | |
| 302 | split dummy |
| 303 | call setline(1, 'change') |
| 304 | call assert_fails('terminal ++curwin ' . cmd, 'E37:') |
| 305 | call assert_equal(2, winnr('$')) |
| 306 | exe 'terminal! ++curwin ' . cmd |
| 307 | call assert_equal(2, winnr('$')) |
| 308 | bwipe! |
| 309 | |
| 310 | split dummy |
| 311 | call setline(1, 'change') |
| 312 | call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:') |
| 313 | call assert_equal(2, winnr('$')) |
| 314 | bwipe! |
| 315 | |
| 316 | split dummy |
| 317 | bwipe! |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 318 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 319 | endfunc |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 320 | |
| 321 | func Test_finish_close() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 322 | " TODO: use something that takes much less than a whole second |
| 323 | echo 'This will take five seconds...' |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 324 | call assert_equal(1, winnr('$')) |
| 325 | |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 326 | if has('win32') |
| 327 | let cmd = $windir . '\system32\timeout.exe 1' |
| 328 | else |
| 329 | let cmd = 'sleep 1' |
| 330 | endif |
| 331 | exe 'terminal ++close ' . cmd |
| 332 | let buf = bufnr('') |
| 333 | call assert_equal(2, winnr('$')) |
| 334 | |
| 335 | wincmd p |
| 336 | sleep 1200 msec |
| 337 | call assert_equal(1, winnr('$')) |
| 338 | |
| 339 | call term_start(cmd, {'term_finish': 'close'}) |
| 340 | call assert_equal(2, winnr('$')) |
| 341 | let buf = bufnr('') |
| 342 | wincmd p |
| 343 | sleep 1200 msec |
| 344 | call assert_equal(1, winnr('$')) |
| 345 | |
| 346 | exe 'terminal ++open ' . cmd |
| 347 | let buf = bufnr('') |
| 348 | close |
| 349 | sleep 1200 msec |
| 350 | call assert_equal(2, winnr('$')) |
| 351 | bwipe |
| 352 | |
| 353 | call term_start(cmd, {'term_finish': 'open'}) |
| 354 | let buf = bufnr('') |
| 355 | close |
| 356 | sleep 1200 msec |
| 357 | call assert_equal(2, winnr('$')) |
| 358 | |
| 359 | bwipe |
| 360 | endfunc |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 361 | |
| 362 | func Test_terminal_cwd() |
| 363 | if !has('unix') |
| 364 | return |
| 365 | endif |
| 366 | call mkdir('Xdir') |
| 367 | let buf = term_start('pwd', {'cwd': 'Xdir'}) |
| 368 | sleep 100m |
| 369 | call term_wait(buf) |
| 370 | call assert_equal(getcwd() . '/Xdir', getline(1)) |
| 371 | |
| 372 | exe buf . 'bwipe' |
| 373 | call delete('Xdir', 'rf') |
| 374 | endfunc |
| 375 | |
| 376 | func Test_terminal_env() |
| 377 | if !has('unix') |
| 378 | return |
| 379 | endif |
| 380 | let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}}) |
| 381 | call term_wait(buf) |
| 382 | call term_sendkeys(buf, "echo $TESTENV\r") |
| 383 | call term_wait(buf) |
| 384 | call Stop_shell_in_terminal(buf) |
| 385 | call term_wait(buf) |
| 386 | call assert_equal('correct', getline(2)) |
| 387 | |
| 388 | exe buf . 'bwipe' |
| 389 | endfunc |