Avoid reinstalling parser in tests

Uses a custom function in the faker user config.  This function can be
called via RPC in the process under test.
This commit is contained in:
HiPhish 2024-01-15 23:47:02 +01:00
parent bbe08e16ae
commit 80ee86a2a8
3 changed files with 18 additions and 3 deletions

3
.gitignore vendored
View file

@ -1,6 +1,7 @@
# Tag file created by Vim
doc/tags
test/xdg/config/nvim/
# Fake user directory structures
test/xdg/local/share/nvim/
test/xdg/local/state/nvim/
!test/xdg/local/share/nvim/site/pack/testing/start/rainbow-delimiters

View file

@ -71,7 +71,7 @@ describe('User settings are respected', function()
end)
it('Uses the strategy returned by the thunk', function()
request('nvim_command', 'TSInstallSync! lua')
request(exec_lua, 'TSEnsure(...)', {'lua'})
request(buf_set_lines, 0, 0, -1, true, {'print "Hello world"', '-- vim:ft=lua'})
request('nvim_command', 'filetype detect')
local attachments = request(exec_lua, 'return the_strategy.attachments[1]', {})
@ -79,7 +79,7 @@ describe('User settings are respected', function()
end)
it('Does nothing if the thunk returns nil', function()
request('nvim_command', 'TSInstallSync! vim')
request(exec_lua, 'TSEnsure(...)', {'vim'})
request(buf_set_lines, 0, 0, -1, true, {'echo "Hello world"', '" vim:ft=vim'})
request('nvim_command', 'filetype detect')
local attachments = request(exec_lua, 'return the_strategy.attachments[1]', {})

View file

@ -0,0 +1,14 @@
local get_runtime_file = vim.api.nvim_get_runtime_file
local parser_pattern = 'parser/%s.*'
---Wrapper around the `:TSinstall` command which will only install a parser if
---it is not installed yet
---@param lang string Language to install
function TSEnsure(lang, ...)
for _, l in ipairs({lang, ...}) do
local parsers = get_runtime_file(parser_pattern:format(l), true)
if #parsers == 0 then
vim.cmd {cmd = 'TSInstallSync', args = {l}}
end
end
end