startup: handle autoload and lua packages during startup

¡NO HAY BANDA!
This commit is contained in:
Björn Linse
2020-10-19 20:05:54 +02:00
parent 6224ec3d4a
commit c60c7375f5
15 changed files with 92 additions and 274 deletions

View File

@@ -7,6 +7,7 @@ local ok = helpers.ok
local eq = helpers.eq
local matches = helpers.matches
local eval = helpers.eval
local exec_lua = helpers.exec_lua
local feed = helpers.feed
local funcs = helpers.funcs
local mkdir = helpers.mkdir
@@ -305,6 +306,27 @@ describe('startup', function()
'+q' })
eq('[\'+q\'] 1', out)
end)
local function pack_clear(cmd)
clear('--cmd', 'set packpath=test/functional/fixtures', '--cmd', cmd)
end
it("handles &packpath during startup", function()
pack_clear [[ let g:x = bar#test() ]]
eq(-3, eval 'g:x')
pack_clear [[ lua _G.y = require'bar'.doit() ]]
eq(9003, exec_lua [[ return _G.y ]])
end)
it("handles :packadd during startup", function()
pack_clear [[ packadd! bonus | let g:x = bonus#secret() ]]
eq('halloj', eval 'g:x')
pack_clear [[ packadd! bonus | lua _G.y = require'bonus'.launch() ]]
eq('CPE 1704 TKS', exec_lua [[ return _G.y ]])
end)
end)
describe('sysinit', function()

View File

@@ -1,74 +0,0 @@
local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear
local eq = helpers.eq
local exec_lua = helpers.exec_lua
describe('packadd', function()
before_each(function()
-- Primarily taken from test/functional/legacy/packadd_spec.lua
clear()
exec_lua [[
TopDirectory = vim.fn.expand(vim.fn.getcwd() .. '/Xdir_lua')
PlugDirectory = TopDirectory .. '/pack/mine/opt/mytest'
vim.o.packpath = TopDirectory
function FindPathsContainingDir(dir)
return vim.fn.filter(
vim.split(package.path, ';'),
function(k, v)
return string.find(v, 'mytest') ~= nil
end
)
end
]]
end)
after_each(function()
exec_lua [[
vim.fn.delete(TopDirectory, 'rf')
]]
end)
it('should immediately update package.path in lua', function()
local count_of_paths = exec_lua [[
vim.fn.mkdir(PlugDirectory .. '/lua/', 'p')
local num_paths_before = #FindPathsContainingDir('mytest')
vim.cmd("packadd mytest")
local num_paths_after = #FindPathsContainingDir('mytest')
return { num_paths_before, num_paths_after }
]]
eq({0, 2}, count_of_paths)
end)
it('should immediately update package.path in lua even if lua directory does not exist', function()
local count_of_paths = exec_lua [[
vim.fn.mkdir(PlugDirectory .. '/plugin/', 'p')
local num_paths_before = #FindPathsContainingDir('mytest')
vim.cmd("packadd mytest")
local num_paths_after = #FindPathsContainingDir('mytest')
return { num_paths_before, num_paths_after }
]]
eq({0, 2}, count_of_paths)
end)
it('should error for invalid paths', function()
local count_of_paths = exec_lua [[
local ok, err = pcall(vim.cmd, "packadd asdf")
return ok
]]
eq(false, count_of_paths)
end)
end)

View File

@@ -0,0 +1,3 @@
func bonus#secret()
return "halloj"
endfunc

View File

@@ -0,0 +1 @@
return {launch=function() return "CPE 1704 TKS" end}

View File

@@ -0,0 +1,3 @@
func bar#test()
return -3
endfunc

View File

@@ -0,0 +1 @@
return {doit=function() return 9003 end}

View File

@@ -285,119 +285,6 @@ describe('debug.debug', function()
end)
end)
describe('package.path/package.cpath', function()
local sl = alter_slashes
local function get_new_paths(sufs, runtimepaths)
runtimepaths = runtimepaths or meths.list_runtime_paths()
local new_paths = {}
local sep = package.config:sub(1, 1)
for _, v in ipairs(runtimepaths) do
for _, suf in ipairs(sufs) do
new_paths[#new_paths + 1] = v .. sep .. 'lua' .. suf
end
end
return new_paths
end
local function eval_lua(expr, ...)
return meths.exec_lua('return '..expr, {...})
end
local function set_path(which, value)
return exec_lua('package[select(1, ...)] = select(2, ...)', which, value)
end
it('contains directories from &runtimepath on first invocation', function()
local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'})
local new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str))
local new_cpaths = get_new_paths(iswin() and {'\\?.dll'} or {'/?.so'})
local new_cpaths_str = table.concat(new_cpaths, ';')
eq(new_cpaths_str, eval_lua('package.cpath'):sub(1, #new_cpaths_str))
end)
it('puts directories from &runtimepath always at the start', function()
meths.set_option('runtimepath', 'a,b')
local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {'a', 'b'})
local new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str))
set_path('path', sl'foo/?.lua;foo/?/init.lua;' .. new_paths_str)
neq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str))
command('set runtimepath+=c')
new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {'a', 'b', 'c'})
new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str))
end)
it('understands uncommon suffixes', function()
set_path('cpath', './?/foo/bar/baz/x.nlua')
meths.set_option('runtimepath', 'a')
local new_paths = get_new_paths({'/?/foo/bar/baz/x.nlua'}, {'a'})
local new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str, eval_lua('package.cpath'):sub(1, #new_paths_str))
set_path('cpath', './yyy?zzz/x')
meths.set_option('runtimepath', 'b')
new_paths = get_new_paths({'/yyy?zzz/x'}, {'b'})
new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str, eval_lua('package.cpath'):sub(1, #new_paths_str))
set_path('cpath', './yyy?zzz/123?ghi/x')
meths.set_option('runtimepath', 'b')
new_paths = get_new_paths({'/yyy?zzz/123?ghi/x'}, {'b'})
new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str, eval_lua('package.cpath'):sub(1, #new_paths_str))
end)
it('preserves empty items', function()
local many_empty_path = ';;;;;;'
local many_empty_cpath = ';;;;;;./?.luaso'
set_path('path', many_empty_path)
set_path('cpath', many_empty_cpath)
meths.set_option('runtimepath', 'a')
local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {'a'})
local new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str .. ';' .. many_empty_path, eval_lua('package.path'))
local new_cpaths = get_new_paths({'/?.luaso'}, {'a'})
local new_cpaths_str = table.concat(new_cpaths, ';')
eq(new_cpaths_str .. ';' .. many_empty_cpath, eval_lua('package.cpath'))
end)
it('preserves empty value', function()
set_path('path', '')
meths.set_option('runtimepath', 'a')
local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {'a'})
local new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str .. ';', eval_lua('package.path'))
end)
it('purges out all additions if runtimepath is set to empty', function()
local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'})
local new_paths_str = table.concat(new_paths, ';')
local path = eval_lua('package.path')
eq(new_paths_str, path:sub(1, #new_paths_str))
local new_cpaths = get_new_paths(iswin() and {'\\?.dll'} or {'/?.so'})
local new_cpaths_str = table.concat(new_cpaths, ';')
local cpath = eval_lua('package.cpath')
eq(new_cpaths_str, cpath:sub(1, #new_cpaths_str))
meths.set_option('runtimepath', '')
eq(path:sub(#new_paths_str + 2, -1), eval_lua('package.path'))
eq(cpath:sub(#new_cpaths_str + 2, -1), eval_lua('package.cpath'))
end)
it('works with paths with escaped commas', function()
meths.set_option('runtimepath', '\\,')
local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {','})
local new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str))
end)
it('ignores paths with semicolons', function()
meths.set_option('runtimepath', 'foo;bar,\\,')
local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {','})
local new_paths_str = table.concat(new_paths, ';')
eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str))
end)
end)
describe('os.getenv', function()
it('returns nothing for undefined env var', function()
eq(NIL, funcs.luaeval('os.getenv("XTEST_1")'))