diff --git a/lua/neotest-golang/health.lua b/lua/neotest-golang/health.lua new file mode 100644 index 00000000..ea06e9fb --- /dev/null +++ b/lua/neotest-golang/health.lua @@ -0,0 +1,42 @@ +local start = vim.health.start or vim.health.report_start +local ok = vim.health.ok or vim.health.report_ok +local warn = vim.health.warn or vim.health.report_warn +local error = vim.health.error or vim.health.report_error +local info = vim.health.info or vim.health.report_info + +local options = require("neotest-golang.options") +local lib = require("neotest-golang.lib") + +local M = {} + +function M.check() + M.go_binary_on_path() + M.go_mod_found() +end + +function M.go_binary_on_path() + local go = vim.fn.executable("go") + if go == 1 then + ok("Go binary found on PATH: " .. vim.fn.exepath("go")) + else + warn("Go binary not found on PATH") + end +end + +function M.go_mod_found() + local go_mod_filepath = nil + local filepaths = lib.find.go_test_filepaths(vim.fn.getcwd()) + for _, filepath in ipairs(filepaths) do + local start_path = vim.fn.fnamemodify(filepath, ":h") + go_mod_filepath = lib.find.file_upwards("go.mod", start_path) + if go_mod_filepath ~= nil then + ok("Found go.mod file for " .. filepath .. " in " .. go_mod_filepath) + break + end + end + if go_mod_filepath == nil then + warn("No go.mod file found") + end +end + +return M diff --git a/lua/neotest-golang/lib/find.lua b/lua/neotest-golang/lib/find.lua index 050fe82f..f759a570 100644 --- a/lua/neotest-golang/lib/find.lua +++ b/lua/neotest-golang/lib/find.lua @@ -1,6 +1,8 @@ --- Helpers around filepaths. -local plenary_scan = require("plenary.scandir") +local scandir = require("plenary.scandir") + +local convert = require("neotest-golang.lib.convert") local M = {} @@ -9,41 +11,32 @@ local M = {} --- @param start_path string --- @return string | nil function M.file_upwards(filename, start_path) - local scan = require("plenary.scandir") local found_filepath = nil while start_path ~= vim.fn.expand("$HOME") do - local files = scan.scan_dir( - start_path, - { search_pattern = filename, hidden = true, depth = 1 } - ) + local files = scandir.scan_dir(start_path, { + search_pattern = convert.to_lua_pattern(filename), + depth = 1, + add_dirs = false, + }) if #files > 0 then found_filepath = files[1] - break + return found_filepath end - start_path = vim.fn.fnamemodify(start_path, ":h") -- go up one directory end if found_filepath == nil then - -- check if filename exists in the current directory - local files = scan.scan_dir( - start_path, - { search_pattern = filename, hidden = true, depth = 1 } - ) - if #files > 0 then - found_filepath = files[1] - end + -- go up one directory and try again + start_path = vim.fn.fnamemodify(start_path, ":h") + return M.file_upwards(filename, start_path) end - - return found_filepath end -- Get all *_test.go files in a directory recursively. function M.go_test_filepaths(folderpath) - local files = plenary_scan.scan_dir(folderpath, { - search_pattern = "_test%.go$", - depth = math.huge, - add_dirs = false, - }) + local files = scandir.scan_dir( + folderpath, + { search_pattern = "_test%.go$", add_dirs = false } + ) return files end