Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 1 | " Functions shared by tests making screen dumps. |
| 2 | |
| 3 | " Only load this script once. |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 4 | if exists('*VerifyScreenDump') |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 5 | finish |
| 6 | endif |
| 7 | |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 8 | source shared.vim |
| 9 | source term_util.vim |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 10 | |
| 11 | " Skip the rest if there is no terminal feature at all. |
| 12 | if !has('terminal') |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 13 | finish |
| 14 | endif |
| 15 | |
Bram Moolenaar | 1190139 | 2022-09-26 19:50:44 +0100 | [diff] [blame] | 16 | " Read a dump file "fname" and if "filter" exists apply it to the text. |
| 17 | def ReadAndFilter(fname: string, filter: string): list<string> |
| 18 | var contents = readfile(fname) |
| 19 | |
| 20 | if filereadable(filter) |
| 21 | # do this in the bottom window so that the terminal window is unaffected |
| 22 | wincmd j |
| 23 | enew |
| 24 | setline(1, contents) |
| 25 | exe "source " .. filter |
| 26 | contents = getline(1, '$') |
| 27 | enew! |
| 28 | wincmd k |
| 29 | redraw |
| 30 | endif |
| 31 | |
| 32 | return contents |
| 33 | enddef |
| 34 | |
| 35 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 36 | " Verify that Vim running in terminal buffer "buf" matches the screen dump. |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 37 | " "options" is passed to term_dumpwrite(). |
Bram Moolenaar | 3e79427 | 2022-05-06 11:21:19 +0100 | [diff] [blame] | 38 | " Additionally, the "wait" entry can specify the maximum time to wait for the |
| 39 | " screen dump to match in msec (default 1000 msec). |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 40 | " The file name used is "dumps/{filename}.dump". |
Bram Moolenaar | 1190139 | 2022-09-26 19:50:44 +0100 | [diff] [blame] | 41 | " |
| 42 | " To ignore part of the dump, provide a "dumps/{filename}.vim" file with |
| 43 | " Vim commands to be applied to both the reference and the current dump, so |
| 44 | " that parts that are irrelevant are not used for the comparison. The result |
| 45 | " is NOT written, thus "term_dumpdiff()" shows the difference anyway. |
| 46 | " |
Bram Moolenaar | 6f8bdab | 2018-09-09 22:02:24 +0200 | [diff] [blame] | 47 | " Optionally an extra argument can be passed which is prepended to the error |
| 48 | " message. Use this when using the same dump file with different options. |
Bram Moolenaar | 6f8bdab | 2018-09-09 22:02:24 +0200 | [diff] [blame] | 49 | " Returns non-zero when verification fails. |
| 50 | func VerifyScreenDump(buf, filename, options, ...) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 51 | let reference = 'dumps/' . a:filename . '.dump' |
Bram Moolenaar | 1190139 | 2022-09-26 19:50:44 +0100 | [diff] [blame] | 52 | let filter = 'dumps/' . a:filename . '.vim' |
Bram Moolenaar | ef7f0e3 | 2019-03-30 15:59:51 +0100 | [diff] [blame] | 53 | let testfile = 'failed/' . a:filename . '.dump' |
| 54 | |
Bram Moolenaar | 3e79427 | 2022-05-06 11:21:19 +0100 | [diff] [blame] | 55 | let max_loops = get(a:options, 'wait', 1000) / 10 |
| 56 | |
Bram Moolenaar | 3cdcb09 | 2020-03-18 19:18:10 +0100 | [diff] [blame] | 57 | " Starting a terminal to make a screendump is always considered flaky. |
Bram Moolenaar | 30d53e2 | 2020-03-18 21:10:44 +0100 | [diff] [blame] | 58 | let g:test_is_flaky = 1 |
Bram Moolenaar | 3cdcb09 | 2020-03-18 19:18:10 +0100 | [diff] [blame] | 59 | |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 60 | " wait for the pending updates to be handled. |
| 61 | call TermWait(a:buf) |
| 62 | |
Bram Moolenaar | 1bc353b | 2019-09-01 14:45:28 +0200 | [diff] [blame] | 63 | " Redraw to execute the code that updates the screen. Otherwise we get the |
Bram Moolenaar | 87dcfd7 | 2019-04-13 22:35:29 +0200 | [diff] [blame] | 64 | " text and attributes only from the internal buffer. |
| 65 | redraw |
| 66 | |
Bram Moolenaar | 1190139 | 2022-09-26 19:50:44 +0100 | [diff] [blame] | 67 | if filereadable(reference) |
| 68 | let refdump = ReadAndFilter(reference, filter) |
| 69 | else |
| 70 | " Must be a new screendump, always fail |
| 71 | let refdump = [] |
| 72 | endif |
| 73 | |
Bram Moolenaar | ef7f0e3 | 2019-03-30 15:59:51 +0100 | [diff] [blame] | 74 | let did_mkdir = 0 |
| 75 | if !isdirectory('failed') |
| 76 | let did_mkdir = 1 |
| 77 | call mkdir('failed') |
| 78 | endif |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 79 | |
| 80 | let i = 0 |
| 81 | while 1 |
Bram Moolenaar | b6fc728 | 2018-12-04 22:24:16 +0100 | [diff] [blame] | 82 | " leave some time for updating the original window |
| 83 | sleep 10m |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 84 | call delete(testfile) |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 85 | call term_dumpwrite(a:buf, testfile, a:options) |
Bram Moolenaar | 1190139 | 2022-09-26 19:50:44 +0100 | [diff] [blame] | 86 | let testdump = ReadAndFilter(testfile, filter) |
Bram Moolenaar | 353aca1 | 2019-02-21 17:05:59 +0100 | [diff] [blame] | 87 | if refdump == testdump |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 88 | call delete(testfile) |
Bram Moolenaar | ef7f0e3 | 2019-03-30 15:59:51 +0100 | [diff] [blame] | 89 | if did_mkdir |
| 90 | call delete('failed', 'd') |
| 91 | endif |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 92 | break |
| 93 | endif |
Bram Moolenaar | 3e79427 | 2022-05-06 11:21:19 +0100 | [diff] [blame] | 94 | if i == max_loops |
Bram Moolenaar | 2d7260d | 2019-04-06 20:51:52 +0200 | [diff] [blame] | 95 | " Leave the failed dump around for inspection. |
| 96 | if filereadable(reference) |
Bram Moolenaar | 8a5c7ef | 2019-06-16 16:14:20 +0200 | [diff] [blame] | 97 | let msg = 'See dump file difference: call term_dumpdiff("testdir/' .. testfile .. '", "testdir/' .. reference .. '")' |
Bram Moolenaar | 2d7260d | 2019-04-06 20:51:52 +0200 | [diff] [blame] | 98 | if a:0 == 1 |
| 99 | let msg = a:1 . ': ' . msg |
| 100 | endif |
| 101 | if len(testdump) != len(refdump) |
| 102 | let msg = msg . '; line count is ' . len(testdump) . ' instead of ' . len(refdump) |
| 103 | endif |
| 104 | else |
Bram Moolenaar | 8a5c7ef | 2019-06-16 16:14:20 +0200 | [diff] [blame] | 105 | let msg = 'See new dump file: call term_dumpload("testdir/' .. testfile .. '")' |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 106 | " no point in retrying |
| 107 | let g:run_nr = 10 |
Bram Moolenaar | 353aca1 | 2019-02-21 17:05:59 +0100 | [diff] [blame] | 108 | endif |
| 109 | for i in range(len(refdump)) |
| 110 | if i >= len(testdump) |
| 111 | break |
| 112 | endif |
| 113 | if testdump[i] != refdump[i] |
| 114 | let msg = msg . '; difference in line ' . (i + 1) . ': "' . testdump[i] . '"' |
| 115 | endif |
| 116 | endfor |
Bram Moolenaar | 6f8bdab | 2018-09-09 22:02:24 +0200 | [diff] [blame] | 117 | call assert_report(msg) |
| 118 | return 1 |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 119 | endif |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 120 | let i += 1 |
| 121 | endwhile |
Bram Moolenaar | 6f8bdab | 2018-09-09 22:02:24 +0200 | [diff] [blame] | 122 | return 0 |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 123 | endfunc |