Skip to content

Commit

Permalink
feat(client): override strategy from adapter spec
Browse files Browse the repository at this point in the history
Allows adapters to implement custom strategies to use in place of
built-in ones such as for nvim-dap.

See #24 (reply in thread)
  • Loading branch information
rcarriga committed May 2, 2023
1 parent 972a7dc commit 98344b4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
9 changes: 7 additions & 2 deletions doc/neotest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1484,9 +1484,13 @@ Fields~
{output_stream} `(async fun(): async)` fun(): string Async iterator of process
output

*neotest.StrategyContext*
Fields~
{position} `(neotest.Position)`

*neotest.Strategy*
Alias~
`neotest.Strategy` → `async fun(spec: neotest.RunSpec): neotest.Process`
`neotest.Strategy` → `async fun(spec: neotest.RunSpec, context: neotest.StrategyContext): neotest.Process`

*neotest.StrategyResult*
Fields~
Expand All @@ -1506,7 +1510,8 @@ Fields~
{cwd?} `(string)`
{context?} `(table)` Arbitrary data to preserve state between running and
result collection
{strategy?} `(table)` Arguments for strategy
{strategy?} `(table|neotest.Strategy)` Arguments for strategy or override for
chosen strategy
{stream} `(fun(output_stream: fun(): string[]): fun():)` table<string,
neotest.Result>

Expand Down
10 changes: 7 additions & 3 deletions lua/neotest/client/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,18 @@ end
---@param spec neotest.RunSpec
---@param adapter neotest.Adapter
function TestRunner:_run_spec(spec, tree, args, adapter_id, adapter, results_callback)
local position = tree:data()
spec.strategy =
vim.tbl_extend("force", spec.strategy or {}, config.strategies[args.strategy] or {})
if type(spec.strategy) == "function" then
args = vim.tbl_extend("keep", { strategy = spec.strategy }, args)
else
spec.strategy =
vim.tbl_extend("force", spec.strategy or {}, config.strategies[args.strategy] or {})
end
spec.env = vim.tbl_extend("force", spec.env or {}, args.env or {})
spec.cwd = args.cwd or spec.cwd
if vim.tbl_isempty(spec.env or {}) then
spec.env = nil
end
local position = tree:data()
local context = {
position = position,
}
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest/types/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
---@field env? table<string, string>
---@field cwd? string
---@field context? table Arbitrary data to preserve state between running and result collection
---@field strategy? table Arguments for strategy
---@field strategy? table|neotest.Strategy Arguments for strategy or override for chosen strategy
---@field stream fun(output_stream: fun(): string[]): fun(): table<string, neotest.Result>

local M = {}
Expand Down
29 changes: 27 additions & 2 deletions tests/unit/client/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("neotest client", function()
return true
end,
output = function()
return spec.strategy.output
return type(spec.strategy) == "table" and spec.strategy.output or "not_a_file"
end,
stop = function()
exit_future.set()
Expand All @@ -111,7 +111,7 @@ describe("neotest client", function()
end,
result = function()
exit_future.wait()
return spec.strategy.exit_code
return type(spec.strategy) == "table" and spec.strategy.exit_code or 0
end,
}
end
Expand Down Expand Up @@ -303,6 +303,31 @@ describe("neotest client", function()
end
end)

a.it("uses spec strategy override", function()
local called = nio.control.future()
mock_adapter.build_spec = function()
return {
strategy = function(...)
called.set()
return mock_strategy(...)
end,
}
end
local tree = get_pos(dir)
exit_future.set()
nio.run(function()
client:run_tree(tree, { strategy = mock_strategy })
end)
called.wait()
local adapter_id = client:get_adapters()[1]
local results = client:get_results(adapter_id)
for _, pos in tree:iter() do
if pos.type == "dir" then
assert.equal(results[pos.id].status, "failed")
end
end
end)

describe("with unsupported roots", function()
describe("supporting files", function()
a.it("breaks up directories to files", function()
Expand Down

0 comments on commit 98344b4

Please sign in to comment.