blob: a2cdef3721b0f79ce400e749632bdd088c38952c [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
117 syntax on
118
119 " extra info for shell variables
120 func ShellInfo()
121 let msg = ''
122 for [key, val] in items(b:)
123 if key =~ '^is_'
124 let msg ..= key .. ': ' .. val .. ', '
125 endif
126 endfor
127 if msg != ''
128 echomsg msg
129 endif
130 endfunc
131
132 au! SwapExists * call HandleSwapExists()
133 func HandleSwapExists()
134 " Ignore finding a swap file for the test input, the user might be
135 " editing it and that's OK.
136 if expand('<afile>') =~ 'input[/\\].*\..*'
137 let v:swapchoice = 'e'
138 endif
139 endfunc
140
141 func LoadFiletype(type)
142 for file in glob("ftplugin/" .. a:type .. "*.vim", 1, 1)
143 exe "source " .. file
144 endfor
145 redraw!
146 endfunc
147
148 END
149 call writefile(lines, 'Xtestscript')
150
151 " close all but the last window
152 while winnr('$') > 1
153 close
154 endwhile
155
156 " Redraw to make sure that messages are cleared and there is enough space
157 " for the terminal window.
158 redraw
159
160 let buf = RunVimInTerminal('-S Xtestscript', {})
161 " edit the file only after catching the SwapExists event
162 call term_sendkeys(buf, ":edit " .. fname .. "\<CR>")
163 " load filetype specific settings
164 call term_sendkeys(buf, ":call LoadFiletype('" .. filetype .. "')\<CR>")
165
166 if filetype == 'sh'
167 call term_sendkeys(buf, ":call ShellInfo()\<CR>")
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100168 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100169
170 " Screendump at the start of the file: failed/root_00.dump
171 let root_00 = root .. '_00'
172 call ch_log('First screendump for ' .. fname .. ': failed/' .. root_00 .. '.dump')
173 let fail = VerifyScreenDump(buf, root_00, {})
174
175 " clear the shell info if there are not enough lines to cause a scroll
176 if filetype == 'sh' && linecount <= 19
177 call term_sendkeys(buf, ":redraw\<CR>")
178 endif
179
180 " Make a Screendump every 18 lines of the file: failed/root_NN.dump
181 let topline = 1
182 let nr = 1
183 while linecount - topline > 20
184 let topline += 18
185 call term_sendkeys(buf, printf("%dGzt", topline))
186 let root_next = root .. printf('_%02d', nr)
187 call ch_log('Next screendump for ' .. fname .. ': failed/' .. root_next .. '.dump')
188 let fail += VerifyScreenDump(buf, root_next, {})
189 let nr += 1
190 endwhile
191
192 " Screendump at the end of the file: failed/root_99.dump
193 call term_sendkeys(buf, 'Gzb')
194 let root_last = root .. '_99'
195 call ch_log('Last screendump for ' .. fname .. ': failed/' .. root_last .. '.dump')
196 let fail += VerifyScreenDump(buf, root_last, {})
197
198 call StopVimInTerminal(buf)
199 call delete('Xtestscript')
200
201 " redraw here to avoid the following messages to get mixed up with screen
202 " output.
203 redraw
204
205 " Add any assert errors to s:messages.
206 if len(v:errors) > 0
207 call extend(s:messages, v:errors)
208 " Echo the errors here, in case the script aborts or the "messages" file
209 " is not displayed later.
210 echomsg v:errors
211 let v:errors = []
212 let fail += 1
213 endif
214
215 if fail == 0
216 call Message("Test " .. root .. " OK")
217
218 call writefile(['OK'], 'done/' .. root)
219
220 let ok_count += 1
221 else
222 call Message("Test " .. root .. " FAILED")
223
224 call delete('done/' .. root)
225
226 eval failed_tests->add(root)
227 if len(failed_tests) > MAX_FAILED_COUNT
228 call Message('')
229 call Message('Too many errors, aborting')
230 endif
231 endif
232 else
233 call Message("Test " .. root .. " skipped")
234 let skipped_count += 1
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100235 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100236
237 " Append messages to the file "testdir/messages"
238 call AppendMessages('Input file ' .. fname .. ':')
239
240 if len(failed_tests) > MAX_FAILED_COUNT
241 break
242 endif
243 endfor
244
245 call Message(s:test_run_message)
246 call Message('OK: ' .. ok_count)
247 call Message('FAILED: ' .. len(failed_tests) .. ': ' .. string(failed_tests))
248 call Message('skipped: ' .. skipped_count)
249 call AppendMessages('== SUMMARY ==')
250
251 if len(failed_tests) > 0
252 " have make report an error
253 cquit
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100254 endif
Christian Brabandt56824432024-02-28 21:24:25 +0100255endfunc
Bram Moolenaar1aa5f1c2023-06-22 21:57:51 +0100256
Christian Brabandt56824432024-02-28 21:24:25 +0100257call RunTest()
Christian Brabandt627c9502024-02-10 13:02:17 +0100258
259" Matching "if 1" at the start.
260endif
261
Bram Moolenaar46acad72023-06-11 19:04:18 +0100262qall!
Christian Brabandt56824432024-02-28 21:24:25 +0100263
264" vim:ts=8