Skip to content

Commit

Permalink
feat: list running positions
Browse files Browse the repository at this point in the history
See #66
  • Loading branch information
rcarriga committed Aug 28, 2022
1 parent d51a9fd commit a3229e9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions doc/neotest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ should be the position ID.
Fields~
{adapter} `(string)` Adapter ID, if not given the first adapter found with
chosen position is used.
{interactive} `(boolean)` Select a running position interactively

neotest.run.attach() *neotest.run.attach()*
`neotest.run.attach`({args})
Expand All @@ -384,6 +385,7 @@ args[1] should be the position ID.
Fields~
{adapter} `(string)` Adapter ID, if not given the first adapter found with
chosen position is used.
{interactive} `(boolean)` Select a running position interactively

neotest.run.adapters() *neotest.run.adapters()*
`neotest.run.adapters`()
Expand Down
4 changes: 4 additions & 0 deletions lua/neotest/async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ end
local async_wrapper = {
api = proxy_vim("api"),
fn = proxy_vim("fn"),
ui = {
select = plen_async.wrap(vim.ui.select, 3),
input = plen_async.wrap(vim.ui.input, 2),
},
lib = {
first = function(...)
local functions = { ... }
Expand Down
5 changes: 5 additions & 0 deletions lua/neotest/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ function NeotestClient:run_tree(tree, args)
self._state:update_results(adapter_id, all_results)
end

---@return table[]
function NeotestClient:running_positions()
return self._runner:running()
end

---@async
---@param position neotest.Tree
---@param args? table
Expand Down
12 changes: 12 additions & 0 deletions lua/neotest/client/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ local lib = require("neotest.lib")

---@class neotest.TestRunner
---@field _processes neotest.ProcessTracker
---@field _running table<string, table>
local TestRunner = {}

function TestRunner:new(processes)
self.__index = self
return setmetatable({
_processes = processes,
_running = {},
}, self)
end

Expand All @@ -20,6 +22,11 @@ end
---@param args table
---@param adapter neotest.Adapter
function TestRunner:run_tree(tree, args, adapter, on_results)
if self._running[tree:data().id] then
logger.warn("Position already running:", tree:data().id)
end
-- TODO: Change adapter.name to adapter ID when project configs are merged
self._running[tree:data().id] = { position = tree, adapter = adapter.name }
local all_results = {}
local results_callback = function(root, results, output_path)
local function fill_results(missing_results)
Expand Down Expand Up @@ -59,9 +66,14 @@ function TestRunner:run_tree(tree, args, adapter, on_results)

self:_run_tree(tree, args, adapter, results_callback)

self._running[tree:data().id] = nil
return all_results
end

function TestRunner:running()
return vim.tbl_values(self._running)
end

function TestRunner:_run_tree(tree, args, adapter, results_callback)
local spec = adapter.build_spec(vim.tbl_extend("force", args, { tree = tree }))

Expand Down
38 changes: 32 additions & 6 deletions lua/neotest/consumers/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,44 @@ function neotest.run.run_last(args)
end)
end

local function get_tree_interactive()
local running = client:running_positions()
local elem = async.ui.select(running, {
prompt = "Select a position",
format_item = function(elem)
return elem.position:data().name
end,
})
if not elem then
return
end
return elem.position, elem.position
end

--- Stop a running process
---
---@param args string|table? Position ID to stop or args. If args then args[1]
--- should be the position ID.
---@field adapter string Adapter ID, if not given the first adapter found with
--- chosen position is used.
---@field interactive boolean Select a running position interactively
function neotest.run.stop(args)
args = args or {}
if type(args) == "string" then
args = { args }
end
async.run(function()
local tree = neotest.run.get_tree_from_args(args)
if not tree then
lib.notify("No tests found", "warn")
local pos
if args.interactive then
pos = get_tree_interactive()
else
pos = neotest.run.get_tree_from_args(args)
end
if not pos then
lib.notify(args.interactive and "No test selected" or "No tests found", "warn")
return
end
client:stop(tree, args)
client:stop(pos, args)
end)
end

Expand All @@ -145,15 +165,21 @@ end
---
---@field adapter string Adapter ID, if not given the first adapter found with
--- chosen position is used.
---@field interactive boolean Select a running position interactively
function neotest.run.attach(args)
args = args or {}
if type(args) == "string" then
args = { args }
end
async.run(function()
local pos = neotest.run.get_tree_from_args(args)
local pos
if args.interactive then
pos = get_tree_interactive()
else
pos = neotest.run.get_tree_from_args(args)
end
if not pos then
lib.notify("No tests found in file", "warn")
lib.notify(args.interactive and "No test selected" or "No tests found", "warn")
return
end
client:attach(pos, args)
Expand Down
1 change: 1 addition & 0 deletions tests/init.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
set noswapfile
set rtp+=.
set rtp+=../plenary.nvim
set rtp+=../nvim-dap
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/client/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ describe("neotest client", function()
end)
end)

a.it("reports running positions", function()
local tree = get_pos(dir)
async.run(function()
client:run_tree(tree, { strategy = mock_strategy })
end)
local running = client:running_positions()
exit_test()
assert.same(running, { { adapter = mock_adapter.name, position = tree } })
end)

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

0 comments on commit a3229e9

Please sign in to comment.