-- Black-box tests for the local Lua harness itself. These spawn a separate
-- Nvim process instead of driving an embedded instance via testnvim.
local t = require('test.testutil')
local describe, it, pending, finally = t.describe, t.it, t.pending, t.finally
local uv = vim.uv
local eq = t.eq
local matches = t.matches
local not_matches = t.not_matches
local root = t.paths.test_source_path
local build_dir = t.paths.test_build_dir
local nvim_prog = build_dir .. '/bin/nvim'
local runner = root .. '/test/runner.lua'
---@param path string
local function mkdir(path)
assert(t.mkdir(path), ('failed to create directory: %s'):format(path))
end
-- Prepended to each *_spec.lua fixture so it can use the test framework.
local spec_preamble = "local h = require('test.harness'); local eq = require('test.assert').eq; "
.. 'local describe, it, before_each, after_each, setup, teardown, pending, finally = '
.. 'h.describe, h.it, h.before_each, h.after_each, h.setup, h.teardown, h.pending, h.finally; '
---@param suite_files table<string, string>
---@return string
local function write_suite(suite_files)
local dir = t.tmpname(false)
mkdir(dir)
for name, contents in pairs(suite_files) do
if name:match('_spec%.lua$') then
contents = spec_preamble .. contents
end
t.write_file(dir .. '/' .. name, contents)
end
return dir
end
---@param overrides table<string, string?>
---@return string[]
local function make_env(overrides)
local env = uv.os_environ()
for k, v in pairs(overrides) do
env[k] = v
end
local items = {}
for k, v in pairs(env) do
if v ~= nil then
items[#items + 1] = k .. '=' .. v
end
end
return items
end
---@param suite_dir string
---@param extra_args? string[]
---@return integer, string
local function run_harness(suite_dir, extra_args)
local env_root = t.tmpname(false)
mkdir(env_root)
mkdir(env_root .. '/config')
mkdir(env_root .. '/share')
mkdir(env_root .. '/state')
mkdir(env_root .. '/tmp')
local stdout = assert(uv.new_pipe(false))
local stderr = assert(uv.new_pipe(false))
local out = {}
local err = {}
local exit_code --- @type integer?
local args = {
'-l',
runner,
'-v',
'--lpath=' .. build_dir .. '/?.lua',
'--lpath=' .. root .. '/src/?.lua',
'--lpath=' .. root .. '/runtime/lua/?.lua',
'--lpath=' .. suite_dir .. '/?.lua',
'--lpath=?.lua',
}
for _, arg in ipairs(extra_args or {}) do
args[#args + 1] = arg
end
args[#args + 1] = suite_dir
local handle = assert(uv<
|