Skip to content

Commit

Permalink
feat: detect nested go projects
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed May 12, 2024
1 parent ffe7826 commit ddf36c1
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ M.Adapter = { name = "neotest-golang" }
---@param dir string @Directory to treat as cwd
---@return string | nil @Absolute root dir of test suite
function M.Adapter.root(dir)
---@type string | nil
local cwd = lib.files.match_root_pattern("go.mod", "go.sum")(dir)
if cwd == nil then
return
if M.find_go_files(vim.fn.getcwd(), 100) then -- TODO: make depth configurable
return dir
end
end

Expand Down Expand Up @@ -322,6 +320,32 @@ function M.Adapter.results(spec, result, tree)
return results
end

--@param root_path string @Root path of project
--@param depth number @Maximum depth to search for go.mod or go.sum
function M.find_go_files(root_path, depth)
local stack = { { root_path, 0 } }
while #stack > 0 do
local top = table.remove(stack)
local dir = top[1]
local level = top[2]
if level > depth then
return false
end
local files = vim.fn.globpath(dir, "*", true, true)
for _, file in ipairs(files) do
if vim.fn.isdirectory(file) == 1 then
table.insert(stack, { file, level + 1 })
elseif
vim.fn.fnamemodify(file, ":t") == "go.mod"
or vim.fn.fnamemodify(file, ":t") == "go.sum"
then
return true
end
end
end
return false
end

--- Build runspec for a single test
---@param pos neotest.Position
---@param strategy string
Expand Down

0 comments on commit ddf36c1

Please sign in to comment.