Skip to content

Commit

Permalink
feat: add healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 13, 2024
1 parent 86bb540 commit 8c3abd2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
42 changes: 42 additions & 0 deletions lua/neotest-golang/health.lua
Original file line number Diff line number Diff line change
@@ -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
39 changes: 16 additions & 23 deletions lua/neotest-golang/lib/find.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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

Expand Down

0 comments on commit 8c3abd2

Please sign in to comment.