tests/modules/performance/byte-compile-lua: use shared stub plugins

This commit finalizes using shared utils stub plugins for
performance.byteCompileLua tests.
To re-use more code from utils module, 'pluginChecksFor', 'libChecksFor'
and 'pythonChecksFor' functions were introduced. These functions
generate a check code for the given plugins/libs names.
This commit is contained in:
Stanislav Asunkin 2025-05-11 16:15:35 +03:00
parent 75f2c1b1f1
commit a49b270861
2 changed files with 162 additions and 155 deletions

View file

@ -50,46 +50,41 @@ let
end end
''; '';
# Stub plugin built with mkDerivation # In order to know where lua library come from
stubDrvPlugin = pkgs.stdenvNoCC.mkDerivation { # split plugins and libs to avoid lua libraries intersection.
name = "stub_drv_plugin"; shortPluginList = with pluginStubs; [
src = pkgs.emptyDirectory; plugin1
buildPhase = '' pluginWithDep4
mkdir -p "$out/lua/$name" ];
echo "return '$name'" >"$out/lua/$name/init.lua" shortPluginNames = [
mkdir $out/plugin "plugin1"
echo "vim.g['$name'] = true" >"$out/plugin/$name.lua" "plugin_with_dep4"
echo "let g:$name = 1" >"$out/plugin/$name.vim" "plugin4"
''; "plugin3"
dependencies = [ stubBuildCommandPlugin ]; ];
}; shortPluginLuaNames = [
# Stub plugin built with buildCommand with python dependency "lib1"
stubBuildCommandPlugin = pkgs.writeTextFile { "lib2"
name = "stub_build_command_plugin"; # "lib3" # is in both
text = '' ];
return "stub_build_command_plugin" shortLuaList = with pluginStubs; [
''; lib4
destination = "/lua/stub_build_command_plugin/init.lua"; libWithDep5
derivationArgs = { ];
dependencies = [ stubDependentPlugin ]; shortLuaNames = [
passthru.python3Dependencies = ps: [ ps.pyyaml ]; "lib4"
}; "lib_with_dep5"
}; "lib5"
# Dependent stub plugin # "lib3" # is in both
stubDependentPlugin = pkgs.writeTextFile { ];
name = "stub_dependent_plugin"; shortLuaAllNames = shortPluginLuaNames ++ shortLuaNames ++ [ "lib3" ];
text = '' shortChecks =
return "stub_dependent_plugin" pluginStubs.pluginChecksFor shortPluginNames
''; + pluginStubs.libChecksFor shortLuaAllNames
destination = "/lua/stub_dependent_plugin/init.lua"; + pluginStubs.pythonChecksFor [
}; "yaml"
# Stub plugin with an invalid lua file "numpy"
stubInvalidFilePlugin = pkgs.runCommand "stub_invalid_file_plugin" { } '' ];
mkdir -p "$out/lua/$name"
echo "return '$name'" >"$out/lua/$name/init.lua"
mkdir "$out/ftplugin"
echo "if true then" >"$out/ftplugin/invalid.lua"
'';
in in
{ {
default = default =
@ -143,9 +138,9 @@ in
}; };
}; };
extraPlugins = [ stubDrvPlugin ]; extraPlugins = shortPluginList;
extraLuaPackages = ps: [ ps.say ]; extraLuaPackages = _: shortLuaList;
extraConfigLua = '' extraConfigLua = ''
-- The test will search for the next string in nixvim-print-init's output: VALIDATING_STRING. -- The test will search for the next string in nixvim-print-init's output: VALIDATING_STRING.
@ -160,6 +155,8 @@ in
'' ''
${isByteCompiledFun} ${isByteCompiledFun}
${shortChecks}
-- vimrc is byte compiled -- vimrc is byte compiled
local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name
assert(is_byte_compiled(init), "MYVIMRC is expected to be byte compiled, but it's not") assert(is_byte_compiled(init), "MYVIMRC is expected to be byte compiled, but it's not")
@ -183,9 +180,6 @@ in
{ "plugin/file.lua", true, "file_lua" }, { "plugin/file.lua", true, "file_lua" },
-- other 'files' -- other 'files'
{ "plugin/file.vim", false, "file_vimscript" }, { "plugin/file.vim", false, "file_vimscript" },
-- Plugins aren't byte compiled by default
{ "plugin/stub_drv_plugin.lua", false, "stub_drv_plugin" }
} }
for _, test in ipairs(runtime_lua_files) do for _, test in ipairs(runtime_lua_files) do
local file, expected_byte_compiled, varname = unpack(test) local file, expected_byte_compiled, varname = unpack(test)
@ -198,8 +192,16 @@ in
-- Nvim runtime isn't byte compiled by default -- Nvim runtime isn't byte compiled by default
test_rtp_file("lua/vim/lsp.lua", false) test_rtp_file("lua/vim/lsp.lua", false)
-- Lua library isn't byte compiled by default -- Plugins aren't byte-compiled by default
test_lualib_file(require("say").set, false) ${lib.concatMapStrings (name: ''
test_rtp_file("lua/${name}/init.lua", false)
test_rtp_file("plugin/${name}.lua", false)
'') shortPluginNames}
-- Lua library isn't byte-compiled by default
${lib.concatMapStrings (name: ''
test_lualib_file(require("${name}").name, false)
'') shortLuaAllNames}
''; '';
}; };
@ -211,9 +213,9 @@ in
files."plugin/test2.lua".opts.tabstop = 2; files."plugin/test2.lua".opts.tabstop = 2;
extraPlugins = [ stubDrvPlugin ]; extraPlugins = shortPluginList;
extraLuaPackages = ps: [ ps.say ]; extraLuaPackages = _: shortLuaList;
extraConfigLua = '' extraConfigLua = ''
${isByteCompiledFun} ${isByteCompiledFun}
@ -226,12 +228,13 @@ in
test_rtp_file("plugin/test1.lua", false) test_rtp_file("plugin/test1.lua", false)
-- files -- files
test_rtp_file("plugin/test2.lua", false) test_rtp_file("plugin/test2.lua", false)
-- Plugins
test_rtp_file("lua/stub_drv_plugin/init.lua", false)
-- Neovim runtime -- Neovim runtime
test_rtp_file("lua/vim/lsp.lua", false) test_rtp_file("lua/vim/lsp.lua", false)
-- Lua library -- plugins
test_lualib_file(require("say").set, false) test_rtp_file("lua/${builtins.elemAt shortPluginNames 0}/init.lua", false)
-- lua library
test_lualib_file(require("${builtins.elemAt shortLuaNames 0}").name, false)
test_lualib_file(require("${builtins.elemAt shortPluginLuaNames 0}").name, false)
''; '';
}; };
@ -276,13 +279,14 @@ in
nvimRuntime = true; nvimRuntime = true;
}; };
extraPlugins = [ extraPlugins = shortPluginList;
stubBuildCommandPlugin extraLuaPackages = _: shortLuaList;
];
extraConfigLuaPost = '' extraConfigLuaPost = ''
${isByteCompiledFun} ${isByteCompiledFun}
${shortChecks}
-- vim namespace is working -- vim namespace is working
vim.opt.tabstop = 2 vim.opt.tabstop = 2
vim.api.nvim_get_runtime_file("init.lua", false) vim.api.nvim_get_runtime_file("init.lua", false)
@ -295,9 +299,6 @@ in
test_rtp_file("lua/vim/treesitter/query.lua", true) test_rtp_file("lua/vim/treesitter/query.lua", true)
test_rtp_file("lua/vim/lsp/buf.lua", true) test_rtp_file("lua/vim/lsp/buf.lua", true)
test_rtp_file("plugin/editorconfig.lua", true) test_rtp_file("plugin/editorconfig.lua", true)
-- Python3 packages are importable
vim.cmd.py3("import yaml")
''; '';
}; };
@ -388,7 +389,7 @@ in
} }
// //
# Two equal tests, one with combinePlugins.enable = true # Two equal tests, one with combinePlugins.enable = true
pkgs.lib.genAttrs lib.genAttrs
[ [
"plugins" "plugins"
"plugins-combined" "plugins-combined"
@ -400,44 +401,38 @@ in
plugins = true; plugins = true;
}; };
combinePlugins.enable = pkgs.lib.hasSuffix "combined" name; combinePlugins.enable = lib.hasSuffix "combined" name;
}; };
extraPlugins = [ extraPlugins = pluginStubs.pluginPack ++ [
# Depends on stubBuildCommandPlugin, which itself depends on stubDependentPlugin # A plugin with invalid lua file
stubDrvPlugin (pluginStubs.mkPlugin "invalid" {
# Plugin with invalid file postInstall = ''
stubInvalidFilePlugin mkdir $out/ftplugin
echo "if true then" >$out/ftplugin/invalid.lua
'';
})
]; ];
extraConfigLuaPost = '' extraConfigLuaPost = ''
${pluginStubs.pluginChecks}
${isByteCompiledFun} ${isByteCompiledFun}
local tests = { -- Plugins are byte-compiled
"stub_drv_plugin", ${lib.concatMapStrings (name: ''
"stub_build_command_plugin", test_rtp_file("lua/${name}/init.lua", true)
"stub_dependent_plugin", test_rtp_file("plugin/${name}.lua", true)
"stub_invalid_file_plugin", '') pluginStubs.pluginNames}
}
for _, test in ipairs(tests) do
-- Plugin is loadable
local val = require(test)
-- Valid lua code
assert(val == test, string.format([[expected require("%s") = "%s", got "%s"]], test, test, val))
-- Is byte compiled
test_rtp_file("lua/" .. test .. "/init.lua", true)
end
-- stubDrvPlugin's other files -- Lua modules aren't byte-compiled
test_rtp_file("plugin/stub_drv_plugin.lua", true) ${lib.concatMapStrings (name: ''
-- non-lua file is valid test_lualib_file(require("${name}").name, false)
vim.cmd.runtime("plugin/stub_drv_plugin.vim") '') pluginStubs.libNames}
assert_g_var("stub_drv_plugin", "plugin/stub_drv_plugin.vim")
-- Python modules are importable -- A plugin with invalid file
vim.cmd.py3("import yaml") ${pluginStubs.pluginChecksFor [ "invalid" ]}
-- invalid file exists, but isn't byte compiled
-- stubInvalidFilePlugin's invalid file exists, but isn't byte compiled
test_rtp_file("ftplugin/invalid.lua", false) test_rtp_file("ftplugin/invalid.lua", false)
''; '';
}) })

