blob: 4015419233c83f8b1d83896600a536829a29b0eb [file] [log] [blame]
Bram Moolenaarda650582018-02-20 15:51:40 +01001" Functions shared by tests making screen dumps.
2
3" Only load this script once.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004if exists('*CanRunVimInTerminal')
Bram Moolenaarda650582018-02-20 15:51:40 +01005 finish
6endif
7
Bram Moolenaarf2732452018-06-03 14:47:35 +02008" For most tests we need to be able to run terminal Vim with 256 colors. On
9" MS-Windows the console only has 16 colors and the GUI can't run in a
10" terminal.
11func CanRunVimInTerminal()
12 return has('terminal') && !has('win32')
13endfunc
14
15" Skip the rest if there is no terminal feature at all.
16if !has('terminal')
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010017 finish
18endif
19
Bram Moolenaarda650582018-02-20 15:51:40 +010020source shared.vim
21
22" Run Vim with "arguments" in a new terminal window.
23" By default uses a size of 20 lines and 75 columns.
24" Returns the buffer number of the terminal.
25"
Bram Moolenaarcf67a502018-03-25 20:31:32 +020026" Options is a dictionary, these items are recognized:
27" "rows" - height of the terminal window (max. 20)
28" "cols" - width of the terminal window (max. 78)
Bram Moolenaarda650582018-02-20 15:51:40 +010029func RunVimInTerminal(arguments, options)
Bram Moolenaar948a7962018-03-23 20:37:45 +010030 " If Vim doesn't exit a swap file remains, causing other tests to fail.
31 " Remove it here.
32 call delete(".swp")
33
Bram Moolenaare7499dd2018-03-24 17:56:13 +010034 if exists('$COLORFGBG')
35 " Clear $COLORFGBG to avoid 'background' being set to "dark", which will
36 " only be corrected if the response to t_RB is received, which may be too
37 " late.
38 let $COLORFGBG = ''
39 endif
40
Bram Moolenaarda650582018-02-20 15:51:40 +010041 " Make a horizontal and vertical split, so that we can get exactly the right
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +020042 " size terminal window. Works only when the current window is full width.
43 call assert_equal(&columns, winwidth(0))
Bram Moolenaarda650582018-02-20 15:51:40 +010044 split
45 vsplit
46
Bram Moolenaar6acadda2018-02-24 16:51:32 +010047 " Always do this with 256 colors and a light background.
48 set t_Co=256 background=light
49 hi Normal ctermfg=NONE ctermbg=NONE
Bram Moolenaarda650582018-02-20 15:51:40 +010050
Bram Moolenaarcf67a502018-03-25 20:31:32 +020051 " Make the window 20 lines high and 75 columns, unless told otherwise.
52 let rows = get(a:options, 'rows', 20)
53 let cols = get(a:options, 'cols', 75)
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +020054
Bram Moolenaarda650582018-02-20 15:51:40 +010055 let cmd = GetVimCommandClean()
Bram Moolenaarf2732452018-06-03 14:47:35 +020056
Bram Moolenaarb7ea7cb2018-02-24 14:38:51 +010057 " Add -v to have gvim run in the terminal (if possible)
Bram Moolenaarf587ef32019-04-13 13:13:54 +020058 " The GCOV_ environment variables cause the Vim running in the terminal to
59 " write the coverage information in the "nested" directory, to avoid two Vim
60 " instances try to write to the same coverage info file.
Bram Moolenaarb7ea7cb2018-02-24 14:38:51 +010061 let cmd .= ' -v ' . a:arguments
Bram Moolenaarf587ef32019-04-13 13:13:54 +020062 let buf = term_start(cmd, {
63 \ 'curwin': 1,
64 \ 'term_rows': rows,
65 \ 'term_cols': cols,
66 \ 'env': {'GCOV_PREFIX': 'nested', 'GCOV_PREFIX_STRIP': 99},
67 \ })
Bram Moolenaarb833c1e2018-05-05 16:36:06 +020068 if &termwinsize == ''
Bram Moolenaare40b9d42019-01-27 16:55:47 +010069 " in the GUI we may end up with a different size, try to set it.
70 if term_getsize(buf) != [rows, cols]
71 call term_setsize(buf, rows, cols)
72 endif
Bram Moolenaara7eef3d2018-04-15 22:25:54 +020073 call assert_equal([rows, cols], term_getsize(buf))
74 else
75 let rows = term_getsize(buf)[0]
76 let cols = term_getsize(buf)[1]
77 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010078
Bram Moolenaarf2732452018-06-03 14:47:35 +020079 " Wait for "All" or "Top" of the ruler to be shown in the last line or in
80 " the status line of the last window. This can be quite slow (e.g. when
81 " using valgrind).
Bram Moolenaar50182fa2018-04-28 21:34:40 +020082 " If it fails then show the terminal contents for debugging.
83 try
Bram Moolenaarf2732452018-06-03 14:47:35 +020084 call WaitFor({-> len(term_getline(buf, rows)) >= cols - 1 || len(term_getline(buf, rows - 1)) >= cols - 1})
Bram Moolenaar50182fa2018-04-28 21:34:40 +020085 catch /timed out after/
86 let lines = map(range(1, rows), {key, val -> term_getline(buf, val)})
87 call assert_report('RunVimInTerminal() failed, screen contents: ' . join(lines, "<NL>"))
88 endtry
Bram Moolenaar1834d372018-03-29 17:40:46 +020089
Bram Moolenaarda650582018-02-20 15:51:40 +010090 return buf
91endfunc
92
93" Stop a Vim running in terminal buffer "buf".
94func StopVimInTerminal(buf)
95 call assert_equal("running", term_getstatus(a:buf))
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020096
97 " CTRL-O : works both in Normal mode and Insert mode to start a command line.
98 " In Command-line it's inserted, the CTRL-U removes it again.
Bram Moolenaaracb9eff2018-06-04 19:11:11 +020099 call term_sendkeys(a:buf, "\<C-O>:\<C-U>qa!\<cr>")
Bram Moolenaar3339d3d2018-06-03 17:10:40 +0200100
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200101 call WaitForAssert({-> assert_equal("finished", term_getstatus(a:buf))})
Bram Moolenaarda650582018-02-20 15:51:40 +0100102 only!
103endfunc
104
105" Verify that Vim running in terminal buffer "buf" matches the screen dump.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100106" "options" is passed to term_dumpwrite().
Bram Moolenaarda650582018-02-20 15:51:40 +0100107" The file name used is "dumps/{filename}.dump".
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +0200108" Optionally an extra argument can be passed which is prepended to the error
109" message. Use this when using the same dump file with different options.
Bram Moolenaarda650582018-02-20 15:51:40 +0100110" Will wait for up to a second for the screen dump to match.
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +0200111" Returns non-zero when verification fails.
112func VerifyScreenDump(buf, filename, options, ...)
Bram Moolenaarda650582018-02-20 15:51:40 +0100113 let reference = 'dumps/' . a:filename . '.dump'
Bram Moolenaaref7f0e32019-03-30 15:59:51 +0100114 let testfile = 'failed/' . a:filename . '.dump'
115
116 let did_mkdir = 0
117 if !isdirectory('failed')
118 let did_mkdir = 1
119 call mkdir('failed')
120 endif
Bram Moolenaarda650582018-02-20 15:51:40 +0100121
122 let i = 0
123 while 1
Bram Moolenaarb6fc7282018-12-04 22:24:16 +0100124 " leave some time for updating the original window
125 sleep 10m
Bram Moolenaarda650582018-02-20 15:51:40 +0100126 call delete(testfile)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100127 call term_dumpwrite(a:buf, testfile, a:options)
Bram Moolenaar353aca12019-02-21 17:05:59 +0100128 let testdump = readfile(testfile)
Bram Moolenaar2d7260d2019-04-06 20:51:52 +0200129 if filereadable(reference)
130 let refdump = readfile(reference)
131 else
132 " Must be a new screendump, always fail
133 let refdump = []
134 endif
Bram Moolenaar353aca12019-02-21 17:05:59 +0100135 if refdump == testdump
Bram Moolenaarda650582018-02-20 15:51:40 +0100136 call delete(testfile)
Bram Moolenaaref7f0e32019-03-30 15:59:51 +0100137 if did_mkdir
138 call delete('failed', 'd')
139 endif
Bram Moolenaarda650582018-02-20 15:51:40 +0100140 break
141 endif
142 if i == 100
Bram Moolenaar2d7260d2019-04-06 20:51:52 +0200143 " Leave the failed dump around for inspection.
144 if filereadable(reference)
145 let msg = 'See dump file difference: call term_dumpdiff("' . testfile . '", "' . reference . '")'
146 if a:0 == 1
147 let msg = a:1 . ': ' . msg
148 endif
149 if len(testdump) != len(refdump)
150 let msg = msg . '; line count is ' . len(testdump) . ' instead of ' . len(refdump)
151 endif
152 else
153 let msg = 'See new dump file: call term_dumpload("' . testfile . '")'
Bram Moolenaar353aca12019-02-21 17:05:59 +0100154 endif
155 for i in range(len(refdump))
156 if i >= len(testdump)
157 break
158 endif
159 if testdump[i] != refdump[i]
160 let msg = msg . '; difference in line ' . (i + 1) . ': "' . testdump[i] . '"'
161 endif
162 endfor
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +0200163 call assert_report(msg)
164 return 1
Bram Moolenaarda650582018-02-20 15:51:40 +0100165 endif
Bram Moolenaarda650582018-02-20 15:51:40 +0100166 let i += 1
167 endwhile
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +0200168 return 0
Bram Moolenaarda650582018-02-20 15:51:40 +0100169endfunc