Skip to content

Commit 1feb82e

Browse files
committed
feat: add plugin manager for testing
1 parent e501d3d commit 1feb82e

File tree

8 files changed

+108
-23
lines changed

8 files changed

+108
-23
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
TESTS_INIT=tests/minimal_init.lua
1+
PREPARE_CONFIG=tests/prepare-config.lua
2+
TEST_CONFIG=tests/test-config.lua
23
TESTS_DIR=tests/
34

45
.PHONY: test
56

67
test:
78
@nvim \
89
--headless \
9-
--noplugin \
10-
-u ${TESTS_INIT} \
11-
-c "PlenaryBustedDirectory ${TESTS_DIR} { minimal_init = '${TESTS_INIT}' }"
10+
-u ${PREPARE_CONFIG} \
11+
"+PlenaryBustedDirectory ${TESTS_DIR} { minimal_init = '${TEST_CONFIG}' }"

dev/init.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local cwd = vim.fn.getcwd()
2+
vim.opt.runtimepath:prepend(cwd)
3+
4+
--[[
5+
-- plugin name will be used to reload the loaded modules
6+
--]]
7+
local package_name = 'plugin_name'
8+
9+
-- add the escape character to special characters
10+
local escape_pattern = function(text)
11+
return text:gsub('([^%w])', '%%%1')
12+
end
13+
14+
-- unload loaded modules by the matching text
15+
local unload_packages = function()
16+
local esc_package_name = escape_pattern(package_name)
17+
18+
for module_name, _ in pairs(package.loaded) do
19+
if string.find(module_name, esc_package_name) then
20+
package.loaded[module_name] = nil
21+
end
22+
end
23+
end
24+
25+
-- executes the run method in the package
26+
local run_action = function()
27+
vim.cmd.messages('clear')
28+
29+
require(package_name).hello()
30+
end
31+
32+
-- unload and run the function from the package
33+
function _G.Reload_and_run()
34+
unload_packages()
35+
run_action()
36+
end
37+
38+
---@diagnostic disable-next-line: undefined-global
39+
local set_keymap = vim.api.nvim_set_keymap
40+
41+
set_keymap('n', '<leader><leader>r', '<cmd>luafile dev/init.lua<cr>', {})
42+
set_keymap('n', '<leader><leader>w', '<cmd>lua Reload_and_run()<cr>', {})

lua/plugin_name.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
-- main module file
2-
local module = require("plugin_name.module")
2+
local module = require('plugin_name.module')
33

44
---@class Config
55
---@field opt string Your config option
66
local config = {
7-
opt = "Hello!",
7+
opt = 'Hello!',
88
}
99

1010
---@class MyModule
@@ -17,11 +17,11 @@ M.config = config
1717
-- you can define your setup function here. Usually configurations can be merged, accepting outside params and
1818
-- you can also put some validation here for those.
1919
M.setup = function(args)
20-
M.config = vim.tbl_deep_extend("force", M.config, args or {})
20+
M.config = vim.tbl_deep_extend('force', M.config, args or {})
2121
end
2222

2323
M.hello = function()
24-
module.my_first_function()
24+
print(module.my_first_function())
2525
end
2626

2727
return M

plugin/plugin_name.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
vim.api.nvim_create_user_command("MyFirstFunction", require("plugin_name").hello, {})
1+
vim.api.nvim_create_user_command(
2+
'MyFirstFunction',
3+
require('plugin_name').hello,
4+
{}
5+
)

tests/minimal_init.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
local plenary_dir = os.getenv("PLENARY_DIR") or "/tmp/plenary.nvim"
1+
local plenary_dir = os.getenv('PLENARY_DIR') or '/tmp/plenary.nvim'
22
local is_not_a_directory = vim.fn.isdirectory(plenary_dir) == 0
33
if is_not_a_directory then
4-
vim.fn.system({"git", "clone", "https://github.com/nvim-lua/plenary.nvim", plenary_dir})
4+
vim.fn.system({
5+
'git',
6+
'clone',
7+
'https://github.com/nvim-lua/plenary.nvim',
8+
plenary_dir,
9+
})
510
end
611

7-
vim.opt.rtp:append(".")
12+
vim.opt.rtp:append('.')
813
vim.opt.rtp:append(plenary_dir)
914

10-
vim.cmd("runtime plugin/plenary.vim")
11-
require("plenary.busted")
15+
vim.cmd('runtime plugin/plenary.vim')
16+
require('plenary.busted')
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
local plugin = require("plugin_name")
1+
local plugin = require('plugin_name')
22

3-
describe("setup", function()
4-
it("works with default", function()
5-
assert("my first function with param = Hello!", plugin.hello())
6-
end)
3+
describe('setup', function()
4+
it('works with default', function()
5+
assert('my first function with param = Hello!', plugin.hello())
6+
end)
77

8-
it("works with custom var", function()
9-
plugin.setup({ opt = "custom" })
10-
assert("my first function with param = custom", plugin.hello())
11-
end)
8+
it('works with custom var', function()
9+
plugin.setup({ opt = 'custom' })
10+
assert('my first function with param = custom', plugin.hello())
11+
end)
1212
end)

tests/prepare-config.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim'
2+
3+
if not vim.loop.fs_stat(lazypath) then
4+
vim.fn.system({
5+
'git',
6+
'clone',
7+
'--filter=blob:none',
8+
'https://github.com/folke/lazy.nvim.git',
9+
'--branch=stable',
10+
lazypath,
11+
})
12+
end
13+
14+
vim.opt.rtp:prepend(lazypath)
15+
16+
local temp_path = './.test_plugins'
17+
18+
require('lazy').setup({
19+
{
20+
'nvim-lua/plenary.nvim',
21+
lazy = false,
22+
},
23+
}, {
24+
root = temp_path,
25+
lockfile = temp_path .. '/lazy-lock.json',
26+
defaults = {
27+
lazy = false,
28+
},
29+
})

tests/test-config.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local temp_path = './.test_plugins'
2+
3+
vim.opt.rtp:append(temp_path .. '/plenary.nvim')
4+
-- vim.opt.rtp:append(temp_path .. '/nvim-lspconfig')
5+
-- vim.opt.rtp:append(temp_path .. '/mason.nvim')

0 commit comments

Comments
 (0)