blob: ebefdd00d01bc7757bf0d2a6716d83e20d4d8209 [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 Moolenaard0f8d392022-12-04 23:00:41 +000027 " in case a previous failure left a swap file behind
28 call delete('.Xfoo.swp')
29
Bram Moolenaar3b301682018-09-22 21:37:39 +020030 let buf = term_start('/bin/sh')
31 " Wait for shell prompt.
Bram Moolenaar0f62cf52018-11-03 21:09:15 +010032 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
Bram Moolenaar3b301682018-09-22 21:37:39 +020033
Bram Moolenaar185d68a2019-08-15 11:21:15 +020034 call term_sendkeys(buf, v:progpath
35 \ . " --clean -X"
Bram Moolenaar3b301682018-09-22 21:37:39 +020036 \ . " -c 'set nu'"
37 \ . " -c 'call setline(1, \"foo\")'"
38 \ . " Xfoo\<CR>")
39 " Cursor in terminal buffer should be on first line in spawned vim.
40 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
41
42 for suspend_cmd in [":suspend\<CR>",
43 \ ":stop\<CR>",
44 \ ":suspend!\<CR>",
45 \ ":stop!\<CR>",
46 \ "\<C-Z>"]
47 " Suspend and wait for shell prompt.
48 call term_sendkeys(buf, suspend_cmd)
Bram Moolenaara8356bc2019-04-14 14:31:11 +020049 call CheckSuspended(buf, 0)
Bram Moolenaar3b301682018-09-22 21:37:39 +020050 endfor
51
52 " Test that :suspend! with 'autowrite' writes content of buffers if modified.
53 call term_sendkeys(buf, ":set autowrite\<CR>")
54 call assert_equal(0, filereadable('Xfoo'))
55 call term_sendkeys(buf, ":suspend\<CR>")
56 " Wait for shell prompt.
Bram Moolenaara8356bc2019-04-14 14:31:11 +020057 call CheckSuspended(buf, 1)
Bram Moolenaar3b301682018-09-22 21:37:39 +020058
Bram Moolenaar3020ccb2019-01-17 22:13:54 +010059 " Quit gracefully to dump coverage information.
60 call term_sendkeys(buf, ":qall!\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020061 call TermWait(buf)
Bram Moolenaar99922442019-07-08 20:58:25 +020062 " Wait until Vim actually exited and shell shows a prompt
63 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020064 call StopShellInTerminal(buf)
Bram Moolenaar3020ccb2019-01-17 22:13:54 +010065
Bram Moolenaar3b301682018-09-22 21:37:39 +020066 exe buf . 'bwipe!'
67 call delete('Xfoo')
Bram Moolenaard0f8d392022-12-04 23:00:41 +000068 call delete('.Xfoo.swp')
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 Moolenaara0a6f3a2022-12-04 22:26:40 +000075 call WaitForResponses()
76
Bram Moolenaard0f8d392022-12-04 23:00:41 +000077 " in case a previous failure left a swap file behind
78 call delete('.Xfoo.swp')
79
Bram Moolenaar100118c2020-12-11 19:30:34 +010080 let buf = term_start('/bin/sh', #{term_rows: 6})
81 " Wait for shell prompt.
82 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
83
84 call term_sendkeys(buf, v:progpath
85 \ . " --clean -X"
86 \ . " -c 'set nu'"
87 \ . " -c 'let g:count = 0'"
88 \ . " -c 'au VimSuspend * let g:count += 1'"
89 \ . " -c 'au VimResume * let g:count += 1'"
90 \ . " -c 'call setline(1, \"foo\")'"
91 \ . " Xfoo\<CR>")
92 " Cursor in terminal buffer should be on first line in spawned vim.
93 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
94
95 for suspend_cmd in [":suspend\<CR>",
96 \ ":stop\<CR>",
97 \ ":suspend!\<CR>",
98 \ ":stop!\<CR>",
99 \ "\<C-Z>"]
100 " Suspend and wait for shell prompt. Then "fg" will restore Vim.
101 call term_sendkeys(buf, suspend_cmd)
102 call CheckSuspended(buf, 0)
103 endfor
104
105 call term_sendkeys(buf, ":echo g:count\<CR>")
106 call TermWait(buf)
107 call WaitForAssert({-> assert_match('^10', term_getline(buf, 6))})
108
109 " Quit gracefully to dump coverage information.
110 call term_sendkeys(buf, ":qall!\<CR>")
111 call TermWait(buf)
112 " Wait until Vim actually exited and shell shows a prompt
113 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
114 call StopShellInTerminal(buf)
115
116 exe buf . 'bwipe!'
117 call delete('Xfoo')
Bram Moolenaard0f8d392022-12-04 23:00:41 +0000118 call delete('.Xfoo.swp')
Bram Moolenaar100118c2020-12-11 19:30:34 +0100119endfunc
120
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200121" vim: shiftwidth=2 sts=2 expandtab