blob: f3571d4e4bc4f2b091c38ce98681d4a0662c8eb9 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02005
6source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01007source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02008
Bram Moolenaarb81bc772017-08-11 22:45:01 +02009let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020010let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020011
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 Moolenaar373a8762020-03-19 19:44:32 +010020 let g:test_is_flaky = 1
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020021
22 let termlist = term_list()
23 call assert_equal(1, len(termlist))
24 call assert_equal(buf, termlist[0])
25
26 let g:job = term_getjob(buf)
27 call assert_equal(v:t_job, type(g:job))
28
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020029 let string = string({'job': buf->term_getjob()})
Bram Moolenaar35422f42017-08-05 16:33:56 +020030 call assert_match("{'job': 'process \\d\\+ run'}", string)
31
Bram Moolenaar94053a52017-08-01 21:44:33 +020032 return buf
33endfunc
34
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020035func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020036 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020037 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020038
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020039 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020040 call assert_match('^/dev/', job_info(g:job).tty_out)
41 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020042 else
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010043 " ConPTY works on anonymous pipe.
44 if !has('conpty')
45 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020046 call assert_match('^\\\\.\\pipe\\', ''->term_gettty())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010047 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020048 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020049 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020050 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020052 call assert_match('%aR[^\n]*running]', execute('ls R'))
53 call assert_notmatch('%[^\n]*running]', execute('ls F'))
54 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020055
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020056 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +020057 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020058 call assert_equal('n', mode())
59 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020060 call assert_match('%aF[^\n]*finished]', execute('ls F'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
62 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020063
Bram Moolenaar94053a52017-08-01 21:44:33 +020064 " closing window wipes out the terminal buffer a with finished job
65 close
66 call assert_equal("", bufname(buf))
67
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020068 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020069 unlet g:job
70endfunc
71
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020072func Test_terminal_TerminalWinOpen()
73 au TerminalWinOpen * let b:done = 'yes'
74 let buf = Run_shell_in_terminal({})
75 call assert_equal('yes', b:done)
76 call StopShellInTerminal(buf)
77 " closing window wipes out the terminal buffer with the finished job
78 close
79
80 if has("unix")
81 terminal ++hidden ++open sleep 1
82 sleep 1
83 call assert_fails("echo b:done", 'E121:')
84 endif
85
86 au! TerminalWinOpen
87endfunc
88
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020089func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020090 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020091 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020092 call term_wait(buf)
93
94 setlocal modifiable
95 exe "normal Axxx\<Esc>"
96 call assert_fails(buf . 'bwipe', 'E517')
97 undo
98
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020099 exe buf . 'bwipe'
100 unlet g:job
101endfunc
102
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100103func Test_terminal_paste_register()
104 let @" = "text to paste"
105
106 let buf = Run_shell_in_terminal({})
107 " Wait for the shell to display a prompt
108 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
109
110 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
111 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200112 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100113
114 exe buf . 'bwipe!'
115 unlet g:job
116endfunc
117
Bram Moolenaar94053a52017-08-01 21:44:33 +0200118func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200119 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200120 call assert_fails(buf . 'bwipe', 'E517')
121 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200122 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200123 call assert_equal("", bufname(buf))
124
125 unlet g:job
126endfunc
127
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200128func Test_terminal_split_quit()
129 let buf = Run_shell_in_terminal({})
130 call term_wait(buf)
131 split
132 quit!
133 call term_wait(buf)
134 sleep 50m
135 call assert_equal('run', job_status(g:job))
136
137 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200138 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200139
140 exe buf . 'bwipe'
141 unlet g:job
142endfunc
143
Bram Moolenaar94053a52017-08-01 21:44:33 +0200144func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200145 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200146 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200147 quit
148 for nr in range(1, winnr('$'))
149 call assert_notequal(winbufnr(nr), buf)
150 endfor
151 call assert_true(bufloaded(buf))
152 call assert_true(buflisted(buf))
153
154 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200155 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200156 exe buf . 'bwipe'
157
158 unlet g:job
159endfunc
160
Bram Moolenaar1e115362019-01-09 23:01:02 +0100161func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162 exe g:buf . 'bwipe!'
163 let g:buf = 0
164endfunc
165
Bram Moolenaar9d189612017-09-09 18:11:00 +0200166func Get_cat_123_cmd()
167 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100168 if !has('conpty')
169 return 'cmd /c "cls && color 2 && echo 123"'
170 else
171 " When clearing twice, extra sequence is not output.
172 return 'cmd /c "cls && cls && color 2 && echo 123"'
173 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200174 else
175 call writefile(["\<Esc>[32m123"], 'Xtext')
176 return "cat Xtext"
177 endif
178endfunc
179
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200180func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200181 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200182 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
183 let g:job = term_getjob(g:buf)
184
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200185 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
186 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200187 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100188 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200189 call delete('Xtext')
190endfunc
191
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200192func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200193 let l = term_scrape(a:buf, 0)
194 call assert_true(len(l) == 0)
195 let l = term_scrape(a:buf, 999)
196 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200197 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200198 call assert_true(len(l) > 0)
199 call assert_equal('1', l[0].chars)
200 call assert_equal('2', l[1].chars)
201 call assert_equal('3', l[2].chars)
202 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200203 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
204 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100205 if has('win32')
206 " On Windows 'background' always defaults to dark, even though the terminal
207 " may use a light background. Therefore accept both white and black.
208 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200209 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100210 if &background == 'light'
211 call assert_equal('#ffffff', l[0].bg)
212 else
213 call assert_equal('#000000', l[0].bg)
214 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200215 endif
216
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200217 let l = term_getline(a:buf, -1)
218 call assert_equal('', l)
219 let l = term_getline(a:buf, 0)
220 call assert_equal('', l)
221 let l = term_getline(a:buf, 999)
222 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200223 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200224 call assert_equal('123', l)
225endfunc
226
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200227func Test_terminal_scrape_123()
228 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200229 let buf = term_start(cmd)
230
231 let termlist = term_list()
232 call assert_equal(1, len(termlist))
233 call assert_equal(buf, termlist[0])
234
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200235 " Nothing happens with invalid buffer number
236 call term_wait(1234)
237
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200238 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200239 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200240 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200241 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200242 call Check_123(buf)
243
244 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100245 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200246 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200247 call term_wait(buf)
248 call Check_123(buf)
249
250 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200251 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200252endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200253
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200254func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200255 call writefile(["léttrs"], 'Xtext')
256 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200257 " Run cmd with UTF-8 codepage to make the type command print the expected
258 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100259 let buf = term_start("cmd /K chcp 65001")
260 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200261 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100262 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200263 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100264 let buf = term_start("cat Xtext")
265 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200267
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100268 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
269 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200270 call assert_true(len(l) >= 7)
271 call assert_equal('l', l[0].chars)
272 call assert_equal('é', l[1].chars)
273 call assert_equal(1, l[1].width)
274 call assert_equal('t', l[2].chars)
275 call assert_equal('t', l[3].chars)
276 call assert_equal('ま', l[4].chars)
277 call assert_equal(2, l[4].width)
278 call assert_equal('r', l[5].chars)
279 call assert_equal('s', l[6].chars)
280
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100281 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200282 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100283 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200284
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100285 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200286 call delete('Xtext')
287endfunc
288
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200289func Test_terminal_scroll()
290 call writefile(range(1, 200), 'Xtext')
291 if has('win32')
292 let cmd = 'cmd /c "type Xtext"'
293 else
294 let cmd = "cat Xtext"
295 endif
296 let buf = term_start(cmd)
297
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100298 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200299 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200300 call term_wait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200301
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100302 " wait until the scrolling stops
303 while 1
304 let scrolled = buf->term_getscrolled()
305 sleep 20m
306 if scrolled == buf->term_getscrolled()
307 break
308 endif
309 endwhile
310
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200311 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200312 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200313 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200314 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200315 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200316 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200317
318 exe buf . 'bwipe'
319 call delete('Xtext')
320endfunc
321
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200322func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200323 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200324 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200325 call writefile(range(150), 'Xtext')
326 if has('win32')
327 call term_sendkeys(buf, "type Xtext\<CR>")
328 else
329 call term_sendkeys(buf, "cat Xtext\<CR>")
330 endif
331 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200332 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200333 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200334 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200335 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200336
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200337 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200338 call term_wait(buf)
339 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200340 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100341 call delete('Xtext')
342endfunc
343
344func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200345 " tail -f only works on Unix
346 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100347
348 call writefile(range(50), 'Xtext')
349 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100350 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100351 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100352 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100353 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
354 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100355 \ 'call feedkeys("\<C-W>N", "xt")',
356 \ ], 'XTest_postponed')
357 let buf = RunVimInTerminal('-S XTest_postponed', {})
358 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100359 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100360
361 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200362 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100363 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100364 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100365
366 " Back to Terminal-Job mode, text will scroll and show the extra line.
367 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100368 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100369
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100370 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100371 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100372 call term_wait(buf, 50)
373 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100374 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100375 call term_wait(buf, 100)
376 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100377 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
378
379 " check type of term_sendkeys() return value
380 echo type(tsk_ret)
381
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100382 call StopVimInTerminal(buf)
383 call delete('XTest_postponed')
384 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200385endfunc
386
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100387" Run diff on two dumps with different size.
388func Test_terminal_dumpdiff_size()
389 call assert_equal(1, winnr('$'))
390 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
391 call assert_equal(2, winnr('$'))
392 call assert_match('Test_incsearch_search_01.dump', getline(10))
393 call assert_match(' +++++$', getline(11))
394 call assert_match('Test_popup_command_01.dump', getline(31))
395 call assert_equal(repeat('+', 75), getline(30))
396 quit
397endfunc
398
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200399func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200400 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200401
Bram Moolenaarb2412082017-08-20 18:09:14 +0200402 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200403 let size = term_getsize('')
404 bwipe!
405 call assert_equal(5, size[0])
406
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200407 call term_start(cmd, {'term_rows': 6})
408 let size = term_getsize('')
409 bwipe!
410 call assert_equal(6, size[0])
411
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200412 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200413 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200414 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200415
416 call term_setsize('', 6, 0)
417 call assert_equal([6, 33], term_getsize(''))
418
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200419 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200420 call assert_equal([6, 35], term_getsize(''))
421
422 call term_setsize('', 7, 30)
423 call assert_equal([7, 30], term_getsize(''))
424
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200425 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200426 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200427
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200428 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
429 let size = term_getsize('')
430 bwipe!
431 call assert_equal([6, 36], size)
432
Bram Moolenaarb2412082017-08-20 18:09:14 +0200433 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200434 let size = term_getsize('')
435 bwipe!
436 call assert_equal(20, size[1])
437
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200438 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200439 let size = term_getsize('')
440 bwipe!
441 call assert_equal(26, size[1])
442
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200443 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200444 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200445 let size = term_getsize('')
446 bwipe!
447 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200448
449 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
450 let size = term_getsize('')
451 bwipe!
452 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200453
454 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200455endfunc
456
457func Test_terminal_curwin()
458 let cmd = Get_cat_123_cmd()
459 call assert_equal(1, winnr('$'))
460
461 split dummy
462 exe 'terminal ++curwin ' . cmd
463 call assert_equal(2, winnr('$'))
464 bwipe!
465
466 split dummy
467 call term_start(cmd, {'curwin': 1})
468 call assert_equal(2, winnr('$'))
469 bwipe!
470
471 split dummy
472 call setline(1, 'change')
473 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
474 call assert_equal(2, winnr('$'))
475 exe 'terminal! ++curwin ' . cmd
476 call assert_equal(2, winnr('$'))
477 bwipe!
478
479 split dummy
480 call setline(1, 'change')
481 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
482 call assert_equal(2, winnr('$'))
483 bwipe!
484
485 split dummy
486 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200487 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200488endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200489
Bram Moolenaarff546792017-11-21 14:47:57 +0100490func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200491 if s:python != ''
492 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200493 " 500 was not enough for Travis
494 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200495 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200496 echo 'This will take five seconds...'
497 let waittime = 2000
498 if has('win32')
499 let cmd = $windir . '\system32\timeout.exe 1'
500 else
501 let cmd = 'sleep 1'
502 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200503 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100504 return [cmd, waittime]
505endfunc
506
507func Test_terminal_finish_open_close()
508 call assert_equal(1, winnr('$'))
509
510 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200511
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100512 " shell terminal closes automatically
513 terminal
514 let buf = bufnr('%')
515 call assert_equal(2, winnr('$'))
516 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200517 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200518 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200519 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100520
521 " shell terminal that does not close automatically
522 terminal ++noclose
523 let buf = bufnr('%')
524 call assert_equal(2, winnr('$'))
525 " 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 Moolenaar7a39dd72019-06-23 00:50:15 +0200527 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100528 call assert_equal(2, winnr('$'))
529 quit
530 call assert_equal(1, winnr('$'))
531
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200532 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200533 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200534 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200535 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200536
537 call term_start(cmd, {'term_finish': 'close'})
538 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200539 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200540 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200541 call assert_equal(1, winnr('$'))
542
543 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200544 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200545 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200546 bwipe
547
548 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200549 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200550 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200551 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200552
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200553 exe 'terminal ++hidden ++open ' . cmd
554 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200555 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200556 bwipe
557
558 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
559 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200560 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200561 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200562
563 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
564 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
565 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
566 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
567
568 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200569 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200570 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200571 call assert_equal(4, winheight(0))
572 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200573endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200574
575func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100576 if has('win32')
577 let cmd = 'cmd /c cd'
578 else
579 CheckExecutable pwd
580 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200581 endif
582 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100583 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200584 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200585
586 exe buf . 'bwipe'
587 call delete('Xdir', 'rf')
588endfunc
589
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200590func Test_terminal_cwd_failure()
591 " Case 1: Provided directory is not actually a directory. Attempt to make
592 " the file executable as well.
593 call writefile([], 'Xfile')
594 call setfperm('Xfile', 'rwx------')
595 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
596 call delete('Xfile')
597
598 " Case 2: Directory does not exist.
599 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
600
601 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100602 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200603 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100604 call mkdir('XdirNoAccess', '', '0600')
605 " return early if the directory permissions could not be set properly
606 if getfperm('XdirNoAccess')[2] == 'x'
607 call delete('XdirNoAccess', 'rf')
608 return
609 endif
610 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
611 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200612 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200613endfunc
614
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100615func Test_terminal_servername()
616 if !has('clientserver')
617 return
618 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200619 call s:test_environment("VIM_SERVERNAME", v:servername)
620endfunc
621
622func Test_terminal_version()
623 call s:test_environment("VIM_TERMINAL", string(v:version))
624endfunc
625
626func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100627 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100628 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200629 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100630 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200631 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100632 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200633 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100634 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100635 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200636 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200637 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100638
Bram Moolenaar012eb662018-03-13 17:55:27 +0100639 exe buf . 'bwipe'
640 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100641endfunc
642
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200643func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100644 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200645 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200646 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100647 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100648 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100649 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100650 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100651 endif
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200652 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200653 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200654 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200655
Bram Moolenaar012eb662018-03-13 17:55:27 +0100656 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200657endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200658
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200659func Test_terminal_list_args()
660 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
661 call assert_fails(buf . 'bwipe', 'E517')
662 exe buf . 'bwipe!'
663 call assert_equal("", bufname(buf))
664endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200665
666func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100667 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100668 let wait_time = 5000
669 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100670 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200671 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100672 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200673 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100674 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100675 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100676 " NPFS is used in Windows, nonblocking mode does not work properly.
677 elseif has('win32')
678 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200679 else
680 let len = 5000
681 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200682
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100683 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100684 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100685 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200686 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100687 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200688
689 " On MS-Windows there is an extra empty line below "done". Find "done" in
690 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100691 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100692 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100693 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200694 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100695 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200696 endif
697 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200698
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100699 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200700 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100701 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200702 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200703 bwipe
704endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200705
706func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100707 " TODO: enable once writing to stdin works on MS-Windows
708 CheckNotMSWindows
709 CheckExecutable wc
710
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200711 call setline(1, ['one', 'two', 'three'])
712 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200713 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200714 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200715 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100716 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200717
718 call setline(1, ['one', 'two', 'three', 'four'])
719 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200720 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200721 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200722 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100723 %bwipe!
724endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200725
Bram Moolenaar21109272020-01-30 16:27:20 +0100726func Test_terminal_eof_arg()
727 CheckExecutable python
Bram Moolenaardada6d22017-09-02 17:18:35 +0200728
Bram Moolenaar21109272020-01-30 16:27:20 +0100729 call setline(1, ['print("hello")'])
730 1term ++eof=exit(123) python
731 " MS-Windows echoes the input, Unix doesn't.
732 if has('win32')
733 call WaitFor({-> getline('$') =~ 'exit(123)'})
734 call assert_equal('hello', getline(line('$') - 1))
735 else
736 call WaitFor({-> getline('$') =~ 'hello'})
737 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200738 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100739 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
740 %bwipe!
741endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200742
Bram Moolenaar21109272020-01-30 16:27:20 +0100743func Test_terminal_eof_arg_win32_ctrl_z()
744 CheckMSWindows
745 CheckExecutable python
746
747 call setline(1, ['print("hello")'])
748 1term ++eof=<C-Z> python
749 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
750 call assert_match('\^Z', getline(line('$') - 1))
751 %bwipe!
752endfunc
753
754func Test_terminal_duplicate_eof_arg()
755 CheckExecutable python
756
757 " Check the last specified ++eof arg is used and should not memory leak.
758 new
759 call setline(1, ['print("hello")'])
760 1term ++eof=<C-Z> ++eof=exit(123) python
761 " MS-Windows echoes the input, Unix doesn't.
762 if has('win32')
763 call WaitFor({-> getline('$') =~ 'exit(123)'})
764 call assert_equal('hello', getline(line('$') - 1))
765 else
766 call WaitFor({-> getline('$') =~ 'hello'})
767 call assert_equal('hello', getline('$'))
768 endif
769 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
770 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200771endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200772
773func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200774 let buf = term_start('NONE', {})
775 call assert_notequal(0, buf)
776
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200777 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200778 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100779 if has('gui_running') && !has('win32')
780 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200781 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100782 else
783 " Otherwise using a job works on all systems.
784 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200785 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200786 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200787
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200788 bwipe!
789endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200790
791func Test_terminal_special_chars()
792 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200793 CheckUnix
794
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200795 call mkdir('Xdir with spaces')
796 call writefile(['x'], 'Xdir with spaces/quoted"file')
797 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200798 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100799 " make sure the job has finished
800 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200801
802 call delete('Xdir with spaces', 'rf')
803 bwipe
804endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200805
806func Test_terminal_wrong_options()
807 call assert_fails('call term_start(&shell, {
808 \ "in_io": "file",
809 \ "in_name": "xxx",
810 \ "out_io": "file",
811 \ "out_name": "xxx",
812 \ "err_io": "file",
813 \ "err_name": "xxx"
814 \ })', 'E474:')
815 call assert_fails('call term_start(&shell, {
816 \ "out_buf": bufnr("%")
817 \ })', 'E474:')
818 call assert_fails('call term_start(&shell, {
819 \ "err_buf": bufnr("%")
820 \ })', 'E474:')
821endfunc
822
823func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200824 let cmd = Get_cat_123_cmd()
825 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
826 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100827 " ConPTY may precede escape sequence. There are things that are not so.
828 if !has('conpty')
829 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
830 call assert_match('123', readfile('Xfile')[0])
831 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200832 let g:job = term_getjob(buf)
833 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
834 call delete('Xfile')
835 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200836
837 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200838 call writefile(['one line'], 'Xfile')
839 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
840 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200841 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200842 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200843 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200844 bwipe
845 call delete('Xfile')
846 endif
847endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200848
849func TerminalTmap(remap)
850 let buf = Run_shell_in_terminal({})
851 call assert_equal('t', mode())
852
853 if a:remap
854 tmap 123 456
855 else
856 tnoremap 123 456
857 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100858 " don't use abcde, it's an existing command
859 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200860 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100861 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200862 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200863 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200864 let lnum = term_getcursor(buf)[0]
865 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100866 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200867 else
868 call assert_match('456', term_getline(buf, lnum))
869 endif
870
871 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200872 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200873 call term_wait(buf)
874
875 tunmap 123
876 tunmap 456
877 call assert_equal('', maparg('123', 't'))
878 close
879 unlet g:job
880endfunc
881
882func Test_terminal_tmap()
883 call TerminalTmap(1)
884 call TerminalTmap(0)
885endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200886
887func Test_terminal_wall()
888 let buf = Run_shell_in_terminal({})
889 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200890 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200891 call term_wait(buf)
892 exe buf . 'bwipe'
893 unlet g:job
894endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200895
Bram Moolenaar7a760922018-02-19 23:10:02 +0100896func Test_terminal_wqall()
897 let buf = Run_shell_in_terminal({})
898 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200899 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100900 call term_wait(buf)
901 exe buf . 'bwipe'
902 unlet g:job
903endfunc
904
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200905func Test_terminal_composing_unicode()
Bram Moolenaar9134f1e2019-11-29 20:26:13 +0100906 CheckNotBSD
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200907 let save_enc = &encoding
908 set encoding=utf-8
909
910 if has('win32')
911 let cmd = "cmd /K chcp 65001"
912 let lnum = [3, 6, 9]
913 else
914 let cmd = &shell
915 let lnum = [1, 3, 5]
916 endif
917
918 enew
919 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100920 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200921 call term_wait(buf, 50)
922
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200923 if has('win32')
924 call assert_equal('cmd', job_info(g:job).cmd[0])
925 else
926 call assert_equal(&shell, job_info(g:job).cmd[0])
927 endif
928
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200929 " ascii + composing
930 let txt = "a\u0308bc"
931 call term_sendkeys(buf, "echo " . txt . "\r")
932 call term_wait(buf, 50)
933 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
934 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
935 let l = term_scrape(buf, lnum[0] + 1)
936 call assert_equal("a\u0308", l[0].chars)
937 call assert_equal("b", l[1].chars)
938 call assert_equal("c", l[2].chars)
939
940 " multibyte + composing
941 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
942 call term_sendkeys(buf, "echo " . txt . "\r")
943 call term_wait(buf, 50)
944 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
945 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
946 let l = term_scrape(buf, lnum[1] + 1)
947 call assert_equal("\u304b\u3099", l[0].chars)
948 call assert_equal("\u304e", l[1].chars)
949 call assert_equal("\u304f\u3099", l[2].chars)
950 call assert_equal("\u3052", l[3].chars)
951 call assert_equal("\u3053\u3099", l[4].chars)
952
953 " \u00a0 + composing
954 let txt = "abc\u00a0\u0308"
955 call term_sendkeys(buf, "echo " . txt . "\r")
956 call term_wait(buf, 50)
957 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
958 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
959 let l = term_scrape(buf, lnum[2] + 1)
960 call assert_equal("\u00a0\u0308", l[3].chars)
961
962 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200963 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200964 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100965 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200966 let &encoding = save_enc
967endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100968
969func Test_terminal_aucmd_on_close()
970 fun Nop()
971 let s:called = 1
972 endfun
973
974 aug repro
975 au!
976 au BufWinLeave * call Nop()
977 aug END
978
979 let [cmd, waittime] = s:get_sleep_cmd()
980
981 call assert_equal(1, winnr('$'))
982 new
983 call setline(1, ['one', 'two'])
984 exe 'term ++close ' . cmd
985 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200986 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100987 call assert_equal(1, s:called)
988 bwipe!
989
990 unlet s:called
991 au! repro
992 delfunc Nop
993endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100994
995func Test_terminal_term_start_empty_command()
996 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
997 call assert_fails(cmd, 'E474')
998 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
999 call assert_fails(cmd, 'E474')
1000 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
1001 call assert_fails(cmd, 'E474')
1002 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
1003 call assert_fails(cmd, 'E474')
1004endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001005
1006func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001007 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001008
1009 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001010 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001011
Bram Moolenaar086eb872018-03-25 21:24:12 +02001012 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001013 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001014
Bram Moolenaar086eb872018-03-25 21:24:12 +02001015 " Request the cursor position.
1016 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001017
1018 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001019 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001020
Bram Moolenaar086eb872018-03-25 21:24:12 +02001021 " End "cat" gently.
1022 call term_sendkeys(buf, "\<CR>\<C-D>")
1023
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001024 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001025 exe buf . 'bwipe'
1026 unlet g:job
1027endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001028
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001029" Run Vim, start a terminal in that Vim with the kill argument,
1030" :qall works.
1031func Run_terminal_qall_kill(line1, line2)
1032 " 1. Open a terminal window and wait for the prompt to appear
1033 " 2. set kill using term_setkill()
1034 " 3. make Vim exit, it will kill the shell
1035 let after = [
1036 \ a:line1,
1037 \ 'let buf = bufnr("%")',
1038 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1039 \ ' sleep 10m',
1040 \ 'endwhile',
1041 \ a:line2,
1042 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1043 \ 'qall',
1044 \ ]
1045 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001046 return
1047 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001048 call assert_equal("done", readfile("Xdone")[0])
1049 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001050endfunc
1051
1052" Run Vim in a terminal, then start a terminal in that Vim with a kill
1053" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001054func Test_terminal_qall_kill_arg()
1055 call Run_terminal_qall_kill('term ++kill=kill', '')
1056endfunc
1057
1058" Run Vim, start a terminal in that Vim, set the kill argument with
1059" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001060func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001061 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001062endfunc
1063
1064" Run Vim, start a terminal in that Vim without the kill argument,
1065" check that :qall does not exit, :qall! does.
1066func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001067 let after =<< trim [CODE]
1068 term
1069 let buf = bufnr("%")
1070 while term_getline(buf, 1) =~ "^\\s*$"
1071 sleep 10m
1072 endwhile
1073 set nomore
1074 au VimLeavePre * call writefile(["too early"], "Xdone")
1075 qall
1076 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1077 cquit
1078 [CODE]
1079
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001080 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001081 return
1082 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001083 call assert_equal("done", readfile("Xdone")[0])
1084 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001085endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001086
1087" Run Vim in a terminal, then start a terminal in that Vim without a kill
1088" argument, check that :confirm qall works.
1089func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001090 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001091 let buf = RunVimInTerminal('', {})
1092
1093 " Open a terminal window and wait for the prompt to appear
1094 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001095 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1096 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001097
1098 " make Vim exit, it will prompt to kill the shell
1099 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001100 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001101 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001102 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001103
1104 " close the terminal window where Vim was running
1105 quit
1106endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001107
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001108" Run Vim in a terminal, then start a terminal window with a shell and check
1109" that Vim exits if it is closed.
1110func Test_terminal_exit()
1111 CheckRunVimInTerminal
1112
1113 let lines =<< trim END
1114 let winid = win_getid()
1115 help
1116 term
1117 let termid = win_getid()
1118 call win_gotoid(winid)
1119 close
1120 call win_gotoid(termid)
1121 END
1122 call writefile(lines, 'XtermExit')
1123 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1124 let job = term_getjob(buf)
1125 call WaitForAssert({-> assert_equal("run", job_status(job))})
1126
1127 " quit the shell, it will make Vim exit
1128 call term_sendkeys(buf, "exit\<CR>")
1129 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1130
1131 call delete('XtermExit')
1132endfunc
1133
Bram Moolenaar012eb662018-03-13 17:55:27 +01001134func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001135 augroup repro
1136 au!
1137 au TerminalOpen * let s:called += 1
1138 augroup END
1139
1140 let s:called = 0
1141
1142 " Open a terminal window with :terminal
1143 terminal
1144 call assert_equal(1, s:called)
1145 bwipe!
1146
1147 " Open a terminal window with term_start()
1148 call term_start(&shell)
1149 call assert_equal(2, s:called)
1150 bwipe!
1151
1152 " Open a hidden terminal buffer with :terminal
1153 terminal ++hidden
1154 call assert_equal(3, s:called)
1155 for buf in term_list()
1156 exe buf . "bwipe!"
1157 endfor
1158
1159 " Open a hidden terminal buffer with term_start()
1160 let buf = term_start(&shell, {'hidden': 1})
1161 call assert_equal(4, s:called)
1162 exe buf . "bwipe!"
1163
1164 unlet s:called
1165 au! repro
1166endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001167
1168func Check_dump01(off)
1169 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1170 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001171 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001172endfunc
1173
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001174func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001175 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001176 let save_enc = &encoding
1177 set encoding=utf-8
1178 call assert_equal(1, winnr('$'))
1179
1180 let text = " a\u0300 e\u0302 o\u0308"
1181 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001182 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001183 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001184 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001185 let dumpline = readfile('Xdump')[0]
1186 call assert_match('|à| |ê| |ö', dumpline)
1187
1188 call StopVimInTerminal(buf)
1189 call delete('Xcomposing')
1190 call delete('Xdump')
1191 let &encoding = save_enc
1192endfunc
1193
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001194" just testing basic functionality.
1195func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001196 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001197 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001198 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001199 call assert_equal(2, winnr('$'))
1200 call assert_equal(20, line('$'))
1201 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001202
1203 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001204 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001205 call assert_equal(buf, buf2)
1206 call assert_notequal('one two three four five', trim(getline(1)))
1207
1208 " Load the first dump again in the same window
1209 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1210 call assert_equal(buf, buf2)
1211 call Check_dump01(0)
1212
1213 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1214 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1215 new
1216 let closedbuf = winbufnr('')
1217 quit
1218 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1219
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001220 quit
1221endfunc
1222
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001223func Test_terminal_dumpload_dump()
1224 CheckRunVimInTerminal
1225
1226 let lines =<< trim END
1227 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1228 END
1229 call writefile(lines, 'XtermDumpload')
1230 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1231 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1232
1233 call StopVimInTerminal(buf)
1234 call delete('XtermDumpload')
1235endfunc
1236
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001237func Test_terminal_dumpdiff()
1238 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001239 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001240 call assert_equal(2, winnr('$'))
1241 call assert_equal(62, line('$'))
1242 call Check_dump01(0)
1243 call Check_dump01(42)
1244 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1245 quit
1246endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001247
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001248func Test_terminal_dumpdiff_swap()
1249 call assert_equal(1, winnr('$'))
1250 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1251 call assert_equal(2, winnr('$'))
1252 call assert_equal(62, line('$'))
1253 call assert_match('Test_popup_command_01.dump', getline(21))
1254 call assert_match('Test_popup_command_03.dump', getline(42))
1255 call assert_match('Undo', getline(3))
1256 call assert_match('three four five', getline(45))
1257
1258 normal s
1259 call assert_match('Test_popup_command_03.dump', getline(21))
1260 call assert_match('Test_popup_command_01.dump', getline(42))
1261 call assert_match('three four five', getline(3))
1262 call assert_match('Undo', getline(45))
1263 quit
1264endfunc
1265
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001266func Test_terminal_dumpdiff_options()
1267 set laststatus=0
1268 call assert_equal(1, winnr('$'))
1269 let height = winheight(0)
1270 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1271 call assert_equal(2, winnr('$'))
1272 call assert_equal(height, winheight(winnr()))
1273 call assert_equal(33, winwidth(winnr()))
1274 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1275 quit
1276
1277 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001278 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1279 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001280 call assert_equal(&columns, winwidth(0))
1281 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001282 call assert_equal('something else', bufname('%'))
1283 quit
1284
1285 call assert_equal(1, winnr('$'))
1286 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1287 call assert_equal(1, winnr('$'))
1288 bwipe
1289
1290 set laststatus&
1291endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001292
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001293func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001294 call assert_equal(1, winnr('$'))
1295
1296 " Use the title termcap entries to output the escape sequence.
1297 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001298 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001299 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001300 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001301 \ 'redraw',
1302 \ "set t_ts=",
1303 \ ], 'Xscript')
1304 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001305 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001306 call assert_equal('Xtextfile', expand('%:t'))
1307 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001308 return buf
1309endfunc
1310
1311func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001312 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001313 let buf = Api_drop_common('')
1314 call assert_equal(0, &bin)
1315 call assert_equal('', &fenc)
1316
1317 call StopVimInTerminal(buf)
1318 call delete('Xscript')
1319 bwipe Xtextfile
1320endfunc
1321
1322func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001323 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001324 let buf = Api_drop_common(',{"bin":1}')
1325 call assert_equal(1, &bin)
1326
1327 call StopVimInTerminal(buf)
1328 call delete('Xscript')
1329 bwipe Xtextfile
1330endfunc
1331
1332func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001333 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001334 let buf = Api_drop_common(',{"binary":1}')
1335 call assert_equal(1, &bin)
1336
1337 call StopVimInTerminal(buf)
1338 call delete('Xscript')
1339 bwipe Xtextfile
1340endfunc
1341
1342func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001343 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001344 set binary
1345 let buf = Api_drop_common(',{"nobin":1}')
1346 call assert_equal(0, &bin)
1347
1348 call StopVimInTerminal(buf)
1349 call delete('Xscript')
1350 bwipe Xtextfile
1351 set nobinary
1352endfunc
1353
1354func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001355 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001356 set binary
1357 let buf = Api_drop_common(',{"nobinary":1}')
1358 call assert_equal(0, &bin)
1359
1360 call StopVimInTerminal(buf)
1361 call delete('Xscript')
1362 bwipe Xtextfile
1363 set nobinary
1364endfunc
1365
1366func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001367 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001368 let buf = Api_drop_common(',{"ff":"dos"}')
1369 call assert_equal("dos", &ff)
1370
1371 call StopVimInTerminal(buf)
1372 call delete('Xscript')
1373 bwipe Xtextfile
1374endfunc
1375
1376func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001377 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001378 let buf = Api_drop_common(',{"fileformat":"dos"}')
1379 call assert_equal("dos", &ff)
1380
1381 call StopVimInTerminal(buf)
1382 call delete('Xscript')
1383 bwipe Xtextfile
1384endfunc
1385
1386func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001387 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001388 let buf = Api_drop_common(',{"enc":"utf-16"}')
1389 call assert_equal("utf-16", &fenc)
1390
1391 call StopVimInTerminal(buf)
1392 call delete('Xscript')
1393 bwipe Xtextfile
1394endfunc
1395
1396func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001397 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001398 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1399 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001400
1401 call StopVimInTerminal(buf)
1402 call delete('Xscript')
1403 bwipe Xtextfile
1404endfunc
1405
1406func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001407 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001408 let firstwinid = win_getid()
1409 split Xtextfile
1410 let textfile_winid = win_getid()
1411 call assert_equal(2, winnr('$'))
1412 call win_gotoid(firstwinid)
1413
1414 " Use the title termcap entries to output the escape sequence.
1415 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001416 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001417 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1418 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1419 \ 'redraw',
1420 \ "set t_ts=",
1421 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001422 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001423 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001424 call assert_equal(textfile_winid, win_getid())
1425
1426 call StopVimInTerminal(buf)
1427 call delete('Xscript')
1428 bwipe Xtextfile
1429endfunc
1430
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001431func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001432 let g:called_bufnum = a:bufnum
1433 let g:called_arg = a:arg
1434endfunc
1435
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001436func WriteApiCall(funcname)
1437 " Use the title termcap entries to output the escape sequence.
1438 call writefile([
1439 \ 'set title',
1440 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1441 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1442 \ 'redraw',
1443 \ "set t_ts=",
1444 \ ], 'Xscript')
1445endfunc
1446
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001447func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001448 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001449
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001450 unlet! g:called_bufnum
1451 unlet! g:called_arg
1452
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001453 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001454
1455 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001456 let buf = RunVimInTerminal('-S Xscript', {})
1457 call WaitFor({-> exists('g:called_bufnum')})
1458 call assert_equal(buf, g:called_bufnum)
1459 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001460 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001461
1462 unlet! g:called_bufnum
1463 unlet! g:called_arg
1464
1465 " Enable explicitly
1466 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1467 call WaitFor({-> exists('g:called_bufnum')})
1468 call assert_equal(buf, g:called_bufnum)
1469 call assert_equal(['hello', 123], g:called_arg)
1470 call StopVimInTerminal(buf)
1471
1472 unlet! g:called_bufnum
1473 unlet! g:called_arg
1474
1475 func! ApiCall_TryThis(bufnum, arg)
1476 let g:called_bufnum2 = a:bufnum
1477 let g:called_arg2 = a:arg
1478 endfunc
1479
1480 call WriteApiCall('ApiCall_TryThis')
1481
1482 " Use prefix match
1483 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1484 call WaitFor({-> exists('g:called_bufnum2')})
1485 call assert_equal(buf, g:called_bufnum2)
1486 call assert_equal(['hello', 123], g:called_arg2)
1487 call StopVimInTerminal(buf)
1488
1489 unlet! g:called_bufnum2
1490 unlet! g:called_arg2
1491
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001492 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001493 delfunction! ApiCall_TryThis
1494 unlet! g:called_bufnum2
1495 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001496endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001497
1498func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001499 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001500
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001501 func! TryThis(bufnum, arg)
1502 let g:called_bufnum3 = a:bufnum
1503 let g:called_arg3 = a:arg
1504 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001505
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001506 call WriteApiCall('TryThis')
1507
1508 unlet! g:called_bufnum3
1509 unlet! g:called_arg3
1510
1511 " Not permitted
1512 call ch_logfile('Xlog', 'w')
1513 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1514 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1515 call assert_false(exists('g:called_bufnum3'))
1516 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001517 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001518
1519 " No match
1520 call ch_logfile('Xlog', 'w')
1521 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1522 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1523 call assert_false(exists('g:called_bufnum3'))
1524 call assert_false(exists('g:called_arg3'))
1525 call StopVimInTerminal(buf)
1526
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001527 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001528 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001529 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001530 delfunction! TryThis
1531 unlet! g:called_bufnum3
1532 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001533endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001534
Bram Moolenaara997b452018-04-17 23:24:06 +02001535let s:caught_e937 = 0
1536
1537func Tapi_Delete(bufnum, arg)
1538 try
1539 execute 'bdelete!' a:bufnum
1540 catch /E937:/
1541 let s:caught_e937 = 1
1542 endtry
1543endfunc
1544
1545func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001546 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001547
1548 call WriteApiCall('Tapi_Delete')
1549 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001550 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001551
1552 call StopVimInTerminal(buf)
1553 call delete('Xscript')
1554 call ch_logfile('', '')
1555endfunc
1556
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001557func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001558 if !exists('*term_getansicolors')
1559 throw 'Skipped: term_getansicolors() not supported'
1560 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001561 let colors = [
1562 \ '#000000', '#e00000',
1563 \ '#00e000', '#e0e000',
1564 \ '#0000e0', '#e000e0',
1565 \ '#00e0e0', '#e0e0e0',
1566 \ '#808080', '#ff4040',
1567 \ '#40ff40', '#ffff40',
1568 \ '#4040ff', '#ff40ff',
1569 \ '#40ffff', '#ffffff',
1570 \]
1571
1572 let buf = Run_shell_in_terminal({})
1573 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001574 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001575 call term_wait(buf)
1576
1577 exe buf . 'bwipe'
1578endfunc
1579
1580let s:test_colors = [
1581 \ '#616e64', '#0d0a79',
1582 \ '#6d610d', '#0a7373',
1583 \ '#690d0a', '#6d696e',
1584 \ '#0d0a6f', '#616e0d',
1585 \ '#0a6479', '#6d0d0a',
1586 \ '#617373', '#0d0a69',
1587 \ '#6d690d', '#0a6e6f',
1588 \ '#610d0a', '#6e6479',
1589 \]
1590
1591func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001592 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001593 if !exists('*term_getansicolors')
1594 throw 'Skipped: term_getansicolors() not supported'
1595 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001596 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1597 let buf = Run_shell_in_terminal({})
1598 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001599 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001600 call term_wait(buf)
1601
1602 exe buf . 'bwipe'
1603 unlet g:terminal_ansi_colors
1604endfunc
1605
1606func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001607 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001608 if !exists('*term_getansicolors')
1609 throw 'Skipped: term_getansicolors() not supported'
1610 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001611 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1612 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1613 call assert_equal(s:test_colors, term_getansicolors(buf))
1614
1615 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001616 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001617
1618 let colors = [
1619 \ 'ivory', 'AliceBlue',
1620 \ 'grey67', 'dark goldenrod',
1621 \ 'SteelBlue3', 'PaleVioletRed4',
1622 \ 'MediumPurple2', 'yellow2',
1623 \ 'RosyBrown3', 'OrangeRed2',
1624 \ 'white smoke', 'navy blue',
1625 \ 'grey47', 'gray97',
1626 \ 'MistyRose2', 'DodgerBlue4',
1627 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001628 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001629
1630 let colors[4] = 'Invalid'
1631 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1632
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001633 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001634 call term_wait(buf)
1635 exe buf . 'bwipe'
1636endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001637
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001638func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001639 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001640
1641 " Use all the ANSI colors.
1642 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001643 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001644 \ 'hi Tblack ctermfg=0 ctermbg=8',
1645 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1646 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1647 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1648 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1649 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1650 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1651 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1652 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1653 \ 'hi Tred ctermfg=9 ctermbg=1',
1654 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1655 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1656 \ 'hi Tblue ctermfg=12 ctermbg=4',
1657 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1658 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1659 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001660 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1661 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1662 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001663 \ '',
1664 \ 'call matchadd("Tblack", "A")',
1665 \ 'call matchadd("Tdarkred", "B")',
1666 \ 'call matchadd("Tdarkgreen", "C")',
1667 \ 'call matchadd("Tbrown", "D")',
1668 \ 'call matchadd("Tdarkblue", "E")',
1669 \ 'call matchadd("Tdarkmagenta", "F")',
1670 \ 'call matchadd("Tdarkcyan", "G")',
1671 \ 'call matchadd("Tlightgrey", "H")',
1672 \ 'call matchadd("Tdarkgrey", "I")',
1673 \ 'call matchadd("Tred", "J")',
1674 \ 'call matchadd("Tgreen", "K")',
1675 \ 'call matchadd("Tyellow", "L")',
1676 \ 'call matchadd("Tblue", "M")',
1677 \ 'call matchadd("Tmagenta", "N")',
1678 \ 'call matchadd("Tcyan", "O")',
1679 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001680 \ 'call matchadd("TdarkredBold", "X")',
1681 \ 'call matchadd("TgreenBold", "Y")',
1682 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001683 \ 'redraw',
1684 \ ], 'Xcolorscript')
1685 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1686 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1687
1688 call term_sendkeys(buf, ":q\<CR>")
1689 call StopVimInTerminal(buf)
1690 call delete('Xcolorscript')
1691endfunc
1692
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001693func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001694 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001695 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001696 let text = []
1697 for n in range(10)
1698 call add(text, repeat(n, 50))
1699 endfor
1700 call writefile(text, 'Xwinsize')
1701 let buf = RunVimInTerminal('Xwinsize', {})
1702 let win = bufwinid(buf)
1703 call assert_equal([6, 40], term_getsize(buf))
1704 call assert_equal(6, winheight(win))
1705 call assert_equal(40, winwidth(win))
1706
1707 " resizing the window doesn't resize the terminal.
1708 resize 10
1709 vertical resize 60
1710 call assert_equal([6, 40], term_getsize(buf))
1711 call assert_equal(10, winheight(win))
1712 call assert_equal(60, winwidth(win))
1713
1714 call StopVimInTerminal(buf)
1715 call delete('Xwinsize')
1716
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001717 call assert_fails('set termwinsize=40', 'E474')
1718 call assert_fails('set termwinsize=10+40', 'E474')
1719 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001720
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001721 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001722endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001723
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001724func Test_terminal_termwinsize_option_zero()
1725 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001726 let buf = Run_shell_in_terminal({})
1727 let win = bufwinid(buf)
1728 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001729 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001730 call term_wait(buf)
1731 exe buf . 'bwipe'
1732
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001733 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001734 let buf = Run_shell_in_terminal({})
1735 let win = bufwinid(buf)
1736 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001737 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001738 call term_wait(buf)
1739 exe buf . 'bwipe'
1740
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001741 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001742 let buf = Run_shell_in_terminal({})
1743 let win = bufwinid(buf)
1744 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001745 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001746 call term_wait(buf)
1747 exe buf . 'bwipe'
1748
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001749 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001750endfunc
1751
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001752func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001753 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001754 vsplit
1755 let buf = Run_shell_in_terminal({})
1756 let win = bufwinid(buf)
1757 call assert_inrange(10, 1000, winheight(win))
1758 call assert_inrange(50, 1000, winwidth(win))
1759 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1760
1761 resize 15
1762 vertical resize 60
1763 redraw
1764 call assert_equal([15, 60], term_getsize(buf))
1765 call assert_equal(15, winheight(win))
1766 call assert_equal(60, winwidth(win))
1767
1768 resize 7
1769 vertical resize 30
1770 redraw
1771 call assert_equal([10, 50], term_getsize(buf))
1772 call assert_equal(7, winheight(win))
1773 call assert_equal(30, winwidth(win))
1774
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001775 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001776 call term_wait(buf)
1777 exe buf . 'bwipe'
1778
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001779 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001780 let buf = Run_shell_in_terminal({})
1781 let win = bufwinid(buf)
1782 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001783 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001784 call term_wait(buf)
1785 exe buf . 'bwipe'
1786
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001787 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001788endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001789
1790func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001791 " make three tabpages, terminal in the middle
1792 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001793 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001794 tabnew
1795 tabprev
1796 call assert_equal(1, winnr('$'))
1797 call assert_equal(2, tabpagenr())
1798 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001799
1800 let buf = Run_shell_in_terminal({})
1801 let termwin = bufwinid(buf)
1802 set termwinkey=<C-L>
1803 call feedkeys("\<C-L>w", 'tx')
1804 call assert_equal(thiswin, win_getid())
1805 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001806 call assert_equal(termwin, win_getid())
1807
Bram Moolenaar997d4242019-09-14 21:23:40 +02001808 if has('langmap')
1809 set langmap=xjyk
1810 call feedkeys("\<C-L>x", 'tx')
1811 call assert_equal(thiswin, win_getid())
1812 call feedkeys("\<C-W>y", 'tx')
1813 call assert_equal(termwin, win_getid())
1814 set langmap=
1815 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001816
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001817 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001818 call assert_equal(3, tabpagenr())
1819 tabprev
1820 call assert_equal(2, tabpagenr())
1821 call assert_equal(termwin, win_getid())
1822
1823 call feedkeys("\<C-L>gT", "xt")
1824 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001825 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001826 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001827 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001828
1829 let job = term_getjob(buf)
1830 call feedkeys("\<C-L>\<C-C>", 'tx')
1831 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001832
1833 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001834 tabnext
1835 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001836 tabprev
1837 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001838endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001839
1840func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001841 CheckUnix
1842
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001843 call writefile([
1844 \ '#!/bin/sh',
1845 \ 'echo "this is standard error" >&2',
1846 \ 'echo "this is standard out" >&1',
1847 \ ], 'Xechoerrout.sh')
1848 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1849
1850 let outfile = 'Xtermstdout'
1851 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001852
1853 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1854 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001855 call assert_equal('this is standard error', term_getline(buf, 1))
1856
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001857 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001858 exe buf . 'bwipe'
1859 call delete('Xechoerrout.sh')
1860 call delete(outfile)
1861endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001862
Bram Moolenaare219f732019-11-30 15:34:08 +01001863func Test_termwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001864 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001865
1866 " Let the terminal output more than 'termwinscroll' lines, some at the start
1867 " will be dropped.
1868 exe 'set termwinscroll=' . &lines
1869 let buf = term_start('/bin/sh')
1870 for i in range(1, &lines)
1871 call feedkeys("echo " . i . "\<CR>", 'xt')
1872 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1873 endfor
1874 " Go to Terminal-Normal mode to update the buffer.
1875 call feedkeys("\<C-W>N", 'xt')
1876 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1877
1878 " Every "echo nr" must only appear once
1879 let lines = getline(1, line('$'))
1880 for i in range(&lines - len(lines) / 2 + 2, &lines)
1881 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1882 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1883 endfor
1884
1885 exe buf . 'bwipe!'
1886endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001887
Bram Moolenaar875cf872018-07-08 20:49:07 +02001888" Resizing the terminal window caused an ml_get error.
1889" TODO: This does not reproduce the original problem.
1890func Test_terminal_resize()
1891 set statusline=x
1892 terminal
1893 call assert_equal(2, winnr('$'))
1894
1895 " Fill the terminal with text.
1896 if has('win32')
1897 call feedkeys("dir\<CR>", 'xt')
1898 else
1899 call feedkeys("ls\<CR>", 'xt')
1900 endif
1901 " Go to Terminal-Normal mode for a moment.
1902 call feedkeys("\<C-W>N", 'xt')
1903 " Open a new window
1904 call feedkeys("i\<C-W>n", 'xt')
1905 call assert_equal(3, winnr('$'))
1906 redraw
1907
1908 close
1909 call assert_equal(2, winnr('$'))
1910 call feedkeys("exit\<CR>", 'xt')
1911 set statusline&
1912endfunc
1913
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001914" must be nearly the last, we can't go back from GUI to terminal
1915func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001916 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001917
1918 " Ignore the "failed to create input context" error.
1919 call test_ignore_error('E285:')
1920
1921 gui -f
1922
1923 call assert_equal(1, winnr('$'))
1924 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001925 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001926 call term_wait(buf)
1927
1928 " closing window wipes out the terminal buffer a with finished job
1929 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1930 call assert_equal("", bufname(buf))
1931
1932 unlet g:job
1933endfunc
1934
1935func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001936 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001937 set guioptions+=!
1938
1939 let filename = 'Xtestscript'
1940 if has('win32')
1941 let filename .= '.bat'
1942 let prefix = ''
1943 let contents = ['@echo off', 'exit %1']
1944 else
1945 let filename .= '.sh'
1946 let prefix = './'
1947 let contents = ['#!/bin/sh', 'exit $1']
1948 endif
1949 call writefile(contents, filename)
1950 call setfperm(filename, 'rwxrwx---')
1951
1952 " Check if v:shell_error is equal to the exit status.
1953 let exitval = 0
1954 execute printf(':!%s%s %d', prefix, filename, exitval)
1955 call assert_equal(exitval, v:shell_error)
1956
1957 let exitval = 9
1958 execute printf(':!%s%s %d', prefix, filename, exitval)
1959 call assert_equal(exitval, v:shell_error)
1960
1961 set guioptions&
1962 call delete(filename)
1963endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001964
1965func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001966 CheckUnix
1967
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001968 term ++hidden cat
1969 let bnr = bufnr('$')
1970 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1971 exe 'sbuf ' . bnr
1972 call assert_equal('terminal', &buftype)
1973 call term_sendkeys(bnr, "asdf\<CR>")
1974 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1975 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001976 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001977 bwipe!
1978endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001979
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001980func Test_terminal_switch_mode()
1981 term
1982 let bnr = bufnr('$')
1983 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1984 call feedkeys("\<C-W>N", 'xt')
1985 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1986 call feedkeys("A", 'xt')
1987 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1988 call feedkeys("\<C-W>N", 'xt')
1989 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1990 call feedkeys("I", 'xt')
1991 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1992 call feedkeys("\<C-W>Nv", 'xt')
1993 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1994 call feedkeys("I", 'xt')
1995 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1996 call feedkeys("\<C-W>Nv", 'xt')
1997 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1998 call feedkeys("A", 'xt')
1999 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2000 bwipe!
2001endfunc
2002
Bram Moolenaara3817732019-10-16 16:31:44 +02002003func Test_terminal_normal_mode()
2004 CheckRunVimInTerminal
2005
2006 " Run Vim in a terminal and open a terminal window to run Vim in.
2007 let lines =<< trim END
2008 call setline(1, range(11111, 11122))
2009 3
2010 END
2011 call writefile(lines, 'XtermNormal')
2012 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
2013 call term_wait(buf)
2014
2015 call term_sendkeys(buf, "\<C-W>N")
2016 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
2017 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
2018
2019 call term_sendkeys(buf, ":set culopt=number\r")
2020 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
2021
2022 call term_sendkeys(buf, ":set culopt=line\r")
2023 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
2024
2025 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
2026 call StopVimInTerminal(buf)
2027 call delete('XtermNormal')
2028endfunc
2029
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002030func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002031 CheckUnix
2032
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002033 call assert_equal(1, winnr('$'))
2034 term ++hidden ++close ls
2035 let bnr = bufnr('$')
2036 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2037 call WaitForAssert({-> assert_false(bufexists(bnr))})
2038 call assert_equal(1, winnr('$'))
2039endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002040
2041func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002042 " This test does not pass through ConPTY.
2043 if has('conpty')
2044 return
2045 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002046 let contents = [
2047 \ [ 'One', '', 'X' ],
2048 \ [ 'Two', '', '' ],
2049 \ [ 'Three' ] + repeat([''], 30)
2050 \ ]
2051
2052 for c in contents
2053 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002054 if has('win32')
2055 term cmd /c type Xfile
2056 else
2057 term cat Xfile
2058 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002059 let bnr = bufnr('$')
2060 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2061 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002062 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002063 call assert_equal(c, getline(1, line('$')))
2064 quit
2065 endfor
2066
2067 call delete('Xfile')
2068endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002069
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002070func Test_terminal_no_job()
Bram Moolenaar077ff432019-10-28 00:42:21 +01002071 if has('win32')
2072 let cmd = 'cmd /c ""'
2073 else
2074 CheckExecutable false
2075 let cmd = 'false'
2076 endif
2077 let term = term_start(cmd, {'term_finish': 'close'})
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002078 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2079endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002080
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002081func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002082 CheckUnix
2083
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002084 let buf = Run_shell_in_terminal({})
2085
2086 " Wait for the shell to display a prompt.
2087 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2088
2089 " Hide the cursor.
2090 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2091 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2092
2093 " Show the cursor.
2094 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002095 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002096
2097 " Change color of cursor.
2098 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2099 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2100 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2101 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2102 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2103
2104 " Make cursor a blinking block.
2105 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2106 call WaitForAssert({-> assert_equal([1, 1],
2107 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2108
2109 " Make cursor a steady block.
2110 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2111 call WaitForAssert({-> assert_equal([0, 1],
2112 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2113
2114 " Make cursor a blinking underline.
2115 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2116 call WaitForAssert({-> assert_equal([1, 2],
2117 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2118
2119 " Make cursor a steady underline.
2120 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2121 call WaitForAssert({-> assert_equal([0, 2],
2122 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2123
2124 " Make cursor a blinking vertical bar.
2125 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2126 call WaitForAssert({-> assert_equal([1, 3],
2127 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2128
2129 " Make cursor a steady vertical bar.
2130 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2131 call WaitForAssert({-> assert_equal([0, 3],
2132 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2133
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002134 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002135endfunc
2136
Bram Moolenaar1e115362019-01-09 23:01:02 +01002137func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002138 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002139 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002140 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002141 call assert_equal('', term_gettitle(bufnr('$') + 1))
2142
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002143 if !has('title') || &title == 0 || empty(&t_ts)
2144 throw "Skipped: can't get/set title"
2145 endif
2146
Bram Moolenaar1e115362019-01-09 23:01:02 +01002147 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002148 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002149 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2150 call term_sendkeys(term, ":e Xfoo\r")
2151 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002152 else
2153 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002154 call term_sendkeys(term, ":e Xfoo\r")
2155 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002156 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002157
Bram Moolenaar1e115362019-01-09 23:01:02 +01002158 call term_sendkeys(term, ":set titlestring=foo\r")
2159 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2160
2161 exe term . 'bwipe!'
2162endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002163
2164" When drawing the statusline the cursor position may not have been updated
2165" yet.
2166" 1. create a terminal, make it show 2 lines
2167" 2. 0.5 sec later: leave terminal window, execute "i"
2168" 3. 0.5 sec later: clear terminal window, now it's 1 line
2169" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2170" 4. 0.5 sec later: should be done, clean up
2171func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002172 CheckUnix
2173
Bram Moolenaar10772302019-01-20 18:25:54 +01002174 set statusline=x
2175 terminal
2176 let tbuf = bufnr('')
2177 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2178 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2179 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2180 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2181
2182 sleep 2
2183 exe tbuf . 'bwipe!'
2184 au! BufLeave
2185 set statusline=
2186endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002187
2188func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002189 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002190
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002191 " split, go to the bottom-right window
2192 split
2193 wincmd j
2194 set splitright
2195
2196 call writefile([
2197 \ 'echo getwinpos()',
2198 \ ], 'XTest_getwinpos')
2199 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2200 call term_wait(buf)
2201
2202 " Find the output of getwinpos() in the bottom line.
2203 let rows = term_getsize(buf)[0]
2204 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2205 let line = term_getline(buf, rows)
2206 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2207 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2208
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002209 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002210 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002211 " In the GUI it can be more, let's assume a 20 x 14 cell.
2212 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002213 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002214 let winpos = 50->getwinpos()
2215 call assert_equal(xroot, winpos[0])
2216 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002217 let [winrow, wincol] = win_screenpos('.')
2218 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2219 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2220 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2221 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002222
2223 call term_wait(buf)
2224 call term_sendkeys(buf, ":q\<CR>")
2225 call StopVimInTerminal(buf)
2226 call delete('XTest_getwinpos')
2227 exe buf . 'bwipe!'
2228 set splitright&
2229 only!
2230endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002231
2232func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002233 " somehow doesn't work on MS-Windows
2234 CheckUnix
2235 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002236
2237 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002238 call writefile(["\<Esc>[?1047h"], 'Xtext')
2239 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002240 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2241
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002242 call writefile(["\<Esc>[?1047l"], 'Xtext')
2243 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002244 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002245
2246 call term_sendkeys(buf, "exit\r")
2247 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002248 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002249endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002250
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002251func Test_terminal_shell_option()
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002252 if has('unix')
2253 " exec is a shell builtin command, should fail without a shell.
2254 term exec ls runtest.vim
2255 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
2256 bwipe!
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002257
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002258 term ++shell exec ls runtest.vim
2259 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2260 bwipe!
2261 elseif has('win32')
2262 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar36ec6f62019-11-05 22:38:47 +01002263 try
2264 term dir /b runtest.vim
2265 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
2266 catch /CreateProcess/
2267 " ignore
2268 endtry
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002269 bwipe!
2270
2271 term ++shell dir /b runtest.vim
2272 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2273 bwipe!
2274 endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002275endfunc
2276
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002277func Test_terminal_setapi_and_call()
Bram Moolenaar21109272020-01-30 16:27:20 +01002278 CheckRunVimInTerminal
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002279
2280 call WriteApiCall('Tapi_TryThis')
2281 call ch_logfile('Xlog', 'w')
2282
2283 unlet! g:called_bufnum
2284 unlet! g:called_arg
2285
Bram Moolenaar21109272020-01-30 16:27:20 +01002286 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002287 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2288 call assert_false(exists('g:called_bufnum'))
2289 call assert_false(exists('g:called_arg'))
2290
Bram Moolenaar21109272020-01-30 16:27:20 +01002291 eval buf->term_setapi('Tapi_')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002292 call term_sendkeys(buf, ":set notitle\<CR>")
2293 call term_sendkeys(buf, ":source Xscript\<CR>")
2294 call WaitFor({-> exists('g:called_bufnum')})
2295 call assert_equal(buf, g:called_bufnum)
2296 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar21109272020-01-30 16:27:20 +01002297
2298 call StopVimInTerminal(buf)
2299
2300 call delete('Xscript')
2301 call ch_logfile('')
2302 call delete('Xlog')
2303 unlet! g:called_bufnum
2304 unlet! g:called_arg
2305endfunc
2306
2307func Test_terminal_api_arg()
2308 CheckRunVimInTerminal
2309
2310 call WriteApiCall('Tapi_TryThis')
2311 call ch_logfile('Xlog', 'w')
2312
2313 unlet! g:called_bufnum
2314 unlet! g:called_arg
2315
2316 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2317 let buf = bufnr('%')
2318 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2319 call assert_false(exists('g:called_bufnum'))
2320 call assert_false(exists('g:called_arg'))
2321
2322 call StopVimInTerminal(buf)
2323
2324 call ch_logfile('Xlog', 'w')
2325
2326 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2327 let buf = bufnr('%')
2328 call WaitFor({-> exists('g:called_bufnum')})
2329 call assert_equal(buf, g:called_bufnum)
2330 call assert_equal(['hello', 123], g:called_arg)
2331
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002332 call StopVimInTerminal(buf)
2333
2334 call delete('Xscript')
2335 call ch_logfile('')
2336 call delete('Xlog')
2337 unlet! g:called_bufnum
2338 unlet! g:called_arg
2339endfunc
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002340
Bram Moolenaar9b9be002020-03-22 14:41:22 +01002341func Test_terminal_invalid_arg()
2342 call assert_fails('terminal ++xyz', 'E181:')
2343endfunc
2344
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002345func Test_terminal_in_popup()
2346 CheckRunVimInTerminal
2347
2348 let text =<< trim END
2349 some text
2350 to edit
2351 in a popup window
2352 END
2353 call writefile(text, 'Xtext')
Bram Moolenaar3180fe62020-02-02 13:47:06 +01002354 let cmd = GetVimCommandCleanTerm()
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002355 let lines = [
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01002356 \ 'set t_u7=',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002357 \ 'call setline(1, range(20))',
2358 \ 'hi PopTerm ctermbg=grey',
2359 \ 'func OpenTerm(setColor)',
Bram Moolenaar353c3512020-03-15 14:19:26 +01002360 \ " set noruler",
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002361 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
Bram Moolenaar57c33952020-02-29 16:09:16 +01002362 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002363 \ ' if a:setColor',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002364 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002365 \ ' endif',
2366 \ 'endfunc',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002367 \ 'func HidePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002368 \ ' call popup_hide(g:winid)',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002369 \ 'endfunc',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002370 \ 'func ClosePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002371 \ ' call popup_close(g:winid)',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002372 \ 'endfunc',
2373 \ 'func ReopenPopup()',
2374 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
2375 \ 'endfunc',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002376 \ ]
2377 call writefile(lines, 'XtermPopup')
2378 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002379 call term_wait(buf, 200)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002380 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002381 call term_wait(buf, 200)
Bram Moolenaarc2adc002020-02-14 17:05:18 +01002382 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002383 call term_wait(buf, 200)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002384 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002385 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
2386
2387 call term_sendkeys(buf, ":q\<CR>")
2388 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
2389
2390 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002391 call term_wait(buf, 300)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002392 call term_sendkeys(buf, ":set hlsearch\<CR>")
2393 call term_sendkeys(buf, "/edit\<CR>")
2394 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
2395
Bram Moolenaar1af5ce02020-02-06 11:54:35 +01002396 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
2397 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
2398 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar67021882020-02-07 22:20:53 +01002399 call term_wait(buf, 100)
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002400
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002401 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
2402 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
2403
2404 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
2405 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002406
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002407 " Go to terminal-Normal mode and visually select text.
2408 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
2409 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
2410
2411 " Back to job mode, redraws
2412 call term_sendkeys(buf, "A")
2413 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
2414
2415 call term_wait(buf, 100)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002416 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002417 call term_wait(buf, 200) " wait for terminal to vanish
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002418
2419 call StopVimInTerminal(buf)
Bram Moolenaar19398262020-03-14 15:28:08 +01002420 call delete('Xtext')
2421 call delete('XtermPopup')
2422endfunc
2423
2424" Check a terminal in popup window uses the default mininum size.
2425func Test_terminal_in_popup_min_size()
2426 CheckRunVimInTerminal
2427
2428 let text =<< trim END
2429 another text
2430 to show
2431 in a popup window
2432 END
2433 call writefile(text, 'Xtext')
2434 let lines = [
2435 \ 'set t_u7=',
2436 \ 'call setline(1, range(20))',
2437 \ 'hi PopTerm ctermbg=grey',
2438 \ 'func OpenTerm()',
2439 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
2440 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2441 \ 'endfunc',
2442 \ ]
2443 call writefile(lines, 'XtermPopup')
2444 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002445 call term_wait(buf, 200)
2446 call term_sendkeys(buf, ":set noruler\<CR>")
Bram Moolenaar19398262020-03-14 15:28:08 +01002447 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
2448 call term_wait(buf, 100)
2449 call term_sendkeys(buf, ":\<CR>")
2450 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
2451
2452 call term_wait(buf, 100)
2453 call term_sendkeys(buf, ":q\<CR>")
2454 call term_wait(buf, 100) " wait for terminal to vanish
2455 call StopVimInTerminal(buf)
2456 call delete('Xtext')
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002457 call delete('XtermPopup')
2458endfunc
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002459
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002460func Test_double_popup_terminal()
2461 let buf1 = term_start(&shell, #{hidden: 1})
2462 let win1 = popup_create(buf1, {})
2463 let buf2 = term_start(&shell, #{hidden: 1})
2464 let win2 = popup_create(buf2, {})
2465 call popup_close(win1)
2466 call popup_close(win2)
2467 exe buf1 .. 'bwipe!'
2468 exe buf2 .. 'bwipe!'
2469endfunc
2470
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002471func Test_issue_5607()
2472 let wincount = winnr('$')
2473 exe 'terminal' &shell &shellcmdflag 'exit'
2474 let job = term_getjob(bufnr())
2475 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2476
2477 let old_wincolor = &wincolor
2478 try
2479 set wincolor=
2480 finally
2481 let &wincolor = old_wincolor
2482 bw!
2483 endtry
2484endfunc
Bram Moolenaare010c722020-02-24 21:37:54 +01002485
2486func Test_hidden_terminal()
2487 let buf = term_start(&shell, #{hidden: 1})
2488 call assert_equal('', bufname('^$'))
2489 call StopShellInTerminal(buf)
2490endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002491
2492func Test_term_nasty_callback()
2493 func OpenTerms()
2494 set hidden
2495 let g:buf0 = term_start('sh', #{hidden: 1})
2496 call popup_create(g:buf0, {})
2497 let g:buf1 = term_start('sh', #{hidden: 1, term_finish: 'close'})
2498 call popup_create(g:buf1, {})
2499 let g:buf2 = term_start(['sh', '-c'], #{curwin: 1, exit_cb: function('TermExit')})
2500 sleep 100m
2501 call popup_close(win_getid())
2502 endfunc
2503 func TermExit(...)
2504 call term_sendkeys(bufnr('#'), "exit\<CR>")
2505 call popup_close(win_getid())
Bram Moolenaarab505b12020-03-23 19:28:44 +01002506 endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002507 call OpenTerms()
2508
2509 call term_sendkeys(g:buf0, "exit\<CR>")
Bram Moolenaarfa5d8a12020-03-13 14:55:23 +01002510 sleep 100m
Bram Moolenaar24ebd832020-03-16 21:25:24 +01002511 exe g:buf0 .. 'bwipe!'
Bram Moolenaarcee52202020-03-11 14:19:58 +01002512 set hidden&
2513endfunc