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