blob: 821154a9f87e84b590271c8149429454adb5a5ad [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarea5d6fa2017-08-18 21:07:11 +02003if !has('terminal')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004 finish
5endif
6
7source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01008source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
11
Bram Moolenaar94053a52017-08-01 21:44:33 +020012" Open a terminal with a shell, assign the job to g:job and return the buffer
13" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020014func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010015 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 Moolenaarc6df10e2017-07-29 20:15:08 +020020
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 Moolenaar35422f42017-08-05 16:33:56 +020028 let string = string({'job': term_getjob(buf)})
29 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020035 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020042 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
43 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020045 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020046 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020047 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020048 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 Moolenaar2bb7b6b2017-08-13 20:58:33 +020051
Bram Moolenaar94053a52017-08-01 21:44:33 +020052 call Stop_shell_in_terminal(buf)
53 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054 call assert_equal('n', mode())
55 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020056 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 Moolenaar20e6cd02017-08-01 20:25:22 +020059
Bram Moolenaar94053a52017-08-01 21:44:33 +020060 " closing window wipes out the terminal buffer a with finished job
61 close
62 call assert_equal("", bufname(buf))
63
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020064 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020065 unlet g:job
66endfunc
67
68func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020069 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020070 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071 call term_wait(buf)
72
73 setlocal modifiable
74 exe "normal Axxx\<Esc>"
75 call assert_fails(buf . 'bwipe', 'E517')
76 undo
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078 exe buf . 'bwipe'
79 unlet g:job
80endfunc
81
Bram Moolenaar94053a52017-08-01 21:44:33 +020082func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020083 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020084 call assert_fails(buf . 'bwipe', 'E517')
85 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +020086 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +020087 call assert_equal("", bufname(buf))
88
89 unlet g:job
90endfunc
91
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020092func 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 Moolenaar50182fa2018-04-28 21:34:40 +0200102 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200103
104 exe buf . 'bwipe'
105 unlet g:job
106endfunc
107
Bram Moolenaar94053a52017-08-01 21:44:33 +0200108func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200109 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200110 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200111 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
123endfunc
124
Bram Moolenaar1e115362019-01-09 23:01:02 +0100125func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200126 exe g:buf . 'bwipe!'
127 let g:buf = 0
128endfunc
129
Bram Moolenaar9d189612017-09-09 18:11:00 +0200130func 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
137endfunc
138
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200139func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200140 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200141 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
142 let g:job = term_getjob(g:buf)
143
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200144 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
145 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200146 unlet g:buf
147 unlet g:job
148 call delete('Xtext')
149endfunc
150
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200151func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200152 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 Moolenaar9c844842017-08-01 18:41:21 +0200156 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200157 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)
Bram Moolenaar81df6352018-12-22 18:25:30 +0100162 if has('win32')
163 " On Windows 'background' always defaults to dark, even though the terminal
164 " may use a light background. Therefore accept both white and black.
165 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200166 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100167 if &background == 'light'
168 call assert_equal('#ffffff', l[0].bg)
169 else
170 call assert_equal('#000000', l[0].bg)
171 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200172 endif
173
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200174 let l = term_getline(a:buf, -1)
175 call assert_equal('', l)
176 let l = term_getline(a:buf, 0)
177 call assert_equal('', l)
178 let l = term_getline(a:buf, 999)
179 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200180 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200181 call assert_equal('123', l)
182endfunc
183
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200184func Test_terminal_scrape_123()
185 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200186 let buf = term_start(cmd)
187
188 let termlist = term_list()
189 call assert_equal(1, len(termlist))
190 call assert_equal(buf, termlist[0])
191
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200192 " Nothing happens with invalid buffer number
193 call term_wait(1234)
194
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200195 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200196 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200197 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200198 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200199 call Check_123(buf)
200
201 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100202 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200203 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200204 call term_wait(buf)
205 call Check_123(buf)
206
207 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200208 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200209endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200210
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200211func Test_terminal_scrape_multibyte()
212 if !has('multi_byte')
213 return
214 endif
215 call writefile(["léttまrs"], 'Xtext')
216 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200217 " Run cmd with UTF-8 codepage to make the type command print the expected
218 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100219 let buf = term_start("cmd /K chcp 65001")
220 call term_sendkeys(buf, "type Xtext\<CR>")
221 call term_sendkeys(buf, "exit\<CR>")
222 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200223 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100224 let buf = term_start("cat Xtext")
225 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200226 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200227
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100228 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
229 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200230 call assert_true(len(l) >= 7)
231 call assert_equal('l', l[0].chars)
232 call assert_equal('é', l[1].chars)
233 call assert_equal(1, l[1].width)
234 call assert_equal('t', l[2].chars)
235 call assert_equal('t', l[3].chars)
236 call assert_equal('ま', l[4].chars)
237 call assert_equal(2, l[4].width)
238 call assert_equal('r', l[5].chars)
239 call assert_equal('s', l[6].chars)
240
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100241 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200242 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100243 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200244
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100245 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200246 call delete('Xtext')
247endfunc
248
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200249func Test_terminal_scroll()
250 call writefile(range(1, 200), 'Xtext')
251 if has('win32')
252 let cmd = 'cmd /c "type Xtext"'
253 else
254 let cmd = "cat Xtext"
255 endif
256 let buf = term_start(cmd)
257
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100258 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200259 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200260 call term_wait(buf)
261 if has('win32')
262 " TODO: this should not be needed
263 sleep 100m
264 endif
265
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200266 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200267 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200268 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200269 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200270 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200271 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200272 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200273
274 exe buf . 'bwipe'
275 call delete('Xtext')
276endfunc
277
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200278func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200279 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200280 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200281 call writefile(range(150), 'Xtext')
282 if has('win32')
283 call term_sendkeys(buf, "type Xtext\<CR>")
284 else
285 call term_sendkeys(buf, "cat Xtext\<CR>")
286 endif
287 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200288 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200289 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200290 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200291 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200292
293 call Stop_shell_in_terminal(buf)
294 call term_wait(buf)
295 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200296 set termwinscroll&
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200297endfunc
298
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200299func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200300 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301
Bram Moolenaarb2412082017-08-20 18:09:14 +0200302 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200303 let size = term_getsize('')
304 bwipe!
305 call assert_equal(5, size[0])
306
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200307 call term_start(cmd, {'term_rows': 6})
308 let size = term_getsize('')
309 bwipe!
310 call assert_equal(6, size[0])
311
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200312 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200313 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200314 call assert_equal([5, 33], term_getsize(''))
315
316 call term_setsize('', 6, 0)
317 call assert_equal([6, 33], term_getsize(''))
318
319 call term_setsize('', 0, 35)
320 call assert_equal([6, 35], term_getsize(''))
321
322 call term_setsize('', 7, 30)
323 call assert_equal([7, 30], term_getsize(''))
324
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200325 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200326 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200327
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200328 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
329 let size = term_getsize('')
330 bwipe!
331 call assert_equal([6, 36], size)
332
Bram Moolenaarb2412082017-08-20 18:09:14 +0200333 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200334 let size = term_getsize('')
335 bwipe!
336 call assert_equal(20, size[1])
337
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200338 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
339 let size = term_getsize('')
340 bwipe!
341 call assert_equal(26, size[1])
342
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200343 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200344 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200345 let size = term_getsize('')
346 bwipe!
347 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200348
349 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
350 let size = term_getsize('')
351 bwipe!
352 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200353
354 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200355endfunc
356
357func Test_terminal_curwin()
358 let cmd = Get_cat_123_cmd()
359 call assert_equal(1, winnr('$'))
360
361 split dummy
362 exe 'terminal ++curwin ' . cmd
363 call assert_equal(2, winnr('$'))
364 bwipe!
365
366 split dummy
367 call term_start(cmd, {'curwin': 1})
368 call assert_equal(2, winnr('$'))
369 bwipe!
370
371 split dummy
372 call setline(1, 'change')
373 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
374 call assert_equal(2, winnr('$'))
375 exe 'terminal! ++curwin ' . cmd
376 call assert_equal(2, winnr('$'))
377 bwipe!
378
379 split dummy
380 call setline(1, 'change')
381 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
382 call assert_equal(2, winnr('$'))
383 bwipe!
384
385 split dummy
386 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200387 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200388endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200389
Bram Moolenaarff546792017-11-21 14:47:57 +0100390func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200391 if s:python != ''
392 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200393 " 500 was not enough for Travis
394 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200395 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200396 echo 'This will take five seconds...'
397 let waittime = 2000
398 if has('win32')
399 let cmd = $windir . '\system32\timeout.exe 1'
400 else
401 let cmd = 'sleep 1'
402 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200403 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100404 return [cmd, waittime]
405endfunc
406
407func Test_terminal_finish_open_close()
408 call assert_equal(1, winnr('$'))
409
410 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200411
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100412 " shell terminal closes automatically
413 terminal
414 let buf = bufnr('%')
415 call assert_equal(2, winnr('$'))
416 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200417 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100418 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200419 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100420
421 " shell terminal that does not close automatically
422 terminal ++noclose
423 let buf = bufnr('%')
424 call assert_equal(2, winnr('$'))
425 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200426 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100427 call Stop_shell_in_terminal(buf)
428 call assert_equal(2, winnr('$'))
429 quit
430 call assert_equal(1, winnr('$'))
431
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200432 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200433 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200434 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200435 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200436
437 call term_start(cmd, {'term_finish': 'close'})
438 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200439 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200440 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200441 call assert_equal(1, winnr('$'))
442
443 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200444 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200445 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200446 bwipe
447
448 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200449 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200450 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200451 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200452
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200453 exe 'terminal ++hidden ++open ' . cmd
454 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200455 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200456 bwipe
457
458 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
459 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200460 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200461 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200462
463 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
464 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
465 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
466 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
467
468 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200469 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200470 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200471 call assert_equal(4, winheight(0))
472 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200473endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200474
475func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200476 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200477 return
478 endif
479 call mkdir('Xdir')
480 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200481 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200482
483 exe buf . 'bwipe'
484 call delete('Xdir', 'rf')
485endfunc
486
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200487func Test_terminal_cwd_failure()
488 " Case 1: Provided directory is not actually a directory. Attempt to make
489 " the file executable as well.
490 call writefile([], 'Xfile')
491 call setfperm('Xfile', 'rwx------')
492 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
493 call delete('Xfile')
494
495 " Case 2: Directory does not exist.
496 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
497
498 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100499 " Skip this for root, it will be accessible anyway.
500 if $USER != 'root'
501 call mkdir('XdirNoAccess', '', '0600')
502 " return early if the directory permissions could not be set properly
503 if getfperm('XdirNoAccess')[2] == 'x'
504 call delete('XdirNoAccess', 'rf')
505 return
506 endif
507 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
508 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200509 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200510endfunc
511
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100512func Test_terminal_servername()
513 if !has('clientserver')
514 return
515 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200516 call s:test_environment("VIM_SERVERNAME", v:servername)
517endfunc
518
519func Test_terminal_version()
520 call s:test_environment("VIM_TERMINAL", string(v:version))
521endfunc
522
523func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100524 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100525 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200526 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100527 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200528 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100529 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200530 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100531 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100532 call term_wait(buf)
533 call Stop_shell_in_terminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200534 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100535
Bram Moolenaar012eb662018-03-13 17:55:27 +0100536 exe buf . 'bwipe'
537 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100538endfunc
539
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200540func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100541 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200542 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200543 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100544 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100545 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100546 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100547 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100548 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100549 call term_wait(buf)
550 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200551 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200552
Bram Moolenaar012eb662018-03-13 17:55:27 +0100553 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200554endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200555
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200556func Test_terminal_list_args()
557 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
558 call assert_fails(buf . 'bwipe', 'E517')
559 exe buf . 'bwipe!'
560 call assert_equal("", bufname(buf))
561endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200562
563func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100564 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200565 if has('mac')
566 " The shell or something else has a problem dealing with more than 1000
567 " characters at the same time.
568 let len = 1000
569 else
570 let len = 5000
571 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200572
573 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100574 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200575 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100576 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200577
578 " On MS-Windows there is an extra empty line below "done". Find "done" in
579 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100580 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100581 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100582 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200583 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100584 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200585 endif
586 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200587
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100588 let g:job = term_getjob(buf)
589 call Stop_shell_in_terminal(buf)
590 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200591 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200592 bwipe
593endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200594
595func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200596 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200597 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200598 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200599 if has('win32')
600 " TODO: enable once writing to stdin works on MS-Windows
601 return
602 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200603 new
604 call setline(1, ['one', 'two', 'three'])
605 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200606 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200607 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200608 call assert_equal(['3', '3', '14'], nrs)
609 bwipe
610
Bram Moolenaardada6d22017-09-02 17:18:35 +0200611 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200612 call setline(1, ['one', 'two', 'three', 'four'])
613 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200614 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200615 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200616 call assert_equal(['2', '2', '10'], nrs)
617 bwipe
618
Bram Moolenaardada6d22017-09-02 17:18:35 +0200619 if executable('python')
620 new
621 call setline(1, ['print("hello")'])
622 1term ++eof=exit() python
623 " MS-Windows echoes the input, Unix doesn't.
624 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
625 if getline(1) =~ 'hello'
626 call assert_equal('hello', getline(1))
627 else
628 call assert_equal('hello', getline(line('$') - 1))
629 endif
630 bwipe
631
632 if has('win32')
633 new
634 call setline(1, ['print("hello")'])
635 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200636 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200637 call assert_equal('hello', getline(line('$') - 1))
638 bwipe
639 endif
640 endif
641
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200642 bwipe!
643endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200644
645func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200646 let buf = term_start('NONE', {})
647 call assert_notequal(0, buf)
648
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200649 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200650 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100651 if has('gui_running') && !has('win32')
652 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200653 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100654 else
655 " Otherwise using a job works on all systems.
656 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200657 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200658 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200659
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200660 bwipe!
661endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200662
663func Test_terminal_special_chars()
664 " this file name only works on Unix
665 if !has('unix')
666 return
667 endif
668 call mkdir('Xdir with spaces')
669 call writefile(['x'], 'Xdir with spaces/quoted"file')
670 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200671 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200672 call term_wait('')
673
674 call delete('Xdir with spaces', 'rf')
675 bwipe
676endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200677
678func Test_terminal_wrong_options()
679 call assert_fails('call term_start(&shell, {
680 \ "in_io": "file",
681 \ "in_name": "xxx",
682 \ "out_io": "file",
683 \ "out_name": "xxx",
684 \ "err_io": "file",
685 \ "err_name": "xxx"
686 \ })', 'E474:')
687 call assert_fails('call term_start(&shell, {
688 \ "out_buf": bufnr("%")
689 \ })', 'E474:')
690 call assert_fails('call term_start(&shell, {
691 \ "err_buf": bufnr("%")
692 \ })', 'E474:')
693endfunc
694
695func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200696 let cmd = Get_cat_123_cmd()
697 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
698 call term_wait(buf)
699 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
700 call assert_match('123', readfile('Xfile')[0])
701 let g:job = term_getjob(buf)
702 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
703 call delete('Xfile')
704 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200705
706 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200707 call writefile(['one line'], 'Xfile')
708 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
709 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200710 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200711 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200712 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200713 bwipe
714 call delete('Xfile')
715 endif
716endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200717
718func TerminalTmap(remap)
719 let buf = Run_shell_in_terminal({})
720 call assert_equal('t', mode())
721
722 if a:remap
723 tmap 123 456
724 else
725 tnoremap 123 456
726 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100727 " don't use abcde, it's an existing command
728 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200729 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100730 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200731 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200732 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200733 let lnum = term_getcursor(buf)[0]
734 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100735 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200736 else
737 call assert_match('456', term_getline(buf, lnum))
738 endif
739
740 call term_sendkeys(buf, "\r")
741 call Stop_shell_in_terminal(buf)
742 call term_wait(buf)
743
744 tunmap 123
745 tunmap 456
746 call assert_equal('', maparg('123', 't'))
747 close
748 unlet g:job
749endfunc
750
751func Test_terminal_tmap()
752 call TerminalTmap(1)
753 call TerminalTmap(0)
754endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200755
756func Test_terminal_wall()
757 let buf = Run_shell_in_terminal({})
758 wall
759 call Stop_shell_in_terminal(buf)
760 call term_wait(buf)
761 exe buf . 'bwipe'
762 unlet g:job
763endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200764
Bram Moolenaar7a760922018-02-19 23:10:02 +0100765func Test_terminal_wqall()
766 let buf = Run_shell_in_terminal({})
767 call assert_fails('wqall', 'E948')
768 call Stop_shell_in_terminal(buf)
769 call term_wait(buf)
770 exe buf . 'bwipe'
771 unlet g:job
772endfunc
773
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200774func Test_terminal_composing_unicode()
775 let save_enc = &encoding
776 set encoding=utf-8
777
778 if has('win32')
779 let cmd = "cmd /K chcp 65001"
780 let lnum = [3, 6, 9]
781 else
782 let cmd = &shell
783 let lnum = [1, 3, 5]
784 endif
785
786 enew
787 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100788 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200789 call term_wait(buf, 50)
790
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200791 if has('win32')
792 call assert_equal('cmd', job_info(g:job).cmd[0])
793 else
794 call assert_equal(&shell, job_info(g:job).cmd[0])
795 endif
796
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200797 " ascii + composing
798 let txt = "a\u0308bc"
799 call term_sendkeys(buf, "echo " . txt . "\r")
800 call term_wait(buf, 50)
801 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
802 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
803 let l = term_scrape(buf, lnum[0] + 1)
804 call assert_equal("a\u0308", l[0].chars)
805 call assert_equal("b", l[1].chars)
806 call assert_equal("c", l[2].chars)
807
808 " multibyte + composing
809 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
810 call term_sendkeys(buf, "echo " . txt . "\r")
811 call term_wait(buf, 50)
812 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
813 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
814 let l = term_scrape(buf, lnum[1] + 1)
815 call assert_equal("\u304b\u3099", l[0].chars)
816 call assert_equal("\u304e", l[1].chars)
817 call assert_equal("\u304f\u3099", l[2].chars)
818 call assert_equal("\u3052", l[3].chars)
819 call assert_equal("\u3053\u3099", l[4].chars)
820
821 " \u00a0 + composing
822 let txt = "abc\u00a0\u0308"
823 call term_sendkeys(buf, "echo " . txt . "\r")
824 call term_wait(buf, 50)
825 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
826 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
827 let l = term_scrape(buf, lnum[2] + 1)
828 call assert_equal("\u00a0\u0308", l[3].chars)
829
830 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200831 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200832 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100833 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200834 let &encoding = save_enc
835endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100836
837func Test_terminal_aucmd_on_close()
838 fun Nop()
839 let s:called = 1
840 endfun
841
842 aug repro
843 au!
844 au BufWinLeave * call Nop()
845 aug END
846
847 let [cmd, waittime] = s:get_sleep_cmd()
848
849 call assert_equal(1, winnr('$'))
850 new
851 call setline(1, ['one', 'two'])
852 exe 'term ++close ' . cmd
853 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200854 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100855 call assert_equal(1, s:called)
856 bwipe!
857
858 unlet s:called
859 au! repro
860 delfunc Nop
861endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100862
863func Test_terminal_term_start_empty_command()
864 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
865 call assert_fails(cmd, 'E474')
866 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
867 call assert_fails(cmd, 'E474')
868 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
869 call assert_fails(cmd, 'E474')
870 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
871 call assert_fails(cmd, 'E474')
872endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100873
874func Test_terminal_response_to_control_sequence()
875 if !has('unix')
876 return
877 endif
878
879 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200880 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100881
Bram Moolenaar086eb872018-03-25 21:24:12 +0200882 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200883 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100884
Bram Moolenaar086eb872018-03-25 21:24:12 +0200885 " Request the cursor position.
886 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100887
888 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200889 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100890
Bram Moolenaar086eb872018-03-25 21:24:12 +0200891 " End "cat" gently.
892 call term_sendkeys(buf, "\<CR>\<C-D>")
893
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100894 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100895 exe buf . 'bwipe'
896 unlet g:job
897endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100898
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100899" Run Vim, start a terminal in that Vim with the kill argument,
900" :qall works.
901func Run_terminal_qall_kill(line1, line2)
902 " 1. Open a terminal window and wait for the prompt to appear
903 " 2. set kill using term_setkill()
904 " 3. make Vim exit, it will kill the shell
905 let after = [
906 \ a:line1,
907 \ 'let buf = bufnr("%")',
908 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
909 \ ' sleep 10m',
910 \ 'endwhile',
911 \ a:line2,
912 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
913 \ 'qall',
914 \ ]
915 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100916 return
917 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100918 call assert_equal("done", readfile("Xdone")[0])
919 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100920endfunc
921
922" Run Vim in a terminal, then start a terminal in that Vim with a kill
923" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100924func Test_terminal_qall_kill_arg()
925 call Run_terminal_qall_kill('term ++kill=kill', '')
926endfunc
927
928" Run Vim, start a terminal in that Vim, set the kill argument with
929" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100930func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100931 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
932endfunc
933
934" Run Vim, start a terminal in that Vim without the kill argument,
935" check that :qall does not exit, :qall! does.
936func Test_terminal_qall_exit()
937 let after = [
938 \ 'term',
939 \ 'let buf = bufnr("%")',
940 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
941 \ ' sleep 10m',
942 \ 'endwhile',
943 \ 'set nomore',
944 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
945 \ 'qall',
946 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
947 \ 'cquit',
948 \ ]
949 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100950 return
951 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100952 call assert_equal("done", readfile("Xdone")[0])
953 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100954endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100955
956" Run Vim in a terminal, then start a terminal in that Vim without a kill
957" argument, check that :confirm qall works.
958func Test_terminal_qall_prompt()
959 if !CanRunVimInTerminal()
960 return
961 endif
962 let buf = RunVimInTerminal('', {})
963
964 " Open a terminal window and wait for the prompt to appear
965 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200966 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
967 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100968
969 " make Vim exit, it will prompt to kill the shell
970 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200971 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100972 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200973 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100974
975 " close the terminal window where Vim was running
976 quit
977endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100978
Bram Moolenaar012eb662018-03-13 17:55:27 +0100979func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100980 augroup repro
981 au!
982 au TerminalOpen * let s:called += 1
983 augroup END
984
985 let s:called = 0
986
987 " Open a terminal window with :terminal
988 terminal
989 call assert_equal(1, s:called)
990 bwipe!
991
992 " Open a terminal window with term_start()
993 call term_start(&shell)
994 call assert_equal(2, s:called)
995 bwipe!
996
997 " Open a hidden terminal buffer with :terminal
998 terminal ++hidden
999 call assert_equal(3, s:called)
1000 for buf in term_list()
1001 exe buf . "bwipe!"
1002 endfor
1003
1004 " Open a hidden terminal buffer with term_start()
1005 let buf = term_start(&shell, {'hidden': 1})
1006 call assert_equal(4, s:called)
1007 exe buf . "bwipe!"
1008
1009 unlet s:called
1010 au! repro
1011endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001012
1013func Check_dump01(off)
1014 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1015 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001016 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001017endfunc
1018
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001019func Test_terminal_dumpwrite_composing()
1020 if !CanRunVimInTerminal()
1021 return
1022 endif
1023 let save_enc = &encoding
1024 set encoding=utf-8
1025 call assert_equal(1, winnr('$'))
1026
1027 let text = " a\u0300 e\u0302 o\u0308"
1028 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001029 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001030 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001031 call term_dumpwrite(buf, 'Xdump')
1032 let dumpline = readfile('Xdump')[0]
1033 call assert_match('|à| |ê| |ö', dumpline)
1034
1035 call StopVimInTerminal(buf)
1036 call delete('Xcomposing')
1037 call delete('Xdump')
1038 let &encoding = save_enc
1039endfunc
1040
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001041" just testing basic functionality.
1042func Test_terminal_dumpload()
1043 call assert_equal(1, winnr('$'))
1044 call term_dumpload('dumps/Test_popup_command_01.dump')
1045 call assert_equal(2, winnr('$'))
1046 call assert_equal(20, line('$'))
1047 call Check_dump01(0)
1048 quit
1049endfunc
1050
1051func Test_terminal_dumpdiff()
1052 call assert_equal(1, winnr('$'))
1053 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1054 call assert_equal(2, winnr('$'))
1055 call assert_equal(62, line('$'))
1056 call Check_dump01(0)
1057 call Check_dump01(42)
1058 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1059 quit
1060endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001061
1062func Test_terminal_dumpdiff_options()
1063 set laststatus=0
1064 call assert_equal(1, winnr('$'))
1065 let height = winheight(0)
1066 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1067 call assert_equal(2, winnr('$'))
1068 call assert_equal(height, winheight(winnr()))
1069 call assert_equal(33, winwidth(winnr()))
1070 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1071 quit
1072
1073 call assert_equal(1, winnr('$'))
1074 let width = winwidth(0)
1075 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1076 call assert_equal(2, winnr('$'))
1077 call assert_equal(width, winwidth(winnr()))
1078 call assert_equal(13, winheight(winnr()))
1079 call assert_equal('something else', bufname('%'))
1080 quit
1081
1082 call assert_equal(1, winnr('$'))
1083 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1084 call assert_equal(1, winnr('$'))
1085 bwipe
1086
1087 set laststatus&
1088endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001089
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001090func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001091 call assert_equal(1, winnr('$'))
1092
1093 " Use the title termcap entries to output the escape sequence.
1094 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001095 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001096 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001097 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001098 \ 'redraw',
1099 \ "set t_ts=",
1100 \ ], 'Xscript')
1101 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001102 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001103 call assert_equal('Xtextfile', expand('%:t'))
1104 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001105 return buf
1106endfunc
1107
1108func Test_terminal_api_drop_newwin()
1109 if !CanRunVimInTerminal()
1110 return
1111 endif
1112 let buf = Api_drop_common('')
1113 call assert_equal(0, &bin)
1114 call assert_equal('', &fenc)
1115
1116 call StopVimInTerminal(buf)
1117 call delete('Xscript')
1118 bwipe Xtextfile
1119endfunc
1120
1121func Test_terminal_api_drop_newwin_bin()
1122 if !CanRunVimInTerminal()
1123 return
1124 endif
1125 let buf = Api_drop_common(',{"bin":1}')
1126 call assert_equal(1, &bin)
1127
1128 call StopVimInTerminal(buf)
1129 call delete('Xscript')
1130 bwipe Xtextfile
1131endfunc
1132
1133func Test_terminal_api_drop_newwin_binary()
1134 if !CanRunVimInTerminal()
1135 return
1136 endif
1137 let buf = Api_drop_common(',{"binary":1}')
1138 call assert_equal(1, &bin)
1139
1140 call StopVimInTerminal(buf)
1141 call delete('Xscript')
1142 bwipe Xtextfile
1143endfunc
1144
1145func Test_terminal_api_drop_newwin_nobin()
1146 if !CanRunVimInTerminal()
1147 return
1148 endif
1149 set binary
1150 let buf = Api_drop_common(',{"nobin":1}')
1151 call assert_equal(0, &bin)
1152
1153 call StopVimInTerminal(buf)
1154 call delete('Xscript')
1155 bwipe Xtextfile
1156 set nobinary
1157endfunc
1158
1159func Test_terminal_api_drop_newwin_nobinary()
1160 if !CanRunVimInTerminal()
1161 return
1162 endif
1163 set binary
1164 let buf = Api_drop_common(',{"nobinary":1}')
1165 call assert_equal(0, &bin)
1166
1167 call StopVimInTerminal(buf)
1168 call delete('Xscript')
1169 bwipe Xtextfile
1170 set nobinary
1171endfunc
1172
1173func Test_terminal_api_drop_newwin_ff()
1174 if !CanRunVimInTerminal()
1175 return
1176 endif
1177 let buf = Api_drop_common(',{"ff":"dos"}')
1178 call assert_equal("dos", &ff)
1179
1180 call StopVimInTerminal(buf)
1181 call delete('Xscript')
1182 bwipe Xtextfile
1183endfunc
1184
1185func Test_terminal_api_drop_newwin_fileformat()
1186 if !CanRunVimInTerminal()
1187 return
1188 endif
1189 let buf = Api_drop_common(',{"fileformat":"dos"}')
1190 call assert_equal("dos", &ff)
1191
1192 call StopVimInTerminal(buf)
1193 call delete('Xscript')
1194 bwipe Xtextfile
1195endfunc
1196
1197func Test_terminal_api_drop_newwin_enc()
1198 if !CanRunVimInTerminal()
1199 return
1200 endif
1201 let buf = Api_drop_common(',{"enc":"utf-16"}')
1202 call assert_equal("utf-16", &fenc)
1203
1204 call StopVimInTerminal(buf)
1205 call delete('Xscript')
1206 bwipe Xtextfile
1207endfunc
1208
1209func Test_terminal_api_drop_newwin_encoding()
1210 if !CanRunVimInTerminal()
1211 return
1212 endif
1213 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1214 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001215
1216 call StopVimInTerminal(buf)
1217 call delete('Xscript')
1218 bwipe Xtextfile
1219endfunc
1220
1221func Test_terminal_api_drop_oldwin()
1222 if !CanRunVimInTerminal()
1223 return
1224 endif
1225 let firstwinid = win_getid()
1226 split Xtextfile
1227 let textfile_winid = win_getid()
1228 call assert_equal(2, winnr('$'))
1229 call win_gotoid(firstwinid)
1230
1231 " Use the title termcap entries to output the escape sequence.
1232 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001233 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001234 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1235 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1236 \ 'redraw',
1237 \ "set t_ts=",
1238 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001239 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001240 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001241 call assert_equal(textfile_winid, win_getid())
1242
1243 call StopVimInTerminal(buf)
1244 call delete('Xscript')
1245 bwipe Xtextfile
1246endfunc
1247
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001248func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001249 let g:called_bufnum = a:bufnum
1250 let g:called_arg = a:arg
1251endfunc
1252
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001253func WriteApiCall(funcname)
1254 " Use the title termcap entries to output the escape sequence.
1255 call writefile([
1256 \ 'set title',
1257 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1258 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1259 \ 'redraw',
1260 \ "set t_ts=",
1261 \ ], 'Xscript')
1262endfunc
1263
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001264func Test_terminal_api_call()
1265 if !CanRunVimInTerminal()
1266 return
1267 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001268
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001269 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001270 let buf = RunVimInTerminal('-S Xscript', {})
1271 call WaitFor({-> exists('g:called_bufnum')})
1272 call assert_equal(buf, g:called_bufnum)
1273 call assert_equal(['hello', 123], g:called_arg)
1274
1275 call StopVimInTerminal(buf)
1276 call delete('Xscript')
1277 unlet g:called_bufnum
1278 unlet g:called_arg
1279endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001280
1281func Test_terminal_api_call_fails()
1282 if !CanRunVimInTerminal()
1283 return
1284 endif
1285
1286 call WriteApiCall('TryThis')
1287 call ch_logfile('Xlog', 'w')
1288 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001289 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001290
1291 call StopVimInTerminal(buf)
1292 call delete('Xscript')
1293 call ch_logfile('', '')
1294 call delete('Xlog')
1295endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001296
Bram Moolenaara997b452018-04-17 23:24:06 +02001297let s:caught_e937 = 0
1298
1299func Tapi_Delete(bufnum, arg)
1300 try
1301 execute 'bdelete!' a:bufnum
1302 catch /E937:/
1303 let s:caught_e937 = 1
1304 endtry
1305endfunc
1306
1307func Test_terminal_api_call_fail_delete()
1308 if !CanRunVimInTerminal()
1309 return
1310 endif
1311
1312 call WriteApiCall('Tapi_Delete')
1313 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001314 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001315
1316 call StopVimInTerminal(buf)
1317 call delete('Xscript')
1318 call ch_logfile('', '')
1319endfunc
1320
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001321func Test_terminal_ansicolors_default()
1322 let colors = [
1323 \ '#000000', '#e00000',
1324 \ '#00e000', '#e0e000',
1325 \ '#0000e0', '#e000e0',
1326 \ '#00e0e0', '#e0e0e0',
1327 \ '#808080', '#ff4040',
1328 \ '#40ff40', '#ffff40',
1329 \ '#4040ff', '#ff40ff',
1330 \ '#40ffff', '#ffffff',
1331 \]
1332
1333 let buf = Run_shell_in_terminal({})
1334 call assert_equal(colors, term_getansicolors(buf))
1335 call Stop_shell_in_terminal(buf)
1336 call term_wait(buf)
1337
1338 exe buf . 'bwipe'
1339endfunc
1340
1341let s:test_colors = [
1342 \ '#616e64', '#0d0a79',
1343 \ '#6d610d', '#0a7373',
1344 \ '#690d0a', '#6d696e',
1345 \ '#0d0a6f', '#616e0d',
1346 \ '#0a6479', '#6d0d0a',
1347 \ '#617373', '#0d0a69',
1348 \ '#6d690d', '#0a6e6f',
1349 \ '#610d0a', '#6e6479',
1350 \]
1351
1352func Test_terminal_ansicolors_global()
1353 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1354 let buf = Run_shell_in_terminal({})
1355 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1356 call Stop_shell_in_terminal(buf)
1357 call term_wait(buf)
1358
1359 exe buf . 'bwipe'
1360 unlet g:terminal_ansi_colors
1361endfunc
1362
1363func Test_terminal_ansicolors_func()
1364 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1365 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1366 call assert_equal(s:test_colors, term_getansicolors(buf))
1367
1368 call term_setansicolors(buf, g:terminal_ansi_colors)
1369 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1370
1371 let colors = [
1372 \ 'ivory', 'AliceBlue',
1373 \ 'grey67', 'dark goldenrod',
1374 \ 'SteelBlue3', 'PaleVioletRed4',
1375 \ 'MediumPurple2', 'yellow2',
1376 \ 'RosyBrown3', 'OrangeRed2',
1377 \ 'white smoke', 'navy blue',
1378 \ 'grey47', 'gray97',
1379 \ 'MistyRose2', 'DodgerBlue4',
1380 \]
1381 call term_setansicolors(buf, colors)
1382
1383 let colors[4] = 'Invalid'
1384 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1385
1386 call Stop_shell_in_terminal(buf)
1387 call term_wait(buf)
1388 exe buf . 'bwipe'
1389endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001390
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001391func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001392 if !CanRunVimInTerminal()
1393 return
1394 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001395 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001396 let text = []
1397 for n in range(10)
1398 call add(text, repeat(n, 50))
1399 endfor
1400 call writefile(text, 'Xwinsize')
1401 let buf = RunVimInTerminal('Xwinsize', {})
1402 let win = bufwinid(buf)
1403 call assert_equal([6, 40], term_getsize(buf))
1404 call assert_equal(6, winheight(win))
1405 call assert_equal(40, winwidth(win))
1406
1407 " resizing the window doesn't resize the terminal.
1408 resize 10
1409 vertical resize 60
1410 call assert_equal([6, 40], term_getsize(buf))
1411 call assert_equal(10, winheight(win))
1412 call assert_equal(60, winwidth(win))
1413
1414 call StopVimInTerminal(buf)
1415 call delete('Xwinsize')
1416
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001417 call assert_fails('set termwinsize=40', 'E474')
1418 call assert_fails('set termwinsize=10+40', 'E474')
1419 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001420
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001421 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001422endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001423
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001424func Test_terminal_termwinsize_option_zero()
1425 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001426 let buf = Run_shell_in_terminal({})
1427 let win = bufwinid(buf)
1428 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1429 call Stop_shell_in_terminal(buf)
1430 call term_wait(buf)
1431 exe buf . 'bwipe'
1432
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001433 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001434 let buf = Run_shell_in_terminal({})
1435 let win = bufwinid(buf)
1436 call assert_equal([7, winwidth(win)], term_getsize(buf))
1437 call Stop_shell_in_terminal(buf)
1438 call term_wait(buf)
1439 exe buf . 'bwipe'
1440
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001441 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001442 let buf = Run_shell_in_terminal({})
1443 let win = bufwinid(buf)
1444 call assert_equal([winheight(win), 33], term_getsize(buf))
1445 call Stop_shell_in_terminal(buf)
1446 call term_wait(buf)
1447 exe buf . 'bwipe'
1448
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001449 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001450endfunc
1451
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001452func Test_terminal_termwinsize_mininmum()
1453 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001454 vsplit
1455 let buf = Run_shell_in_terminal({})
1456 let win = bufwinid(buf)
1457 call assert_inrange(10, 1000, winheight(win))
1458 call assert_inrange(50, 1000, winwidth(win))
1459 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1460
1461 resize 15
1462 vertical resize 60
1463 redraw
1464 call assert_equal([15, 60], term_getsize(buf))
1465 call assert_equal(15, winheight(win))
1466 call assert_equal(60, winwidth(win))
1467
1468 resize 7
1469 vertical resize 30
1470 redraw
1471 call assert_equal([10, 50], term_getsize(buf))
1472 call assert_equal(7, winheight(win))
1473 call assert_equal(30, winwidth(win))
1474
1475 call Stop_shell_in_terminal(buf)
1476 call term_wait(buf)
1477 exe buf . 'bwipe'
1478
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001479 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001480 let buf = Run_shell_in_terminal({})
1481 let win = bufwinid(buf)
1482 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1483 call Stop_shell_in_terminal(buf)
1484 call term_wait(buf)
1485 exe buf . 'bwipe'
1486
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001487 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001488endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001489
1490func Test_terminal_termwinkey()
1491 call assert_equal(1, winnr('$'))
1492 let thiswin = win_getid()
1493
1494 let buf = Run_shell_in_terminal({})
1495 let termwin = bufwinid(buf)
1496 set termwinkey=<C-L>
1497 call feedkeys("\<C-L>w", 'tx')
1498 call assert_equal(thiswin, win_getid())
1499 call feedkeys("\<C-W>w", 'tx')
1500
1501 let job = term_getjob(buf)
1502 call feedkeys("\<C-L>\<C-C>", 'tx')
1503 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1504endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001505
1506func Test_terminal_out_err()
1507 if !has('unix')
1508 return
1509 endif
1510 call writefile([
1511 \ '#!/bin/sh',
1512 \ 'echo "this is standard error" >&2',
1513 \ 'echo "this is standard out" >&1',
1514 \ ], 'Xechoerrout.sh')
1515 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1516
1517 let outfile = 'Xtermstdout'
1518 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001519
1520 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1521 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001522 call assert_equal('this is standard error', term_getline(buf, 1))
1523
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001524 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001525 exe buf . 'bwipe'
1526 call delete('Xechoerrout.sh')
1527 call delete(outfile)
1528endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001529
1530func Test_terminwinscroll()
1531 if !has('unix')
1532 return
1533 endif
1534
1535 " Let the terminal output more than 'termwinscroll' lines, some at the start
1536 " will be dropped.
1537 exe 'set termwinscroll=' . &lines
1538 let buf = term_start('/bin/sh')
1539 for i in range(1, &lines)
1540 call feedkeys("echo " . i . "\<CR>", 'xt')
1541 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1542 endfor
1543 " Go to Terminal-Normal mode to update the buffer.
1544 call feedkeys("\<C-W>N", 'xt')
1545 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1546
1547 " Every "echo nr" must only appear once
1548 let lines = getline(1, line('$'))
1549 for i in range(&lines - len(lines) / 2 + 2, &lines)
1550 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1551 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1552 endfor
1553
1554 exe buf . 'bwipe!'
1555endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001556
Bram Moolenaar875cf872018-07-08 20:49:07 +02001557" Resizing the terminal window caused an ml_get error.
1558" TODO: This does not reproduce the original problem.
1559func Test_terminal_resize()
1560 set statusline=x
1561 terminal
1562 call assert_equal(2, winnr('$'))
1563
1564 " Fill the terminal with text.
1565 if has('win32')
1566 call feedkeys("dir\<CR>", 'xt')
1567 else
1568 call feedkeys("ls\<CR>", 'xt')
1569 endif
1570 " Go to Terminal-Normal mode for a moment.
1571 call feedkeys("\<C-W>N", 'xt')
1572 " Open a new window
1573 call feedkeys("i\<C-W>n", 'xt')
1574 call assert_equal(3, winnr('$'))
1575 redraw
1576
1577 close
1578 call assert_equal(2, winnr('$'))
1579 call feedkeys("exit\<CR>", 'xt')
1580 set statusline&
1581endfunc
1582
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001583" must be nearly the last, we can't go back from GUI to terminal
1584func Test_zz1_terminal_in_gui()
1585 if !CanRunGui()
1586 return
1587 endif
1588
1589 " Ignore the "failed to create input context" error.
1590 call test_ignore_error('E285:')
1591
1592 gui -f
1593
1594 call assert_equal(1, winnr('$'))
1595 let buf = Run_shell_in_terminal({'term_finish': 'close'})
1596 call Stop_shell_in_terminal(buf)
1597 call term_wait(buf)
1598
1599 " closing window wipes out the terminal buffer a with finished job
1600 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1601 call assert_equal("", bufname(buf))
1602
1603 unlet g:job
1604endfunc
1605
1606func Test_zz2_terminal_guioptions_bang()
1607 if !has('gui_running')
1608 return
1609 endif
1610 set guioptions+=!
1611
1612 let filename = 'Xtestscript'
1613 if has('win32')
1614 let filename .= '.bat'
1615 let prefix = ''
1616 let contents = ['@echo off', 'exit %1']
1617 else
1618 let filename .= '.sh'
1619 let prefix = './'
1620 let contents = ['#!/bin/sh', 'exit $1']
1621 endif
1622 call writefile(contents, filename)
1623 call setfperm(filename, 'rwxrwx---')
1624
1625 " Check if v:shell_error is equal to the exit status.
1626 let exitval = 0
1627 execute printf(':!%s%s %d', prefix, filename, exitval)
1628 call assert_equal(exitval, v:shell_error)
1629
1630 let exitval = 9
1631 execute printf(':!%s%s %d', prefix, filename, exitval)
1632 call assert_equal(exitval, v:shell_error)
1633
1634 set guioptions&
1635 call delete(filename)
1636endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001637
1638func Test_terminal_hidden()
1639 if !has('unix')
1640 return
1641 endif
1642 term ++hidden cat
1643 let bnr = bufnr('$')
1644 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1645 exe 'sbuf ' . bnr
1646 call assert_equal('terminal', &buftype)
1647 call term_sendkeys(bnr, "asdf\<CR>")
1648 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1649 call term_sendkeys(bnr, "\<C-D>")
1650 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
1651 bwipe!
1652endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001653
1654func Test_terminal_hidden_and_close()
1655 if !has('unix')
1656 return
1657 endif
1658 call assert_equal(1, winnr('$'))
1659 term ++hidden ++close ls
1660 let bnr = bufnr('$')
1661 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1662 call WaitForAssert({-> assert_false(bufexists(bnr))})
1663 call assert_equal(1, winnr('$'))
1664endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001665
1666func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001667 let contents = [
1668 \ [ 'One', '', 'X' ],
1669 \ [ 'Two', '', '' ],
1670 \ [ 'Three' ] + repeat([''], 30)
1671 \ ]
1672
1673 for c in contents
1674 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001675 if has('win32')
1676 term cmd /c type Xfile
1677 else
1678 term cat Xfile
1679 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001680 let bnr = bufnr('$')
1681 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1682 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001683 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001684 call assert_equal(c, getline(1, line('$')))
1685 quit
1686 endfor
1687
1688 call delete('Xfile')
1689endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001690
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001691func Test_terminal_no_job()
1692 let term = term_start('false', {'term_finish': 'close'})
1693 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1694endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001695
1696func Test_term_gettitle()
1697 if !has('title') || empty(&t_ts)
1698 return
1699 endif
1700 " TODO: this fails on Travis
1701 return
1702
1703 " term_gettitle() returns an empty string for a non-terminal buffer
1704 " or for a non-existing buffer.
1705 call assert_equal('', term_gettitle(bufnr('%')))
1706 call assert_equal('', term_gettitle(bufnr('$') + 1))
1707
1708 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
1709 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
1710
1711 call term_sendkeys(term, ":e Xfoo\r")
1712 call WaitForAssert({-> assert_match('Xfoo (.*[/\\]testdir) - VIM', term_gettitle(term)) })
1713
1714 call term_sendkeys(term, ":set titlestring=foo\r")
1715 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
1716
1717 exe term . 'bwipe!'
1718endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01001719
1720" When drawing the statusline the cursor position may not have been updated
1721" yet.
1722" 1. create a terminal, make it show 2 lines
1723" 2. 0.5 sec later: leave terminal window, execute "i"
1724" 3. 0.5 sec later: clear terminal window, now it's 1 line
1725" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1726" 4. 0.5 sec later: should be done, clean up
1727func Test_terminal_statusline()
1728 if !has('unix')
1729 return
1730 endif
1731 set statusline=x
1732 terminal
1733 let tbuf = bufnr('')
1734 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1735 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1736 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1737 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1738
1739 sleep 2
1740 exe tbuf . 'bwipe!'
1741 au! BufLeave
1742 set statusline=
1743endfunc