blob: c96a1a6a8f446489ac7cd5efb37fb6e3de56ec19 [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()
Bram Moolenaar65214052022-12-04 23:30:19 +000026 if has('mac')
27 " Mac OS machines tend to be slow, wait a bit longer
28 sleep 150m
29 endif
Bram Moolenaara0a6f3a2022-12-04 22:26:40 +000030
Bram Moolenaard0f8d392022-12-04 23:00:41 +000031 " in case a previous failure left a swap file behind
32 call delete('.Xfoo.swp')
33
Bram Moolenaar3b301682018-09-22 21:37:39 +020034 let buf = term_start('/bin/sh')
35 " Wait for shell prompt.
Bram Moolenaar0f62cf52018-11-03 21:09:15 +010036 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
Bram Moolenaar3b301682018-09-22 21:37:39 +020037
Bram Moolenaar185d68a2019-08-15 11:21:15 +020038 call term_sendkeys(buf, v:progpath
39 \ . " --clean -X"
Bram Moolenaar3b301682018-09-22 21:37:39 +020040 \ . " -c 'set nu'"
41 \ . " -c 'call setline(1, \"foo\")'"
42 \ . " Xfoo\<CR>")
43 " Cursor in terminal buffer should be on first line in spawned vim.
44 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
45
46 for suspend_cmd in [":suspend\<CR>",
47 \ ":stop\<CR>",
48 \ ":suspend!\<CR>",
49 \ ":stop!\<CR>",
50 \ "\<C-Z>"]
51 " Suspend and wait for shell prompt.
52 call term_sendkeys(buf, suspend_cmd)
Bram Moolenaara8356bc2019-04-14 14:31:11 +020053 call CheckSuspended(buf, 0)
Bram Moolenaar3b301682018-09-22 21:37:39 +020054 endfor
55
56 " Test that :suspend! with 'autowrite' writes content of buffers if modified.
57 call term_sendkeys(buf, ":set autowrite\<CR>")
58 call assert_equal(0, filereadable('Xfoo'))
59 call term_sendkeys(buf, ":suspend\<CR>")
60 " Wait for shell prompt.
Bram Moolenaara8356bc2019-04-14 14:31:11 +020061 call CheckSuspended(buf, 1)
Bram Moolenaar3b301682018-09-22 21:37:39 +020062
Bram Moolenaar3020ccb2019-01-17 22:13:54 +010063 " Quit gracefully to dump coverage information.
64 call term_sendkeys(buf, ":qall!\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020065 call TermWait(buf)
Bram Moolenaar99922442019-07-08 20:58:25 +020066 " Wait until Vim actually exited and shell shows a prompt
67 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020068 call StopShellInTerminal(buf)
Bram Moolenaar3020ccb2019-01-17 22:13:54 +010069
Bram Moolenaar3b301682018-09-22 21:37:39 +020070 exe buf . 'bwipe!'
71 call delete('Xfoo')
Bram Moolenaard0f8d392022-12-04 23:00:41 +000072 call delete('.Xfoo.swp')
Bram Moolenaar3b301682018-09-22 21:37:39 +020073endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020074
Bram Moolenaar100118c2020-12-11 19:30:34 +010075func Test_suspend_autocmd()
76 CheckFeature terminal
77 CheckExecutable /bin/sh
78
Bram Moolenaara0a6f3a2022-12-04 22:26:40 +000079 call WaitForResponses()
Bram Moolenaar65214052022-12-04 23:30:19 +000080 if has('mac')
81 " Mac OS machines tend to be slow, wait a bit longer
82 sleep 150m
83 endif
Bram Moolenaara0a6f3a2022-12-04 22:26:40 +000084
Bram Moolenaard0f8d392022-12-04 23:00:41 +000085 " in case a previous failure left a swap file behind
86 call delete('.Xfoo.swp')
87
Bram Moolenaar100118c2020-12-11 19:30:34 +010088 let buf = term_start('/bin/sh', #{term_rows: 6})
89 " Wait for shell prompt.
90 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
91
92 call term_sendkeys(buf, v:progpath
93 \ . " --clean -X"
94 \ . " -c 'set nu'"
95 \ . " -c 'let g:count = 0'"
96 \ . " -c 'au VimSuspend * let g:count += 1'"
97 \ . " -c 'au VimResume * let g:count += 1'"
98 \ . " -c 'call setline(1, \"foo\")'"
99 \ . " Xfoo\<CR>")
100 " Cursor in terminal buffer should be on first line in spawned vim.
101 call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
102
103 for suspend_cmd in [":suspend\<CR>",
104 \ ":stop\<CR>",
105 \ ":suspend!\<CR>",
106 \ ":stop!\<CR>",
107 \ "\<C-Z>"]
108 " Suspend and wait for shell prompt. Then "fg" will restore Vim.
109 call term_sendkeys(buf, suspend_cmd)
110 call CheckSuspended(buf, 0)
111 endfor
112
113 call term_sendkeys(buf, ":echo g:count\<CR>")
114 call TermWait(buf)
115 call WaitForAssert({-> assert_match('^10', term_getline(buf, 6))})
116
117 " Quit gracefully to dump coverage information.
118 call term_sendkeys(buf, ":qall!\<CR>")
119 call TermWait(buf)
120 " Wait until Vim actually exited and shell shows a prompt
121 call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
122 call StopShellInTerminal(buf)
123
124 exe buf . 'bwipe!'
125 call delete('Xfoo')
Bram Moolenaard0f8d392022-12-04 23:00:41 +0000126 call delete('.Xfoo.swp')
Bram Moolenaar100118c2020-12-11 19:30:34 +0100127endfunc
128
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200129" vim: shiftwidth=2 sts=2 expandtab