blob: 6d25c4ce5acb60708d6d69c2edaa495de56d74b2 [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)
152 " Defend against rogue TEST_SETUP commands.
153 for _ in range(20)
154 let lnum = search('\<TEST_SETUP\>', 'eW', 20)
155 if lnum < 1
156 break
157 endif
158 exe substitute(getline(lnum), '.*TEST_SETUP', '', '')
159 endfor
160 call cursor(1, 1)
161 " BEGIN [runtime/defaults.vim]
162 set display=truncate ruler scrolloff=5
163 " Provide pre-TEST_SETUP support for input/*.c.
164 let g:c_comment_strings = 1
165 syntax on
166 " END [runtime/defaults.vim]
167 redraw!
168 endfunc
Christian Brabandt56824432024-02-28 21:24:25 +0100169 END
170 call writefile(lines, 'Xtestscript')
171
172 " close all but the last window
173 while winnr('$') > 1
174 close
175 endwhile
176
177 " Redraw to make sure that messages are cleared and there is enough space
178 " for the terminal window.
179 redraw
180
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300181 " Let "Xtestscript#SetUpVim()" turn the syntax on.
Aliaksei Budaveif6069a72024-03-05 22:34:36 +0300182 let prefix = '-Nu NONE -S Xtestscript'
183 let path = get(setup, root, '')
184 " Source the found setup configuration file.
185 let args = !empty(path)
186 \ ? prefix .. ' -S ' .. path
187 \ : prefix
188 let buf = RunVimInTerminal(args, {})
Christian Brabandt56824432024-02-28 21:24:25 +0100189 " edit the file only after catching the SwapExists event
190 call term_sendkeys(buf, ":edit " .. fname .. "\<CR>")
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300191 " set up the testing environment
192 call term_sendkeys(buf, ":call SetUpVim()\<CR>")
Christian Brabandt56824432024-02-28 21:24:25 +0100193 " load filetype specific settings
194 call term_sendkeys(buf, ":call LoadFiletype('" .. filetype .. "')\<CR>")
195
196 if filetype == 'sh'
197 call term_sendkeys(buf, ":call ShellInfo()\<CR>")
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100198 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100199
200 " Screendump at the start of the file: failed/root_00.dump
201 let root_00 = root .. '_00'
202 call ch_log('First screendump for ' .. fname .. ': failed/' .. root_00 .. '.dump')
203 let fail = VerifyScreenDump(buf, root_00, {})
204
205 " clear the shell info if there are not enough lines to cause a scroll
206 if filetype == 'sh' && linecount <= 19
207 call term_sendkeys(buf, ":redraw\<CR>")
208 endif
209
210 " Make a Screendump every 18 lines of the file: failed/root_NN.dump
211 let topline = 1
212 let nr = 1
213 while linecount - topline > 20
214 let topline += 18
215 call term_sendkeys(buf, printf("%dGzt", topline))
216 let root_next = root .. printf('_%02d', nr)
217 call ch_log('Next screendump for ' .. fname .. ': failed/' .. root_next .. '.dump')
218 let fail += VerifyScreenDump(buf, root_next, {})
219 let nr += 1
220 endwhile
221
222 " Screendump at the end of the file: failed/root_99.dump
223 call term_sendkeys(buf, 'Gzb')
224 let root_last = root .. '_99'
225 call ch_log('Last screendump for ' .. fname .. ': failed/' .. root_last .. '.dump')
226 let fail += VerifyScreenDump(buf, root_last, {})
227
228 call StopVimInTerminal(buf)
229 call delete('Xtestscript')
230
231 " redraw here to avoid the following messages to get mixed up with screen
232 " output.
233 redraw
234
235 " Add any assert errors to s:messages.
236 if len(v:errors) > 0
237 call extend(s:messages, v:errors)
238 " Echo the errors here, in case the script aborts or the "messages" file
239 " is not displayed later.
240 echomsg v:errors
241 let v:errors = []
242 let fail += 1
243 endif
244
245 if fail == 0
246 call Message("Test " .. root .. " OK")
247
248 call writefile(['OK'], 'done/' .. root)
249
250 let ok_count += 1
251 else
252 call Message("Test " .. root .. " FAILED")
253
254 call delete('done/' .. root)
255
256 eval failed_tests->add(root)
257 if len(failed_tests) > MAX_FAILED_COUNT
258 call Message('')
259 call Message('Too many errors, aborting')
260 endif
261 endif
262 else
263 call Message("Test " .. root .. " skipped")
264 let skipped_count += 1
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100265 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100266
267 " Append messages to the file "testdir/messages"
268 call AppendMessages('Input file ' .. fname .. ':')
269
270 if len(failed_tests) > MAX_FAILED_COUNT
271 break
272 endif
273 endfor
274
275 call Message(s:test_run_message)
276 call Message('OK: ' .. ok_count)
277 call Message('FAILED: ' .. len(failed_tests) .. ': ' .. string(failed_tests))
278 call Message('skipped: ' .. skipped_count)
279 call AppendMessages('== SUMMARY ==')
280
281 if len(failed_tests) > 0
282 " have make report an error
283 cquit
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100284 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100285endfunc
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100286
Christian Brabandt56824432024-02-28 21:24:25 +0100287call RunTest()
Christian Brabandt627c9502024-02-10 13:02:17 +0100288
289" Matching "if 1" at the start.
290endif
291
Bram Moolenaar46acad72023-06-11 19:04:18 +0100292qall!
Christian Brabandt56824432024-02-28 21:24:25 +0100293
294" vim:ts=8