From 169ac399d4efab8dd7f6fbd94c8b8b48568a1f82 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Sun, 26 May 2024 16:40:38 +0300 Subject: [PATCH] feat(plugins): add some sanity checking and error reporting --- lua/yazi/plugin.lua | 49 ++++++++++++++++++++++++++------- tests/yazi/plugin_spec.lua | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 tests/yazi/plugin_spec.lua diff --git a/lua/yazi/plugin.lua b/lua/yazi/plugin.lua index aa82bf5e..16ade53c 100644 --- a/lua/yazi/plugin.lua +++ b/lua/yazi/plugin.lua @@ -12,7 +12,7 @@ local M = {} --- lazy = true, --- build = function(plugin) --- -- this will be called by lazy.nvim after the plugin was updated ---- require("yazi.plugin").build(plugin) +--- require("yazi.plugin").build_plugin(plugin) --- end, --- } --- ``` @@ -21,22 +21,48 @@ local M = {} --- ---@param plugin YaziLazyNvimPlugin ---@param options? { yazi_dir: string } +---@return YaziPluginInstallationResultSuccess | YaziPluginInstallationResultFailure function M.build_plugin(plugin, options) local yazi_dir = options and options.yazi_dir or vim.fn.expand('~/.config/yazi') local to = vim.fs.normalize(vim.fs.joinpath(yazi_dir, 'plugins', plugin.name)) - if not vim.fn.isdirectory(plugin.dir) then - vim.notify( - vim.inspect({ 'yazi.nvim: plugin directory does not exist', plugin.dir }) - ) - return + local dir = vim.loop.fs_stat(plugin.dir) + if dir == nil or dir.type ~= 'directory' then + ---@type YaziPluginInstallationResultFailure + local result = { + error = 'plugin directory does not exist', + from = plugin.dir, + message = 'yazi.nvim: failed to install plugin', + } + vim.notify(vim.inspect(result)) + return result end - vim.notify( - vim.inspect({ 'yazi.nvim: installing plugin', from = plugin.dir, to = to }) - ) - vim.uv.fs_symlink(plugin.dir, to) + local success, error = vim.uv.fs_symlink(plugin.dir, to) + + if not success then + ---@type YaziPluginInstallationResultFailure + local result = { + message = 'yazi.nvim: failed to install plugin', + from = plugin.dir, + to = to, + error = error or 'unknown error', + } + vim.notify(vim.inspect(result)) + + return result + end + + ---@type YaziPluginInstallationResultSuccess + local result = { + message = 'yazi.nvim: successfully installed plugin ' .. plugin.name, + from = plugin.dir, + to = to, + } + + vim.notify(vim.inspect(result)) + return result end --- Represents information about the plugin to install. Note that this is @@ -44,4 +70,7 @@ end --- you can just pass that. ---@alias YaziLazyNvimPlugin { name: string, dir: string } +---@alias YaziPluginInstallationResultSuccess { message: string, from: string, to: string } +---@alias YaziPluginInstallationResultFailure { message: string, from: string, to?: string, error: string } + return M diff --git a/tests/yazi/plugin_spec.lua b/tests/yazi/plugin_spec.lua new file mode 100644 index 00000000..66f072d2 --- /dev/null +++ b/tests/yazi/plugin_spec.lua @@ -0,0 +1,55 @@ +local assert = require('luassert') +local plugin = require('yazi.plugin') + +describe('installing a plugin', function() + local base_dir = os.tmpname() -- create a temporary file with a unique name + + before_each(function() + -- convert the unique name from a file to a directory + assert(base_dir:match('/tmp/'), 'Failed to create a temporary directory') + os.remove(base_dir) + vim.fn.mkdir(base_dir, 'p') + end) + + after_each(function() + vim.fn.delete(base_dir, 'rf') + end) + + it('can install if everything goes well', function() + local plugin_dir = vim.fs.joinpath(base_dir, 'test-plugin') + local yazi_dir = vim.fs.joinpath(base_dir, 'fake-yazi-dir') + + vim.fn.mkdir(plugin_dir) + vim.fn.mkdir(yazi_dir) + vim.fn.mkdir(vim.fs.joinpath(yazi_dir, 'plugins')) + + plugin.build_plugin({ + dir = plugin_dir, + name = 'test-plugin', + }, { yazi_dir = yazi_dir }) + + -- verify that the plugin was symlinked + -- yazi_dir/plugins/test-plugin -> plugin_dir + local symlink = + vim.loop.fs_readlink(vim.fs.joinpath(yazi_dir, 'plugins', 'test-plugin')) + + assert.are.same(plugin_dir, symlink) + end) + + it('warns the user if the plugin directory does not exist', function() + local plugin_dir = vim.fs.joinpath(base_dir, 'test-plugin') + local yazi_dir = vim.fs.joinpath(base_dir, 'fake-yazi-dir') + + -- NOTE: we are not creating the plugin directory + -- vim.fn.mkdir(plugin_dir) + vim.fn.mkdir(yazi_dir) + vim.fn.mkdir(vim.fs.joinpath(yazi_dir, 'plugins')) + + local result = plugin.build_plugin({ + dir = plugin_dir, + name = 'test-plugin-2', + }, { yazi_dir = yazi_dir }) + + assert.is_equal(result.error, 'plugin directory does not exist') + end) +end)