Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 1 | " 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 |
| 6 | if 1 |
| 7 | |
Bram Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 8 | " Remember the directory where we started. Will change to "testdir" below. |
| 9 | let syntaxDir = getcwd() |
| 10 | |
| 11 | let s:messagesFname = fnameescape(syntaxDir .. '/testdir/messages') |
| 12 | |
| 13 | let s:messages = [] |
| 14 | |
| 15 | " Add one message to the list of messages |
| 16 | func Message(msg) |
| 17 | echomsg a:msg |
| 18 | call add(s:messages, a:msg) |
| 19 | endfunc |
| 20 | |
| 21 | " Report a fatal message and exit |
| 22 | func Fatal(msg) |
| 23 | echoerr a:msg |
| 24 | call AppendMessages(a:msg) |
| 25 | qall! |
| 26 | endfunc |
| 27 | |
| 28 | " Append s:messages to the messages file and make it empty. |
| 29 | func 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 |
| 36 | endfunc |
| 37 | |
| 38 | " Relevant messages are written to the "messages" file. |
| 39 | " If the file already exists it is appended to. |
| 40 | exe 'split ' .. s:messagesFname |
| 41 | call append(line('$'), repeat('=-', 70)) |
| 42 | call append(line('$'), '') |
Bram Moolenaar | 031d632 | 2023-06-22 22:38:54 +0100 | [diff] [blame] | 43 | let s:test_run_message = 'Test run on ' .. strftime("%Y %b %d %H:%M:%S") |
| 44 | call append(line('$'), s:test_run_message) |
Bram Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 45 | wq |
| 46 | |
| 47 | if syntaxDir !~ '[/\\]runtime[/\\]syntax\>' |
| 48 | call Fatal('Current directory must be "runtime/syntax"') |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 49 | endif |
| 50 | if !isdirectory('testdir') |
Bram Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 51 | call Fatal('"testdir" directory not found') |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 52 | endif |
| 53 | |
| 54 | " Use the script for source code screendump testing. It sources other scripts, |
| 55 | " therefore we must "cd" there. |
| 56 | cd ../../src/testdir |
| 57 | source screendump.vim |
Bram Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 58 | exe 'cd ' .. fnameescape(syntaxDir) |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 59 | |
| 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. |
| 63 | if !CanRunVimInTerminal() |
Bram Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 64 | call Fatal('Cannot make screendumps, aborting') |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 65 | endif |
| 66 | |
| 67 | cd testdir |
| 68 | if !isdirectory('done') |
| 69 | call mkdir('done') |
| 70 | endif |
| 71 | |
| 72 | set nocp |
| 73 | set nowrapscan |
| 74 | set report=9999 |
| 75 | set modeline |
| 76 | set debug=throw |
| 77 | set nomore |
| 78 | |
| 79 | au! SwapExists * call HandleSwapExists() |
| 80 | func 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 |
| 86 | endfunc |
| 87 | |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 88 | func RunTest() |
| 89 | let ok_count = 0 |
| 90 | let failed_tests = [] |
| 91 | let skipped_count = 0 |
| 92 | let MAX_FAILED_COUNT = 5 |
Aliaksei Budavei | f6069a7 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 93 | " 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 Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 97 | for fname in glob('input/*.*', 1, 1) |
| 98 | if fname =~ '\~$' |
| 99 | " backup file, skip |
| 100 | continue |
Bram Moolenaar | 7d0dbd0 | 2023-06-24 00:56:50 +0100 | [diff] [blame] | 101 | endif |
| 102 | |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 103 | 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 Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 107 | |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 108 | " 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 Moolenaar | 7d0dbd0 | 2023-06-24 00:56:50 +0100 | [diff] [blame] | 114 | |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 115 | for dumpname in glob(failed_root .. '_\d*\.dump', 1, 1) |
| 116 | call delete(dumpname) |
| 117 | endfor |
Bram Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 118 | call delete('done/' .. root) |
| 119 | |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 120 | let lines =<< trim END |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 121 | " 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 Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 150 | func SetUpVim() |
| 151 | call cursor(1, 1) |
Aliaksei Budavei | a2addeb | 2024-03-18 20:39:32 +0100 | [diff] [blame] | 152 | " Defend against rogue VIM_TEST_SETUP commands. |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 153 | for _ in range(20) |
Aliaksei Budavei | a2addeb | 2024-03-18 20:39:32 +0100 | [diff] [blame] | 154 | let lnum = search('\C\<VIM_TEST_SETUP\>', 'eW', 20) |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 155 | if lnum < 1 |
| 156 | break |
| 157 | endif |
Aliaksei Budavei | a2addeb | 2024-03-18 20:39:32 +0100 | [diff] [blame] | 158 | exe substitute(getline(lnum), '\C.*\<VIM_TEST_SETUP\>', '', '') |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 159 | endfor |
| 160 | call cursor(1, 1) |
| 161 | " BEGIN [runtime/defaults.vim] |
Aliaksei Budavei | a2addeb | 2024-03-18 20:39:32 +0100 | [diff] [blame] | 162 | " Also, disable italic highlighting to avoid issues on some terminals. |
| 163 | set display=truncate ruler scrolloff=5 t_ZH= t_ZR= |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 164 | syntax on |
| 165 | " END [runtime/defaults.vim] |
| 166 | redraw! |
| 167 | endfunc |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 168 | 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 Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 180 | " Let "Xtestscript#SetUpVim()" turn the syntax on. |
Aliaksei Budavei | f6069a7 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 181 | 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 Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 188 | " edit the file only after catching the SwapExists event |
| 189 | call term_sendkeys(buf, ":edit " .. fname .. "\<CR>") |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 190 | " set up the testing environment |
| 191 | call term_sendkeys(buf, ":call SetUpVim()\<CR>") |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 192 | " 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 Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 197 | endif |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 198 | |
| 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 Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 264 | endif |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 265 | |
| 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 Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 283 | endif |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 284 | endfunc |
Bram Moolenaar | 1aa5f1c | 2023-06-22 21:57:51 +0100 | [diff] [blame] | 285 | |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 286 | call RunTest() |
Christian Brabandt | 627c950 | 2024-02-10 13:02:17 +0100 | [diff] [blame] | 287 | |
| 288 | " Matching "if 1" at the start. |
| 289 | endif |
| 290 | |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 291 | qall! |
Christian Brabandt | 5682443 | 2024-02-28 21:24:25 +0100 | [diff] [blame] | 292 | |
| 293 | " vim:ts=8 |