Skip to content

Commit 958a6bf

Browse files
committed
feat: init commit
0 parents  commit 958a6bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4107
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
neovim/
2+
plenary.nvim/
3+
Session.vim

lua/neotest/adapters/init.lua

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
local async = require("plenary.async")
2+
local logger = require("neotest.logging")
3+
local lib = require("neotest.lib")
4+
5+
local M = {}
6+
7+
---@type table<string, NeotestAdapter>
8+
local adapters = {}
9+
10+
function M.set_adapters(a)
11+
adapters = a
12+
end
13+
14+
local function get_current_adapter_from_cwd()
15+
for _, adapter in pairs(adapters) do
16+
logger.info("Checking current directory for test files with", adapter.name, "adapter")
17+
local tree = adapter.discover_positions(async.fn.getcwd())
18+
if tree and #tree:children() > 0 then
19+
return adapter
20+
end
21+
end
22+
end
23+
24+
local function get_adapter_from_open_bufs()
25+
local buffers = async.api.nvim_list_bufs()
26+
for _, adapter in pairs(adapters) do
27+
for _, bufnr in pairs(buffers) do
28+
if adapter.is_test_file(async.fn.fnamemodify(async.fn.bufname(bufnr), ":p")) then
29+
return adapter
30+
end
31+
end
32+
end
33+
end
34+
35+
local function get_adapter_from_file_path(file_path)
36+
for _, adapter in pairs(adapters) do
37+
if adapter.is_test_file(file_path) then
38+
return adapter
39+
end
40+
end
41+
end
42+
43+
local adapter = nil
44+
45+
function M.get_adapter(opts)
46+
local file_path = opts.file_path
47+
if not adapter and file_path then
48+
adapter = get_adapter_from_file_path(file_path)
49+
end
50+
if not adapter then
51+
adapter = get_adapter_from_open_bufs()
52+
end
53+
if opts.from_dir and not adapter then
54+
adapter = get_current_adapter_from_cwd()
55+
end
56+
if adapter then
57+
logger.debug("Using adapter " .. adapter.name)
58+
else
59+
logger.info("No adapter found")
60+
end
61+
return adapter
62+
end
63+
64+
return M

lua/neotest/adapters/interface.lua

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---@class NeotestAdapter
2+
---@field name string
3+
local NeotestAdapter = {}
4+
5+
6+
---@async
7+
---@param file_path string
8+
---@return boolean
9+
function NeotestAdapter.is_test_file(file_path) end
10+
11+
---@async
12+
---@param path string
13+
---@return Tree | nil
14+
function NeotestAdapter.discover_positions(path) end
15+
16+
---@param args NeotestRunArgs
17+
---@return NeotestRunSpec
18+
function NeotestAdapter.build_spec(args) end
19+
20+
---@async
21+
---@param spec NeotestRunSpec
22+
---@param result NeotestStrategyResult
23+
---@return table<string, NeotestResult>
24+
function NeotestAdapter.results(spec, result) end

lua/neotest/client/events/init.lua

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
local logger = require("neotest.logging")
2+
3+
local M = {}
4+
5+
---@alias NeotestEvent "discover_positions" | "run" | "results"
6+
---@class NeotestEvents
7+
---@field DISCOVER_POSITIONS "discover_positions"
8+
---@field RUN "run"
9+
---@field RESULTS "results"
10+
local NeotestEvents = {
11+
DISCOVER_POSITIONS = "discover_positions",
12+
RUN = "run",
13+
RESULTS = "results",
14+
}
15+
16+
M.events = NeotestEvents
17+
18+
---@class NeotestEventListeners
19+
---@field discover_files table<string, fun(files: string[])>
20+
---@field discover_positions table<string, fun(file_path: string, tree: Tree)>
21+
---@field run table<string, fun(position_ids: string[])>
22+
---@field results table<string, fun(results: table<string, NeotestResult>)>
23+
24+
---@class NeotestEventProcessor
25+
---@field listeners NeotestEventListeners
26+
local NeotestEventProcessor = {}
27+
28+
function NeotestEventProcessor:new()
29+
local events = {}
30+
local listeners = {}
31+
for _, event in pairs(NeotestEvents) do
32+
listeners[event] = {}
33+
end
34+
events.listeners = listeners
35+
setmetatable(events, self)
36+
self.__index = self
37+
return events
38+
end
39+
40+
---@param event NeotestEvent
41+
---@vararg any Arguments for the event
42+
function NeotestEventProcessor:emit(event, ...)
43+
logger.info("Emitting", event, "event")
44+
for name, listener in pairs(self.listeners[event] or {}) do
45+
logger.debug("Calling listener", name, "for event", event)
46+
local success, err = pcall(listener, ...)
47+
if not success then
48+
logger.error("Error during listener", name, "for event:", err)
49+
end
50+
end
51+
end
52+
53+
---@return NeotestEventProcessor
54+
function M.processor()
55+
return NeotestEventProcessor:new()
56+
end
57+
58+
return M

0 commit comments

Comments
 (0)