blob: e19d4f5e5b172d8e7e13dfbbe50688a35bbd8739 [file] [log] [blame]
Bram Moolenaarda650582018-02-20 15:51:40 +01001" Functions shared by tests making screen dumps.
2
3" Only load this script once.
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02004if exists('*VerifyScreenDump')
Bram Moolenaarda650582018-02-20 15:51:40 +01005 finish
6endif
7
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02008source shared.vim
9source term_util.vim
Bram Moolenaarf2732452018-06-03 14:47:35 +020010
11" Skip the rest if there is no terminal feature at all.
12if !has('terminal')
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010013 finish
14endif
15
Bram Moolenaarda650582018-02-20 15:51:40 +010016" Verify that Vim running in terminal buffer "buf" matches the screen dump.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010017" "options" is passed to term_dumpwrite().
Bram Moolenaar3e794272022-05-06 11:21:19 +010018" Additionally, the "wait" entry can specify the maximum time to wait for the
19" screen dump to match in msec (default 1000 msec).
Bram Moolenaarda650582018-02-20 15:51:40 +010020" The file name used is "dumps/{filename}.dump".
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020021" Optionally an extra argument can be passed which is prepended to the error
22" message. Use this when using the same dump file with different options.
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020023" Returns non-zero when verification fails.
24func VerifyScreenDump(buf, filename, options, ...)
Bram Moolenaarda650582018-02-20 15:51:40 +010025 let reference = 'dumps/' . a:filename . '.dump'
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010026 let testfile = 'failed/' . a:filename . '.dump'
27
Bram Moolenaar3e794272022-05-06 11:21:19 +010028 let max_loops = get(a:options, 'wait', 1000) / 10
29
Bram Moolenaar3cdcb092020-03-18 19:18:10 +010030 " Starting a terminal to make a screendump is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +010031 let g:test_is_flaky = 1
Bram Moolenaar3cdcb092020-03-18 19:18:10 +010032
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000033 " wait for the pending updates to be handled.
34 call TermWait(a:buf)
35
Bram Moolenaar1bc353b2019-09-01 14:45:28 +020036 " Redraw to execute the code that updates the screen. Otherwise we get the
Bram Moolenaar87dcfd72019-04-13 22:35:29 +020037 " text and attributes only from the internal buffer.
38 redraw
39
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010040 let did_mkdir = 0
41 if !isdirectory('failed')
42 let did_mkdir = 1
43 call mkdir('failed')
44 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010045
46 let i = 0
47 while 1
Bram Moolenaarb6fc7282018-12-04 22:24:16 +010048 " leave some time for updating the original window
49 sleep 10m
Bram Moolenaarda650582018-02-20 15:51:40 +010050 call delete(testfile)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010051 call term_dumpwrite(a:buf, testfile, a:options)
Bram Moolenaar353aca12019-02-21 17:05:59 +010052 let testdump = readfile(testfile)
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020053 if filereadable(reference)
54 let refdump = readfile(reference)
55 else
56 " Must be a new screendump, always fail
57 let refdump = []
58 endif
Bram Moolenaar353aca12019-02-21 17:05:59 +010059 if refdump == testdump
Bram Moolenaarda650582018-02-20 15:51:40 +010060 call delete(testfile)
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010061 if did_mkdir
62 call delete('failed', 'd')
63 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010064 break
65 endif
Bram Moolenaar3e794272022-05-06 11:21:19 +010066 if i == max_loops
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020067 " Leave the failed dump around for inspection.
68 if filereadable(reference)
Bram Moolenaar8a5c7ef2019-06-16 16:14:20 +020069 let msg = 'See dump file difference: call term_dumpdiff("testdir/' .. testfile .. '", "testdir/' .. reference .. '")'
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020070 if a:0 == 1
71 let msg = a:1 . ': ' . msg
72 endif
73 if len(testdump) != len(refdump)
74 let msg = msg . '; line count is ' . len(testdump) . ' instead of ' . len(refdump)
75 endif
76 else
Bram Moolenaar8a5c7ef2019-06-16 16:14:20 +020077 let msg = 'See new dump file: call term_dumpload("testdir/' .. testfile .. '")'
Bram Moolenaar96ba25a2022-07-04 17:34:33 +010078 " no point in retrying
79 let g:run_nr = 10
Bram Moolenaar353aca12019-02-21 17:05:59 +010080 endif
81 for i in range(len(refdump))
82 if i >= len(testdump)
83 break
84 endif
85 if testdump[i] != refdump[i]
86 let msg = msg . '; difference in line ' . (i + 1) . ': "' . testdump[i] . '"'
87 endif
88 endfor
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020089 call assert_report(msg)
90 return 1
Bram Moolenaarda650582018-02-20 15:51:40 +010091 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010092 let i += 1
93 endwhile
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020094 return 0
Bram Moolenaarda650582018-02-20 15:51:40 +010095endfunc