Skip to content

Commit

Permalink
feat(plugins): add some sanity checking and error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed May 26, 2024
1 parent fd727d8 commit 7fe8372
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
49 changes: 39 additions & 10 deletions lua/yazi/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
--- }
--- ```
Expand All @@ -21,27 +21,56 @@ 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
--- compatible with LazyPlugin, the lazy.nvim plugin specification table, so
--- 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
55 changes: 55 additions & 0 deletions tests/yazi/plugin_spec.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7fe8372

Please sign in to comment.