]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/test.lua
nvim: update lazy-lock
[dotfiles.git] / nvim / lua / plugins / test.lua
1 return {
2 { "nvim-neotest/neotest",
3 dependencies = { "nvim-neotest/nvim-nio" },
4 opts = {
5 adapters = {},
6 status = { virtual_text = true },
7 output = { open_on_run = true },
8 quickfix = { enabled = true },
9 },
10 config = function(_, opts)
11 local neotest_ns = vim.api.nvim_create_namespace("neotest")
12 vim.diagnostic.config({
13 virtual_text = {
14 format = function(diagnostic)
15 -- Replace newline and tab characters with space for more compact diagnostics
16 local message = diagnostic.message:gsub("\n", " "):gsub("\t", " "):gsub("%s+", " "):gsub("^%s+", "")
17 return message
18 end,
19 },
20 }, neotest_ns)
21
22 opts.consumers = opts.consumers or {}
23 -- Refresh and auto close trouble after running tests
24 ---@type neotest.Consumer
25 opts.consumers.trouble = function(client)
26 client.listeners.results = function(adapter_id, results, partial)
27 if partial then
28 return
29 end
30 local tree = assert(client:get_position(nil, { adapter = adapter_id }))
31
32 local failed = 0
33 for pos_id, result in pairs(results) do
34 if result.status == "failed" and tree:get_key(pos_id) then
35 failed = failed + 1
36 end
37 end
38 vim.schedule(function()
39 local trouble = require("trouble")
40 if trouble.is_open() then
41 trouble.refresh()
42 if failed == 0 then
43 trouble.close()
44 end
45 end
46 end)
47 end
48 return {}
49 end
50
51 -- NOTE: support setting up test adapters in lang
52 if opts.adapters then
53 local adapters = {}
54 for name, config in pairs(opts.adapters or {}) do
55 if type(name) == "number" then
56 if type(config) == "string" then
57 config = require(config)
58 end
59 adapters[#adapters + 1] = config
60 elseif config ~= false then
61 local adapter = require(name)
62 if type(config) == "table" and not vim.tbl_isempty(config) then
63 local meta = getmetatable(adapter)
64 if adapter.setup then
65 adapter.setup(config)
66 elseif adapter.adapter then
67 adapter.adapter(config)
68 adapter = adapter.adapter
69 elseif meta and meta.__call then
70 adapter = adapter(config)
71 else
72 error("Adapter " .. name .. " does not support setup")
73 end
74 end
75 adapters[#adapters + 1] = adapter
76 end
77 end
78 opts.adapters = adapters
79 end
80
81 require("neotest").setup(opts)
82 end,
83 -- stylua: ignore
84 keys = {
85 {"<leader>t", "", desc = "+test"},
86 { "<leader>tt", function() require("neotest").run.run(vim.fn.expand("%")) end, desc = "Run File" },
87 { "<leader>tT", function() require("neotest").run.run(vim.loop.cwd()) end, desc = "Run All Test Files" },
88 { "<leader>tr", function() require("neotest").run.run() end, desc = "Run Nearest" },
89 { "<leader>tl", function() require("neotest").run.run_last() end, desc = "Run Last" },
90 { "<leader>ts", function() require("neotest").summary.toggle() end, desc = "Toggle Summary" },
91 { "<leader>to", function() require("neotest").output.open({ enter = true, auto_close = true }) end, desc = "Show Output" },
92 { "<leader>tO", function() require("neotest").output_panel.toggle() end, desc = "Toggle Output Panel" },
93 { "<leader>tS", function() require("neotest").run.stop() end, desc = "Stop" },
94 { "<leader>tw", function() require("neotest").watch.toggle(vim.fn.expand("%")) end, desc = "Toggle Watch" },
95 },
96 },
97 {
98 "mfussenegger/nvim-dap",
99 -- stylua: ignore
100 keys = {
101 { "<leader>td", function() require("neotest").run.run({strategy = "dap"}) end, desc = "Debug Nearest" },
102 },
103 },
104 }