View file

@ -236,6 +236,11 @@ let
"lib_with_dep5" "lib_with_dep5"
"lib5" "lib5"
]; ];
pythonNames = [
"yaml"
"requests"
"numpy"
];
pluginNames = [ pluginNames = [
"plugin1" "plugin1"
"plugin2" "plugin2"
@ -246,9 +251,8 @@ let
"plugin_with_dep5" "plugin_with_dep5"
"plugin5" "plugin5"
]; ];
# Lua code to validate that everything works # Lua code to validate lua libraries of the given names
libChecks = lib.concatMapStringsSep "\n" ( libChecksFor = lib.concatMapStringsSep "\n" (name:
name:
# lua # lua
'' ''
-- Lua dependencies require check -- Lua dependencies require check
@ -262,10 +266,21 @@ let
) )
) )
end end
'') libNames; '');
pluginChecks = # Lua code to validate all lua libraries
lib.concatMapStringsSep "\n" ( libChecks = libChecksFor libNames;
name: # Lua code to validate python dependencies of the given names
pythonChecksFor = lib.concatMapStringsSep "\n" (name:
# lua
''
-- Python dependencies checks
vim.cmd.py3('import ${name}')
'');
# Lua code to validate all python dependencies
pythonChecks = pythonChecksFor pythonNames;
# Lua code to validate plugins of the given names
# Python and lua dependencies aren't checked
pluginChecksFor = lib.concatMapStringsSep "\n" (name:
# lua # lua
'' ''
-- Require check -- Require check
@ -305,17 +320,9 @@ let
assert(doc[1], [["doc/${name}.txt" not found in runtime]]) assert(doc[1], [["doc/${name}.txt" not found in runtime]])
assert(vim.fn.getcompletion("${name}", "help")[1], [[no help tags for "${name}"]]) assert(vim.fn.getcompletion("${name}", "help")[1], [[no help tags for "${name}"]])
end end
'') pluginNames '');
# lua # Lua code to validate all plugins along with python and lua dependencies
+ '' pluginChecks = pluginChecksFor pluginNames + libChecks + pythonChecks;
-- Python dependencies check
vim.cmd.py3("import yaml")
vim.cmd.py3("import requests")
vim.cmd.py3("import numpy")
''
+ libChecks;
in in
{ {
inherit inherit
@ -346,10 +353,15 @@ in
libWithDeepDep libWithDeepDep
# checks # checks
pluginChecksFor
pluginChecks pluginChecks
pluginNames pluginNames
libChecksFor
libChecks libChecks
libNames libNames
pythonChecksFor
pythonChecks
pythonNames
; ;
# a pack of top-level plugins # a pack of top-level plugins