Skip to content

Commit

Permalink
refactor: simplify go.mod search function, show errors when no go.mod…
Browse files Browse the repository at this point in the history
… is found (#143)

* refactor: simplify search function

* fix: show errors when no go.mod file is found
  • Loading branch information
fredrikaverpil authored Jul 15, 2024
1 parent 91dabb0 commit 5daa99a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 37 deletions.
15 changes: 7 additions & 8 deletions lua/neotest-golang/lib/find.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

local scandir = require("plenary.scandir")

local convert = require("neotest-golang.lib.convert")
local logger = require("neotest-golang.logging")

local M = {}

Expand All @@ -17,13 +17,12 @@ function M.file_upwards(filename, start_path)
local home_dir = vim.fn.expand("$HOME")

while start_dir ~= home_dir do
local files = scandir.scan_dir(start_dir, {
search_pattern = convert.to_lua_pattern(filename),
depth = 1,
add_dirs = false,
})
if #files > 0 then
return files[1]
logger.debug("Searching for " .. filename .. " in " .. start_dir)

local try_path = start_dir .. "/" .. filename
if vim.fn.filereadable(try_path) == 1 then
logger.debug("Found " .. filename .. " at " .. try_path)
return try_path
end

-- Go up one directory
Expand Down
28 changes: 4 additions & 24 deletions lua/neotest-golang/runspec/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ local M = {}
function M.build(pos)
local go_mod_filepath = lib.find.file_upwards("go.mod", pos.path)
if go_mod_filepath == nil then
-- if no go.mod file was found up the directory tree, until reaching $CWD,
-- then we cannot determine the Go project root.
return M.fail_fast(pos)
local msg =
"The selected directory does not contain a go.mod file or is not part of a Go module."
logger.error(msg)
error(msg)
end

local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
Expand Down Expand Up @@ -59,25 +60,4 @@ function M.build(pos)
return run_spec
end

function M.fail_fast(pos)
local msg = "The selected folder must contain a go.mod file "
.. "or be a subdirectory of a Go package."
logger.error(msg)

--- @type RunspecContext
local context = {
pos_id = pos.id,
pos_type = "dir",
golist_data = {}, -- no golist output
parse_test_results = false,
}

--- @type neotest.RunSpec
local run_spec = {
command = { "echo", msg },
context = context,
}
return run_spec
end

return M
12 changes: 7 additions & 5 deletions lua/neotest-golang/runspec/file.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--- Helpers to build the command and context around running all tests of a file.

local logger = require("neotest-golang.logging")
local lib = require("neotest-golang.lib")

local M = {}
Expand All @@ -10,14 +11,15 @@ local M = {}
--- @return neotest.RunSpec | neotest.RunSpec[] | nil
function M.build(pos, tree)
if vim.tbl_isempty(tree:children()) then
return M.fail_fast(pos)
return M.return_skipped(pos)
end

local go_mod_filepath = lib.find.file_upwards("go.mod", pos.path)
if go_mod_filepath == nil then
-- if no go.mod file was found up the directory tree, until reaching $CWD,
-- then we cannot determine the Go project root.
return M.fail_fast(pos)
local msg =
"The selected file does not appear to be part of a valid Go module (no go.mod file found)."
logger.error(msg)
error(msg)
end

local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
Expand Down Expand Up @@ -72,7 +74,7 @@ function M.build(pos, tree)
return run_spec
end

function M.fail_fast(pos)
function M.return_skipped(pos)
--- @type RunspecContext
local context = {
pos_id = pos.id,
Expand Down

0 comments on commit 5daa99a

Please sign in to comment.