Skip to content

Commit

Permalink
fix(client): avoid matching bufs under existing roots
Browse files Browse the repository at this point in the history
If an adapter root is not the cwd and an open buf under that root is
matched on startup then a second adapter will be recorded with the cwd
as the root, despite being under an existing root.

See #127
  • Loading branch information
rcarriga committed Nov 3, 2022
1 parent 2e975ae commit 2a8d8d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
25 changes: 18 additions & 7 deletions lua/neotest/adapters/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ function AdapterGroup:adapters_with_root_dir(cwd)
return adapters
end

function AdapterGroup:adapters_matching_open_bufs()
function AdapterGroup:adapters_matching_open_bufs(existing_roots)
local function is_under_roots(path)
for _, root in ipairs(existing_roots) do
if vim.startswith(path, root) then
return true
end
end
return false
end

local adapters = {}
local buffers = async.api.nvim_list_bufs()

Expand All @@ -30,12 +39,14 @@ function AdapterGroup:adapters_matching_open_bufs()

local matched_files = {}
for _, path in ipairs(paths) do
for _, adapter in ipairs(self:_path_adapters(path)) do
if adapter.is_test_file(path) and not matched_files[path] then
logger.info("Adapter", adapter.name, "matched buffer", path)
matched_files[path] = true
table.insert(adapters, adapter)
break
if not is_under_roots(path) then
for _, adapter in ipairs(self:_path_adapters(path)) do
if adapter.is_test_file(path) and not matched_files[path] then
logger.info("Adapter", adapter.name, "matched buffer", path)
matched_files[path] = true
table.insert(adapters, adapter)
break
end
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions lua/neotest/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,13 @@ end
function NeotestClient:_update_adapters(dir)
local adapters_with_root = lib.files.is_dir(dir)
and self._adapter_group:adapters_with_root_dir(dir)
or {}
local adapters_with_bufs = self._adapter_group:adapters_matching_open_bufs()
or {}

local adapters_with_bufs =
self._adapter_group:adapters_matching_open_bufs(lib.func_util.map(function(i, entry)
return i, entry.root
end, adapters_with_root))

local found = {}
for adapter_id, _ in pairs(self._adapters) do
found[adapter_id] = true
Expand Down

0 comments on commit 2a8d8d7

Please sign in to comment.