blob: 4802315baeb40af3abb38831f572a9a0179bca25 [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
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300163 syntax on
164 " END [runtime/defaults.vim]
165 redraw!
166 endfunc
Christian Brabandt56824432024-02-28 21:24:25 +0100167 END
168 call writefile(lines, 'Xtestscript')
169
170 " close all but the last window
171 while winnr('$') > 1
172 close
173 endwhile
174
175 " Redraw to make sure that messages are cleared and there is enough space
176 " for the terminal window.
177 redraw
178
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300179 " Let "Xtestscript#SetUpVim()" turn the syntax on.
Aliaksei Budaveif6069a72024-03-05 22:34:36 +0300180 let prefix = '-Nu NONE -S Xtestscript'
181 let path = get(setup, root, '')
182 " Source the found setup configuration file.
183 let args = !empty(path)
184 \ ? prefix .. ' -S ' .. path
185 \ : prefix
186 let buf = RunVimInTerminal(args, {})
Christian Brabandt56824432024-02-28 21:24:25 +0100187 " edit the file only after catching the SwapExists event
188 call term_sendkeys(buf, ":edit " .. fname .. "\<CR>")
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300189 " set up the testing environment
190 call term_sendkeys(buf, ":call SetUpVim()\<CR>")
Christian Brabandt56824432024-02-28 21:24:25 +0100191 " load filetype specific settings
192 call term_sendkeys(buf, ":call LoadFiletype('" .. filetype .. "')\<CR>")
193
194 if filetype == 'sh'
195 call term_sendkeys(buf, ":call ShellInfo()\<CR>")
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100196 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100197
198 " Screendump at the start of the file: failed/root_00.dump
199 let root_00 = root .. '_00'
200 call ch_log('First screendump for ' .. fname .. ': failed/' .. root_00 .. '.dump')
201 let fail = VerifyScreenDump(buf, root_00, {})
202
203 " clear the shell info if there are not enough lines to cause a scroll
204 if filetype == 'sh' && linecount <= 19
205 call term_sendkeys(buf, ":redraw\<CR>")
206 endif
207
208 " Make a Screendump every 18 lines of the file: failed/root_NN.dump
209 let topline = 1
210 let nr = 1
211 while linecount - topline > 20
212 let topline += 18
213 call term_sendkeys(buf, printf("%dGzt", topline))
214 let root_next = root .. printf('_%02d', nr)
215 call ch_log('Next screendump for ' .. fname .. ': failed/' .. root_next .. '.dump')
216 let fail += VerifyScreenDump(buf, root_next, {})
217 let nr += 1
218 endwhile
219
220 " Screendump at the end of the file: failed/root_99.dump
221 call term_sendkeys(buf, 'Gzb')
222 let root_last = root .. '_99'
223 call ch_log('Last screendump for ' .. fname .. ': failed/' .. root_last .. '.dump')
224 let fail += VerifyScreenDump(buf, root_last, {})
225
226 call StopVimInTerminal(buf)
227 call delete('Xtestscript')
228
229 " redraw here to avoid the following messages to get mixed up with screen
230 " output.
231 redraw
232
233 " Add any assert errors to s:messages.
234 if len(v:errors) > 0
235 call extend(s:messages, v:errors)
236 " Echo the errors here, in case the script aborts or the "messages" file
237 " is not displayed later.
238 echomsg v:errors
239 let v:errors = []
240 let fail += 1
241 endif
242
243 if fail == 0
244 call Message("Test " .. root .. " OK")
245
246 call writefile(['OK'], 'done/' .. root)
247
248 let ok_count += 1
249 else
250 call Message("Test " .. root .. " FAILED")
251
252 call delete('done/' .. root)
253
254 eval failed_tests->add(root)
255 if len(failed_tests) > MAX_FAILED_COUNT
256 call Message('')
257 call Message('Too many errors, aborting')
258 endif
259 endif
260 else
261 call Message("Test " .. root .. " skipped")
262 let skipped_count += 1
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100263 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100264
265 " Append messages to the file "testdir/messages"
266 call AppendMessages('Input file ' .. fname .. ':')
267
268 if len(failed_tests) > MAX_FAILED_COUNT
269 break
270 endif
271 endfor
272
273 call Message(s:test_run_message)
274 call Message('OK: ' .. ok_count)
275 call Message('FAILED: ' .. len(failed_tests) .. ': ' .. string(failed_tests))
276 call Message('skipped: ' .. skipped_count)
277 call AppendMessages('== SUMMARY ==')
278
279 if len(failed_tests) > 0
280 " have make report an error
281 cquit
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100282 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100283endfunc
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100284
Christian Brabandt56824432024-02-28 21:24:25 +0100285call RunTest()
Christian Brabandt627c9502024-02-10 13:02:17 +0100286
287" Matching "if 1" at the start.
288endif
289
Bram Moolenaar46acad72023-06-11 19:04:18 +0100290qall!
Christian Brabandt56824432024-02-28 21:24:25 +0100291
292" vim:ts=8