blob: ae05dd21f29798093123e0b51c42bb8a101d4de1 [file] [log] [blame]
Bram Moolenaar3b301682018-09-22 21:37:39 +02001" Test :suspend
2
Bram Moolenaara8356bc2019-04-14 14:31:11 +02003func CheckSuspended(buf, fileExists)
4 call WaitForAssert({-> assert_match('[$#] $', term_getline(a:buf, '.'))})
5
6 if a:fileExists
7 call assert_equal(['foo'], readfile('Xfoo'))
8 else
9 " Without 'autowrite', buffer should not be written.
10 call assert_equal(0, filereadable('Xfoo'))
11 endif
12
13 call term_sendkeys(a:buf, "fg\<CR>\<C-L>")
14 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(a:buf, '.'))})
15endfunc
16
Bram Moolenaar3b301682018-09-22 21:37:39 +020017func Test_suspend()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020018 CheckFeature terminal
19 CheckExecutable /bin/sh
Bram Moolenaar3b301682018-09-22 21:37:39 +020020
Bram Moolenaar7155fb62022-12-06 09:11:39 +000021 " Somehow the modifyOtherKeys response may get to the terminal when using
22 " Mac OS. Make t_RK and 'keyprotocol' empty to avoid that.
23 set t_RK= keyprotocol=
Bram Moolenaar0a2f8912022-12-05 21:21:46 +000024
Bram Moolenaara0a6f3a2022-12-04 22:26:40 +000025 call WaitForResponses()
26
Bram Moolenaar3b301682018-09-22 21:37:39 +020027 let buf = term_start('/bin/sh')
28 " Wait for shell prompt.
Bram Moolenaar0f62cf52018-11-03 21:09:15 +010029 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
Bram Moolenaar3b301682018-09-22 21:37:39 +020030
Bram Moolenaar185d68a2019-08-15 11:21:15 +020031 call term_sendkeys(buf, v:progpath
32 \ . " --clean -X"
Bram Moolenaaraf345432022-12-06 16:09:52 +000033 \ . " -c 'set nu keyprotocol='"
Bram Moolenaar3b301682018-09-22 21:37:39 +020034 \ . " -c 'call setline(1, \"foo\")'"
35 \ . " Xfoo\<CR>")
36 " Cursor in terminal buffer should be on first line in spawned vim.
37 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
38
39 for suspend_cmd in [":suspend\<CR>",
40 \ ":stop\<CR>",
41 \ ":suspend!\<CR>",
42 \ ":stop!\<CR>",
43 \ "\<C-Z>"]
44 " Suspend and wait for shell prompt.
45 call term_sendkeys(buf, suspend_cmd)
Bram Moolenaara8356bc2019-04-14 14:31:11 +020046 call CheckSuspended(buf, 0)
Bram Moolenaar3b301682018-09-22 21:37:39 +020047 endfor
48
49 " Test that :suspend! with 'autowrite' writes content of buffers if modified.
50 call term_sendkeys(buf, ":set autowrite\<CR>")
51 call assert_equal(0, filereadable('Xfoo'))
52 call term_sendkeys(buf, ":suspend\<CR>")
53 " Wait for shell prompt.
Bram Moolenaara8356bc2019-04-14 14:31:11 +020054 call CheckSuspended(buf, 1)
Bram Moolenaar3b301682018-09-22 21:37:39 +020055
Bram Moolenaar3020ccb2019-01-17 22:13:54 +010056 " Quit gracefully to dump coverage information.
57 call term_sendkeys(buf, ":qall!\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020058 call TermWait(buf)
Bram Moolenaar99922442019-07-08 20:58:25 +020059 " Wait until Vim actually exited and shell shows a prompt
60 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020061 call StopShellInTerminal(buf)
Bram Moolenaar3020ccb2019-01-17 22:13:54 +010062
Bram Moolenaar3b301682018-09-22 21:37:39 +020063 exe buf . 'bwipe!'
64 call delete('Xfoo')
Bram Moolenaar3b301682018-09-22 21:37:39 +020065endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020066
Bram Moolenaar100118c2020-12-11 19:30:34 +010067func Test_suspend_autocmd()
68 CheckFeature terminal
69 CheckExecutable /bin/sh
70
Bram Moolenaar7155fb62022-12-06 09:11:39 +000071 " Somehow the modifyOtherKeys response may get to the terminal when using
72 " Mac OS. Make t_RK and 'keyprotocol' empty to avoid that.
73 set t_RK= keyprotocol=
Bram Moolenaar0a2f8912022-12-05 21:21:46 +000074
Bram Moolenaara0a6f3a2022-12-04 22:26:40 +000075 call WaitForResponses()
76
Bram Moolenaar100118c2020-12-11 19:30:34 +010077 let buf = term_start('/bin/sh', #{term_rows: 6})
78 " Wait for shell prompt.
79 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
80
81 call term_sendkeys(buf, v:progpath
82 \ . " --clean -X"
Bram Moolenaaraf345432022-12-06 16:09:52 +000083 \ . " -c 'set nu keyprotocol='"
Bram Moolenaar100118c2020-12-11 19:30:34 +010084 \ . " -c 'let g:count = 0'"
85 \ . " -c 'au VimSuspend * let g:count += 1'"
86 \ . " -c 'au VimResume * let g:count += 1'"
87 \ . " -c 'call setline(1, \"foo\")'"
88 \ . " Xfoo\<CR>")
89 " Cursor in terminal buffer should be on first line in spawned vim.
90 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
91
92 for suspend_cmd in [":suspend\<CR>",
93 \ ":stop\<CR>",
94 \ ":suspend!\<CR>",
95 \ ":stop!\<CR>",
96 \ "\<C-Z>"]
97 " Suspend and wait for shell prompt. Then "fg" will restore Vim.
98 call term_sendkeys(buf, suspend_cmd)
99 call CheckSuspended(buf, 0)
100 endfor
101
102 call term_sendkeys(buf, ":echo g:count\<CR>")
103 call TermWait(buf)
104 call WaitForAssert({-> assert_match('^10', term_getline(buf, 6))})
105
106 " Quit gracefully to dump coverage information.
107 call term_sendkeys(buf, ":qall!\<CR>")
108 call TermWait(buf)
109 " Wait until Vim actually exited and shell shows a prompt
110 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
111 call StopShellInTerminal(buf)
112
113 exe buf . 'bwipe!'
114 call delete('Xfoo')
115endfunc
116
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200117" vim: shiftwidth=2 sts=2 expandtab