blob: ffa2be8dde265545059e23f4b88f44713134f1c4 [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
93 for fname in glob('input/*.*', 1, 1)
94 if fname =~ '\~$'
95 " backup file, skip
96 continue
Bram Moolenaar7d0dbd02023-06-24 00:56:50 +010097 endif
98
Christian Brabandt56824432024-02-28 21:24:25 +010099 let linecount = readfile(fname)->len()
100 let root = fnamemodify(fname, ':t:r')
101 let filetype = substitute(root, '\([^_.]*\)[_.].*', '\1', '')
102 let failed_root = 'failed/' .. root
Bram Moolenaar46acad72023-06-11 19:04:18 +0100103
Christian Brabandt56824432024-02-28 21:24:25 +0100104 " Execute the test if the "done" file does not exist or when the input file
105 " is newer.
106 let in_time = getftime(fname)
107 let out_time = getftime('done/' .. root)
108 if out_time < 0 || in_time > out_time
109 call ch_log('running tests for: ' .. fname)
Bram Moolenaar7d0dbd02023-06-24 00:56:50 +0100110
Christian Brabandt56824432024-02-28 21:24:25 +0100111 for dumpname in glob(failed_root .. '_\d*\.dump', 1, 1)
112 call delete(dumpname)
113 endfor
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100114 call delete('done/' .. root)
115
Christian Brabandt56824432024-02-28 21:24:25 +0100116 let lines =<< trim END
Christian Brabandt56824432024-02-28 21:24:25 +0100117 " extra info for shell variables
118 func ShellInfo()
119 let msg = ''
120 for [key, val] in items(b:)
121 if key =~ '^is_'
122 let msg ..= key .. ': ' .. val .. ', '
123 endif
124 endfor
125 if msg != ''
126 echomsg msg
127 endif
128 endfunc
129
130 au! SwapExists * call HandleSwapExists()
131 func HandleSwapExists()
132 " Ignore finding a swap file for the test input, the user might be
133 " editing it and that's OK.
134 if expand('<afile>') =~ 'input[/\\].*\..*'
135 let v:swapchoice = 'e'
136 endif
137 endfunc
138
139 func LoadFiletype(type)
140 for file in glob("ftplugin/" .. a:type .. "*.vim", 1, 1)
141 exe "source " .. file
142 endfor
143 redraw!
144 endfunc
145
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300146 func SetUpVim()
147 call cursor(1, 1)
148 " Defend against rogue TEST_SETUP commands.
149 for _ in range(20)
150 let lnum = search('\<TEST_SETUP\>', 'eW', 20)
151 if lnum < 1
152 break
153 endif
154 exe substitute(getline(lnum), '.*TEST_SETUP', '', '')
155 endfor
156 call cursor(1, 1)
157 " BEGIN [runtime/defaults.vim]
158 set display=truncate ruler scrolloff=5
159 " Provide pre-TEST_SETUP support for input/*.c.
160 let g:c_comment_strings = 1
161 syntax on
162 " END [runtime/defaults.vim]
163 redraw!
164 endfunc
Christian Brabandt56824432024-02-28 21:24:25 +0100165 END
166 call writefile(lines, 'Xtestscript')
167
168 " close all but the last window
169 while winnr('$') > 1
170 close
171 endwhile
172
173 " Redraw to make sure that messages are cleared and there is enough space
174 " for the terminal window.
175 redraw
176
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300177 " Let "Xtestscript#SetUpVim()" turn the syntax on.
178 let buf = RunVimInTerminal('-Nu NONE -S Xtestscript', {})
Christian Brabandt56824432024-02-28 21:24:25 +0100179 " edit the file only after catching the SwapExists event
180 call term_sendkeys(buf, ":edit " .. fname .. "\<CR>")
Aliaksei Budavei93edd252024-03-05 22:34:36 +0300181 " set up the testing environment
182 call term_sendkeys(buf, ":call SetUpVim()\<CR>")
Christian Brabandt56824432024-02-28 21:24:25 +0100183 " load filetype specific settings
184 call term_sendkeys(buf, ":call LoadFiletype('" .. filetype .. "')\<CR>")
185
186 if filetype == 'sh'
187 call term_sendkeys(buf, ":call ShellInfo()\<CR>")
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100188 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100189
190 " Screendump at the start of the file: failed/root_00.dump
191 let root_00 = root .. '_00'
192 call ch_log('First screendump for ' .. fname .. ': failed/' .. root_00 .. '.dump')
193 let fail = VerifyScreenDump(buf, root_00, {})
194
195 " clear the shell info if there are not enough lines to cause a scroll
196 if filetype == 'sh' && linecount <= 19
197 call term_sendkeys(buf, ":redraw\<CR>")
198 endif
199
200 " Make a Screendump every 18 lines of the file: failed/root_NN.dump
201 let topline = 1
202 let nr = 1
203 while linecount - topline > 20
204 let topline += 18
205 call term_sendkeys(buf, printf("%dGzt", topline))
206 let root_next = root .. printf('_%02d', nr)
207 call ch_log('Next screendump for ' .. fname .. ': failed/' .. root_next .. '.dump')
208 let fail += VerifyScreenDump(buf, root_next, {})
209 let nr += 1
210 endwhile
211
212 " Screendump at the end of the file: failed/root_99.dump
213 call term_sendkeys(buf, 'Gzb')
214 let root_last = root .. '_99'
215 call ch_log('Last screendump for ' .. fname .. ': failed/' .. root_last .. '.dump')
216 let fail += VerifyScreenDump(buf, root_last, {})
217
218 call StopVimInTerminal(buf)
219 call delete('Xtestscript')
220
221 " redraw here to avoid the following messages to get mixed up with screen
222 " output.
223 redraw
224
225 " Add any assert errors to s:messages.
226 if len(v:errors) > 0
227 call extend(s:messages, v:errors)
228 " Echo the errors here, in case the script aborts or the "messages" file
229 " is not displayed later.
230 echomsg v:errors
231 let v:errors = []
232 let fail += 1
233 endif
234
235 if fail == 0
236 call Message("Test " .. root .. " OK")
237
238 call writefile(['OK'], 'done/' .. root)
239
240 let ok_count += 1
241 else
242 call Message("Test " .. root .. " FAILED")
243
244 call delete('done/' .. root)
245
246 eval failed_tests->add(root)
247 if len(failed_tests) > MAX_FAILED_COUNT
248 call Message('')
249 call Message('Too many errors, aborting')
250 endif
251 endif
252 else
253 call Message("Test " .. root .. " skipped")
254 let skipped_count += 1
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100255 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100256
257 " Append messages to the file "testdir/messages"
258 call AppendMessages('Input file ' .. fname .. ':')
259
260 if len(failed_tests) > MAX_FAILED_COUNT
261 break
262 endif
263 endfor
264
265 call Message(s:test_run_message)
266 call Message('OK: ' .. ok_count)
267 call Message('FAILED: ' .. len(failed_tests) .. ': ' .. string(failed_tests))
268 call Message('skipped: ' .. skipped_count)
269 call AppendMessages('== SUMMARY ==')
270
271 if len(failed_tests) > 0
272 " have make report an error
273 cquit
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100274 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100275endfunc
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100276
Christian Brabandt56824432024-02-28 21:24:25 +0100277call RunTest()
Christian Brabandt627c9502024-02-10 13:02:17 +0100278
279" Matching "if 1" at the start.
280endif
281
Bram Moolenaar46acad72023-06-11 19:04:18 +0100282qall!
Christian Brabandt56824432024-02-28 21:24:25 +0100283
284" vim:ts=8