blob: ef8fea53ef21a1fc39e5c1d1f0c409b97337f2c9 [file] [log] [blame]
Bram Moolenaar3b301682018-09-22 21:37:39 +02001" Test :suspend
2
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003source check.vim
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02004source term_util.vim
Bram Moolenaara0a6f3a2022-12-04 22:26:40 +00005source shared.vim
Bram Moolenaar3b301682018-09-22 21:37:39 +02006
Bram Moolenaara8356bc2019-04-14 14:31:11 +02007func CheckSuspended(buf, fileExists)
8 call WaitForAssert({-> assert_match('[$#] $', term_getline(a:buf, '.'))})
9
10 if a:fileExists
11 call assert_equal(['foo'], readfile('Xfoo'))
12 else
13 " Without 'autowrite', buffer should not be written.
14 call assert_equal(0, filereadable('Xfoo'))
15 endif
16
17 call term_sendkeys(a:buf, "fg\<CR>\<C-L>")
18 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(a:buf, '.'))})
19endfunc
20
Bram Moolenaar3b301682018-09-22 21:37:39 +020021func Test_suspend()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020022 CheckFeature terminal
23 CheckExecutable /bin/sh
Bram Moolenaar3b301682018-09-22 21:37:39 +020024
Bram Moolenaar7155fb62022-12-06 09:11:39 +000025 " Somehow the modifyOtherKeys response may get to the terminal when using
26 " Mac OS. Make t_RK and 'keyprotocol' empty to avoid that.
27 set t_RK= keyprotocol=
Bram Moolenaar0a2f8912022-12-05 21:21:46 +000028
Bram Moolenaara0a6f3a2022-12-04 22:26:40 +000029 call WaitForResponses()
30
Bram Moolenaar3b301682018-09-22 21:37:39 +020031 let buf = term_start('/bin/sh')
32 " Wait for shell prompt.
Bram Moolenaar0f62cf52018-11-03 21:09:15 +010033 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
Bram Moolenaar3b301682018-09-22 21:37:39 +020034
Bram Moolenaar185d68a2019-08-15 11:21:15 +020035 call term_sendkeys(buf, v:progpath
36 \ . " --clean -X"
Bram Moolenaar3b301682018-09-22 21:37:39 +020037 \ . " -c 'set nu'"
38 \ . " -c 'call setline(1, \"foo\")'"
39 \ . " Xfoo\<CR>")
40 " Cursor in terminal buffer should be on first line in spawned vim.
41 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
42
43 for suspend_cmd in [":suspend\<CR>",
44 \ ":stop\<CR>",
45 \ ":suspend!\<CR>",
46 \ ":stop!\<CR>",
47 \ "\<C-Z>"]
48 " Suspend and wait for shell prompt.
49 call term_sendkeys(buf, suspend_cmd)
Bram Moolenaara8356bc2019-04-14 14:31:11 +020050 call CheckSuspended(buf, 0)
Bram Moolenaar3b301682018-09-22 21:37:39 +020051 endfor
52
53 " Test that :suspend! with 'autowrite' writes content of buffers if modified.
54 call term_sendkeys(buf, ":set autowrite\<CR>")
55 call assert_equal(0, filereadable('Xfoo'))
56 call term_sendkeys(buf, ":suspend\<CR>")
57 " Wait for shell prompt.
Bram Moolenaara8356bc2019-04-14 14:31:11 +020058 call CheckSuspended(buf, 1)
Bram Moolenaar3b301682018-09-22 21:37:39 +020059
Bram Moolenaar3020ccb2019-01-17 22:13:54 +010060 " Quit gracefully to dump coverage information.
61 call term_sendkeys(buf, ":qall!\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020062 call TermWait(buf)
Bram Moolenaar99922442019-07-08 20:58:25 +020063 " Wait until Vim actually exited and shell shows a prompt
64 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020065 call StopShellInTerminal(buf)
Bram Moolenaar3020ccb2019-01-17 22:13:54 +010066
Bram Moolenaar3b301682018-09-22 21:37:39 +020067 exe buf . 'bwipe!'
68 call delete('Xfoo')
Bram Moolenaar3b301682018-09-22 21:37:39 +020069endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020070
Bram Moolenaar100118c2020-12-11 19:30:34 +010071func Test_suspend_autocmd()
72 CheckFeature terminal
73 CheckExecutable /bin/sh
74
Bram Moolenaar7155fb62022-12-06 09:11:39 +000075 " Somehow the modifyOtherKeys response may get to the terminal when using
76 " Mac OS. Make t_RK and 'keyprotocol' empty to avoid that.
77 set t_RK= keyprotocol=
Bram Moolenaar0a2f8912022-12-05 21:21:46 +000078
Bram Moolenaara0a6f3a2022-12-04 22:26:40 +000079 call WaitForResponses()
80
Bram Moolenaar100118c2020-12-11 19:30:34 +010081 let buf = term_start('/bin/sh', #{term_rows: 6})
82 " Wait for shell prompt.
83 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
84
85 call term_sendkeys(buf, v:progpath
86 \ . " --clean -X"
87 \ . " -c 'set nu'"
88 \ . " -c 'let g:count = 0'"
89 \ . " -c 'au VimSuspend * let g:count += 1'"
90 \ . " -c 'au VimResume * let g:count += 1'"
91 \ . " -c 'call setline(1, \"foo\")'"
92 \ . " Xfoo\<CR>")
93 " Cursor in terminal buffer should be on first line in spawned vim.
94 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
95
96 for suspend_cmd in [":suspend\<CR>",
97 \ ":stop\<CR>",
98 \ ":suspend!\<CR>",
99 \ ":stop!\<CR>",
100 \ "\<C-Z>"]
101 " Suspend and wait for shell prompt. Then "fg" will restore Vim.
102 call term_sendkeys(buf, suspend_cmd)
103 call CheckSuspended(buf, 0)
104 endfor
105
106 call term_sendkeys(buf, ":echo g:count\<CR>")
107 call TermWait(buf)
108 call WaitForAssert({-> assert_match('^10', term_getline(buf, 6))})
109
110 " Quit gracefully to dump coverage information.
111 call term_sendkeys(buf, ":qall!\<CR>")
112 call TermWait(buf)
113 " Wait until Vim actually exited and shell shows a prompt
114 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
115 call StopShellInTerminal(buf)
116
117 exe buf . 'bwipe!'
118 call delete('Xfoo')
119endfunc
120
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200121" vim: shiftwidth=2 sts=2 expandtab