blob: e1cfdcfe1c163dfac7a5527900571ea1cb187cb5 [file] [log] [blame]
Bram Moolenaar46acad72023-06-11 19:04:18 +01001" Runs all the syntax tests for which there is no "done/name" file.
2"
3" Current directory must be runtime/syntax.
4
5" Only do this with the +eval feature
6if 1
7
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +01008" Remember the directory where we started. Will change to "testdir" below.
9let syntaxDir = getcwd()
10
11let s:messagesFname = fnameescape(syntaxDir .. '/testdir/messages')
12
13let s:messages = []
14
15" Add one message to the list of messages
16func Message(msg)
17 echomsg a:msg
18 call add(s:messages, a:msg)
19endfunc
20
21" Report a fatal message and exit
22func Fatal(msg)
23 echoerr a:msg
24 call AppendMessages(a:msg)
25 qall!
26endfunc
27
28" Append s:messages to the messages file and make it empty.
29func AppendMessages(header)
30 exe 'split ' .. s:messagesFname
31 call append(line('$'), '')
32 call append(line('$'), a:header)
33 call append(line('$'), s:messages)
34 let s:messages = []
35 wq
36endfunc
37
38" Relevant messages are written to the "messages" file.
39" If the file already exists it is appended to.
40exe 'split ' .. s:messagesFname
41call append(line('$'), repeat('=-', 70))
42call append(line('$'), '')
Bram Moolenaar031d6322023-06-22 22:38:54 +010043let s:test_run_message = 'Test run on ' .. strftime("%Y %b %d %H:%M:%S")
44call append(line('$'), s:test_run_message)
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +010045wq
46
47if syntaxDir !~ '[/\\]runtime[/\\]syntax\>'
48 call Fatal('Current directory must be "runtime/syntax"')
Bram Moolenaar46acad72023-06-11 19:04:18 +010049endif
50if !isdirectory('testdir')
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +010051 call Fatal('"testdir" directory not found')
Bram Moolenaar46acad72023-06-11 19:04:18 +010052endif
53
54" Use the script for source code screendump testing. It sources other scripts,
55" therefore we must "cd" there.
56cd ../../src/testdir
57source screendump.vim
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +010058exe 'cd ' .. fnameescape(syntaxDir)
Bram Moolenaar46acad72023-06-11 19:04:18 +010059
60" For these tests we need to be able to run terminal Vim with 256 colors. On
61" MS-Windows the console only has 16 colors and the GUI can't run in a
62" terminal.
63if !CanRunVimInTerminal()
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +010064 call Fatal('Cannot make screendumps, aborting')
Bram Moolenaar46acad72023-06-11 19:04:18 +010065endif
66
67cd testdir
68if !isdirectory('done')
69 call mkdir('done')
70endif
71
72set nocp
73set nowrapscan
74set report=9999
75set modeline
76set debug=throw
77set nomore
78
79au! SwapExists * call HandleSwapExists()
80func HandleSwapExists()
81 " Ignore finding a swap file for the test input, the user might be editing
82 " it and that's OK.
83 if expand('<afile>') =~ 'input[/\\].*\..*'
84 let v:swapchoice = 'e'
85 endif
86endfunc
87
Christian Brabandt56824432024-02-28 21:24:25 +010088func RunTest()
89 let ok_count = 0
90 let failed_tests = []
91 let skipped_count = 0
92 let MAX_FAILED_COUNT = 5
Aliaksei Budaveif6069a72024-03-05 22:34:36 +030093 " Create a map of setup configuration filenames with their basenames as keys.
94 let setup = glob('input/setup/*.vim', 1, 1)
95 \ ->reduce({d, f -> extend(d, {fnamemodify(f, ':t:r'): f})}, {})
96
Christian Brabandt56824432024-02-28 21:24:25 +010097 for fname in glob('input/*.*', 1, 1)
98 if fname =~ '\~$'
99 " backup file, skip
100 continue
Bram Moolenaar7d0dbd02023-06-24 00:56:50 +0100101 endif
102
Christian Brabandt56824432024-02-28 21:24:25 +0100103 let linecount = readfile(fname)->len()
104 let root = fnamemodify(fname, ':t:r')
105 let filetype = substitute(root, '\([^_.]*\)[_.].*', '\1', '')
106 let failed_root = 'failed/' .. root
Bram Moolenaar46acad72023-06-11 19:04:18 +0100107
Christian Brabandt56824432024-02-28 21:24:25 +0100108 " Execute the test if the "done" file does not exist or when the input file
109 " is newer.
110 let in_time = getftime(fname)
111 let out_time = getftime('done/' .. root)
112 if out_time < 0 || in_time > out_time
113 call ch_log('running tests for: ' .. fname)
Bram Moolenaar7d0dbd02023-06-24 00:56:50 +0100114
Christian Brabandt56824432024-02-28 21:24:25 +0100115 for dumpname in glob(failed_root .. '_\d*\.dump', 1, 1)
116 call delete(dumpname)
117 endfor
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100118 call delete('done/' .. root)
119
Christian Brabandt56824432024-02-28 21:24:25 +0100120 let lines =<< trim END
Christian Brabandt56824432024-02-28 21:24:25 +0100121 " extra info for shell variables
122 func ShellInfo()
123 let msg = ''
124 for [key, val] in items(b:)
125 if key =~ '^is_'
126 let msg ..= key .. ': ' .. val .. ', '
127 endif
128 endfor
129 if msg != ''
130 echomsg msg
131 endif
132 endfunc
133
134 au! SwapExists * call HandleSwapExists()
135 func HandleSwapExists()
136 " Ignore finding a swap file for the test input, the user might be
137 " editing it and that's OK.
138 if expand('<afile>') =~ 'input[/\\].*\..*'
139 let v:swapchoice = 'e'
140 endif
141 endfunc
142
143 func LoadFiletype(type)
144 for file in glob("ftplugin/" .. a:type .. "*.vim", 1, 1)
145 exe "source " .. file
146 endfor
147 redraw!
148 endfunc
149
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300150 func SetUpVim()
151 call cursor(1, 1)
Aliaksei Budaveia2addeb2024-03-18 20:39:32 +0100152 " Defend against rogue VIM_TEST_SETUP commands.
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300153 for _ in range(20)
Aliaksei Budaveia2addeb2024-03-18 20:39:32 +0100154 let lnum = search('\C\<VIM_TEST_SETUP\>', 'eW', 20)
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300155 if lnum < 1
156 break
157 endif
Aliaksei Budaveia2addeb2024-03-18 20:39:32 +0100158 exe substitute(getline(lnum), '\C.*\<VIM_TEST_SETUP\>', '', '')
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300159 endfor
160 call cursor(1, 1)
161 " BEGIN [runtime/defaults.vim]
Aliaksei Budaveia2addeb2024-03-18 20:39:32 +0100162 " Also, disable italic highlighting to avoid issues on some terminals.
163 set display=truncate ruler scrolloff=5 t_ZH= t_ZR=
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300164 syntax on
165 " END [runtime/defaults.vim]
166 redraw!
167 endfunc
Christian Brabandt56824432024-02-28 21:24:25 +0100168 END
169 call writefile(lines, 'Xtestscript')
170
171 " close all but the last window
172 while winnr('$') > 1
173 close
174 endwhile
175
176 " Redraw to make sure that messages are cleared and there is enough space
177 " for the terminal window.
178 redraw
179
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300180 " Let "Xtestscript#SetUpVim()" turn the syntax on.
Aliaksei Budaveif6069a72024-03-05 22:34:36 +0300181 let prefix = '-Nu NONE -S Xtestscript'
182 let path = get(setup, root, '')
183 " Source the found setup configuration file.
184 let args = !empty(path)
185 \ ? prefix .. ' -S ' .. path
186 \ : prefix
187 let buf = RunVimInTerminal(args, {})
Christian Brabandt56824432024-02-28 21:24:25 +0100188 " edit the file only after catching the SwapExists event
189 call term_sendkeys(buf, ":edit " .. fname .. "\<CR>")
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300190 " set up the testing environment
191 call term_sendkeys(buf, ":call SetUpVim()\<CR>")
Christian Brabandt56824432024-02-28 21:24:25 +0100192 " load filetype specific settings
193 call term_sendkeys(buf, ":call LoadFiletype('" .. filetype .. "')\<CR>")
194
195 if filetype == 'sh'
196 call term_sendkeys(buf, ":call ShellInfo()\<CR>")
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100197 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100198
199 " Screendump at the start of the file: failed/root_00.dump
200 let root_00 = root .. '_00'
201 call ch_log('First screendump for ' .. fname .. ': failed/' .. root_00 .. '.dump')
202 let fail = VerifyScreenDump(buf, root_00, {})
203
204 " clear the shell info if there are not enough lines to cause a scroll
205 if filetype == 'sh' && linecount <= 19
206 call term_sendkeys(buf, ":redraw\<CR>")
207 endif
208
209 " Make a Screendump every 18 lines of the file: failed/root_NN.dump
210 let topline = 1
211 let nr = 1
212 while linecount - topline > 20
213 let topline += 18
214 call term_sendkeys(buf, printf("%dGzt", topline))
215 let root_next = root .. printf('_%02d', nr)
216 call ch_log('Next screendump for ' .. fname .. ': failed/' .. root_next .. '.dump')
217 let fail += VerifyScreenDump(buf, root_next, {})
218 let nr += 1
219 endwhile
220
221 " Screendump at the end of the file: failed/root_99.dump
222 call term_sendkeys(buf, 'Gzb')
223 let root_last = root .. '_99'
224 call ch_log('Last screendump for ' .. fname .. ': failed/' .. root_last .. '.dump')
225 let fail += VerifyScreenDump(buf, root_last, {})
226
227 call StopVimInTerminal(buf)
228 call delete('Xtestscript')
229
230 " redraw here to avoid the following messages to get mixed up with screen
231 " output.
232 redraw
233
234 " Add any assert errors to s:messages.
235 if len(v:errors) > 0
236 call extend(s:messages, v:errors)
237 " Echo the errors here, in case the script aborts or the "messages" file
238 " is not displayed later.
239 echomsg v:errors
240 let v:errors = []
241 let fail += 1
242 endif
243
244 if fail == 0
245 call Message("Test " .. root .. " OK")
246
247 call writefile(['OK'], 'done/' .. root)
248
249 let ok_count += 1
250 else
251 call Message("Test " .. root .. " FAILED")
252
253 call delete('done/' .. root)
254
255 eval failed_tests->add(root)
256 if len(failed_tests) > MAX_FAILED_COUNT
257 call Message('')
258 call Message('Too many errors, aborting')
259 endif
260 endif
261 else
262 call Message("Test " .. root .. " skipped")
263 let skipped_count += 1
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100264 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100265
266 " Append messages to the file "testdir/messages"
267 call AppendMessages('Input file ' .. fname .. ':')
268
269 if len(failed_tests) > MAX_FAILED_COUNT
270 break
271 endif
272 endfor
273
274 call Message(s:test_run_message)
275 call Message('OK: ' .. ok_count)
276 call Message('FAILED: ' .. len(failed_tests) .. ': ' .. string(failed_tests))
277 call Message('skipped: ' .. skipped_count)
278 call AppendMessages('== SUMMARY ==')
279
280 if len(failed_tests) > 0
281 " have make report an error
282 cquit
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100283 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100284endfunc
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100285
Christian Brabandt56824432024-02-28 21:24:25 +0100286call RunTest()
Christian Brabandt627c9502024-02-10 13:02:17 +0100287
288" Matching "if 1" at the start.
289endif
290
Bram Moolenaar46acad72023-06-11 19:04:18 +0100291qall!
Christian Brabandt56824432024-02-28 21:24:25 +0100292
293" vim:ts=8