blob: 1c6ebbc31236b76b60511a47d8bfda708b567f4d [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 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 Moolenaar3b301682018-09-22 21:37:39 +020033 \ . " -c 'set nu'"
34 \ . " -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 Moolenaara0a6f3a2022-12-04 22:26:40 +000071 call WaitForResponses()
72
Bram Moolenaar100118c2020-12-11 19:30:34 +010073 let buf = term_start('/bin/sh', #{term_rows: 6})
74 " Wait for shell prompt.
75 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
76
77 call term_sendkeys(buf, v:progpath
78 \ . " --clean -X"
79 \ . " -c 'set nu'"
80 \ . " -c 'let g:count = 0'"
81 \ . " -c 'au VimSuspend * let g:count += 1'"
82 \ . " -c 'au VimResume * let g:count += 1'"
83 \ . " -c 'call setline(1, \"foo\")'"
84 \ . " Xfoo\<CR>")
85 " Cursor in terminal buffer should be on first line in spawned vim.
86 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
87
88 for suspend_cmd in [":suspend\<CR>",
89 \ ":stop\<CR>",
90 \ ":suspend!\<CR>",
91 \ ":stop!\<CR>",
92 \ "\<C-Z>"]
93 " Suspend and wait for shell prompt. Then "fg" will restore Vim.
94 call term_sendkeys(buf, suspend_cmd)
95 call CheckSuspended(buf, 0)
96 endfor
97
98 call term_sendkeys(buf, ":echo g:count\<CR>")
99 call TermWait(buf)
100 call WaitForAssert({-> assert_match('^10', term_getline(buf, 6))})
101
102 " Quit gracefully to dump coverage information.
103 call term_sendkeys(buf, ":qall!\<CR>")
104 call TermWait(buf)
105 " Wait until Vim actually exited and shell shows a prompt
106 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
107 call StopShellInTerminal(buf)
108
109 exe buf . 'bwipe!'
110 call delete('Xfoo')
111endfunc
112
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200113" vim: shiftwidth=2 sts=2 expandtab