blob: e99ab7f5a9ab68fabaa10ead1ed4a685baa71023 [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 Moolenaarc6df10e2017-07-29 20:15:08 +020020
21 let termlist = term_list()
22 call assert_equal(1, len(termlist))
23 call assert_equal(buf, termlist[0])
24
25 let g:job = term_getjob(buf)
26 call assert_equal(v:t_job, type(g:job))
27
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020028 let string = string({'job': buf->term_getjob()})
Bram Moolenaar35422f42017-08-05 16:33:56 +020029 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020035 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010042 " ConPTY works on anonymous pipe.
43 if !has('conpty')
44 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020045 call assert_match('^\\\\.\\pipe\\', ''->term_gettty())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010046 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020047 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020048 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020049 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020050 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020051 call assert_match('%aR[^\n]*running]', execute('ls R'))
52 call assert_notmatch('%[^\n]*running]', execute('ls F'))
53 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020055 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +020056 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020057 call assert_equal('n', mode())
58 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020059 call assert_match('%aF[^\n]*finished]', execute('ls F'))
60 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020062
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 " closing window wipes out the terminal buffer a with finished job
64 close
65 call assert_equal("", bufname(buf))
66
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020067 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020068 unlet g:job
69endfunc
70
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020071func Test_terminal_TerminalWinOpen()
72 au TerminalWinOpen * let b:done = 'yes'
73 let buf = Run_shell_in_terminal({})
74 call assert_equal('yes', b:done)
75 call StopShellInTerminal(buf)
76 " closing window wipes out the terminal buffer with the finished job
77 close
78
79 if has("unix")
80 terminal ++hidden ++open sleep 1
81 sleep 1
82 call assert_fails("echo b:done", 'E121:')
83 endif
84
85 au! TerminalWinOpen
86endfunc
87
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020088func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020089 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020090 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020091 call term_wait(buf)
92
93 setlocal modifiable
94 exe "normal Axxx\<Esc>"
95 call assert_fails(buf . 'bwipe', 'E517')
96 undo
97
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020098 exe buf . 'bwipe'
99 unlet g:job
100endfunc
101
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100102func Test_terminal_paste_register()
103 let @" = "text to paste"
104
105 let buf = Run_shell_in_terminal({})
106 " Wait for the shell to display a prompt
107 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
108
109 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
110 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200111 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100112
113 exe buf . 'bwipe!'
114 unlet g:job
115endfunc
116
Bram Moolenaar94053a52017-08-01 21:44:33 +0200117func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200118 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200119 call assert_fails(buf . 'bwipe', 'E517')
120 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200121 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200122 call assert_equal("", bufname(buf))
123
124 unlet g:job
125endfunc
126
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200127func Test_terminal_split_quit()
128 let buf = Run_shell_in_terminal({})
129 call term_wait(buf)
130 split
131 quit!
132 call term_wait(buf)
133 sleep 50m
134 call assert_equal('run', job_status(g:job))
135
136 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200137 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200138
139 exe buf . 'bwipe'
140 unlet g:job
141endfunc
142
Bram Moolenaar94053a52017-08-01 21:44:33 +0200143func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200144 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200145 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200146 quit
147 for nr in range(1, winnr('$'))
148 call assert_notequal(winbufnr(nr), buf)
149 endfor
150 call assert_true(bufloaded(buf))
151 call assert_true(buflisted(buf))
152
153 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200154 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200155 exe buf . 'bwipe'
156
157 unlet g:job
158endfunc
159
Bram Moolenaar1e115362019-01-09 23:01:02 +0100160func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200161 exe g:buf . 'bwipe!'
162 let g:buf = 0
163endfunc
164
Bram Moolenaar9d189612017-09-09 18:11:00 +0200165func Get_cat_123_cmd()
166 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100167 if !has('conpty')
168 return 'cmd /c "cls && color 2 && echo 123"'
169 else
170 " When clearing twice, extra sequence is not output.
171 return 'cmd /c "cls && cls && color 2 && echo 123"'
172 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200173 else
174 call writefile(["\<Esc>[32m123"], 'Xtext')
175 return "cat Xtext"
176 endif
177endfunc
178
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200179func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200180 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200181 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
182 let g:job = term_getjob(g:buf)
183
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200184 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
185 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200186 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100187 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200188 call delete('Xtext')
189endfunc
190
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200191func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200192 let l = term_scrape(a:buf, 0)
193 call assert_true(len(l) == 0)
194 let l = term_scrape(a:buf, 999)
195 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200196 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200197 call assert_true(len(l) > 0)
198 call assert_equal('1', l[0].chars)
199 call assert_equal('2', l[1].chars)
200 call assert_equal('3', l[2].chars)
201 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200202 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
203 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100204 if has('win32')
205 " On Windows 'background' always defaults to dark, even though the terminal
206 " may use a light background. Therefore accept both white and black.
207 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200208 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100209 if &background == 'light'
210 call assert_equal('#ffffff', l[0].bg)
211 else
212 call assert_equal('#000000', l[0].bg)
213 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200214 endif
215
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200216 let l = term_getline(a:buf, -1)
217 call assert_equal('', l)
218 let l = term_getline(a:buf, 0)
219 call assert_equal('', l)
220 let l = term_getline(a:buf, 999)
221 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200222 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200223 call assert_equal('123', l)
224endfunc
225
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200226func Test_terminal_scrape_123()
227 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200228 let buf = term_start(cmd)
229
230 let termlist = term_list()
231 call assert_equal(1, len(termlist))
232 call assert_equal(buf, termlist[0])
233
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200234 " Nothing happens with invalid buffer number
235 call term_wait(1234)
236
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200237 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200238 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200239 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200240 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200241 call Check_123(buf)
242
243 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100244 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200245 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200246 call term_wait(buf)
247 call Check_123(buf)
248
249 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200250 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200251endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200252
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200253func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200254 call writefile(["léttrs"], 'Xtext')
255 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200256 " Run cmd with UTF-8 codepage to make the type command print the expected
257 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100258 let buf = term_start("cmd /K chcp 65001")
259 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200260 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100261 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200262 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let buf = term_start("cat Xtext")
264 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200265 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100267 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
268 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200269 call assert_true(len(l) >= 7)
270 call assert_equal('l', l[0].chars)
271 call assert_equal('é', l[1].chars)
272 call assert_equal(1, l[1].width)
273 call assert_equal('t', l[2].chars)
274 call assert_equal('t', l[3].chars)
275 call assert_equal('ま', l[4].chars)
276 call assert_equal(2, l[4].width)
277 call assert_equal('r', l[5].chars)
278 call assert_equal('s', l[6].chars)
279
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100280 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200281 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100282 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200283
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100284 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200285 call delete('Xtext')
286endfunc
287
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200288func Test_terminal_scroll()
289 call writefile(range(1, 200), 'Xtext')
290 if has('win32')
291 let cmd = 'cmd /c "type Xtext"'
292 else
293 let cmd = "cat Xtext"
294 endif
295 let buf = term_start(cmd)
296
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100297 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200298 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200299 call term_wait(buf)
300 if has('win32')
301 " TODO: this should not be needed
302 sleep 100m
303 endif
304
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200305 let scrolled = buf->term_getscrolled()
306 call assert_equal(scrolled, term_getscrolled(buf))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200307 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200308 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200309 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200310 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200311 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200312 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200313
314 exe buf . 'bwipe'
315 call delete('Xtext')
316endfunc
317
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200318func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200319 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200320 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200321 call writefile(range(150), 'Xtext')
322 if has('win32')
323 call term_sendkeys(buf, "type Xtext\<CR>")
324 else
325 call term_sendkeys(buf, "cat Xtext\<CR>")
326 endif
327 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200328 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200329 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200330 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200331 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200332
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200333 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200334 call term_wait(buf)
335 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200336 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100337 call delete('Xtext')
338endfunc
339
340func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200341 " tail -f only works on Unix
342 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100343
344 call writefile(range(50), 'Xtext')
345 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100346 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100347 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100348 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100349 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
350 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100351 \ 'call feedkeys("\<C-W>N", "xt")',
352 \ ], 'XTest_postponed')
353 let buf = RunVimInTerminal('-S XTest_postponed', {})
354 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100355 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100356
357 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200358 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100359 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100360 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100361
362 " Back to Terminal-Job mode, text will scroll and show the extra line.
363 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100364 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100365
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100366 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100367 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100368 call term_wait(buf, 50)
369 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100370 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100371 call term_wait(buf, 100)
372 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100373 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
374
375 " check type of term_sendkeys() return value
376 echo type(tsk_ret)
377
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100378 call StopVimInTerminal(buf)
379 call delete('XTest_postponed')
380 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200381endfunc
382
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100383" Run diff on two dumps with different size.
384func Test_terminal_dumpdiff_size()
385 call assert_equal(1, winnr('$'))
386 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
387 call assert_equal(2, winnr('$'))
388 call assert_match('Test_incsearch_search_01.dump', getline(10))
389 call assert_match(' +++++$', getline(11))
390 call assert_match('Test_popup_command_01.dump', getline(31))
391 call assert_equal(repeat('+', 75), getline(30))
392 quit
393endfunc
394
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200395func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200396 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200397
Bram Moolenaarb2412082017-08-20 18:09:14 +0200398 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200399 let size = term_getsize('')
400 bwipe!
401 call assert_equal(5, size[0])
402
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200403 call term_start(cmd, {'term_rows': 6})
404 let size = term_getsize('')
405 bwipe!
406 call assert_equal(6, size[0])
407
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200408 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200409 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200410 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200411
412 call term_setsize('', 6, 0)
413 call assert_equal([6, 33], term_getsize(''))
414
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200415 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200416 call assert_equal([6, 35], term_getsize(''))
417
418 call term_setsize('', 7, 30)
419 call assert_equal([7, 30], term_getsize(''))
420
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200421 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200422 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200423
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200424 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
425 let size = term_getsize('')
426 bwipe!
427 call assert_equal([6, 36], size)
428
Bram Moolenaarb2412082017-08-20 18:09:14 +0200429 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200430 let size = term_getsize('')
431 bwipe!
432 call assert_equal(20, size[1])
433
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200434 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200435 let size = term_getsize('')
436 bwipe!
437 call assert_equal(26, size[1])
438
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200439 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200440 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200441 let size = term_getsize('')
442 bwipe!
443 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200444
445 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
446 let size = term_getsize('')
447 bwipe!
448 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200449
450 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200451endfunc
452
453func Test_terminal_curwin()
454 let cmd = Get_cat_123_cmd()
455 call assert_equal(1, winnr('$'))
456
457 split dummy
458 exe 'terminal ++curwin ' . cmd
459 call assert_equal(2, winnr('$'))
460 bwipe!
461
462 split dummy
463 call term_start(cmd, {'curwin': 1})
464 call assert_equal(2, winnr('$'))
465 bwipe!
466
467 split dummy
468 call setline(1, 'change')
469 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
470 call assert_equal(2, winnr('$'))
471 exe 'terminal! ++curwin ' . cmd
472 call assert_equal(2, winnr('$'))
473 bwipe!
474
475 split dummy
476 call setline(1, 'change')
477 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
478 call assert_equal(2, winnr('$'))
479 bwipe!
480
481 split dummy
482 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200483 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200484endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200485
Bram Moolenaarff546792017-11-21 14:47:57 +0100486func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200487 if s:python != ''
488 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200489 " 500 was not enough for Travis
490 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200491 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200492 echo 'This will take five seconds...'
493 let waittime = 2000
494 if has('win32')
495 let cmd = $windir . '\system32\timeout.exe 1'
496 else
497 let cmd = 'sleep 1'
498 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200499 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100500 return [cmd, waittime]
501endfunc
502
503func Test_terminal_finish_open_close()
504 call assert_equal(1, winnr('$'))
505
506 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200507
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100508 " shell terminal closes automatically
509 terminal
510 let buf = bufnr('%')
511 call assert_equal(2, winnr('$'))
512 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200513 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200514 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200515 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100516
517 " shell terminal that does not close automatically
518 terminal ++noclose
519 let buf = bufnr('%')
520 call assert_equal(2, winnr('$'))
521 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200522 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200523 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100524 call assert_equal(2, winnr('$'))
525 quit
526 call assert_equal(1, winnr('$'))
527
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200528 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200529 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200530 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200531 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200532
533 call term_start(cmd, {'term_finish': 'close'})
534 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200535 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200536 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200537 call assert_equal(1, winnr('$'))
538
539 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200540 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200541 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200542 bwipe
543
544 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200545 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200546 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200547 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200548
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200549 exe 'terminal ++hidden ++open ' . cmd
550 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200551 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200552 bwipe
553
554 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
555 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200556 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200557 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200558
559 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
560 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
561 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
562 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
563
564 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200565 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200566 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200567 call assert_equal(4, winheight(0))
568 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200569endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200570
571func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100572 if has('win32')
573 let cmd = 'cmd /c cd'
574 else
575 CheckExecutable pwd
576 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200577 endif
578 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100579 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200580 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200581
582 exe buf . 'bwipe'
583 call delete('Xdir', 'rf')
584endfunc
585
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200586func Test_terminal_cwd_failure()
587 " Case 1: Provided directory is not actually a directory. Attempt to make
588 " the file executable as well.
589 call writefile([], 'Xfile')
590 call setfperm('Xfile', 'rwx------')
591 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
592 call delete('Xfile')
593
594 " Case 2: Directory does not exist.
595 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
596
597 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100598 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200599 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100600 call mkdir('XdirNoAccess', '', '0600')
601 " return early if the directory permissions could not be set properly
602 if getfperm('XdirNoAccess')[2] == 'x'
603 call delete('XdirNoAccess', 'rf')
604 return
605 endif
606 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
607 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200608 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200609endfunc
610
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100611func Test_terminal_servername()
612 if !has('clientserver')
613 return
614 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200615 call s:test_environment("VIM_SERVERNAME", v:servername)
616endfunc
617
618func Test_terminal_version()
619 call s:test_environment("VIM_TERMINAL", string(v:version))
620endfunc
621
622func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100623 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100624 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200625 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100626 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200627 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100628 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200629 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100630 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100631 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200632 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200633 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100634
Bram Moolenaar012eb662018-03-13 17:55:27 +0100635 exe buf . 'bwipe'
636 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100637endfunc
638
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200639func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100640 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200641 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200642 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100643 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100644 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100645 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100646 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100647 endif
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200648 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200649 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200650 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200651
Bram Moolenaar012eb662018-03-13 17:55:27 +0100652 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200653endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200654
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200655func Test_terminal_list_args()
656 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
657 call assert_fails(buf . 'bwipe', 'E517')
658 exe buf . 'bwipe!'
659 call assert_equal("", bufname(buf))
660endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200661
662func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100663 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100664 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200665 " The shell or something else has a problem dealing with more than 1000
666 " characters at the same time.
667 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100668 " NPFS is used in Windows, nonblocking mode does not work properly.
669 elseif has('win32')
670 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200671 else
672 let len = 5000
673 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200674
675 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100676 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200677 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100678 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200679
680 " On MS-Windows there is an extra empty line below "done". Find "done" in
681 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100682 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100683 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100684 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200685 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100686 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200687 endif
688 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200689
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100690 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200691 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100692 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200693 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200694 bwipe
695endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200696
697func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100698 " TODO: enable once writing to stdin works on MS-Windows
699 CheckNotMSWindows
700 CheckExecutable wc
701
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200702 call setline(1, ['one', 'two', 'three'])
703 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200704 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200705 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200706 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100707 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200708
709 call setline(1, ['one', 'two', 'three', 'four'])
710 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200711 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200712 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200713 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100714 %bwipe!
715endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200716
Bram Moolenaar21109272020-01-30 16:27:20 +0100717func Test_terminal_eof_arg()
718 CheckExecutable python
Bram Moolenaardada6d22017-09-02 17:18:35 +0200719
Bram Moolenaar21109272020-01-30 16:27:20 +0100720 call setline(1, ['print("hello")'])
721 1term ++eof=exit(123) python
722 " MS-Windows echoes the input, Unix doesn't.
723 if has('win32')
724 call WaitFor({-> getline('$') =~ 'exit(123)'})
725 call assert_equal('hello', getline(line('$') - 1))
726 else
727 call WaitFor({-> getline('$') =~ 'hello'})
728 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200729 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100730 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
731 %bwipe!
732endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200733
Bram Moolenaar21109272020-01-30 16:27:20 +0100734func Test_terminal_eof_arg_win32_ctrl_z()
735 CheckMSWindows
736 CheckExecutable python
737
738 call setline(1, ['print("hello")'])
739 1term ++eof=<C-Z> python
740 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
741 call assert_match('\^Z', getline(line('$') - 1))
742 %bwipe!
743endfunc
744
745func Test_terminal_duplicate_eof_arg()
746 CheckExecutable python
747
748 " Check the last specified ++eof arg is used and should not memory leak.
749 new
750 call setline(1, ['print("hello")'])
751 1term ++eof=<C-Z> ++eof=exit(123) python
752 " MS-Windows echoes the input, Unix doesn't.
753 if has('win32')
754 call WaitFor({-> getline('$') =~ 'exit(123)'})
755 call assert_equal('hello', getline(line('$') - 1))
756 else
757 call WaitFor({-> getline('$') =~ 'hello'})
758 call assert_equal('hello', getline('$'))
759 endif
760 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
761 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200762endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200763
764func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200765 let buf = term_start('NONE', {})
766 call assert_notequal(0, buf)
767
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200768 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200769 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100770 if has('gui_running') && !has('win32')
771 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200772 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100773 else
774 " Otherwise using a job works on all systems.
775 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200776 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200777 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200778
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200779 bwipe!
780endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200781
782func Test_terminal_special_chars()
783 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200784 CheckUnix
785
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200786 call mkdir('Xdir with spaces')
787 call writefile(['x'], 'Xdir with spaces/quoted"file')
788 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200789 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100790 " make sure the job has finished
791 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200792
793 call delete('Xdir with spaces', 'rf')
794 bwipe
795endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200796
797func Test_terminal_wrong_options()
798 call assert_fails('call term_start(&shell, {
799 \ "in_io": "file",
800 \ "in_name": "xxx",
801 \ "out_io": "file",
802 \ "out_name": "xxx",
803 \ "err_io": "file",
804 \ "err_name": "xxx"
805 \ })', 'E474:')
806 call assert_fails('call term_start(&shell, {
807 \ "out_buf": bufnr("%")
808 \ })', 'E474:')
809 call assert_fails('call term_start(&shell, {
810 \ "err_buf": bufnr("%")
811 \ })', 'E474:')
812endfunc
813
814func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200815 let cmd = Get_cat_123_cmd()
816 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
817 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100818 " ConPTY may precede escape sequence. There are things that are not so.
819 if !has('conpty')
820 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
821 call assert_match('123', readfile('Xfile')[0])
822 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200823 let g:job = term_getjob(buf)
824 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
825 call delete('Xfile')
826 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200827
828 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200829 call writefile(['one line'], 'Xfile')
830 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
831 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200832 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200833 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200834 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200835 bwipe
836 call delete('Xfile')
837 endif
838endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200839
840func TerminalTmap(remap)
841 let buf = Run_shell_in_terminal({})
842 call assert_equal('t', mode())
843
844 if a:remap
845 tmap 123 456
846 else
847 tnoremap 123 456
848 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100849 " don't use abcde, it's an existing command
850 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200851 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100852 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200853 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200854 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200855 let lnum = term_getcursor(buf)[0]
856 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100857 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200858 else
859 call assert_match('456', term_getline(buf, lnum))
860 endif
861
862 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200863 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200864 call term_wait(buf)
865
866 tunmap 123
867 tunmap 456
868 call assert_equal('', maparg('123', 't'))
869 close
870 unlet g:job
871endfunc
872
873func Test_terminal_tmap()
874 call TerminalTmap(1)
875 call TerminalTmap(0)
876endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200877
878func Test_terminal_wall()
879 let buf = Run_shell_in_terminal({})
880 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200881 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200882 call term_wait(buf)
883 exe buf . 'bwipe'
884 unlet g:job
885endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200886
Bram Moolenaar7a760922018-02-19 23:10:02 +0100887func Test_terminal_wqall()
888 let buf = Run_shell_in_terminal({})
889 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200890 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100891 call term_wait(buf)
892 exe buf . 'bwipe'
893 unlet g:job
894endfunc
895
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200896func Test_terminal_composing_unicode()
Bram Moolenaar9134f1e2019-11-29 20:26:13 +0100897 CheckNotBSD
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200898 let save_enc = &encoding
899 set encoding=utf-8
900
901 if has('win32')
902 let cmd = "cmd /K chcp 65001"
903 let lnum = [3, 6, 9]
904 else
905 let cmd = &shell
906 let lnum = [1, 3, 5]
907 endif
908
909 enew
910 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100911 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200912 call term_wait(buf, 50)
913
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200914 if has('win32')
915 call assert_equal('cmd', job_info(g:job).cmd[0])
916 else
917 call assert_equal(&shell, job_info(g:job).cmd[0])
918 endif
919
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200920 " ascii + composing
921 let txt = "a\u0308bc"
922 call term_sendkeys(buf, "echo " . txt . "\r")
923 call term_wait(buf, 50)
924 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
925 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
926 let l = term_scrape(buf, lnum[0] + 1)
927 call assert_equal("a\u0308", l[0].chars)
928 call assert_equal("b", l[1].chars)
929 call assert_equal("c", l[2].chars)
930
931 " multibyte + composing
932 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
933 call term_sendkeys(buf, "echo " . txt . "\r")
934 call term_wait(buf, 50)
935 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
936 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
937 let l = term_scrape(buf, lnum[1] + 1)
938 call assert_equal("\u304b\u3099", l[0].chars)
939 call assert_equal("\u304e", l[1].chars)
940 call assert_equal("\u304f\u3099", l[2].chars)
941 call assert_equal("\u3052", l[3].chars)
942 call assert_equal("\u3053\u3099", l[4].chars)
943
944 " \u00a0 + composing
945 let txt = "abc\u00a0\u0308"
946 call term_sendkeys(buf, "echo " . txt . "\r")
947 call term_wait(buf, 50)
948 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
949 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
950 let l = term_scrape(buf, lnum[2] + 1)
951 call assert_equal("\u00a0\u0308", l[3].chars)
952
953 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200954 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200955 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100956 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200957 let &encoding = save_enc
958endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100959
960func Test_terminal_aucmd_on_close()
961 fun Nop()
962 let s:called = 1
963 endfun
964
965 aug repro
966 au!
967 au BufWinLeave * call Nop()
968 aug END
969
970 let [cmd, waittime] = s:get_sleep_cmd()
971
972 call assert_equal(1, winnr('$'))
973 new
974 call setline(1, ['one', 'two'])
975 exe 'term ++close ' . cmd
976 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200977 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100978 call assert_equal(1, s:called)
979 bwipe!
980
981 unlet s:called
982 au! repro
983 delfunc Nop
984endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100985
986func Test_terminal_term_start_empty_command()
987 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
988 call assert_fails(cmd, 'E474')
989 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
990 call assert_fails(cmd, 'E474')
991 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
992 call assert_fails(cmd, 'E474')
993 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
994 call assert_fails(cmd, 'E474')
995endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100996
997func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200998 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100999
1000 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001001 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001002
Bram Moolenaar086eb872018-03-25 21:24:12 +02001003 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001004 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001005
Bram Moolenaar086eb872018-03-25 21:24:12 +02001006 " Request the cursor position.
1007 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001008
1009 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001010 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001011
Bram Moolenaar086eb872018-03-25 21:24:12 +02001012 " End "cat" gently.
1013 call term_sendkeys(buf, "\<CR>\<C-D>")
1014
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001015 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001016 exe buf . 'bwipe'
1017 unlet g:job
1018endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001019
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001020" Run Vim, start a terminal in that Vim with the kill argument,
1021" :qall works.
1022func Run_terminal_qall_kill(line1, line2)
1023 " 1. Open a terminal window and wait for the prompt to appear
1024 " 2. set kill using term_setkill()
1025 " 3. make Vim exit, it will kill the shell
1026 let after = [
1027 \ a:line1,
1028 \ 'let buf = bufnr("%")',
1029 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1030 \ ' sleep 10m',
1031 \ 'endwhile',
1032 \ a:line2,
1033 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1034 \ 'qall',
1035 \ ]
1036 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001037 return
1038 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001039 call assert_equal("done", readfile("Xdone")[0])
1040 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001041endfunc
1042
1043" Run Vim in a terminal, then start a terminal in that Vim with a kill
1044" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001045func Test_terminal_qall_kill_arg()
1046 call Run_terminal_qall_kill('term ++kill=kill', '')
1047endfunc
1048
1049" Run Vim, start a terminal in that Vim, set the kill argument with
1050" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001051func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001052 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001053endfunc
1054
1055" Run Vim, start a terminal in that Vim without the kill argument,
1056" check that :qall does not exit, :qall! does.
1057func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001058 let after =<< trim [CODE]
1059 term
1060 let buf = bufnr("%")
1061 while term_getline(buf, 1) =~ "^\\s*$"
1062 sleep 10m
1063 endwhile
1064 set nomore
1065 au VimLeavePre * call writefile(["too early"], "Xdone")
1066 qall
1067 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1068 cquit
1069 [CODE]
1070
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001071 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001072 return
1073 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001074 call assert_equal("done", readfile("Xdone")[0])
1075 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001076endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001077
1078" Run Vim in a terminal, then start a terminal in that Vim without a kill
1079" argument, check that :confirm qall works.
1080func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001081 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001082 let buf = RunVimInTerminal('', {})
1083
1084 " Open a terminal window and wait for the prompt to appear
1085 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001086 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1087 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001088
1089 " make Vim exit, it will prompt to kill the shell
1090 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001091 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001092 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001093 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001094
1095 " close the terminal window where Vim was running
1096 quit
1097endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001098
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001099" Run Vim in a terminal, then start a terminal window with a shell and check
1100" that Vim exits if it is closed.
1101func Test_terminal_exit()
1102 CheckRunVimInTerminal
1103
1104 let lines =<< trim END
1105 let winid = win_getid()
1106 help
1107 term
1108 let termid = win_getid()
1109 call win_gotoid(winid)
1110 close
1111 call win_gotoid(termid)
1112 END
1113 call writefile(lines, 'XtermExit')
1114 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1115 let job = term_getjob(buf)
1116 call WaitForAssert({-> assert_equal("run", job_status(job))})
1117
1118 " quit the shell, it will make Vim exit
1119 call term_sendkeys(buf, "exit\<CR>")
1120 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1121
1122 call delete('XtermExit')
1123endfunc
1124
Bram Moolenaar012eb662018-03-13 17:55:27 +01001125func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001126 augroup repro
1127 au!
1128 au TerminalOpen * let s:called += 1
1129 augroup END
1130
1131 let s:called = 0
1132
1133 " Open a terminal window with :terminal
1134 terminal
1135 call assert_equal(1, s:called)
1136 bwipe!
1137
1138 " Open a terminal window with term_start()
1139 call term_start(&shell)
1140 call assert_equal(2, s:called)
1141 bwipe!
1142
1143 " Open a hidden terminal buffer with :terminal
1144 terminal ++hidden
1145 call assert_equal(3, s:called)
1146 for buf in term_list()
1147 exe buf . "bwipe!"
1148 endfor
1149
1150 " Open a hidden terminal buffer with term_start()
1151 let buf = term_start(&shell, {'hidden': 1})
1152 call assert_equal(4, s:called)
1153 exe buf . "bwipe!"
1154
1155 unlet s:called
1156 au! repro
1157endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001158
1159func Check_dump01(off)
1160 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1161 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001162 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001163endfunc
1164
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001165func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001166 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001167 let save_enc = &encoding
1168 set encoding=utf-8
1169 call assert_equal(1, winnr('$'))
1170
1171 let text = " a\u0300 e\u0302 o\u0308"
1172 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001173 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001174 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001175 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001176 let dumpline = readfile('Xdump')[0]
1177 call assert_match('|à| |ê| |ö', dumpline)
1178
1179 call StopVimInTerminal(buf)
1180 call delete('Xcomposing')
1181 call delete('Xdump')
1182 let &encoding = save_enc
1183endfunc
1184
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001185" just testing basic functionality.
1186func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001187 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001188 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001189 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001190 call assert_equal(2, winnr('$'))
1191 call assert_equal(20, line('$'))
1192 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001193
1194 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001195 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001196 call assert_equal(buf, buf2)
1197 call assert_notequal('one two three four five', trim(getline(1)))
1198
1199 " Load the first dump again in the same window
1200 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1201 call assert_equal(buf, buf2)
1202 call Check_dump01(0)
1203
1204 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1205 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1206 new
1207 let closedbuf = winbufnr('')
1208 quit
1209 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1210
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001211 quit
1212endfunc
1213
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001214func Test_terminal_dumpload_dump()
1215 CheckRunVimInTerminal
1216
1217 let lines =<< trim END
1218 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1219 END
1220 call writefile(lines, 'XtermDumpload')
1221 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1222 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1223
1224 call StopVimInTerminal(buf)
1225 call delete('XtermDumpload')
1226endfunc
1227
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001228func Test_terminal_dumpdiff()
1229 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001230 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001231 call assert_equal(2, winnr('$'))
1232 call assert_equal(62, line('$'))
1233 call Check_dump01(0)
1234 call Check_dump01(42)
1235 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1236 quit
1237endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001238
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001239func Test_terminal_dumpdiff_swap()
1240 call assert_equal(1, winnr('$'))
1241 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1242 call assert_equal(2, winnr('$'))
1243 call assert_equal(62, line('$'))
1244 call assert_match('Test_popup_command_01.dump', getline(21))
1245 call assert_match('Test_popup_command_03.dump', getline(42))
1246 call assert_match('Undo', getline(3))
1247 call assert_match('three four five', getline(45))
1248
1249 normal s
1250 call assert_match('Test_popup_command_03.dump', getline(21))
1251 call assert_match('Test_popup_command_01.dump', getline(42))
1252 call assert_match('three four five', getline(3))
1253 call assert_match('Undo', getline(45))
1254 quit
1255endfunc
1256
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001257func Test_terminal_dumpdiff_options()
1258 set laststatus=0
1259 call assert_equal(1, winnr('$'))
1260 let height = winheight(0)
1261 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1262 call assert_equal(2, winnr('$'))
1263 call assert_equal(height, winheight(winnr()))
1264 call assert_equal(33, winwidth(winnr()))
1265 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1266 quit
1267
1268 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001269 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1270 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001271 call assert_equal(&columns, winwidth(0))
1272 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001273 call assert_equal('something else', bufname('%'))
1274 quit
1275
1276 call assert_equal(1, winnr('$'))
1277 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1278 call assert_equal(1, winnr('$'))
1279 bwipe
1280
1281 set laststatus&
1282endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001283
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001284func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001285 call assert_equal(1, winnr('$'))
1286
1287 " Use the title termcap entries to output the escape sequence.
1288 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001289 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001290 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001291 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001292 \ 'redraw',
1293 \ "set t_ts=",
1294 \ ], 'Xscript')
1295 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001296 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001297 call assert_equal('Xtextfile', expand('%:t'))
1298 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001299 return buf
1300endfunc
1301
1302func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001303 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001304 let buf = Api_drop_common('')
1305 call assert_equal(0, &bin)
1306 call assert_equal('', &fenc)
1307
1308 call StopVimInTerminal(buf)
1309 call delete('Xscript')
1310 bwipe Xtextfile
1311endfunc
1312
1313func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001314 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001315 let buf = Api_drop_common(',{"bin":1}')
1316 call assert_equal(1, &bin)
1317
1318 call StopVimInTerminal(buf)
1319 call delete('Xscript')
1320 bwipe Xtextfile
1321endfunc
1322
1323func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001324 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001325 let buf = Api_drop_common(',{"binary":1}')
1326 call assert_equal(1, &bin)
1327
1328 call StopVimInTerminal(buf)
1329 call delete('Xscript')
1330 bwipe Xtextfile
1331endfunc
1332
1333func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001334 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001335 set binary
1336 let buf = Api_drop_common(',{"nobin":1}')
1337 call assert_equal(0, &bin)
1338
1339 call StopVimInTerminal(buf)
1340 call delete('Xscript')
1341 bwipe Xtextfile
1342 set nobinary
1343endfunc
1344
1345func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001346 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001347 set binary
1348 let buf = Api_drop_common(',{"nobinary":1}')
1349 call assert_equal(0, &bin)
1350
1351 call StopVimInTerminal(buf)
1352 call delete('Xscript')
1353 bwipe Xtextfile
1354 set nobinary
1355endfunc
1356
1357func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001358 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001359 let buf = Api_drop_common(',{"ff":"dos"}')
1360 call assert_equal("dos", &ff)
1361
1362 call StopVimInTerminal(buf)
1363 call delete('Xscript')
1364 bwipe Xtextfile
1365endfunc
1366
1367func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001368 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001369 let buf = Api_drop_common(',{"fileformat":"dos"}')
1370 call assert_equal("dos", &ff)
1371
1372 call StopVimInTerminal(buf)
1373 call delete('Xscript')
1374 bwipe Xtextfile
1375endfunc
1376
1377func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001378 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001379 let buf = Api_drop_common(',{"enc":"utf-16"}')
1380 call assert_equal("utf-16", &fenc)
1381
1382 call StopVimInTerminal(buf)
1383 call delete('Xscript')
1384 bwipe Xtextfile
1385endfunc
1386
1387func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001388 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001389 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1390 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001391
1392 call StopVimInTerminal(buf)
1393 call delete('Xscript')
1394 bwipe Xtextfile
1395endfunc
1396
1397func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001398 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001399 let firstwinid = win_getid()
1400 split Xtextfile
1401 let textfile_winid = win_getid()
1402 call assert_equal(2, winnr('$'))
1403 call win_gotoid(firstwinid)
1404
1405 " Use the title termcap entries to output the escape sequence.
1406 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001407 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001408 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1409 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1410 \ 'redraw',
1411 \ "set t_ts=",
1412 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001413 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001414 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001415 call assert_equal(textfile_winid, win_getid())
1416
1417 call StopVimInTerminal(buf)
1418 call delete('Xscript')
1419 bwipe Xtextfile
1420endfunc
1421
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001422func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001423 let g:called_bufnum = a:bufnum
1424 let g:called_arg = a:arg
1425endfunc
1426
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001427func WriteApiCall(funcname)
1428 " Use the title termcap entries to output the escape sequence.
1429 call writefile([
1430 \ 'set title',
1431 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1432 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1433 \ 'redraw',
1434 \ "set t_ts=",
1435 \ ], 'Xscript')
1436endfunc
1437
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001438func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001439 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001440
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001441 unlet! g:called_bufnum
1442 unlet! g:called_arg
1443
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001444 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001445
1446 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001447 let buf = RunVimInTerminal('-S Xscript', {})
1448 call WaitFor({-> exists('g:called_bufnum')})
1449 call assert_equal(buf, g:called_bufnum)
1450 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001451 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001452
1453 unlet! g:called_bufnum
1454 unlet! g:called_arg
1455
1456 " Enable explicitly
1457 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1458 call WaitFor({-> exists('g:called_bufnum')})
1459 call assert_equal(buf, g:called_bufnum)
1460 call assert_equal(['hello', 123], g:called_arg)
1461 call StopVimInTerminal(buf)
1462
1463 unlet! g:called_bufnum
1464 unlet! g:called_arg
1465
1466 func! ApiCall_TryThis(bufnum, arg)
1467 let g:called_bufnum2 = a:bufnum
1468 let g:called_arg2 = a:arg
1469 endfunc
1470
1471 call WriteApiCall('ApiCall_TryThis')
1472
1473 " Use prefix match
1474 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1475 call WaitFor({-> exists('g:called_bufnum2')})
1476 call assert_equal(buf, g:called_bufnum2)
1477 call assert_equal(['hello', 123], g:called_arg2)
1478 call StopVimInTerminal(buf)
1479
1480 unlet! g:called_bufnum2
1481 unlet! g:called_arg2
1482
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001483 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001484 delfunction! ApiCall_TryThis
1485 unlet! g:called_bufnum2
1486 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001487endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001488
1489func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001490 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001491
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001492 func! TryThis(bufnum, arg)
1493 let g:called_bufnum3 = a:bufnum
1494 let g:called_arg3 = a:arg
1495 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001496
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001497 call WriteApiCall('TryThis')
1498
1499 unlet! g:called_bufnum3
1500 unlet! g:called_arg3
1501
1502 " Not permitted
1503 call ch_logfile('Xlog', 'w')
1504 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1505 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1506 call assert_false(exists('g:called_bufnum3'))
1507 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001508 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001509
1510 " No match
1511 call ch_logfile('Xlog', 'w')
1512 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1513 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1514 call assert_false(exists('g:called_bufnum3'))
1515 call assert_false(exists('g:called_arg3'))
1516 call StopVimInTerminal(buf)
1517
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001518 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001519 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001520 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001521 delfunction! TryThis
1522 unlet! g:called_bufnum3
1523 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001524endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001525
Bram Moolenaara997b452018-04-17 23:24:06 +02001526let s:caught_e937 = 0
1527
1528func Tapi_Delete(bufnum, arg)
1529 try
1530 execute 'bdelete!' a:bufnum
1531 catch /E937:/
1532 let s:caught_e937 = 1
1533 endtry
1534endfunc
1535
1536func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001537 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001538
1539 call WriteApiCall('Tapi_Delete')
1540 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001541 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001542
1543 call StopVimInTerminal(buf)
1544 call delete('Xscript')
1545 call ch_logfile('', '')
1546endfunc
1547
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001548func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001549 if !exists('*term_getansicolors')
1550 throw 'Skipped: term_getansicolors() not supported'
1551 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001552 let colors = [
1553 \ '#000000', '#e00000',
1554 \ '#00e000', '#e0e000',
1555 \ '#0000e0', '#e000e0',
1556 \ '#00e0e0', '#e0e0e0',
1557 \ '#808080', '#ff4040',
1558 \ '#40ff40', '#ffff40',
1559 \ '#4040ff', '#ff40ff',
1560 \ '#40ffff', '#ffffff',
1561 \]
1562
1563 let buf = Run_shell_in_terminal({})
1564 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001565 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001566 call term_wait(buf)
1567
1568 exe buf . 'bwipe'
1569endfunc
1570
1571let s:test_colors = [
1572 \ '#616e64', '#0d0a79',
1573 \ '#6d610d', '#0a7373',
1574 \ '#690d0a', '#6d696e',
1575 \ '#0d0a6f', '#616e0d',
1576 \ '#0a6479', '#6d0d0a',
1577 \ '#617373', '#0d0a69',
1578 \ '#6d690d', '#0a6e6f',
1579 \ '#610d0a', '#6e6479',
1580 \]
1581
1582func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001583 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001584 if !exists('*term_getansicolors')
1585 throw 'Skipped: term_getansicolors() not supported'
1586 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001587 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1588 let buf = Run_shell_in_terminal({})
1589 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001590 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001591 call term_wait(buf)
1592
1593 exe buf . 'bwipe'
1594 unlet g:terminal_ansi_colors
1595endfunc
1596
1597func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001598 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001599 if !exists('*term_getansicolors')
1600 throw 'Skipped: term_getansicolors() not supported'
1601 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001602 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1603 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1604 call assert_equal(s:test_colors, term_getansicolors(buf))
1605
1606 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001607 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001608
1609 let colors = [
1610 \ 'ivory', 'AliceBlue',
1611 \ 'grey67', 'dark goldenrod',
1612 \ 'SteelBlue3', 'PaleVioletRed4',
1613 \ 'MediumPurple2', 'yellow2',
1614 \ 'RosyBrown3', 'OrangeRed2',
1615 \ 'white smoke', 'navy blue',
1616 \ 'grey47', 'gray97',
1617 \ 'MistyRose2', 'DodgerBlue4',
1618 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001619 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001620
1621 let colors[4] = 'Invalid'
1622 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1623
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001624 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001625 call term_wait(buf)
1626 exe buf . 'bwipe'
1627endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001628
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001629func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001630 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001631
1632 " Use all the ANSI colors.
1633 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001634 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001635 \ 'hi Tblack ctermfg=0 ctermbg=8',
1636 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1637 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1638 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1639 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1640 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1641 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1642 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1643 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1644 \ 'hi Tred ctermfg=9 ctermbg=1',
1645 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1646 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1647 \ 'hi Tblue ctermfg=12 ctermbg=4',
1648 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1649 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1650 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001651 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1652 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1653 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001654 \ '',
1655 \ 'call matchadd("Tblack", "A")',
1656 \ 'call matchadd("Tdarkred", "B")',
1657 \ 'call matchadd("Tdarkgreen", "C")',
1658 \ 'call matchadd("Tbrown", "D")',
1659 \ 'call matchadd("Tdarkblue", "E")',
1660 \ 'call matchadd("Tdarkmagenta", "F")',
1661 \ 'call matchadd("Tdarkcyan", "G")',
1662 \ 'call matchadd("Tlightgrey", "H")',
1663 \ 'call matchadd("Tdarkgrey", "I")',
1664 \ 'call matchadd("Tred", "J")',
1665 \ 'call matchadd("Tgreen", "K")',
1666 \ 'call matchadd("Tyellow", "L")',
1667 \ 'call matchadd("Tblue", "M")',
1668 \ 'call matchadd("Tmagenta", "N")',
1669 \ 'call matchadd("Tcyan", "O")',
1670 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001671 \ 'call matchadd("TdarkredBold", "X")',
1672 \ 'call matchadd("TgreenBold", "Y")',
1673 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001674 \ 'redraw',
1675 \ ], 'Xcolorscript')
1676 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1677 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1678
1679 call term_sendkeys(buf, ":q\<CR>")
1680 call StopVimInTerminal(buf)
1681 call delete('Xcolorscript')
1682endfunc
1683
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001684func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001685 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001686 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001687 let text = []
1688 for n in range(10)
1689 call add(text, repeat(n, 50))
1690 endfor
1691 call writefile(text, 'Xwinsize')
1692 let buf = RunVimInTerminal('Xwinsize', {})
1693 let win = bufwinid(buf)
1694 call assert_equal([6, 40], term_getsize(buf))
1695 call assert_equal(6, winheight(win))
1696 call assert_equal(40, winwidth(win))
1697
1698 " resizing the window doesn't resize the terminal.
1699 resize 10
1700 vertical resize 60
1701 call assert_equal([6, 40], term_getsize(buf))
1702 call assert_equal(10, winheight(win))
1703 call assert_equal(60, winwidth(win))
1704
1705 call StopVimInTerminal(buf)
1706 call delete('Xwinsize')
1707
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001708 call assert_fails('set termwinsize=40', 'E474')
1709 call assert_fails('set termwinsize=10+40', 'E474')
1710 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001711
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001712 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001713endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001714
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001715func Test_terminal_termwinsize_option_zero()
1716 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001717 let buf = Run_shell_in_terminal({})
1718 let win = bufwinid(buf)
1719 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001720 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001721 call term_wait(buf)
1722 exe buf . 'bwipe'
1723
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001724 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001725 let buf = Run_shell_in_terminal({})
1726 let win = bufwinid(buf)
1727 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001728 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001729 call term_wait(buf)
1730 exe buf . 'bwipe'
1731
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001732 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001733 let buf = Run_shell_in_terminal({})
1734 let win = bufwinid(buf)
1735 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001736 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001737 call term_wait(buf)
1738 exe buf . 'bwipe'
1739
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001740 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001741endfunc
1742
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001743func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001744 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001745 vsplit
1746 let buf = Run_shell_in_terminal({})
1747 let win = bufwinid(buf)
1748 call assert_inrange(10, 1000, winheight(win))
1749 call assert_inrange(50, 1000, winwidth(win))
1750 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1751
1752 resize 15
1753 vertical resize 60
1754 redraw
1755 call assert_equal([15, 60], term_getsize(buf))
1756 call assert_equal(15, winheight(win))
1757 call assert_equal(60, winwidth(win))
1758
1759 resize 7
1760 vertical resize 30
1761 redraw
1762 call assert_equal([10, 50], term_getsize(buf))
1763 call assert_equal(7, winheight(win))
1764 call assert_equal(30, winwidth(win))
1765
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001766 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001767 call term_wait(buf)
1768 exe buf . 'bwipe'
1769
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001770 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001771 let buf = Run_shell_in_terminal({})
1772 let win = bufwinid(buf)
1773 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001774 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001775 call term_wait(buf)
1776 exe buf . 'bwipe'
1777
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001778 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001779endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001780
1781func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001782 " make three tabpages, terminal in the middle
1783 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001784 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001785 tabnew
1786 tabprev
1787 call assert_equal(1, winnr('$'))
1788 call assert_equal(2, tabpagenr())
1789 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001790
1791 let buf = Run_shell_in_terminal({})
1792 let termwin = bufwinid(buf)
1793 set termwinkey=<C-L>
1794 call feedkeys("\<C-L>w", 'tx')
1795 call assert_equal(thiswin, win_getid())
1796 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001797 call assert_equal(termwin, win_getid())
1798
Bram Moolenaar997d4242019-09-14 21:23:40 +02001799 if has('langmap')
1800 set langmap=xjyk
1801 call feedkeys("\<C-L>x", 'tx')
1802 call assert_equal(thiswin, win_getid())
1803 call feedkeys("\<C-W>y", 'tx')
1804 call assert_equal(termwin, win_getid())
1805 set langmap=
1806 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001807
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001808 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001809 call assert_equal(3, tabpagenr())
1810 tabprev
1811 call assert_equal(2, tabpagenr())
1812 call assert_equal(termwin, win_getid())
1813
1814 call feedkeys("\<C-L>gT", "xt")
1815 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001816 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001817 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001818 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001819
1820 let job = term_getjob(buf)
1821 call feedkeys("\<C-L>\<C-C>", 'tx')
1822 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001823
1824 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001825 tabnext
1826 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001827 tabprev
1828 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001829endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001830
1831func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001832 CheckUnix
1833
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001834 call writefile([
1835 \ '#!/bin/sh',
1836 \ 'echo "this is standard error" >&2',
1837 \ 'echo "this is standard out" >&1',
1838 \ ], 'Xechoerrout.sh')
1839 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1840
1841 let outfile = 'Xtermstdout'
1842 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001843
1844 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1845 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001846 call assert_equal('this is standard error', term_getline(buf, 1))
1847
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001848 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001849 exe buf . 'bwipe'
1850 call delete('Xechoerrout.sh')
1851 call delete(outfile)
1852endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001853
Bram Moolenaare219f732019-11-30 15:34:08 +01001854func Test_termwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001855 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001856
1857 " Let the terminal output more than 'termwinscroll' lines, some at the start
1858 " will be dropped.
1859 exe 'set termwinscroll=' . &lines
1860 let buf = term_start('/bin/sh')
1861 for i in range(1, &lines)
1862 call feedkeys("echo " . i . "\<CR>", 'xt')
1863 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1864 endfor
1865 " Go to Terminal-Normal mode to update the buffer.
1866 call feedkeys("\<C-W>N", 'xt')
1867 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1868
1869 " Every "echo nr" must only appear once
1870 let lines = getline(1, line('$'))
1871 for i in range(&lines - len(lines) / 2 + 2, &lines)
1872 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1873 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1874 endfor
1875
1876 exe buf . 'bwipe!'
1877endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001878
Bram Moolenaar875cf872018-07-08 20:49:07 +02001879" Resizing the terminal window caused an ml_get error.
1880" TODO: This does not reproduce the original problem.
1881func Test_terminal_resize()
1882 set statusline=x
1883 terminal
1884 call assert_equal(2, winnr('$'))
1885
1886 " Fill the terminal with text.
1887 if has('win32')
1888 call feedkeys("dir\<CR>", 'xt')
1889 else
1890 call feedkeys("ls\<CR>", 'xt')
1891 endif
1892 " Go to Terminal-Normal mode for a moment.
1893 call feedkeys("\<C-W>N", 'xt')
1894 " Open a new window
1895 call feedkeys("i\<C-W>n", 'xt')
1896 call assert_equal(3, winnr('$'))
1897 redraw
1898
1899 close
1900 call assert_equal(2, winnr('$'))
1901 call feedkeys("exit\<CR>", 'xt')
1902 set statusline&
1903endfunc
1904
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001905" must be nearly the last, we can't go back from GUI to terminal
1906func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001907 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001908
1909 " Ignore the "failed to create input context" error.
1910 call test_ignore_error('E285:')
1911
1912 gui -f
1913
1914 call assert_equal(1, winnr('$'))
1915 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001916 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001917 call term_wait(buf)
1918
1919 " closing window wipes out the terminal buffer a with finished job
1920 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1921 call assert_equal("", bufname(buf))
1922
1923 unlet g:job
1924endfunc
1925
1926func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001927 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001928 set guioptions+=!
1929
1930 let filename = 'Xtestscript'
1931 if has('win32')
1932 let filename .= '.bat'
1933 let prefix = ''
1934 let contents = ['@echo off', 'exit %1']
1935 else
1936 let filename .= '.sh'
1937 let prefix = './'
1938 let contents = ['#!/bin/sh', 'exit $1']
1939 endif
1940 call writefile(contents, filename)
1941 call setfperm(filename, 'rwxrwx---')
1942
1943 " Check if v:shell_error is equal to the exit status.
1944 let exitval = 0
1945 execute printf(':!%s%s %d', prefix, filename, exitval)
1946 call assert_equal(exitval, v:shell_error)
1947
1948 let exitval = 9
1949 execute printf(':!%s%s %d', prefix, filename, exitval)
1950 call assert_equal(exitval, v:shell_error)
1951
1952 set guioptions&
1953 call delete(filename)
1954endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001955
1956func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001957 CheckUnix
1958
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001959 term ++hidden cat
1960 let bnr = bufnr('$')
1961 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1962 exe 'sbuf ' . bnr
1963 call assert_equal('terminal', &buftype)
1964 call term_sendkeys(bnr, "asdf\<CR>")
1965 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1966 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001967 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001968 bwipe!
1969endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001970
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001971func Test_terminal_switch_mode()
1972 term
1973 let bnr = bufnr('$')
1974 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1975 call feedkeys("\<C-W>N", 'xt')
1976 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1977 call feedkeys("A", 'xt')
1978 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1979 call feedkeys("\<C-W>N", 'xt')
1980 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1981 call feedkeys("I", 'xt')
1982 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1983 call feedkeys("\<C-W>Nv", 'xt')
1984 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1985 call feedkeys("I", 'xt')
1986 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1987 call feedkeys("\<C-W>Nv", 'xt')
1988 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1989 call feedkeys("A", 'xt')
1990 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1991 bwipe!
1992endfunc
1993
Bram Moolenaara3817732019-10-16 16:31:44 +02001994func Test_terminal_normal_mode()
1995 CheckRunVimInTerminal
1996
1997 " Run Vim in a terminal and open a terminal window to run Vim in.
1998 let lines =<< trim END
1999 call setline(1, range(11111, 11122))
2000 3
2001 END
2002 call writefile(lines, 'XtermNormal')
2003 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
2004 call term_wait(buf)
2005
2006 call term_sendkeys(buf, "\<C-W>N")
2007 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
2008 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
2009
2010 call term_sendkeys(buf, ":set culopt=number\r")
2011 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
2012
2013 call term_sendkeys(buf, ":set culopt=line\r")
2014 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
2015
2016 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
2017 call StopVimInTerminal(buf)
2018 call delete('XtermNormal')
2019endfunc
2020
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002021func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002022 CheckUnix
2023
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002024 call assert_equal(1, winnr('$'))
2025 term ++hidden ++close ls
2026 let bnr = bufnr('$')
2027 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2028 call WaitForAssert({-> assert_false(bufexists(bnr))})
2029 call assert_equal(1, winnr('$'))
2030endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002031
2032func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002033 " This test does not pass through ConPTY.
2034 if has('conpty')
2035 return
2036 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002037 let contents = [
2038 \ [ 'One', '', 'X' ],
2039 \ [ 'Two', '', '' ],
2040 \ [ 'Three' ] + repeat([''], 30)
2041 \ ]
2042
2043 for c in contents
2044 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002045 if has('win32')
2046 term cmd /c type Xfile
2047 else
2048 term cat Xfile
2049 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002050 let bnr = bufnr('$')
2051 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2052 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002053 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002054 call assert_equal(c, getline(1, line('$')))
2055 quit
2056 endfor
2057
2058 call delete('Xfile')
2059endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002060
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002061func Test_terminal_no_job()
Bram Moolenaar077ff432019-10-28 00:42:21 +01002062 if has('win32')
2063 let cmd = 'cmd /c ""'
2064 else
2065 CheckExecutable false
2066 let cmd = 'false'
2067 endif
2068 let term = term_start(cmd, {'term_finish': 'close'})
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002069 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2070endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002071
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002072func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002073 CheckUnix
2074
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002075 let buf = Run_shell_in_terminal({})
2076
2077 " Wait for the shell to display a prompt.
2078 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2079
2080 " Hide the cursor.
2081 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2082 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2083
2084 " Show the cursor.
2085 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002086 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002087
2088 " Change color of cursor.
2089 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2090 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2091 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2092 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2093 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2094
2095 " Make cursor a blinking block.
2096 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2097 call WaitForAssert({-> assert_equal([1, 1],
2098 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2099
2100 " Make cursor a steady block.
2101 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2102 call WaitForAssert({-> assert_equal([0, 1],
2103 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2104
2105 " Make cursor a blinking underline.
2106 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2107 call WaitForAssert({-> assert_equal([1, 2],
2108 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2109
2110 " Make cursor a steady underline.
2111 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2112 call WaitForAssert({-> assert_equal([0, 2],
2113 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2114
2115 " Make cursor a blinking vertical bar.
2116 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2117 call WaitForAssert({-> assert_equal([1, 3],
2118 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2119
2120 " Make cursor a steady vertical bar.
2121 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2122 call WaitForAssert({-> assert_equal([0, 3],
2123 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2124
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002125 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002126endfunc
2127
Bram Moolenaar1e115362019-01-09 23:01:02 +01002128func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002129 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002130 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002131 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002132 call assert_equal('', term_gettitle(bufnr('$') + 1))
2133
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002134 if !has('title') || &title == 0 || empty(&t_ts)
2135 throw "Skipped: can't get/set title"
2136 endif
2137
Bram Moolenaar1e115362019-01-09 23:01:02 +01002138 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002139 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002140 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2141 call term_sendkeys(term, ":e Xfoo\r")
2142 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002143 else
2144 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002145 call term_sendkeys(term, ":e Xfoo\r")
2146 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002147 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002148
Bram Moolenaar1e115362019-01-09 23:01:02 +01002149 call term_sendkeys(term, ":set titlestring=foo\r")
2150 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2151
2152 exe term . 'bwipe!'
2153endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002154
2155" When drawing the statusline the cursor position may not have been updated
2156" yet.
2157" 1. create a terminal, make it show 2 lines
2158" 2. 0.5 sec later: leave terminal window, execute "i"
2159" 3. 0.5 sec later: clear terminal window, now it's 1 line
2160" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2161" 4. 0.5 sec later: should be done, clean up
2162func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002163 CheckUnix
2164
Bram Moolenaar10772302019-01-20 18:25:54 +01002165 set statusline=x
2166 terminal
2167 let tbuf = bufnr('')
2168 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2169 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2170 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2171 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2172
2173 sleep 2
2174 exe tbuf . 'bwipe!'
2175 au! BufLeave
2176 set statusline=
2177endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002178
2179func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002180 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002181
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002182 " split, go to the bottom-right window
2183 split
2184 wincmd j
2185 set splitright
2186
2187 call writefile([
2188 \ 'echo getwinpos()',
2189 \ ], 'XTest_getwinpos')
2190 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2191 call term_wait(buf)
2192
2193 " Find the output of getwinpos() in the bottom line.
2194 let rows = term_getsize(buf)[0]
2195 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2196 let line = term_getline(buf, rows)
2197 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2198 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2199
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002200 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002201 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002202 " In the GUI it can be more, let's assume a 20 x 14 cell.
2203 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002204 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002205 let winpos = 50->getwinpos()
2206 call assert_equal(xroot, winpos[0])
2207 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002208 let [winrow, wincol] = win_screenpos('.')
2209 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2210 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2211 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2212 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002213
2214 call term_wait(buf)
2215 call term_sendkeys(buf, ":q\<CR>")
2216 call StopVimInTerminal(buf)
2217 call delete('XTest_getwinpos')
2218 exe buf . 'bwipe!'
2219 set splitright&
2220 only!
2221endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002222
2223func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002224 " somehow doesn't work on MS-Windows
2225 CheckUnix
2226 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002227
2228 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002229 call writefile(["\<Esc>[?1047h"], 'Xtext')
2230 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002231 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2232
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002233 call writefile(["\<Esc>[?1047l"], 'Xtext')
2234 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002235 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002236
2237 call term_sendkeys(buf, "exit\r")
2238 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002239 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002240endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002241
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002242func Test_terminal_shell_option()
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002243 if has('unix')
2244 " exec is a shell builtin command, should fail without a shell.
2245 term exec ls runtest.vim
2246 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
2247 bwipe!
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002248
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002249 term ++shell exec ls runtest.vim
2250 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2251 bwipe!
2252 elseif has('win32')
2253 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar36ec6f62019-11-05 22:38:47 +01002254 try
2255 term dir /b runtest.vim
2256 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
2257 catch /CreateProcess/
2258 " ignore
2259 endtry
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002260 bwipe!
2261
2262 term ++shell dir /b runtest.vim
2263 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2264 bwipe!
2265 endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002266endfunc
2267
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002268func Test_terminal_setapi_and_call()
Bram Moolenaar21109272020-01-30 16:27:20 +01002269 CheckRunVimInTerminal
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002270
2271 call WriteApiCall('Tapi_TryThis')
2272 call ch_logfile('Xlog', 'w')
2273
2274 unlet! g:called_bufnum
2275 unlet! g:called_arg
2276
Bram Moolenaar21109272020-01-30 16:27:20 +01002277 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002278 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2279 call assert_false(exists('g:called_bufnum'))
2280 call assert_false(exists('g:called_arg'))
2281
Bram Moolenaar21109272020-01-30 16:27:20 +01002282 eval buf->term_setapi('Tapi_')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002283 call term_sendkeys(buf, ":set notitle\<CR>")
2284 call term_sendkeys(buf, ":source Xscript\<CR>")
2285 call WaitFor({-> exists('g:called_bufnum')})
2286 call assert_equal(buf, g:called_bufnum)
2287 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar21109272020-01-30 16:27:20 +01002288
2289 call StopVimInTerminal(buf)
2290
2291 call delete('Xscript')
2292 call ch_logfile('')
2293 call delete('Xlog')
2294 unlet! g:called_bufnum
2295 unlet! g:called_arg
2296endfunc
2297
2298func Test_terminal_api_arg()
2299 CheckRunVimInTerminal
2300
2301 call WriteApiCall('Tapi_TryThis')
2302 call ch_logfile('Xlog', 'w')
2303
2304 unlet! g:called_bufnum
2305 unlet! g:called_arg
2306
2307 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2308 let buf = bufnr('%')
2309 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2310 call assert_false(exists('g:called_bufnum'))
2311 call assert_false(exists('g:called_arg'))
2312
2313 call StopVimInTerminal(buf)
2314
2315 call ch_logfile('Xlog', 'w')
2316
2317 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2318 let buf = bufnr('%')
2319 call WaitFor({-> exists('g:called_bufnum')})
2320 call assert_equal(buf, g:called_bufnum)
2321 call assert_equal(['hello', 123], g:called_arg)
2322
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002323 call StopVimInTerminal(buf)
2324
2325 call delete('Xscript')
2326 call ch_logfile('')
2327 call delete('Xlog')
2328 unlet! g:called_bufnum
2329 unlet! g:called_arg
2330endfunc
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002331
2332func Test_terminal_in_popup()
2333 CheckRunVimInTerminal
2334
2335 let text =<< trim END
2336 some text
2337 to edit
2338 in a popup window
2339 END
2340 call writefile(text, 'Xtext')
Bram Moolenaar3180fe62020-02-02 13:47:06 +01002341 let cmd = GetVimCommandCleanTerm()
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002342 let lines = [
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01002343 \ 'set t_u7=',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002344 \ 'call setline(1, range(20))',
2345 \ 'hi PopTerm ctermbg=grey',
2346 \ 'func OpenTerm(setColor)',
Bram Moolenaar353c3512020-03-15 14:19:26 +01002347 \ " set noruler",
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002348 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
Bram Moolenaar57c33952020-02-29 16:09:16 +01002349 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002350 \ ' if a:setColor',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002351 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002352 \ ' endif',
2353 \ 'endfunc',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002354 \ 'func HidePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002355 \ ' call popup_hide(g:winid)',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002356 \ 'endfunc',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002357 \ 'func ClosePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002358 \ ' call popup_close(g:winid)',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002359 \ 'endfunc',
2360 \ 'func ReopenPopup()',
2361 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
2362 \ 'endfunc',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002363 \ ]
2364 call writefile(lines, 'XtermPopup')
2365 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002366 call term_wait(buf, 200)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002367 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002368 call term_wait(buf, 200)
Bram Moolenaarc2adc002020-02-14 17:05:18 +01002369 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002370 call term_wait(buf, 200)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002371 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002372 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
2373
2374 call term_sendkeys(buf, ":q\<CR>")
2375 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
2376
2377 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002378 call term_wait(buf, 300)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002379 call term_sendkeys(buf, ":set hlsearch\<CR>")
2380 call term_sendkeys(buf, "/edit\<CR>")
2381 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
2382
Bram Moolenaar1af5ce02020-02-06 11:54:35 +01002383 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
2384 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
2385 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar67021882020-02-07 22:20:53 +01002386 call term_wait(buf, 100)
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002387
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002388 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
2389 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
2390
2391 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
2392 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002393
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002394 " Go to terminal-Normal mode and visually select text.
2395 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
2396 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
2397
2398 " Back to job mode, redraws
2399 call term_sendkeys(buf, "A")
2400 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
2401
2402 call term_wait(buf, 100)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002403 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002404 call term_wait(buf, 200) " wait for terminal to vanish
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002405
2406 call StopVimInTerminal(buf)
Bram Moolenaar19398262020-03-14 15:28:08 +01002407 call delete('Xtext')
2408 call delete('XtermPopup')
2409endfunc
2410
2411" Check a terminal in popup window uses the default mininum size.
2412func Test_terminal_in_popup_min_size()
2413 CheckRunVimInTerminal
2414
2415 let text =<< trim END
2416 another text
2417 to show
2418 in a popup window
2419 END
2420 call writefile(text, 'Xtext')
2421 let lines = [
2422 \ 'set t_u7=',
2423 \ 'call setline(1, range(20))',
2424 \ 'hi PopTerm ctermbg=grey',
2425 \ 'func OpenTerm()',
2426 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
2427 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2428 \ 'endfunc',
2429 \ ]
2430 call writefile(lines, 'XtermPopup')
2431 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002432 call term_wait(buf, 200)
2433 call term_sendkeys(buf, ":set noruler\<CR>")
Bram Moolenaar19398262020-03-14 15:28:08 +01002434 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
2435 call term_wait(buf, 100)
2436 call term_sendkeys(buf, ":\<CR>")
2437 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
2438
2439 call term_wait(buf, 100)
2440 call term_sendkeys(buf, ":q\<CR>")
2441 call term_wait(buf, 100) " wait for terminal to vanish
2442 call StopVimInTerminal(buf)
2443 call delete('Xtext')
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002444 call delete('XtermPopup')
2445endfunc
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002446
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002447func Test_double_popup_terminal()
2448 let buf1 = term_start(&shell, #{hidden: 1})
2449 let win1 = popup_create(buf1, {})
2450 let buf2 = term_start(&shell, #{hidden: 1})
2451 let win2 = popup_create(buf2, {})
2452 call popup_close(win1)
2453 call popup_close(win2)
2454 exe buf1 .. 'bwipe!'
2455 exe buf2 .. 'bwipe!'
2456endfunc
2457
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002458func Test_issue_5607()
2459 let wincount = winnr('$')
2460 exe 'terminal' &shell &shellcmdflag 'exit'
2461 let job = term_getjob(bufnr())
2462 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2463
2464 let old_wincolor = &wincolor
2465 try
2466 set wincolor=
2467 finally
2468 let &wincolor = old_wincolor
2469 bw!
2470 endtry
2471endfunc
Bram Moolenaare010c722020-02-24 21:37:54 +01002472
2473func Test_hidden_terminal()
2474 let buf = term_start(&shell, #{hidden: 1})
2475 call assert_equal('', bufname('^$'))
2476 call StopShellInTerminal(buf)
2477endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002478
2479func Test_term_nasty_callback()
2480 func OpenTerms()
2481 set hidden
2482 let g:buf0 = term_start('sh', #{hidden: 1})
2483 call popup_create(g:buf0, {})
2484 let g:buf1 = term_start('sh', #{hidden: 1, term_finish: 'close'})
2485 call popup_create(g:buf1, {})
2486 let g:buf2 = term_start(['sh', '-c'], #{curwin: 1, exit_cb: function('TermExit')})
2487 sleep 100m
2488 call popup_close(win_getid())
2489 endfunc
2490 func TermExit(...)
2491 call term_sendkeys(bufnr('#'), "exit\<CR>")
2492 call popup_close(win_getid())
2493 endfu
2494 call OpenTerms()
2495
2496 call term_sendkeys(g:buf0, "exit\<CR>")
Bram Moolenaarfa5d8a12020-03-13 14:55:23 +01002497 sleep 100m
Bram Moolenaar24ebd832020-03-16 21:25:24 +01002498 exe g:buf0 .. 'bwipe!'
Bram Moolenaarcee52202020-03-11 14:19:58 +01002499 set hidden&
2500endfunc
2501