robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

bfredlMerge pull request #39074 from bfredl/retromania772cfcf267

master
3.7 KiB160 linesraw
1-- execute old style tests
2
3-- when compared to the testdir/Makefile based approach, this replaces
4-- all wrapper process logic:
5-- Makefile
6-- runnvim.sh and its library test.sh
7-- runnvim.vim (wrapper which runs a nested vim inside a terminal)
8
9-- it DOES NOT not replace the logic running inside the nvim-under-test
10-- runtest.vim and its libraries setup.vim, shared.vim, check.vim
11
12local nvim_bin = vim.v.progpath -- must be absolute
13
14vim.api.nvim_set_current_dir('test/old/testdir/')
15
16local function readable(fn)
17  return vim.fn.filereadable(fn) ~= 0
18end
19
20local errlog = 'test.log'
21local messages = 'messages'
22local starttime = 'starttime'
23local gen_opt_log = 'gen_opt_test.log'
24
25function oksystem(args)
26  local res = vim.system(args, {}):wait()
27  if res.code ~= 0 then
28    error(vim.inspect(args) .. ' failed:\n' .. res.stdout .. res.stderr)
29  end
30  return res
31end
32
33function clean_output()
34  for _, f in ipairs { errlog, messages, starttime, gen_opt_log, 'opt_test.vim' } do
35    vim.uv.fs_unlink(f)
36  end
37  -- check status, some tests mess with perms so this could fail:
38  oksystem { 'bash', '-c', 'rm -rf X* viminfo test.out test.ok' }
39end
40
41function if_nonempty(rawtext, name)
42  text = vim.trim(rawtext)
43  if #text > 0 then
44    if name then
45      print('== ' .. name)
46    end
47    print(text)
48    print(name and '== END ' .. name or '\n')
49  end
50end
51
52function if_exists(fn, name)
53  local fil = io.open(fn)
54  if fil then
55    if_nonempty(fil:read '*a', name)
56    fil:close()
57    return true
58  end
59end
60
61local verbose = vim.env.VERBOSE
62
63function run_nvim(...)
64  local cmd = vim.system(
65    { nvim_bin, '-u', 'NONE', '-i', 'NONE', '--headless', ... },
66    { env = { VIMRUNTIME = '../../../runtime' } }
67  )
68  return cmd:wait()
69end
70
71function run_test(testfile)
72  if not vim.endswith(testfile, '.vim') then
73    error 'not an oldtest'
74  end
75
76  clean_output()
77
78  if testfile == 'test_options_all.vim' then
79    local res = run_nvim(
80      '-S',
81      'gen_opt_test.vim',
82      '../../../src/nvim/options.lua',
83      '../../../runtime/doc/options.txt'
84    )
85
86    if if_exists(gen_opt_log, 'GEN opt_test.vim') or res.code > 0 then
87      return false
88    end
89  end
90
91  local resfile = string.sub(testfile, 1, -4) .. 'res'
92  vim.uv.fs_unlink(resfile)
93  assert(not readable(resfile))
94
95  local opts = 'set shortmess-=F backupdir=. undodir=. viewdir=.'
96  local res = run_nvim('--cmd', opts, '-S', 'runtest.vim', testfile)
97  local passed = (readable(resfile) and res.code == 0)
98
99  if_exists(messages)
100
101  if verbose or not passed then
102    if_nonempty(res.stdout, 'STDOUT')
103    if_nonempty(res.stderr, 'STDERR')
104    -- if_exists(starttime, "starttime")
105    if_exists(errlog, 'test.log')
106  end
107  return passed
108end
109
110local function all_tests()
111  local nested_tests = {}
112  local alot = io.open('test_alot.vim')
113  local sourcepat = '^source (test_.*%.vim)$'
114  for line in alot:lines() do
115    local m = string.match(line, sourcepat)
116    if m then
117      nested_tests[m] = true
118    end
119  end
120  alot:close()
121
122  local files = {}
123  for name, type in vim.fs.dir('.') do
124    match = string.match(name, '^test_.*%.vim$')
125    if match then
126      if nested_tests[match] then
127        nested_tests[match] = nil
128      elseif match == 'test_largefile.vim' then
129        -- ignored: uses too much resources to run on CI
130      else
131        table.insert(files, match)
132      end
133    end
134  end
135  return files
136end
137
138local files
139if #arg > 0 then
140  files = {}
141  for _, a in ipairs(arg) do
142    table.insert(files, string.match(a, '[^/]*$'))
143  end
144else
145  files = all_tests()
146end
147
148local count = #files
149local failures = {}
150for i, fil in ipairs(files) do
151  print('run: ', fil)
152  if not run_test(fil) then
153    table.insert(failures, fil)
154  end
155end
156
157if #failures > 0 then
158  print('failed:', vim.inspect(failures))
159  vim.cmd 'cquit'
160end