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
'';
# Stub plugin built with mkDerivation
stubDrvPlugin = pkgs.stdenvNoCC.mkDerivation {
name = "stub_drv_plugin";
src = pkgs.emptyDirectory;
buildPhase = ''
mkdir -p "$out/lua/$name"
echo "return '$name'" >"$out/lua/$name/init.lua"
mkdir $out/plugin
echo "vim.g['$name'] = true" >"$out/plugin/$name.lua"
echo "let g:$name = 1" >"$out/plugin/$name.vim"
'';
dependencies = [ stubBuildCommandPlugin ];
};
# Stub plugin built with buildCommand with python dependency
stubBuildCommandPlugin = pkgs.writeTextFile {
name = "stub_build_command_plugin";
text = ''
return "stub_build_command_plugin"
'';
destination = "/lua/stub_build_command_plugin/init.lua";
derivationArgs = {
dependencies = [ stubDependentPlugin ];
passthru.python3Dependencies = ps: [ ps.pyyaml ];
};
};
# Dependent stub plugin
stubDependentPlugin = pkgs.writeTextFile {
name = "stub_dependent_plugin";
text = ''
return "stub_dependent_plugin"
'';
destination = "/lua/stub_dependent_plugin/init.lua";
};
# Stub plugin with an invalid lua file
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 order to know where lua library come from
# split plugins and libs to avoid lua libraries intersection.
shortPluginList = with pluginStubs; [
plugin1
pluginWithDep4
];
shortPluginNames = [
"plugin1"
"plugin_with_dep4"
"plugin4"
"plugin3"
];
shortPluginLuaNames = [
"lib1"
"lib2"
# "lib3" # is in both
];
shortLuaList = with pluginStubs; [
lib4
libWithDep5
];
shortLuaNames = [
"lib4"
"lib_with_dep5"
"lib5"
# "lib3" # is in both
];
shortLuaAllNames = shortPluginLuaNames ++ shortLuaNames ++ [ "lib3" ];
shortChecks =
pluginStubs.pluginChecksFor shortPluginNames
+ pluginStubs.libChecksFor shortLuaAllNames
+ pluginStubs.pythonChecksFor [
"yaml"
"numpy"
];
in
{
default =
@ -143,9 +138,9 @@ in
};
};
extraPlugins = [ stubDrvPlugin ];
extraPlugins = shortPluginList;
extraLuaPackages = ps: [ ps.say ];
extraLuaPackages = _: shortLuaList;
extraConfigLua = ''
-- The test will search for the next string in nixvim-print-init's output: VALIDATING_STRING.
@ -160,6 +155,8 @@ in
''
${isByteCompiledFun}
${shortChecks}
-- vimrc is byte compiled
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")
@ -183,9 +180,6 @@ in
{ "plugin/file.lua", true, "file_lua" },
-- other 'files'
{ "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
local file, expected_byte_compiled, varname = unpack(test)
@ -198,8 +192,16 @@ in
-- Nvim runtime isn't byte compiled by default
test_rtp_file("lua/vim/lsp.lua", false)
-- Lua library isn't byte compiled by default
test_lualib_file(require("say").set, false)
-- Plugins aren't byte-compiled by default
${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;
extraPlugins = [ stubDrvPlugin ];
extraPlugins = shortPluginList;
extraLuaPackages = ps: [ ps.say ];
extraLuaPackages = _: shortLuaList;
extraConfigLua = ''
${isByteCompiledFun}
@ -226,12 +228,13 @@ in
test_rtp_file("plugin/test1.lua", false)
-- files
test_rtp_file("plugin/test2.lua", false)
-- Plugins
test_rtp_file("lua/stub_drv_plugin/init.lua", false)
-- Neovim runtime
test_rtp_file("lua/vim/lsp.lua", false)
-- Lua library
test_lualib_file(require("say").set, false)
-- plugins
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;
};
extraPlugins = [
stubBuildCommandPlugin
];
extraPlugins = shortPluginList;
extraLuaPackages = _: shortLuaList;
extraConfigLuaPost = ''
${isByteCompiledFun}
${shortChecks}
-- vim namespace is working
vim.opt.tabstop = 2
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/lsp/buf.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
pkgs.lib.genAttrs
lib.genAttrs
[
"plugins"
"plugins-combined"
@ -400,44 +401,38 @@ in
plugins = true;
};
combinePlugins.enable = pkgs.lib.hasSuffix "combined" name;
combinePlugins.enable = lib.hasSuffix "combined" name;
};
extraPlugins = [
# Depends on stubBuildCommandPlugin, which itself depends on stubDependentPlugin
stubDrvPlugin
# Plugin with invalid file
stubInvalidFilePlugin
extraPlugins = pluginStubs.pluginPack ++ [
# A plugin with invalid lua file
(pluginStubs.mkPlugin "invalid" {
postInstall = ''
mkdir $out/ftplugin
echo "if true then" >$out/ftplugin/invalid.lua
'';
})
];
extraConfigLuaPost = ''
${pluginStubs.pluginChecks}
${isByteCompiledFun}
local tests = {
"stub_drv_plugin",
"stub_build_command_plugin",
"stub_dependent_plugin",
"stub_invalid_file_plugin",
}
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
-- Plugins are byte-compiled
${lib.concatMapStrings (name: ''
test_rtp_file("lua/${name}/init.lua", true)
test_rtp_file("plugin/${name}.lua", true)
'') pluginStubs.pluginNames}
-- stubDrvPlugin's other files
test_rtp_file("plugin/stub_drv_plugin.lua", true)
-- non-lua file is valid
vim.cmd.runtime("plugin/stub_drv_plugin.vim")
assert_g_var("stub_drv_plugin", "plugin/stub_drv_plugin.vim")
-- Lua modules aren't byte-compiled
${lib.concatMapStrings (name: ''
test_lualib_file(require("${name}").name, false)
'') pluginStubs.libNames}
-- Python modules are importable
vim.cmd.py3("import yaml")
-- stubInvalidFilePlugin's invalid file exists, but isn't byte compiled
-- A plugin with invalid file
${pluginStubs.pluginChecksFor [ "invalid" ]}
-- invalid file exists, but isn't byte compiled
test_rtp_file("ftplugin/invalid.lua", false)
'';
})

View file

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