Skip to content

Commit 79c68af

Browse files
PriceHillerNTBBloodbath
authored andcommitted
test: overhaul tests to support make test & minimal init
This now allows tests to be ran via `make test`. Currently the tests defined in `main_spec.lua` are invalid, as the `run_file` method appears to have been removed at some point. At the bare minimum though, this commit gives `rest.nvim` a decent framework to work off of for running tests in the future when written.
1 parent eff6f1b commit 79c68af

24 files changed

+176
-1
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: lint format docgen
1+
.PHONY: lint format test docgen
22
.SILENT: docgen
33

44
lint:
@@ -7,6 +7,9 @@ lint:
77
format:
88
stylua .
99

10+
test:
11+
nvim --headless --clean -n -u tests/test.lua "$(FILE)"
12+
1013
docgen:
1114
lemmy-help lua/rest-nvim/client/curl.lua > doc/rest-nvim-curl.txt
1215
lemmy-help lua/rest-nvim/commands.lua > doc/rest-nvim-commands.txt

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.deps

tests/minimal_init.lua

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
local M = {}
2+
3+
---@alias PluginName string The plugin name, will be used as part of the git clone destination
4+
---@alias PluginUrl string The git url at which a plugin is located, can be a path. See https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols for details
5+
---@alias GitPlugins table<PluginName, PluginUrl> Plugins to clone with git
6+
7+
-- Gets the current directory of this file
8+
local test_dir = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h")
9+
local rest_nvim_dir = vim.fn.fnamemodify(test_dir, ":h")
10+
11+
---Gets the root directory of the minimal init and if path is specified appends the given path to the root allowing for
12+
---subdirectories within the current cwd
13+
---@param path string? The additional path to append to the root, not required
14+
---@return string root The root path suffixed with the path provided or an empty suffix if none was given
15+
function M.root(path)
16+
return test_dir .. "/.deps/" .. (path or "")
17+
end
18+
19+
---Returns the package root and ensures the path exists on disk
20+
---@return string pkg_root The package root suffixed with a `/`
21+
M.package_root = function()
22+
local pkg_root = M.root("plugins/")
23+
if not vim.uv.fs_stat(pkg_root) then
24+
vim.fn.mkdir(pkg_root, "p")
25+
end
26+
return pkg_root
27+
end
28+
29+
---Run a system command through `vim.system` and error on failed commands
30+
---@param cmd string[] The command to run
31+
---@param opts vim.SystemOpts? Options to pass to `vim.system`
32+
---@return vim.SystemCompleted
33+
function M.system(cmd, opts)
34+
local out = vim.system(cmd, opts or {}):wait()
35+
if out.code ~= 0 then
36+
error(
37+
string.format(
38+
">> Failed to run command: "
39+
.. table.concat(cmd, " ")
40+
.. "\n>> Exit code: %d\n>> Signal: %d\n===== STDOUT =====\n%s\n===== STDERR =====\n%s\n",
41+
out.code,
42+
out.signal,
43+
out.stdout,
44+
out.stderr
45+
),
46+
vim.log.levels.ERROR
47+
)
48+
end
49+
return out
50+
end
51+
52+
---Make the `rest.nvim` lua rock and register the resulting plugin on the `runtimepath`
53+
function M.make_luarock()
54+
print(">> Making rest.nvim luarock")
55+
local luarocks_binary = "luarocks"
56+
local install_destination = M.root("rocks/")
57+
58+
M.system({
59+
luarocks_binary,
60+
"--lua-version=5.1",
61+
"--tree=" .. install_destination,
62+
"make",
63+
rest_nvim_dir .. "/rest.nvim-scm-2.rockspec",
64+
})
65+
66+
local sysname = vim.uv.os_uname().sysname:lower()
67+
local lib_extension = (sysname:find("windows") and "dll") or (sysname:find("darwin") and "dylib") or "so"
68+
local rocks_nvim_rtps = {
69+
{ "lib", "lua", "5.1", "?." .. lib_extension },
70+
{ "share", "lua", "5.1", "?.lua" },
71+
{ "share", "lua", "5.1", "?", "init.lua" },
72+
}
73+
for _, rocks_nvim_rtp in ipairs(rocks_nvim_rtps) do
74+
local path = vim.fs.joinpath(install_destination, unpack(rocks_nvim_rtp))
75+
if path:match(".*" .. vim.pesc(lib_extension) .. "$") then
76+
package.cpath = package.cpath .. ";" .. path
77+
else
78+
package.path = package.path .. ";" .. path
79+
end
80+
end
81+
print(">> Finished making rest.nvim luarock")
82+
end
83+
84+
---Downloads a plugin from a given url and registers it on the 'runtimepath'
85+
---@param plugin_name PluginName
86+
---@param plugin_url PluginUrl
87+
function M.load_plugin(plugin_name, plugin_url)
88+
local package_root = M.root("plugins/")
89+
local install_destination = package_root .. plugin_name
90+
vim.opt.runtimepath:append(install_destination)
91+
92+
if not vim.loop.fs_stat(package_root) then
93+
vim.fn.mkdir(package_root, "p")
94+
end
95+
96+
-- If the plugin install path already exists, we don't need to clone it again.
97+
if not vim.loop.fs_stat(install_destination) then
98+
print(string.format('>> Downloading plugin "%s" to "%s"', plugin_name, install_destination))
99+
M.system({ "git", "clone", "--depth=1", plugin_url, install_destination })
100+
end
101+
end
102+
103+
---@class SetupOpts
104+
---@field rocks_config string Path to a `rocks.nvim` toml configuration file containing plugins
105+
106+
---Do the initial setup. Downloads plugins, ensures the minimal init does not pollute the filesystem by keeping
107+
---everything self contained to the CWD of the minimal init file. Run prior to running tests, reproducing issues, etc.
108+
---@param plugins? GitPlugins
109+
function M.setup(plugins)
110+
plugins = plugins or {}
111+
112+
vim.env.XDG_CONFIG_HOME = M.root("xdg/config")
113+
vim.env.XDG_DATA_HOME = M.root("xdg/data")
114+
vim.env.XDG_STATE_HOME = M.root("xdg/state")
115+
vim.env.XDG_CACHE_HOME = M.root("xdg/cache")
116+
117+
local std_paths = {
118+
"cache",
119+
"data",
120+
"config",
121+
}
122+
123+
for _, std_path in pairs(std_paths) do
124+
vim.fn.mkdir(vim.fn.stdpath(std_path), "p")
125+
end
126+
127+
-- NOTE: Cleanup the xdg cache on exit so new runs of the minimal init doesn't share any previous state, e.g. shada
128+
vim.api.nvim_create_autocmd("VimLeave", {
129+
callback = function()
130+
vim.fn.delete(M.root("xdg"), "rf")
131+
end,
132+
})
133+
134+
-- Build our local lua rock for usage
135+
M.make_luarock()
136+
137+
for plugin_name, plugin_url in pairs(plugins) do
138+
M.load_plugin(plugin_name, plugin_url)
139+
end
140+
end
141+
142+
M.setup({
143+
plenary = "https://github.com/nvim-lua/plenary.nvim.git",
144+
treesitter = "https://github.com/nvim-treesitter/nvim-treesitter.git",
145+
})
146+
147+
-- WARN: Do all plugin setup, test runs, reproductions, etc. AFTER calling setup with a list of plugins!
148+
-- Basically, do all that stuff AFTER this line.
149+
150+
require("nvim-treesitter.configs").setup({
151+
ensure_installed = { "lua", "xml", "http", "json", "graphql" },
152+
sync_install = true,
153+
})
154+
155+
-- Ensure we can use `rest.nvim` by registering it on the rtp
156+
-- vim.opt.runtimepath:append(rest_nvim_dir)
157+
require("rest-nvim").setup({})
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)