Skip to content

Commit

Permalink
fix: show errors when no go.mod file is found
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 15, 2024
1 parent c00c081 commit 886f796
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
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 886f796

Please sign in to comment.