blob: 7e7aaff947f5b7fcd2fc6afe6b937f8d7fe8b63c [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02005
6source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01007source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02008
Bram Moolenaarb81bc772017-08-11 22:45:01 +02009let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020010let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020011
Bram Moolenaar94053a52017-08-01 21:44:33 +020012" Open a terminal with a shell, assign the job to g:job and return the buffer
13" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020014func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010015 if has('win32')
16 let buf = term_start([&shell,'/k'], a:options)
17 else
18 let buf = term_start(&shell, a:options)
19 endif
Bram Moolenaar373a8762020-03-19 19:44:32 +010020 let g:test_is_flaky = 1
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020021
22 let termlist = term_list()
23 call assert_equal(1, len(termlist))
24 call assert_equal(buf, termlist[0])
25
26 let g:job = term_getjob(buf)
27 call assert_equal(v:t_job, type(g:job))
28
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020029 let string = string({'job': buf->term_getjob()})
Bram Moolenaar35422f42017-08-05 16:33:56 +020030 call assert_match("{'job': 'process \\d\\+ run'}", string)
31
Bram Moolenaar94053a52017-08-01 21:44:33 +020032 return buf
33endfunc
34
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020035func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020036 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020037 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020038
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020039 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020040 call assert_match('^/dev/', job_info(g:job).tty_out)
41 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020042 else
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010043 " ConPTY works on anonymous pipe.
44 if !has('conpty')
45 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020046 call assert_match('^\\\\.\\pipe\\', ''->term_gettty())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010047 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020048 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020049 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020050 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020052 call assert_match('%aR[^\n]*running]', execute('ls R'))
53 call assert_notmatch('%[^\n]*running]', execute('ls F'))
54 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020055
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020056 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +020057 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020058 call assert_equal('n', mode())
59 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020060 call assert_match('%aF[^\n]*finished]', execute('ls F'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
62 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020063
Bram Moolenaar94053a52017-08-01 21:44:33 +020064 " closing window wipes out the terminal buffer a with finished job
65 close
66 call assert_equal("", bufname(buf))
67
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020068 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020069 unlet g:job
70endfunc
71
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020072func Test_terminal_TerminalWinOpen()
73 au TerminalWinOpen * let b:done = 'yes'
74 let buf = Run_shell_in_terminal({})
75 call assert_equal('yes', b:done)
76 call StopShellInTerminal(buf)
77 " closing window wipes out the terminal buffer with the finished job
78 close
79
80 if has("unix")
81 terminal ++hidden ++open sleep 1
82 sleep 1
83 call assert_fails("echo b:done", 'E121:')
84 endif
85
86 au! TerminalWinOpen
87endfunc
88
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020089func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020090 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020091 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020092 call term_wait(buf)
93
94 setlocal modifiable
95 exe "normal Axxx\<Esc>"
96 call assert_fails(buf . 'bwipe', 'E517')
97 undo
98
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020099 exe buf . 'bwipe'
100 unlet g:job
101endfunc
102
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100103func Test_terminal_paste_register()
104 let @" = "text to paste"
105
106 let buf = Run_shell_in_terminal({})
107 " Wait for the shell to display a prompt
108 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
109
110 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
111 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200112 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100113
114 exe buf . 'bwipe!'
115 unlet g:job
116endfunc
117
Bram Moolenaar94053a52017-08-01 21:44:33 +0200118func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200119 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200120 call assert_fails(buf . 'bwipe', 'E517')
121 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200122 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200123 call assert_equal("", bufname(buf))
124
125 unlet g:job
126endfunc
127
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200128func Test_terminal_split_quit()
129 let buf = Run_shell_in_terminal({})
130 call term_wait(buf)
131 split
132 quit!
133 call term_wait(buf)
134 sleep 50m
135 call assert_equal('run', job_status(g:job))
136
137 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200138 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200139
140 exe buf . 'bwipe'
141 unlet g:job
142endfunc
143
Bram Moolenaar94053a52017-08-01 21:44:33 +0200144func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200145 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200146 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200147 quit
148 for nr in range(1, winnr('$'))
149 call assert_notequal(winbufnr(nr), buf)
150 endfor
151 call assert_true(bufloaded(buf))
152 call assert_true(buflisted(buf))
153
154 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200155 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200156 exe buf . 'bwipe'
157
158 unlet g:job
159endfunc
160
Bram Moolenaar1e115362019-01-09 23:01:02 +0100161func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162 exe g:buf . 'bwipe!'
163 let g:buf = 0
164endfunc
165
Bram Moolenaar9d189612017-09-09 18:11:00 +0200166func Get_cat_123_cmd()
167 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100168 if !has('conpty')
169 return 'cmd /c "cls && color 2 && echo 123"'
170 else
171 " When clearing twice, extra sequence is not output.
172 return 'cmd /c "cls && cls && color 2 && echo 123"'
173 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200174 else
175 call writefile(["\<Esc>[32m123"], 'Xtext')
176 return "cat Xtext"
177 endif
178endfunc
179
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200180func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200181 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200182 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
183 let g:job = term_getjob(g:buf)
184
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200185 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
186 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200187 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100188 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200189 call delete('Xtext')
190endfunc
191
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200192func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200193 let l = term_scrape(a:buf, 0)
194 call assert_true(len(l) == 0)
195 let l = term_scrape(a:buf, 999)
196 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200197 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200198 call assert_true(len(l) > 0)
199 call assert_equal('1', l[0].chars)
200 call assert_equal('2', l[1].chars)
201 call assert_equal('3', l[2].chars)
202 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200203 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
204 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100205 if has('win32')
206 " On Windows 'background' always defaults to dark, even though the terminal
207 " may use a light background. Therefore accept both white and black.
208 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200209 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100210 if &background == 'light'
211 call assert_equal('#ffffff', l[0].bg)
212 else
213 call assert_equal('#000000', l[0].bg)
214 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200215 endif
216
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200217 let l = term_getline(a:buf, -1)
218 call assert_equal('', l)
219 let l = term_getline(a:buf, 0)
220 call assert_equal('', l)
221 let l = term_getline(a:buf, 999)
222 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200223 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200224 call assert_equal('123', l)
225endfunc
226
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200227func Test_terminal_scrape_123()
228 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200229 let buf = term_start(cmd)
230
231 let termlist = term_list()
232 call assert_equal(1, len(termlist))
233 call assert_equal(buf, termlist[0])
234
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200235 " Nothing happens with invalid buffer number
236 call term_wait(1234)
237
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200238 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200239 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200240 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200241 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200242 call Check_123(buf)
243
244 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100245 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200246 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200247 call term_wait(buf)
248 call Check_123(buf)
249
250 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200251 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200252endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200253
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200254func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200255 call writefile(["léttrs"], 'Xtext')
256 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200257 " Run cmd with UTF-8 codepage to make the type command print the expected
258 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100259 let buf = term_start("cmd /K chcp 65001")
260 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200261 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100262 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200263 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100264 let buf = term_start("cat Xtext")
265 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200267
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100268 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
269 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200270 call assert_true(len(l) >= 7)
271 call assert_equal('l', l[0].chars)
272 call assert_equal('é', l[1].chars)
273 call assert_equal(1, l[1].width)
274 call assert_equal('t', l[2].chars)
275 call assert_equal('t', l[3].chars)
276 call assert_equal('ま', l[4].chars)
277 call assert_equal(2, l[4].width)
278 call assert_equal('r', l[5].chars)
279 call assert_equal('s', l[6].chars)
280
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100281 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200282 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100283 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200284
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100285 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200286 call delete('Xtext')
287endfunc
288
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200289func Test_terminal_scroll()
290 call writefile(range(1, 200), 'Xtext')
291 if has('win32')
292 let cmd = 'cmd /c "type Xtext"'
293 else
294 let cmd = "cat Xtext"
295 endif
296 let buf = term_start(cmd)
297
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100298 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200299 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200300 call term_wait(buf)
301 if has('win32')
302 " TODO: this should not be needed
303 sleep 100m
304 endif
305
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200306 let scrolled = buf->term_getscrolled()
307 call assert_equal(scrolled, term_getscrolled(buf))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200308 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200309 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200310 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200311 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200312 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200313 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200314
315 exe buf . 'bwipe'
316 call delete('Xtext')
317endfunc
318
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200319func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200320 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200321 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200322 call writefile(range(150), 'Xtext')
323 if has('win32')
324 call term_sendkeys(buf, "type Xtext\<CR>")
325 else
326 call term_sendkeys(buf, "cat Xtext\<CR>")
327 endif
328 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200329 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200330 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200331 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200332 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200333
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200334 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200335 call term_wait(buf)
336 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200337 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100338 call delete('Xtext')
339endfunc
340
341func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200342 " tail -f only works on Unix
343 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100344
345 call writefile(range(50), 'Xtext')
346 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100347 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100348 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100349 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100350 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
351 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100352 \ 'call feedkeys("\<C-W>N", "xt")',
353 \ ], 'XTest_postponed')
354 let buf = RunVimInTerminal('-S XTest_postponed', {})
355 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100356 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100357
358 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200359 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100360 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100361 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100362
363 " Back to Terminal-Job mode, text will scroll and show the extra line.
364 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100365 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100366
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100367 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100368 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100369 call term_wait(buf, 50)
370 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100371 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100372 call term_wait(buf, 100)
373 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100374 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
375
376 " check type of term_sendkeys() return value
377 echo type(tsk_ret)
378
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100379 call StopVimInTerminal(buf)
380 call delete('XTest_postponed')
381 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200382endfunc
383
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100384" Run diff on two dumps with different size.
385func Test_terminal_dumpdiff_size()
386 call assert_equal(1, winnr('$'))
387 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
388 call assert_equal(2, winnr('$'))
389 call assert_match('Test_incsearch_search_01.dump', getline(10))
390 call assert_match(' +++++$', getline(11))
391 call assert_match('Test_popup_command_01.dump', getline(31))
392 call assert_equal(repeat('+', 75), getline(30))
393 quit
394endfunc
395
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200396func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200397 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200398
Bram Moolenaarb2412082017-08-20 18:09:14 +0200399 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200400 let size = term_getsize('')
401 bwipe!
402 call assert_equal(5, size[0])
403
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200404 call term_start(cmd, {'term_rows': 6})
405 let size = term_getsize('')
406 bwipe!
407 call assert_equal(6, size[0])
408
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200409 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200410 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200411 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200412
413 call term_setsize('', 6, 0)
414 call assert_equal([6, 33], term_getsize(''))
415
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200416 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200417 call assert_equal([6, 35], term_getsize(''))
418
419 call term_setsize('', 7, 30)
420 call assert_equal([7, 30], term_getsize(''))
421
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200422 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200423 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200424
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200425 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
426 let size = term_getsize('')
427 bwipe!
428 call assert_equal([6, 36], size)
429
Bram Moolenaarb2412082017-08-20 18:09:14 +0200430 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200431 let size = term_getsize('')
432 bwipe!
433 call assert_equal(20, size[1])
434
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200435 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200436 let size = term_getsize('')
437 bwipe!
438 call assert_equal(26, size[1])
439
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200440 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200441 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200442 let size = term_getsize('')
443 bwipe!
444 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200445
446 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
447 let size = term_getsize('')
448 bwipe!
449 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200450
451 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200452endfunc
453
454func Test_terminal_curwin()
455 let cmd = Get_cat_123_cmd()
456 call assert_equal(1, winnr('$'))
457
458 split dummy
459 exe 'terminal ++curwin ' . cmd
460 call assert_equal(2, winnr('$'))
461 bwipe!
462
463 split dummy
464 call term_start(cmd, {'curwin': 1})
465 call assert_equal(2, winnr('$'))
466 bwipe!
467
468 split dummy
469 call setline(1, 'change')
470 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
471 call assert_equal(2, winnr('$'))
472 exe 'terminal! ++curwin ' . cmd
473 call assert_equal(2, winnr('$'))
474 bwipe!
475
476 split dummy
477 call setline(1, 'change')
478 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
479 call assert_equal(2, winnr('$'))
480 bwipe!
481
482 split dummy
483 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200484 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200485endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200486
Bram Moolenaarff546792017-11-21 14:47:57 +0100487func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200488 if s:python != ''
489 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200490 " 500 was not enough for Travis
491 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200492 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200493 echo 'This will take five seconds...'
494 let waittime = 2000
495 if has('win32')
496 let cmd = $windir . '\system32\timeout.exe 1'
497 else
498 let cmd = 'sleep 1'
499 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200500 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100501 return [cmd, waittime]
502endfunc
503
504func Test_terminal_finish_open_close()
505 call assert_equal(1, winnr('$'))
506
507 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200508
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100509 " shell terminal closes automatically
510 terminal
511 let buf = bufnr('%')
512 call assert_equal(2, winnr('$'))
513 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200514 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200515 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200516 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100517
518 " shell terminal that does not close automatically
519 terminal ++noclose
520 let buf = bufnr('%')
521 call assert_equal(2, winnr('$'))
522 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200523 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200524 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100525 call assert_equal(2, winnr('$'))
526 quit
527 call assert_equal(1, winnr('$'))
528
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200529 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200530 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200531 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200532 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200533
534 call term_start(cmd, {'term_finish': 'close'})
535 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200536 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200537 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200538 call assert_equal(1, winnr('$'))
539
540 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200541 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200542 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200543 bwipe
544
545 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200546 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200547 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200548 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200549
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200550 exe 'terminal ++hidden ++open ' . cmd
551 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200552 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200553 bwipe
554
555 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
556 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200557 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200558 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200559
560 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
561 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
562 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
563 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
564
565 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200566 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200567 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200568 call assert_equal(4, winheight(0))
569 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200570endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200571
572func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100573 if has('win32')
574 let cmd = 'cmd /c cd'
575 else
576 CheckExecutable pwd
577 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200578 endif
579 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100580 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200581 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200582
583 exe buf . 'bwipe'
584 call delete('Xdir', 'rf')
585endfunc
586
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200587func Test_terminal_cwd_failure()
588 " Case 1: Provided directory is not actually a directory. Attempt to make
589 " the file executable as well.
590 call writefile([], 'Xfile')
591 call setfperm('Xfile', 'rwx------')
592 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
593 call delete('Xfile')
594
595 " Case 2: Directory does not exist.
596 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
597
598 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100599 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200600 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100601 call mkdir('XdirNoAccess', '', '0600')
602 " return early if the directory permissions could not be set properly
603 if getfperm('XdirNoAccess')[2] == 'x'
604 call delete('XdirNoAccess', 'rf')
605 return
606 endif
607 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
608 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200609 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200610endfunc
611
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100612func Test_terminal_servername()
613 if !has('clientserver')
614 return
615 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200616 call s:test_environment("VIM_SERVERNAME", v:servername)
617endfunc
618
619func Test_terminal_version()
620 call s:test_environment("VIM_TERMINAL", string(v:version))
621endfunc
622
623func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100624 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100625 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200626 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100627 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200628 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100629 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200630 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100631 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100632 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200633 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200634 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100635
Bram Moolenaar012eb662018-03-13 17:55:27 +0100636 exe buf . 'bwipe'
637 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100638endfunc
639
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200640func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100641 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200642 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200643 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100644 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100645 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100646 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100647 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100648 endif
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200649 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200650 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200651 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200652
Bram Moolenaar012eb662018-03-13 17:55:27 +0100653 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200654endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200655
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200656func Test_terminal_list_args()
657 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
658 call assert_fails(buf . 'bwipe', 'E517')
659 exe buf . 'bwipe!'
660 call assert_equal("", bufname(buf))
661endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200662
663func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100664 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100665 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200666 " The shell or something else has a problem dealing with more than 1000
667 " characters at the same time.
668 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100669 " NPFS is used in Windows, nonblocking mode does not work properly.
670 elseif has('win32')
671 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200672 else
673 let len = 5000
674 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200675
Bram Moolenaarab505b12020-03-23 19:28:44 +0100676 for c in split('abcdefghijklmnopqrstuvwxyz', '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100677 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaard7b77702020-03-23 22:46:44 +0100678 call term_wait(buf, 1)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200679 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100680 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200681
682 " On MS-Windows there is an extra empty line below "done". Find "done" in
683 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100684 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard7b77702020-03-23 22:46:44 +0100685 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100686 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200687 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100688 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200689 endif
690 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200691
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100692 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200693 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100694 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200695 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200696 bwipe
697endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200698
699func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100700 " TODO: enable once writing to stdin works on MS-Windows
701 CheckNotMSWindows
702 CheckExecutable wc
703
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200704 call setline(1, ['one', 'two', 'three'])
705 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200706 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200707 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200708 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100709 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200710
711 call setline(1, ['one', 'two', 'three', 'four'])
712 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200713 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200714 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200715 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100716 %bwipe!
717endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200718
Bram Moolenaar21109272020-01-30 16:27:20 +0100719func Test_terminal_eof_arg()
720 CheckExecutable python
Bram Moolenaardada6d22017-09-02 17:18:35 +0200721
Bram Moolenaar21109272020-01-30 16:27:20 +0100722 call setline(1, ['print("hello")'])
723 1term ++eof=exit(123) python
724 " MS-Windows echoes the input, Unix doesn't.
725 if has('win32')
726 call WaitFor({-> getline('$') =~ 'exit(123)'})
727 call assert_equal('hello', getline(line('$') - 1))
728 else
729 call WaitFor({-> getline('$') =~ 'hello'})
730 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200731 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100732 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
733 %bwipe!
734endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200735
Bram Moolenaar21109272020-01-30 16:27:20 +0100736func Test_terminal_eof_arg_win32_ctrl_z()
737 CheckMSWindows
738 CheckExecutable python
739
740 call setline(1, ['print("hello")'])
741 1term ++eof=<C-Z> python
742 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
743 call assert_match('\^Z', getline(line('$') - 1))
744 %bwipe!
745endfunc
746
747func Test_terminal_duplicate_eof_arg()
748 CheckExecutable python
749
750 " Check the last specified ++eof arg is used and should not memory leak.
751 new
752 call setline(1, ['print("hello")'])
753 1term ++eof=<C-Z> ++eof=exit(123) python
754 " MS-Windows echoes the input, Unix doesn't.
755 if has('win32')
756 call WaitFor({-> getline('$') =~ 'exit(123)'})
757 call assert_equal('hello', getline(line('$') - 1))
758 else
759 call WaitFor({-> getline('$') =~ 'hello'})
760 call assert_equal('hello', getline('$'))
761 endif
762 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
763 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200764endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200765
766func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200767 let buf = term_start('NONE', {})
768 call assert_notequal(0, buf)
769
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200770 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200771 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100772 if has('gui_running') && !has('win32')
773 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200774 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100775 else
776 " Otherwise using a job works on all systems.
777 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200778 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200779 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200780
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200781 bwipe!
782endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200783
784func Test_terminal_special_chars()
785 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200786 CheckUnix
787
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200788 call mkdir('Xdir with spaces')
789 call writefile(['x'], 'Xdir with spaces/quoted"file')
790 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200791 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100792 " make sure the job has finished
793 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200794
795 call delete('Xdir with spaces', 'rf')
796 bwipe
797endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200798
799func Test_terminal_wrong_options()
800 call assert_fails('call term_start(&shell, {
801 \ "in_io": "file",
802 \ "in_name": "xxx",
803 \ "out_io": "file",
804 \ "out_name": "xxx",
805 \ "err_io": "file",
806 \ "err_name": "xxx"
807 \ })', 'E474:')
808 call assert_fails('call term_start(&shell, {
809 \ "out_buf": bufnr("%")
810 \ })', 'E474:')
811 call assert_fails('call term_start(&shell, {
812 \ "err_buf": bufnr("%")
813 \ })', 'E474:')
814endfunc
815
816func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200817 let cmd = Get_cat_123_cmd()
818 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
819 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100820 " ConPTY may precede escape sequence. There are things that are not so.
821 if !has('conpty')
822 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
823 call assert_match('123', readfile('Xfile')[0])
824 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200825 let g:job = term_getjob(buf)
826 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
827 call delete('Xfile')
828 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200829
830 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200831 call writefile(['one line'], 'Xfile')
832 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
833 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200834 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200835 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200836 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200837 bwipe
838 call delete('Xfile')
839 endif
840endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200841
842func TerminalTmap(remap)
843 let buf = Run_shell_in_terminal({})
844 call assert_equal('t', mode())
845
846 if a:remap
847 tmap 123 456
848 else
849 tnoremap 123 456
850 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100851 " don't use abcde, it's an existing command
852 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200853 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100854 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200855 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200856 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200857 let lnum = term_getcursor(buf)[0]
858 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100859 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200860 else
861 call assert_match('456', term_getline(buf, lnum))
862 endif
863
864 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200865 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200866 call term_wait(buf)
867
868 tunmap 123
869 tunmap 456
870 call assert_equal('', maparg('123', 't'))
871 close
872 unlet g:job
873endfunc
874
875func Test_terminal_tmap()
876 call TerminalTmap(1)
877 call TerminalTmap(0)
878endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200879
880func Test_terminal_wall()
881 let buf = Run_shell_in_terminal({})
882 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200883 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200884 call term_wait(buf)
885 exe buf . 'bwipe'
886 unlet g:job
887endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200888
Bram Moolenaar7a760922018-02-19 23:10:02 +0100889func Test_terminal_wqall()
890 let buf = Run_shell_in_terminal({})
891 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200892 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100893 call term_wait(buf)
894 exe buf . 'bwipe'
895 unlet g:job
896endfunc
897
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200898func Test_terminal_composing_unicode()
Bram Moolenaar9134f1e2019-11-29 20:26:13 +0100899 CheckNotBSD
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200900 let save_enc = &encoding
901 set encoding=utf-8
902
903 if has('win32')
904 let cmd = "cmd /K chcp 65001"
905 let lnum = [3, 6, 9]
906 else
907 let cmd = &shell
908 let lnum = [1, 3, 5]
909 endif
910
911 enew
912 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100913 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200914 call term_wait(buf, 50)
915
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200916 if has('win32')
917 call assert_equal('cmd', job_info(g:job).cmd[0])
918 else
919 call assert_equal(&shell, job_info(g:job).cmd[0])
920 endif
921
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200922 " ascii + composing
923 let txt = "a\u0308bc"
924 call term_sendkeys(buf, "echo " . txt . "\r")
925 call term_wait(buf, 50)
926 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
927 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
928 let l = term_scrape(buf, lnum[0] + 1)
929 call assert_equal("a\u0308", l[0].chars)
930 call assert_equal("b", l[1].chars)
931 call assert_equal("c", l[2].chars)
932
933 " multibyte + composing
934 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
935 call term_sendkeys(buf, "echo " . txt . "\r")
936 call term_wait(buf, 50)
937 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
938 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
939 let l = term_scrape(buf, lnum[1] + 1)
940 call assert_equal("\u304b\u3099", l[0].chars)
941 call assert_equal("\u304e", l[1].chars)
942 call assert_equal("\u304f\u3099", l[2].chars)
943 call assert_equal("\u3052", l[3].chars)
944 call assert_equal("\u3053\u3099", l[4].chars)
945
946 " \u00a0 + composing
947 let txt = "abc\u00a0\u0308"
948 call term_sendkeys(buf, "echo " . txt . "\r")
949 call term_wait(buf, 50)
950 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
951 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
952 let l = term_scrape(buf, lnum[2] + 1)
953 call assert_equal("\u00a0\u0308", l[3].chars)
954
955 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200956 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200957 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100958 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200959 let &encoding = save_enc
960endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100961
962func Test_terminal_aucmd_on_close()
963 fun Nop()
964 let s:called = 1
965 endfun
966
967 aug repro
968 au!
969 au BufWinLeave * call Nop()
970 aug END
971
972 let [cmd, waittime] = s:get_sleep_cmd()
973
974 call assert_equal(1, winnr('$'))
975 new
976 call setline(1, ['one', 'two'])
977 exe 'term ++close ' . cmd
978 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200979 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100980 call assert_equal(1, s:called)
981 bwipe!
982
983 unlet s:called
984 au! repro
985 delfunc Nop
986endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100987
988func Test_terminal_term_start_empty_command()
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({}, {'curwin' : 1, 'term_finish' : 'close'})"
994 call assert_fails(cmd, 'E474')
995 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
996 call assert_fails(cmd, 'E474')
997endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100998
999func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001000 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001001
1002 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001003 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001004
Bram Moolenaar086eb872018-03-25 21:24:12 +02001005 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001006 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001007
Bram Moolenaar086eb872018-03-25 21:24:12 +02001008 " Request the cursor position.
1009 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001010
1011 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001012 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001013
Bram Moolenaar086eb872018-03-25 21:24:12 +02001014 " End "cat" gently.
1015 call term_sendkeys(buf, "\<CR>\<C-D>")
1016
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001017 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001018 exe buf . 'bwipe'
1019 unlet g:job
1020endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001021
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001022" Run Vim, start a terminal in that Vim with the kill argument,
1023" :qall works.
1024func Run_terminal_qall_kill(line1, line2)
1025 " 1. Open a terminal window and wait for the prompt to appear
1026 " 2. set kill using term_setkill()
1027 " 3. make Vim exit, it will kill the shell
1028 let after = [
1029 \ a:line1,
1030 \ 'let buf = bufnr("%")',
1031 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1032 \ ' sleep 10m',
1033 \ 'endwhile',
1034 \ a:line2,
1035 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1036 \ 'qall',
1037 \ ]
1038 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001039 return
1040 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001041 call assert_equal("done", readfile("Xdone")[0])
1042 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001043endfunc
1044
1045" Run Vim in a terminal, then start a terminal in that Vim with a kill
1046" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001047func Test_terminal_qall_kill_arg()
1048 call Run_terminal_qall_kill('term ++kill=kill', '')
1049endfunc
1050
1051" Run Vim, start a terminal in that Vim, set the kill argument with
1052" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001053func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001054 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001055endfunc
1056
1057" Run Vim, start a terminal in that Vim without the kill argument,
1058" check that :qall does not exit, :qall! does.
1059func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001060 let after =<< trim [CODE]
1061 term
1062 let buf = bufnr("%")
1063 while term_getline(buf, 1) =~ "^\\s*$"
1064 sleep 10m
1065 endwhile
1066 set nomore
1067 au VimLeavePre * call writefile(["too early"], "Xdone")
1068 qall
1069 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1070 cquit
1071 [CODE]
1072
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001073 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001074 return
1075 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001076 call assert_equal("done", readfile("Xdone")[0])
1077 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001078endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001079
1080" Run Vim in a terminal, then start a terminal in that Vim without a kill
1081" argument, check that :confirm qall works.
1082func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001083 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001084 let buf = RunVimInTerminal('', {})
1085
1086 " Open a terminal window and wait for the prompt to appear
1087 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001088 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1089 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001090
1091 " make Vim exit, it will prompt to kill the shell
1092 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001093 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001094 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001095 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001096
1097 " close the terminal window where Vim was running
1098 quit
1099endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001100
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001101" Run Vim in a terminal, then start a terminal window with a shell and check
1102" that Vim exits if it is closed.
1103func Test_terminal_exit()
1104 CheckRunVimInTerminal
1105
1106 let lines =<< trim END
1107 let winid = win_getid()
1108 help
1109 term
1110 let termid = win_getid()
1111 call win_gotoid(winid)
1112 close
1113 call win_gotoid(termid)
1114 END
1115 call writefile(lines, 'XtermExit')
1116 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1117 let job = term_getjob(buf)
1118 call WaitForAssert({-> assert_equal("run", job_status(job))})
1119
1120 " quit the shell, it will make Vim exit
1121 call term_sendkeys(buf, "exit\<CR>")
1122 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1123
1124 call delete('XtermExit')
1125endfunc
1126
Bram Moolenaar012eb662018-03-13 17:55:27 +01001127func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001128 augroup repro
1129 au!
1130 au TerminalOpen * let s:called += 1
1131 augroup END
1132
1133 let s:called = 0
1134
1135 " Open a terminal window with :terminal
1136 terminal
1137 call assert_equal(1, s:called)
1138 bwipe!
1139
1140 " Open a terminal window with term_start()
1141 call term_start(&shell)
1142 call assert_equal(2, s:called)
1143 bwipe!
1144
1145 " Open a hidden terminal buffer with :terminal
1146 terminal ++hidden
1147 call assert_equal(3, s:called)
1148 for buf in term_list()
1149 exe buf . "bwipe!"
1150 endfor
1151
1152 " Open a hidden terminal buffer with term_start()
1153 let buf = term_start(&shell, {'hidden': 1})
1154 call assert_equal(4, s:called)
1155 exe buf . "bwipe!"
1156
1157 unlet s:called
1158 au! repro
1159endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001160
1161func Check_dump01(off)
1162 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1163 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001164 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001165endfunc
1166
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001167func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001168 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001169 let save_enc = &encoding
1170 set encoding=utf-8
1171 call assert_equal(1, winnr('$'))
1172
1173 let text = " a\u0300 e\u0302 o\u0308"
1174 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001175 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001176 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001177 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001178 let dumpline = readfile('Xdump')[0]
1179 call assert_match('|à| |ê| |ö', dumpline)
1180
1181 call StopVimInTerminal(buf)
1182 call delete('Xcomposing')
1183 call delete('Xdump')
1184 let &encoding = save_enc
1185endfunc
1186
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001187" just testing basic functionality.
1188func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001189 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001190 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001191 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001192 call assert_equal(2, winnr('$'))
1193 call assert_equal(20, line('$'))
1194 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001195
1196 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001197 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001198 call assert_equal(buf, buf2)
1199 call assert_notequal('one two three four five', trim(getline(1)))
1200
1201 " Load the first dump again in the same window
1202 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1203 call assert_equal(buf, buf2)
1204 call Check_dump01(0)
1205
1206 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1207 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1208 new
1209 let closedbuf = winbufnr('')
1210 quit
1211 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1212
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001213 quit
1214endfunc
1215
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001216func Test_terminal_dumpload_dump()
1217 CheckRunVimInTerminal
1218
1219 let lines =<< trim END
1220 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1221 END
1222 call writefile(lines, 'XtermDumpload')
1223 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1224 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1225
1226 call StopVimInTerminal(buf)
1227 call delete('XtermDumpload')
1228endfunc
1229
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001230func Test_terminal_dumpdiff()
1231 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001232 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001233 call assert_equal(2, winnr('$'))
1234 call assert_equal(62, line('$'))
1235 call Check_dump01(0)
1236 call Check_dump01(42)
1237 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1238 quit
1239endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001240
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001241func Test_terminal_dumpdiff_swap()
1242 call assert_equal(1, winnr('$'))
1243 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1244 call assert_equal(2, winnr('$'))
1245 call assert_equal(62, line('$'))
1246 call assert_match('Test_popup_command_01.dump', getline(21))
1247 call assert_match('Test_popup_command_03.dump', getline(42))
1248 call assert_match('Undo', getline(3))
1249 call assert_match('three four five', getline(45))
1250
1251 normal s
1252 call assert_match('Test_popup_command_03.dump', getline(21))
1253 call assert_match('Test_popup_command_01.dump', getline(42))
1254 call assert_match('three four five', getline(3))
1255 call assert_match('Undo', getline(45))
1256 quit
1257endfunc
1258
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001259func Test_terminal_dumpdiff_options()
1260 set laststatus=0
1261 call assert_equal(1, winnr('$'))
1262 let height = winheight(0)
1263 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1264 call assert_equal(2, winnr('$'))
1265 call assert_equal(height, winheight(winnr()))
1266 call assert_equal(33, winwidth(winnr()))
1267 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1268 quit
1269
1270 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001271 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1272 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001273 call assert_equal(&columns, winwidth(0))
1274 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001275 call assert_equal('something else', bufname('%'))
1276 quit
1277
1278 call assert_equal(1, winnr('$'))
1279 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1280 call assert_equal(1, winnr('$'))
1281 bwipe
1282
1283 set laststatus&
1284endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001285
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001286func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001287 call assert_equal(1, winnr('$'))
1288
1289 " Use the title termcap entries to output the escape sequence.
1290 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001291 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001292 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001293 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001294 \ 'redraw',
1295 \ "set t_ts=",
1296 \ ], 'Xscript')
1297 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001298 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001299 call assert_equal('Xtextfile', expand('%:t'))
1300 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001301 return buf
1302endfunc
1303
1304func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001305 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001306 let buf = Api_drop_common('')
1307 call assert_equal(0, &bin)
1308 call assert_equal('', &fenc)
1309
1310 call StopVimInTerminal(buf)
1311 call delete('Xscript')
1312 bwipe Xtextfile
1313endfunc
1314
1315func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001316 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001317 let buf = Api_drop_common(',{"bin":1}')
1318 call assert_equal(1, &bin)
1319
1320 call StopVimInTerminal(buf)
1321 call delete('Xscript')
1322 bwipe Xtextfile
1323endfunc
1324
1325func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001326 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001327 let buf = Api_drop_common(',{"binary":1}')
1328 call assert_equal(1, &bin)
1329
1330 call StopVimInTerminal(buf)
1331 call delete('Xscript')
1332 bwipe Xtextfile
1333endfunc
1334
1335func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001336 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001337 set binary
1338 let buf = Api_drop_common(',{"nobin":1}')
1339 call assert_equal(0, &bin)
1340
1341 call StopVimInTerminal(buf)
1342 call delete('Xscript')
1343 bwipe Xtextfile
1344 set nobinary
1345endfunc
1346
1347func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001348 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001349 set binary
1350 let buf = Api_drop_common(',{"nobinary":1}')
1351 call assert_equal(0, &bin)
1352
1353 call StopVimInTerminal(buf)
1354 call delete('Xscript')
1355 bwipe Xtextfile
1356 set nobinary
1357endfunc
1358
1359func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001360 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001361 let buf = Api_drop_common(',{"ff":"dos"}')
1362 call assert_equal("dos", &ff)
1363
1364 call StopVimInTerminal(buf)
1365 call delete('Xscript')
1366 bwipe Xtextfile
1367endfunc
1368
1369func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001370 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001371 let buf = Api_drop_common(',{"fileformat":"dos"}')
1372 call assert_equal("dos", &ff)
1373
1374 call StopVimInTerminal(buf)
1375 call delete('Xscript')
1376 bwipe Xtextfile
1377endfunc
1378
1379func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001380 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001381 let buf = Api_drop_common(',{"enc":"utf-16"}')
1382 call assert_equal("utf-16", &fenc)
1383
1384 call StopVimInTerminal(buf)
1385 call delete('Xscript')
1386 bwipe Xtextfile
1387endfunc
1388
1389func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001390 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001391 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1392 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001393
1394 call StopVimInTerminal(buf)
1395 call delete('Xscript')
1396 bwipe Xtextfile
1397endfunc
1398
1399func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001400 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001401 let firstwinid = win_getid()
1402 split Xtextfile
1403 let textfile_winid = win_getid()
1404 call assert_equal(2, winnr('$'))
1405 call win_gotoid(firstwinid)
1406
1407 " Use the title termcap entries to output the escape sequence.
1408 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001409 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001410 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1411 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1412 \ 'redraw',
1413 \ "set t_ts=",
1414 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001415 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001416 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001417 call assert_equal(textfile_winid, win_getid())
1418
1419 call StopVimInTerminal(buf)
1420 call delete('Xscript')
1421 bwipe Xtextfile
1422endfunc
1423
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001424func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001425 let g:called_bufnum = a:bufnum
1426 let g:called_arg = a:arg
1427endfunc
1428
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001429func WriteApiCall(funcname)
1430 " Use the title termcap entries to output the escape sequence.
1431 call writefile([
1432 \ 'set title',
1433 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1434 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1435 \ 'redraw',
1436 \ "set t_ts=",
1437 \ ], 'Xscript')
1438endfunc
1439
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001440func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001441 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001442
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001443 unlet! g:called_bufnum
1444 unlet! g:called_arg
1445
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001446 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001447
1448 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001449 let buf = RunVimInTerminal('-S Xscript', {})
1450 call WaitFor({-> exists('g:called_bufnum')})
1451 call assert_equal(buf, g:called_bufnum)
1452 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001453 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001454
1455 unlet! g:called_bufnum
1456 unlet! g:called_arg
1457
1458 " Enable explicitly
1459 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1460 call WaitFor({-> exists('g:called_bufnum')})
1461 call assert_equal(buf, g:called_bufnum)
1462 call assert_equal(['hello', 123], g:called_arg)
1463 call StopVimInTerminal(buf)
1464
1465 unlet! g:called_bufnum
1466 unlet! g:called_arg
1467
1468 func! ApiCall_TryThis(bufnum, arg)
1469 let g:called_bufnum2 = a:bufnum
1470 let g:called_arg2 = a:arg
1471 endfunc
1472
1473 call WriteApiCall('ApiCall_TryThis')
1474
1475 " Use prefix match
1476 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1477 call WaitFor({-> exists('g:called_bufnum2')})
1478 call assert_equal(buf, g:called_bufnum2)
1479 call assert_equal(['hello', 123], g:called_arg2)
1480 call StopVimInTerminal(buf)
1481
1482 unlet! g:called_bufnum2
1483 unlet! g:called_arg2
1484
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001485 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001486 delfunction! ApiCall_TryThis
1487 unlet! g:called_bufnum2
1488 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001489endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001490
1491func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001492 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001493
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001494 func! TryThis(bufnum, arg)
1495 let g:called_bufnum3 = a:bufnum
1496 let g:called_arg3 = a:arg
1497 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001498
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001499 call WriteApiCall('TryThis')
1500
1501 unlet! g:called_bufnum3
1502 unlet! g:called_arg3
1503
1504 " Not permitted
1505 call ch_logfile('Xlog', 'w')
1506 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1507 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1508 call assert_false(exists('g:called_bufnum3'))
1509 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001510 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001511
1512 " No match
1513 call ch_logfile('Xlog', 'w')
1514 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1515 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1516 call assert_false(exists('g:called_bufnum3'))
1517 call assert_false(exists('g:called_arg3'))
1518 call StopVimInTerminal(buf)
1519
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001520 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001521 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001522 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001523 delfunction! TryThis
1524 unlet! g:called_bufnum3
1525 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001526endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001527
Bram Moolenaara997b452018-04-17 23:24:06 +02001528let s:caught_e937 = 0
1529
1530func Tapi_Delete(bufnum, arg)
1531 try
1532 execute 'bdelete!' a:bufnum
1533 catch /E937:/
1534 let s:caught_e937 = 1
1535 endtry
1536endfunc
1537
1538func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001539 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001540
1541 call WriteApiCall('Tapi_Delete')
1542 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001543 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001544
1545 call StopVimInTerminal(buf)
1546 call delete('Xscript')
1547 call ch_logfile('', '')
1548endfunc
1549
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001550func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001551 if !exists('*term_getansicolors')
1552 throw 'Skipped: term_getansicolors() not supported'
1553 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001554 let colors = [
1555 \ '#000000', '#e00000',
1556 \ '#00e000', '#e0e000',
1557 \ '#0000e0', '#e000e0',
1558 \ '#00e0e0', '#e0e0e0',
1559 \ '#808080', '#ff4040',
1560 \ '#40ff40', '#ffff40',
1561 \ '#4040ff', '#ff40ff',
1562 \ '#40ffff', '#ffffff',
1563 \]
1564
1565 let buf = Run_shell_in_terminal({})
1566 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001567 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001568 call term_wait(buf)
1569
1570 exe buf . 'bwipe'
1571endfunc
1572
1573let s:test_colors = [
1574 \ '#616e64', '#0d0a79',
1575 \ '#6d610d', '#0a7373',
1576 \ '#690d0a', '#6d696e',
1577 \ '#0d0a6f', '#616e0d',
1578 \ '#0a6479', '#6d0d0a',
1579 \ '#617373', '#0d0a69',
1580 \ '#6d690d', '#0a6e6f',
1581 \ '#610d0a', '#6e6479',
1582 \]
1583
1584func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001585 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001586 if !exists('*term_getansicolors')
1587 throw 'Skipped: term_getansicolors() not supported'
1588 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001589 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1590 let buf = Run_shell_in_terminal({})
1591 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001592 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001593 call term_wait(buf)
1594
1595 exe buf . 'bwipe'
1596 unlet g:terminal_ansi_colors
1597endfunc
1598
1599func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001600 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001601 if !exists('*term_getansicolors')
1602 throw 'Skipped: term_getansicolors() not supported'
1603 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001604 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1605 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1606 call assert_equal(s:test_colors, term_getansicolors(buf))
1607
1608 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001609 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001610
1611 let colors = [
1612 \ 'ivory', 'AliceBlue',
1613 \ 'grey67', 'dark goldenrod',
1614 \ 'SteelBlue3', 'PaleVioletRed4',
1615 \ 'MediumPurple2', 'yellow2',
1616 \ 'RosyBrown3', 'OrangeRed2',
1617 \ 'white smoke', 'navy blue',
1618 \ 'grey47', 'gray97',
1619 \ 'MistyRose2', 'DodgerBlue4',
1620 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001621 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001622
1623 let colors[4] = 'Invalid'
1624 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1625
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001626 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001627 call term_wait(buf)
1628 exe buf . 'bwipe'
1629endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001630
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001631func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001632 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001633
1634 " Use all the ANSI colors.
1635 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001636 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001637 \ 'hi Tblack ctermfg=0 ctermbg=8',
1638 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1639 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1640 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1641 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1642 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1643 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1644 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1645 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1646 \ 'hi Tred ctermfg=9 ctermbg=1',
1647 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1648 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1649 \ 'hi Tblue ctermfg=12 ctermbg=4',
1650 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1651 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1652 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001653 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1654 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1655 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001656 \ '',
1657 \ 'call matchadd("Tblack", "A")',
1658 \ 'call matchadd("Tdarkred", "B")',
1659 \ 'call matchadd("Tdarkgreen", "C")',
1660 \ 'call matchadd("Tbrown", "D")',
1661 \ 'call matchadd("Tdarkblue", "E")',
1662 \ 'call matchadd("Tdarkmagenta", "F")',
1663 \ 'call matchadd("Tdarkcyan", "G")',
1664 \ 'call matchadd("Tlightgrey", "H")',
1665 \ 'call matchadd("Tdarkgrey", "I")',
1666 \ 'call matchadd("Tred", "J")',
1667 \ 'call matchadd("Tgreen", "K")',
1668 \ 'call matchadd("Tyellow", "L")',
1669 \ 'call matchadd("Tblue", "M")',
1670 \ 'call matchadd("Tmagenta", "N")',
1671 \ 'call matchadd("Tcyan", "O")',
1672 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001673 \ 'call matchadd("TdarkredBold", "X")',
1674 \ 'call matchadd("TgreenBold", "Y")',
1675 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001676 \ 'redraw',
1677 \ ], 'Xcolorscript')
1678 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1679 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1680
1681 call term_sendkeys(buf, ":q\<CR>")
1682 call StopVimInTerminal(buf)
1683 call delete('Xcolorscript')
1684endfunc
1685
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001686func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001687 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001688 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001689 let text = []
1690 for n in range(10)
1691 call add(text, repeat(n, 50))
1692 endfor
1693 call writefile(text, 'Xwinsize')
1694 let buf = RunVimInTerminal('Xwinsize', {})
1695 let win = bufwinid(buf)
1696 call assert_equal([6, 40], term_getsize(buf))
1697 call assert_equal(6, winheight(win))
1698 call assert_equal(40, winwidth(win))
1699
1700 " resizing the window doesn't resize the terminal.
1701 resize 10
1702 vertical resize 60
1703 call assert_equal([6, 40], term_getsize(buf))
1704 call assert_equal(10, winheight(win))
1705 call assert_equal(60, winwidth(win))
1706
1707 call StopVimInTerminal(buf)
1708 call delete('Xwinsize')
1709
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001710 call assert_fails('set termwinsize=40', 'E474')
1711 call assert_fails('set termwinsize=10+40', 'E474')
1712 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001713
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001714 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001715endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001716
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001717func Test_terminal_termwinsize_option_zero()
1718 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001719 let buf = Run_shell_in_terminal({})
1720 let win = bufwinid(buf)
1721 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001722 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001723 call term_wait(buf)
1724 exe buf . 'bwipe'
1725
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001726 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001727 let buf = Run_shell_in_terminal({})
1728 let win = bufwinid(buf)
1729 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001730 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001731 call term_wait(buf)
1732 exe buf . 'bwipe'
1733
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001734 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001735 let buf = Run_shell_in_terminal({})
1736 let win = bufwinid(buf)
1737 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001738 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001739 call term_wait(buf)
1740 exe buf . 'bwipe'
1741
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001742 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001743endfunc
1744
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001745func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001746 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001747 vsplit
1748 let buf = Run_shell_in_terminal({})
1749 let win = bufwinid(buf)
1750 call assert_inrange(10, 1000, winheight(win))
1751 call assert_inrange(50, 1000, winwidth(win))
1752 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1753
1754 resize 15
1755 vertical resize 60
1756 redraw
1757 call assert_equal([15, 60], term_getsize(buf))
1758 call assert_equal(15, winheight(win))
1759 call assert_equal(60, winwidth(win))
1760
1761 resize 7
1762 vertical resize 30
1763 redraw
1764 call assert_equal([10, 50], term_getsize(buf))
1765 call assert_equal(7, winheight(win))
1766 call assert_equal(30, winwidth(win))
1767
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001768 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001769 call term_wait(buf)
1770 exe buf . 'bwipe'
1771
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001772 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001773 let buf = Run_shell_in_terminal({})
1774 let win = bufwinid(buf)
1775 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001776 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001777 call term_wait(buf)
1778 exe buf . 'bwipe'
1779
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001780 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001781endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001782
1783func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001784 " make three tabpages, terminal in the middle
1785 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001786 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001787 tabnew
1788 tabprev
1789 call assert_equal(1, winnr('$'))
1790 call assert_equal(2, tabpagenr())
1791 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001792
1793 let buf = Run_shell_in_terminal({})
1794 let termwin = bufwinid(buf)
1795 set termwinkey=<C-L>
1796 call feedkeys("\<C-L>w", 'tx')
1797 call assert_equal(thiswin, win_getid())
1798 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001799 call assert_equal(termwin, win_getid())
1800
Bram Moolenaar997d4242019-09-14 21:23:40 +02001801 if has('langmap')
1802 set langmap=xjyk
1803 call feedkeys("\<C-L>x", 'tx')
1804 call assert_equal(thiswin, win_getid())
1805 call feedkeys("\<C-W>y", 'tx')
1806 call assert_equal(termwin, win_getid())
1807 set langmap=
1808 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001809
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001810 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001811 call assert_equal(3, tabpagenr())
1812 tabprev
1813 call assert_equal(2, tabpagenr())
1814 call assert_equal(termwin, win_getid())
1815
1816 call feedkeys("\<C-L>gT", "xt")
1817 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001818 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001819 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001820 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001821
1822 let job = term_getjob(buf)
1823 call feedkeys("\<C-L>\<C-C>", 'tx')
1824 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001825
1826 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001827 tabnext
1828 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001829 tabprev
1830 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001831endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001832
1833func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001834 CheckUnix
1835
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001836 call writefile([
1837 \ '#!/bin/sh',
1838 \ 'echo "this is standard error" >&2',
1839 \ 'echo "this is standard out" >&1',
1840 \ ], 'Xechoerrout.sh')
1841 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1842
1843 let outfile = 'Xtermstdout'
1844 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001845
1846 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1847 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001848 call assert_equal('this is standard error', term_getline(buf, 1))
1849
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001850 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001851 exe buf . 'bwipe'
1852 call delete('Xechoerrout.sh')
1853 call delete(outfile)
1854endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001855
Bram Moolenaare219f732019-11-30 15:34:08 +01001856func Test_termwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001857 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001858
1859 " Let the terminal output more than 'termwinscroll' lines, some at the start
1860 " will be dropped.
1861 exe 'set termwinscroll=' . &lines
1862 let buf = term_start('/bin/sh')
1863 for i in range(1, &lines)
1864 call feedkeys("echo " . i . "\<CR>", 'xt')
1865 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1866 endfor
1867 " Go to Terminal-Normal mode to update the buffer.
1868 call feedkeys("\<C-W>N", 'xt')
1869 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1870
1871 " Every "echo nr" must only appear once
1872 let lines = getline(1, line('$'))
1873 for i in range(&lines - len(lines) / 2 + 2, &lines)
1874 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1875 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1876 endfor
1877
1878 exe buf . 'bwipe!'
1879endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001880
Bram Moolenaar875cf872018-07-08 20:49:07 +02001881" Resizing the terminal window caused an ml_get error.
1882" TODO: This does not reproduce the original problem.
1883func Test_terminal_resize()
1884 set statusline=x
1885 terminal
1886 call assert_equal(2, winnr('$'))
1887
1888 " Fill the terminal with text.
1889 if has('win32')
1890 call feedkeys("dir\<CR>", 'xt')
1891 else
1892 call feedkeys("ls\<CR>", 'xt')
1893 endif
1894 " Go to Terminal-Normal mode for a moment.
1895 call feedkeys("\<C-W>N", 'xt')
1896 " Open a new window
1897 call feedkeys("i\<C-W>n", 'xt')
1898 call assert_equal(3, winnr('$'))
1899 redraw
1900
1901 close
1902 call assert_equal(2, winnr('$'))
1903 call feedkeys("exit\<CR>", 'xt')
1904 set statusline&
1905endfunc
1906
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001907" must be nearly the last, we can't go back from GUI to terminal
1908func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001909 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001910
1911 " Ignore the "failed to create input context" error.
1912 call test_ignore_error('E285:')
1913
1914 gui -f
1915
1916 call assert_equal(1, winnr('$'))
1917 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001918 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001919 call term_wait(buf)
1920
1921 " closing window wipes out the terminal buffer a with finished job
1922 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1923 call assert_equal("", bufname(buf))
1924
1925 unlet g:job
1926endfunc
1927
1928func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001929 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001930 set guioptions+=!
1931
1932 let filename = 'Xtestscript'
1933 if has('win32')
1934 let filename .= '.bat'
1935 let prefix = ''
1936 let contents = ['@echo off', 'exit %1']
1937 else
1938 let filename .= '.sh'
1939 let prefix = './'
1940 let contents = ['#!/bin/sh', 'exit $1']
1941 endif
1942 call writefile(contents, filename)
1943 call setfperm(filename, 'rwxrwx---')
1944
1945 " Check if v:shell_error is equal to the exit status.
1946 let exitval = 0
1947 execute printf(':!%s%s %d', prefix, filename, exitval)
1948 call assert_equal(exitval, v:shell_error)
1949
1950 let exitval = 9
1951 execute printf(':!%s%s %d', prefix, filename, exitval)
1952 call assert_equal(exitval, v:shell_error)
1953
1954 set guioptions&
1955 call delete(filename)
1956endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001957
1958func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001959 CheckUnix
1960
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001961 term ++hidden cat
1962 let bnr = bufnr('$')
1963 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1964 exe 'sbuf ' . bnr
1965 call assert_equal('terminal', &buftype)
1966 call term_sendkeys(bnr, "asdf\<CR>")
1967 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1968 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001969 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001970 bwipe!
1971endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001972
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001973func Test_terminal_switch_mode()
1974 term
1975 let bnr = bufnr('$')
1976 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1977 call feedkeys("\<C-W>N", 'xt')
1978 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1979 call feedkeys("A", 'xt')
1980 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1981 call feedkeys("\<C-W>N", 'xt')
1982 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1983 call feedkeys("I", 'xt')
1984 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1985 call feedkeys("\<C-W>Nv", 'xt')
1986 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1987 call feedkeys("I", 'xt')
1988 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1989 call feedkeys("\<C-W>Nv", 'xt')
1990 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1991 call feedkeys("A", 'xt')
1992 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1993 bwipe!
1994endfunc
1995
Bram Moolenaara3817732019-10-16 16:31:44 +02001996func Test_terminal_normal_mode()
1997 CheckRunVimInTerminal
1998
1999 " Run Vim in a terminal and open a terminal window to run Vim in.
2000 let lines =<< trim END
2001 call setline(1, range(11111, 11122))
2002 3
2003 END
2004 call writefile(lines, 'XtermNormal')
2005 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
2006 call term_wait(buf)
2007
2008 call term_sendkeys(buf, "\<C-W>N")
2009 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
2010 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
2011
2012 call term_sendkeys(buf, ":set culopt=number\r")
2013 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
2014
2015 call term_sendkeys(buf, ":set culopt=line\r")
2016 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
2017
2018 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
2019 call StopVimInTerminal(buf)
2020 call delete('XtermNormal')
2021endfunc
2022
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002023func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002024 CheckUnix
2025
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002026 call assert_equal(1, winnr('$'))
2027 term ++hidden ++close ls
2028 let bnr = bufnr('$')
2029 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2030 call WaitForAssert({-> assert_false(bufexists(bnr))})
2031 call assert_equal(1, winnr('$'))
2032endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002033
2034func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002035 " This test does not pass through ConPTY.
2036 if has('conpty')
2037 return
2038 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002039 let contents = [
2040 \ [ 'One', '', 'X' ],
2041 \ [ 'Two', '', '' ],
2042 \ [ 'Three' ] + repeat([''], 30)
2043 \ ]
2044
2045 for c in contents
2046 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002047 if has('win32')
2048 term cmd /c type Xfile
2049 else
2050 term cat Xfile
2051 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002052 let bnr = bufnr('$')
2053 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2054 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002055 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002056 call assert_equal(c, getline(1, line('$')))
2057 quit
2058 endfor
2059
2060 call delete('Xfile')
2061endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002062
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002063func Test_terminal_no_job()
Bram Moolenaar077ff432019-10-28 00:42:21 +01002064 if has('win32')
2065 let cmd = 'cmd /c ""'
2066 else
2067 CheckExecutable false
2068 let cmd = 'false'
2069 endif
2070 let term = term_start(cmd, {'term_finish': 'close'})
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002071 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2072endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002073
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002074func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002075 CheckUnix
2076
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002077 let buf = Run_shell_in_terminal({})
2078
2079 " Wait for the shell to display a prompt.
2080 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2081
2082 " Hide the cursor.
2083 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2084 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2085
2086 " Show the cursor.
2087 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002088 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002089
2090 " Change color of cursor.
2091 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2092 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2093 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2094 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2095 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2096
2097 " Make cursor a blinking block.
2098 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2099 call WaitForAssert({-> assert_equal([1, 1],
2100 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2101
2102 " Make cursor a steady block.
2103 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2104 call WaitForAssert({-> assert_equal([0, 1],
2105 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2106
2107 " Make cursor a blinking underline.
2108 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2109 call WaitForAssert({-> assert_equal([1, 2],
2110 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2111
2112 " Make cursor a steady underline.
2113 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2114 call WaitForAssert({-> assert_equal([0, 2],
2115 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2116
2117 " Make cursor a blinking vertical bar.
2118 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2119 call WaitForAssert({-> assert_equal([1, 3],
2120 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2121
2122 " Make cursor a steady vertical bar.
2123 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2124 call WaitForAssert({-> assert_equal([0, 3],
2125 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2126
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002127 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002128endfunc
2129
Bram Moolenaar1e115362019-01-09 23:01:02 +01002130func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002131 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002132 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002133 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002134 call assert_equal('', term_gettitle(bufnr('$') + 1))
2135
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002136 if !has('title') || &title == 0 || empty(&t_ts)
2137 throw "Skipped: can't get/set title"
2138 endif
2139
Bram Moolenaar1e115362019-01-09 23:01:02 +01002140 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002141 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002142 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2143 call term_sendkeys(term, ":e Xfoo\r")
2144 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002145 else
2146 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002147 call term_sendkeys(term, ":e Xfoo\r")
2148 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002149 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002150
Bram Moolenaar1e115362019-01-09 23:01:02 +01002151 call term_sendkeys(term, ":set titlestring=foo\r")
2152 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2153
2154 exe term . 'bwipe!'
2155endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002156
2157" When drawing the statusline the cursor position may not have been updated
2158" yet.
2159" 1. create a terminal, make it show 2 lines
2160" 2. 0.5 sec later: leave terminal window, execute "i"
2161" 3. 0.5 sec later: clear terminal window, now it's 1 line
2162" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2163" 4. 0.5 sec later: should be done, clean up
2164func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002165 CheckUnix
2166
Bram Moolenaar10772302019-01-20 18:25:54 +01002167 set statusline=x
2168 terminal
2169 let tbuf = bufnr('')
2170 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2171 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2172 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2173 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2174
2175 sleep 2
2176 exe tbuf . 'bwipe!'
2177 au! BufLeave
2178 set statusline=
2179endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002180
2181func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002182 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002183
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002184 " split, go to the bottom-right window
2185 split
2186 wincmd j
2187 set splitright
2188
2189 call writefile([
2190 \ 'echo getwinpos()',
2191 \ ], 'XTest_getwinpos')
2192 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2193 call term_wait(buf)
2194
2195 " Find the output of getwinpos() in the bottom line.
2196 let rows = term_getsize(buf)[0]
2197 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2198 let line = term_getline(buf, rows)
2199 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2200 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2201
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002202 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002203 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002204 " In the GUI it can be more, let's assume a 20 x 14 cell.
2205 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002206 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002207 let winpos = 50->getwinpos()
2208 call assert_equal(xroot, winpos[0])
2209 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002210 let [winrow, wincol] = win_screenpos('.')
2211 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2212 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2213 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2214 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002215
2216 call term_wait(buf)
2217 call term_sendkeys(buf, ":q\<CR>")
2218 call StopVimInTerminal(buf)
2219 call delete('XTest_getwinpos')
2220 exe buf . 'bwipe!'
2221 set splitright&
2222 only!
2223endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002224
2225func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002226 " somehow doesn't work on MS-Windows
2227 CheckUnix
2228 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002229
2230 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002231 call writefile(["\<Esc>[?1047h"], 'Xtext')
2232 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002233 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2234
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002235 call writefile(["\<Esc>[?1047l"], 'Xtext')
2236 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002237 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002238
2239 call term_sendkeys(buf, "exit\r")
2240 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002241 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002242endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002243
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002244func Test_terminal_shell_option()
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002245 if has('unix')
2246 " exec is a shell builtin command, should fail without a shell.
2247 term exec ls runtest.vim
2248 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
2249 bwipe!
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002250
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002251 term ++shell exec ls runtest.vim
2252 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2253 bwipe!
2254 elseif has('win32')
2255 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar36ec6f62019-11-05 22:38:47 +01002256 try
2257 term dir /b runtest.vim
2258 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
2259 catch /CreateProcess/
2260 " ignore
2261 endtry
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002262 bwipe!
2263
2264 term ++shell dir /b runtest.vim
2265 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2266 bwipe!
2267 endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002268endfunc
2269
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002270func Test_terminal_setapi_and_call()
Bram Moolenaar21109272020-01-30 16:27:20 +01002271 CheckRunVimInTerminal
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002272
2273 call WriteApiCall('Tapi_TryThis')
2274 call ch_logfile('Xlog', 'w')
2275
2276 unlet! g:called_bufnum
2277 unlet! g:called_arg
2278
Bram Moolenaar21109272020-01-30 16:27:20 +01002279 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002280 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2281 call assert_false(exists('g:called_bufnum'))
2282 call assert_false(exists('g:called_arg'))
2283
Bram Moolenaar21109272020-01-30 16:27:20 +01002284 eval buf->term_setapi('Tapi_')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002285 call term_sendkeys(buf, ":set notitle\<CR>")
2286 call term_sendkeys(buf, ":source Xscript\<CR>")
2287 call WaitFor({-> exists('g:called_bufnum')})
2288 call assert_equal(buf, g:called_bufnum)
2289 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar21109272020-01-30 16:27:20 +01002290
2291 call StopVimInTerminal(buf)
2292
2293 call delete('Xscript')
2294 call ch_logfile('')
2295 call delete('Xlog')
2296 unlet! g:called_bufnum
2297 unlet! g:called_arg
2298endfunc
2299
2300func Test_terminal_api_arg()
2301 CheckRunVimInTerminal
2302
2303 call WriteApiCall('Tapi_TryThis')
2304 call ch_logfile('Xlog', 'w')
2305
2306 unlet! g:called_bufnum
2307 unlet! g:called_arg
2308
2309 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2310 let buf = bufnr('%')
2311 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2312 call assert_false(exists('g:called_bufnum'))
2313 call assert_false(exists('g:called_arg'))
2314
2315 call StopVimInTerminal(buf)
2316
2317 call ch_logfile('Xlog', 'w')
2318
2319 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2320 let buf = bufnr('%')
2321 call WaitFor({-> exists('g:called_bufnum')})
2322 call assert_equal(buf, g:called_bufnum)
2323 call assert_equal(['hello', 123], g:called_arg)
2324
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002325 call StopVimInTerminal(buf)
2326
2327 call delete('Xscript')
2328 call ch_logfile('')
2329 call delete('Xlog')
2330 unlet! g:called_bufnum
2331 unlet! g:called_arg
2332endfunc
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002333
Bram Moolenaar9b9be002020-03-22 14:41:22 +01002334func Test_terminal_invalid_arg()
2335 call assert_fails('terminal ++xyz', 'E181:')
2336endfunc
2337
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002338func Test_terminal_in_popup()
2339 CheckRunVimInTerminal
2340
2341 let text =<< trim END
2342 some text
2343 to edit
2344 in a popup window
2345 END
2346 call writefile(text, 'Xtext')
Bram Moolenaar3180fe62020-02-02 13:47:06 +01002347 let cmd = GetVimCommandCleanTerm()
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002348 let lines = [
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01002349 \ 'set t_u7=',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002350 \ 'call setline(1, range(20))',
2351 \ 'hi PopTerm ctermbg=grey',
2352 \ 'func OpenTerm(setColor)',
Bram Moolenaar353c3512020-03-15 14:19:26 +01002353 \ " set noruler",
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002354 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
Bram Moolenaar57c33952020-02-29 16:09:16 +01002355 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002356 \ ' if a:setColor',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002357 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002358 \ ' endif',
2359 \ 'endfunc',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002360 \ 'func HidePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002361 \ ' call popup_hide(g:winid)',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002362 \ 'endfunc',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002363 \ 'func ClosePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002364 \ ' call popup_close(g:winid)',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002365 \ 'endfunc',
2366 \ 'func ReopenPopup()',
2367 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
2368 \ 'endfunc',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002369 \ ]
2370 call writefile(lines, 'XtermPopup')
2371 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002372 call term_wait(buf, 200)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002373 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002374 call term_wait(buf, 200)
Bram Moolenaarc2adc002020-02-14 17:05:18 +01002375 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002376 call term_wait(buf, 200)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002377 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002378 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
2379
2380 call term_sendkeys(buf, ":q\<CR>")
2381 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
2382
2383 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002384 call term_wait(buf, 300)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002385 call term_sendkeys(buf, ":set hlsearch\<CR>")
2386 call term_sendkeys(buf, "/edit\<CR>")
2387 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
2388
Bram Moolenaar1af5ce02020-02-06 11:54:35 +01002389 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
2390 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
2391 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar67021882020-02-07 22:20:53 +01002392 call term_wait(buf, 100)
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002393
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002394 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
2395 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
2396
2397 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
2398 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002399
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002400 " Go to terminal-Normal mode and visually select text.
2401 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
2402 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
2403
2404 " Back to job mode, redraws
2405 call term_sendkeys(buf, "A")
2406 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
2407
2408 call term_wait(buf, 100)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002409 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002410 call term_wait(buf, 200) " wait for terminal to vanish
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002411
2412 call StopVimInTerminal(buf)
Bram Moolenaar19398262020-03-14 15:28:08 +01002413 call delete('Xtext')
2414 call delete('XtermPopup')
2415endfunc
2416
2417" Check a terminal in popup window uses the default mininum size.
2418func Test_terminal_in_popup_min_size()
2419 CheckRunVimInTerminal
2420
2421 let text =<< trim END
2422 another text
2423 to show
2424 in a popup window
2425 END
2426 call writefile(text, 'Xtext')
2427 let lines = [
2428 \ 'set t_u7=',
2429 \ 'call setline(1, range(20))',
2430 \ 'hi PopTerm ctermbg=grey',
2431 \ 'func OpenTerm()',
2432 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
2433 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2434 \ 'endfunc',
2435 \ ]
2436 call writefile(lines, 'XtermPopup')
2437 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002438 call term_wait(buf, 200)
2439 call term_sendkeys(buf, ":set noruler\<CR>")
Bram Moolenaar19398262020-03-14 15:28:08 +01002440 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
2441 call term_wait(buf, 100)
2442 call term_sendkeys(buf, ":\<CR>")
2443 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
2444
2445 call term_wait(buf, 100)
2446 call term_sendkeys(buf, ":q\<CR>")
2447 call term_wait(buf, 100) " wait for terminal to vanish
2448 call StopVimInTerminal(buf)
2449 call delete('Xtext')
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002450 call delete('XtermPopup')
2451endfunc
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002452
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002453func Test_double_popup_terminal()
2454 let buf1 = term_start(&shell, #{hidden: 1})
2455 let win1 = popup_create(buf1, {})
2456 let buf2 = term_start(&shell, #{hidden: 1})
2457 let win2 = popup_create(buf2, {})
2458 call popup_close(win1)
2459 call popup_close(win2)
2460 exe buf1 .. 'bwipe!'
2461 exe buf2 .. 'bwipe!'
2462endfunc
2463
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002464func Test_issue_5607()
2465 let wincount = winnr('$')
2466 exe 'terminal' &shell &shellcmdflag 'exit'
2467 let job = term_getjob(bufnr())
2468 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2469
2470 let old_wincolor = &wincolor
2471 try
2472 set wincolor=
2473 finally
2474 let &wincolor = old_wincolor
2475 bw!
2476 endtry
2477endfunc
Bram Moolenaare010c722020-02-24 21:37:54 +01002478
2479func Test_hidden_terminal()
2480 let buf = term_start(&shell, #{hidden: 1})
2481 call assert_equal('', bufname('^$'))
2482 call StopShellInTerminal(buf)
2483endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002484
2485func Test_term_nasty_callback()
2486 func OpenTerms()
2487 set hidden
2488 let g:buf0 = term_start('sh', #{hidden: 1})
2489 call popup_create(g:buf0, {})
2490 let g:buf1 = term_start('sh', #{hidden: 1, term_finish: 'close'})
2491 call popup_create(g:buf1, {})
2492 let g:buf2 = term_start(['sh', '-c'], #{curwin: 1, exit_cb: function('TermExit')})
2493 sleep 100m
2494 call popup_close(win_getid())
2495 endfunc
2496 func TermExit(...)
2497 call term_sendkeys(bufnr('#'), "exit\<CR>")
2498 call popup_close(win_getid())
Bram Moolenaarab505b12020-03-23 19:28:44 +01002499 endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002500 call OpenTerms()
2501
2502 call term_sendkeys(g:buf0, "exit\<CR>")
Bram Moolenaarfa5d8a12020-03-13 14:55:23 +01002503 sleep 100m
Bram Moolenaar24ebd832020-03-16 21:25:24 +01002504 exe g:buf0 .. 'bwipe!'
Bram Moolenaarcee52202020-03-11 14:19:58 +01002505 set hidden&
2506endfunc