From eb6fb1d7d34c0a61ddb2c661b4af4d80e260fdf9 Mon Sep 17 00:00:00 2001 From: Frans de Jonge Date: Sun, 8 Mar 2026 22:31:56 +0100 Subject: [PATCH] PluginLoader: use directory name for enabled/disabled, deprecate metadata name Presumably fairly similar to given the similar goal. --- frontend/pluginloader.lua | 15 +-- spec/unit/pluginloader_spec.lua | 162 ++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 spec/unit/pluginloader_spec.lua diff --git a/frontend/pluginloader.lua b/frontend/pluginloader.lua index a004e6a46..2b7a64fd2 100644 --- a/frontend/pluginloader.lua +++ b/frontend/pluginloader.lua @@ -14,7 +14,6 @@ Plugins are controlled by the following settings. local dbg = require("dbg") local lfs = require("libs/libkoreader-lfs") local logger = require("logger") -local util = require("util") local _ = require("gettext") local DEFAULT_PLUGIN_PATH = "plugins" @@ -166,19 +165,19 @@ function PluginLoader:_discover() if mode == "directory" and entry:sub(-9) == ".koplugin" then local mainfile = plugin_root.."/main.lua" local metafile = plugin_root.."/_meta.lua" + local plugin_name = entry:sub(1, -10) local disabled = false - if plugins_disabled and plugins_disabled[entry:sub(1, -10)] then + if plugins_disabled and plugins_disabled[plugin_name] then mainfile = metafile disabled = true end - local __, name = util.splitFilePathName(plugin_root) table.insert(discovered, { ["main"] = mainfile, ["meta"] = metafile, ["path"] = plugin_root, ["disabled"] = disabled, - ["name"] = name, + ["name"] = plugin_name, }) end end @@ -204,14 +203,18 @@ function PluginLoader:_load(t) logger.warn("Error when loading", mainfile, plugin_module) elseif type(plugin_module.disabled) ~= "boolean" or not plugin_module.disabled then plugin_module.path = plugin_root - plugin_module.name = plugin_module.name or plugin_root:match("/(.-)%.koplugin") + plugin_module.name = v.name if disabled then table.insert(self.disabled_plugins, plugin_module) else local ok_meta, plugin_metamodule = pcall(dofile, metafile) if ok_meta and plugin_metamodule then for k, module in pairs(plugin_metamodule) do - plugin_module[k] = module + if k ~= "name" then + plugin_module[k] = module + else + logger.warn("PluginLoader:", plugin_module.name, "name in _meta.lua, is deprecated and will be ignored.") + end end end sandboxPluginEventHandlers(plugin_module) diff --git a/spec/unit/pluginloader_spec.lua b/spec/unit/pluginloader_spec.lua new file mode 100644 index 000000000..ddbc42be1 --- /dev/null +++ b/spec/unit/pluginloader_spec.lua @@ -0,0 +1,162 @@ +describe("PluginLoader module", function() + local PluginLoader, UIManager + local original_lfs + + setup(function() + require("commonrequire") + UIManager = require("ui/uimanager") + original_lfs = require("libs/libkoreader-lfs") + end) + + before_each(function() + local lfs_mock = { + dir_results = {}, + attributes_results = {}, + } + + function lfs_mock.dir(path) + local entries = lfs_mock.dir_results[path] or {} + local index = 0 + return function() + index = index + 1 + return entries[index] + end + end + + function lfs_mock.attributes(path, key) + local attrs = lfs_mock.attributes_results[path] + if not attrs then + return nil + end + if key then + return attrs[key] + end + return attrs + end + + G_reader_settings:reset({}) + G_reader_settings:saveSetting("extra_plugin_paths", {}) + + package.replace("libs/libkoreader-lfs", lfs_mock) + PluginLoader = package.reload("pluginloader") + PluginLoader.show_info = true + PluginLoader.enabled_plugins = {} + PluginLoader.disabled_plugins = {} + PluginLoader.loaded_plugins = {} + PluginLoader.all_plugins = nil + end) + + after_each(function() + package.replace("libs/libkoreader-lfs", original_lfs) + package.unload("pluginloader") + end) + + it("discovers plugins by directory id and matches disabled settings against that id", function() + local lfs_mock = require("libs/libkoreader-lfs") + lfs_mock.dir_results.plugins = { + ".", + "..", + "pluginone.koplugin", + "plugintwo.koplugin", + } + lfs_mock.attributes_results["plugins/pluginone.koplugin"] = { mode = "directory" } + lfs_mock.attributes_results["plugins/plugintwo.koplugin"] = { mode = "directory" } + + G_reader_settings:saveSetting("plugins_disabled", { + plugintwo = true, + }) + + local discovered = PluginLoader:_discover() + + assert.equals("pluginone", discovered[1].name) + assert.is_false(discovered[1].disabled) + assert.equals("plugintwo", discovered[2].name) + assert.is_true(discovered[2].disabled) + end) + + it("normalizes the internal id for enabled plugins", function() + stub(_G, "dofile", function(path) + if path == "plugins/test.koplugin/main.lua" then + return { + name = "wrong_main_name", + description = "from main", + } + end + if path == "plugins/test.koplugin/_meta.lua" then + return { + name = "wrong_meta_name", + description = "from meta", + } + end + error("unexpected path: " .. path) + end) + + PluginLoader:_load({ { + main = "plugins/test.koplugin/main.lua", + meta = "plugins/test.koplugin/_meta.lua", + path = "plugins/test.koplugin", + disabled = false, + name = "test", + } }) + + local plugin = PluginLoader.enabled_plugins[1] + assert.equals("test", plugin.name) + assert.equals("from meta", plugin.description) + + _G.dofile:revert() + end) + + it("normalizes the internal id for disabled plugins", function() + stub(_G, "dofile", function(path) + if path == "plugins/test.koplugin/_meta.lua" then + return { + name = "wrong_meta_name", + description = "from meta", + } + end + error("unexpected path: " .. path) + end) + + PluginLoader:_load({ { + main = "plugins/test.koplugin/_meta.lua", + meta = "plugins/test.koplugin/_meta.lua", + path = "plugins/test.koplugin", + disabled = true, + name = "test", + } }) + + local plugin = PluginLoader.disabled_plugins[1] + assert.equals("test", plugin.name) + assert.equals("from meta", plugin.description) + + _G.dofile:revert() + end) + + it("stores plugin manager toggles by internal plugin id", function() + stub(PluginLoader, "loadPlugins", function() + return { + { + name = "test", + fullname = "Pretty Test", + description = "from meta", + path = "plugins/test.koplugin", + }, + }, {} + end) + stub(PluginLoader, "getPluginInstance", function() + return nil + end) + stub(UIManager, "askForRestart") + + local plugin_items = PluginLoader:genPluginManagerSubItem() + plugin_items[1].callback() + + local plugins_disabled = G_reader_settings:readSetting("plugins_disabled") + assert.is_true(plugins_disabled.test) + assert.is_nil(plugins_disabled["Pretty Test"]) + + UIManager.askForRestart:revert() + PluginLoader.getPluginInstance:revert() + PluginLoader.loadPlugins:revert() + end) +end) \ No newline at end of file