From ddf36c1bf64663d0f54cc17c1b823ff6e91af9cd Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sun, 12 May 2024 08:50:42 +0200 Subject: [PATCH] feat: detect nested go projects --- lua/neotest-golang/init.lua | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/lua/neotest-golang/init.lua b/lua/neotest-golang/init.lua index e87ef836..46e20d20 100644 --- a/lua/neotest-golang/init.lua +++ b/lua/neotest-golang/init.lua @@ -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 @@ -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