REFACTOR: Updated from older config
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
-- Smoke test for mason-conform.nvim with stubbed mason/conform.
|
||||
-- Run from the repository root: nvim -l tests/smoke_test.lua
|
||||
local script = (arg and arg[0]) or "tests/smoke_test.lua"
|
||||
local root = script:match("^(.*)/tests/") or "."
|
||||
package.path = ("%s/lua/?.lua;%s/lua/?/init.lua;"):format(root, root) .. package.path
|
||||
|
||||
local failures = {}
|
||||
local function check(cond, msg)
|
||||
if not cond then
|
||||
failures[#failures + 1] = msg
|
||||
print("FAIL: " .. msg)
|
||||
else
|
||||
print("ok: " .. msg)
|
||||
end
|
||||
end
|
||||
|
||||
-- ---- stubs -------------------------------------------------------------
|
||||
local notifications = {}
|
||||
local install_calls = {}
|
||||
local installed = {}
|
||||
local installing = {}
|
||||
|
||||
package.preload["conform"] = function()
|
||||
return {
|
||||
formatters_by_ft = {
|
||||
lua = { "stylua" },
|
||||
python = { "autopep8", lsp_format = "fallback", stop_after_first = true },
|
||||
markdown = { { "prettierd", "prettier" }, "cbfmt" },
|
||||
javascript = function()
|
||||
error("needs a buffer")
|
||||
end,
|
||||
go = function()
|
||||
return { "gofumpt" }
|
||||
end,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
package.preload["mason-registry"] = function()
|
||||
local registry = {}
|
||||
function registry.refresh(cb)
|
||||
if cb then
|
||||
cb(true, {})
|
||||
end
|
||||
end
|
||||
function registry.get_package(name)
|
||||
if name == "not-in-registry" then
|
||||
error(('Cannot find package "%q".'):format(name))
|
||||
end
|
||||
local pkg = { name = name }
|
||||
function pkg:is_installed()
|
||||
return installed[name] or false
|
||||
end
|
||||
function pkg:is_installing()
|
||||
return installing[name] or false
|
||||
end
|
||||
function pkg:install(opts)
|
||||
install_calls[#install_calls + 1] = { name = name, version = opts and opts.version }
|
||||
local handle = {}
|
||||
function handle:once(event, cb2)
|
||||
assert(event == "closed")
|
||||
installed[name] = true
|
||||
cb2()
|
||||
end
|
||||
return handle
|
||||
end
|
||||
return pkg
|
||||
end
|
||||
return registry
|
||||
end
|
||||
|
||||
package.preload["mason-core.package"] = function()
|
||||
local Package = {}
|
||||
function Package.Parse(identifier)
|
||||
local name, version = identifier:match("^([^@]+)@?(.*)$")
|
||||
if version == "" then
|
||||
version = nil
|
||||
end
|
||||
return name, version
|
||||
end
|
||||
return Package
|
||||
end
|
||||
|
||||
package.preload["mason.version"] = function()
|
||||
return { MAJOR_VERSION = 2, MINOR_VERSION = 3 }
|
||||
end
|
||||
|
||||
local original_notify = vim.notify
|
||||
vim.notify = function(msg, level)
|
||||
notifications[#notifications + 1] = { msg = msg, level = level or vim.log.levels.INFO }
|
||||
end
|
||||
|
||||
-- ---- tests -------------------------------------------------------------
|
||||
|
||||
-- setup() with defaults
|
||||
local mc = require("mason-conform")
|
||||
mc.setup()
|
||||
vim.wait(200)
|
||||
check(vim.deep_equal(mc.config, { ignore_install = {} }), "setup() without opts keeps defaults")
|
||||
|
||||
local function installed_names()
|
||||
local names = {}
|
||||
for _, call in ipairs(install_calls) do
|
||||
names[#names + 1] = call.name
|
||||
end
|
||||
table.sort(names)
|
||||
return names
|
||||
end
|
||||
|
||||
check(
|
||||
vim.deep_equal(installed_names(), { "autopep8", "cbfmt", "gofumpt", "prettier", "prettierd", "stylua" }),
|
||||
"installs mapped formatters from every formatters_by_ft shape; skips broken function: "
|
||||
.. table.concat(installed_names(), ",")
|
||||
)
|
||||
|
||||
-- setup() again with ignore_install
|
||||
install_calls = {}
|
||||
notifications = {}
|
||||
installed = {}
|
||||
mc.setup({ ignore_install = { "prettierd" } })
|
||||
vim.wait(200)
|
||||
check(
|
||||
vim.deep_equal(installed_names(), { "autopep8", "cbfmt", "gofumpt", "prettier", "stylua" }),
|
||||
"ignore_install is respected: " .. table.concat(installed_names(), ",")
|
||||
)
|
||||
check(vim.deep_equal(mc.config.ignore_install, { "prettierd" }), "ignore_install stored")
|
||||
|
||||
-- installed / installing packages are skipped
|
||||
install_calls = {}
|
||||
installed = {}
|
||||
installing = {}
|
||||
installed["stylua"] = true
|
||||
installing["autopep8"] = true
|
||||
mc.setup({ ignore_install = {} })
|
||||
vim.wait(200)
|
||||
check(
|
||||
vim.deep_equal(installed_names(), { "cbfmt", "gofumpt", "prettier", "prettierd" }),
|
||||
"already installed/installing packages are skipped: " .. table.concat(installed_names(), ",")
|
||||
)
|
||||
|
||||
-- unknown mason package -> warning, no error
|
||||
local install = require("mason-conform.install")
|
||||
notifications = {}
|
||||
local ok = pcall(install.try_install, "not-in-registry")
|
||||
check(ok, "try_install on unknown package does not raise")
|
||||
check(#notifications == 1 and notifications[1].level == vim.log.levels.WARN, "unknown package emits a warning")
|
||||
check(
|
||||
notifications[1] and notifications[1].msg:find("not%-in%-registry", 1, false) ~= nil,
|
||||
"warning names the formatter"
|
||||
)
|
||||
|
||||
-- version pinning passes through Package.Parse
|
||||
installed = {}
|
||||
install_calls = {}
|
||||
install.try_install("stylua@1.2.0")
|
||||
check(install_calls[1] and install_calls[1].version == "1.2.0", "version suffix forwarded to mason")
|
||||
|
||||
-- setup() validates its argument
|
||||
ok = pcall(mc.setup, "not a table")
|
||||
check(not ok, "setup() rejects non-table opts")
|
||||
|
||||
-- health check runs
|
||||
local health_ok, err = pcall(function()
|
||||
require("mason-conform.health").check()
|
||||
end)
|
||||
check(health_ok, "health check runs without error" .. (health_ok and "" or (": " .. tostring(err))))
|
||||
|
||||
-- formatters module
|
||||
local formatters = require("mason-conform.formatters").configured()
|
||||
check(
|
||||
vim.deep_equal(formatters, { "autopep8", "cbfmt", "gofumpt", "prettier", "prettierd", "stylua" }),
|
||||
"formatters.configured() returns sorted unique names"
|
||||
)
|
||||
|
||||
vim.notify = original_notify
|
||||
|
||||
print(("-"):rep(60))
|
||||
if #failures > 0 then
|
||||
print(("%d FAILURE(S)"):format(#failures))
|
||||
os.exit(1)
|
||||
end
|
||||
print("all tests passed")
|
||||
Reference in New Issue
Block a user