-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: experimental gotestsum support and larger refactoring (#81)
- Loading branch information
1 parent
d9b0bb2
commit 8672472
Showing
9 changed files
with
202 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--- Helper functions building the command to execute. | ||
|
||
local async = require("neotest.async") | ||
|
||
local options = require("neotest-golang.options") | ||
local json = require("neotest-golang.json") | ||
|
||
local M = {} | ||
|
||
function M.golist_output(cwd) | ||
-- call 'go list -json ./...' to get test file data | ||
local go_list_command = { | ||
"go", | ||
"list", | ||
"-json", | ||
"./...", | ||
} | ||
local output = | ||
vim.fn.system("cd " .. cwd .. " && " .. table.concat(go_list_command, " ")) | ||
return json.process_golist_output(output) | ||
end | ||
|
||
function M.test_command_for_individual_test( | ||
test_folder_absolute_path, | ||
test_name | ||
) | ||
local go_test_required_args = { test_folder_absolute_path, "-run", test_name } | ||
local cmd, json_filepath = M.test_command(go_test_required_args) | ||
return cmd, json_filepath | ||
end | ||
|
||
function M.test_command_for_dir(module_name) | ||
local go_test_required_args = { module_name } | ||
local cmd, json_filepath = M.test_command(go_test_required_args) | ||
return cmd, json_filepath | ||
end | ||
|
||
function M.test_command(go_test_required_args) | ||
--- The runner to use for running tests. | ||
--- @type string | ||
local runner = M.fallback_to_go_test(options.get().runner) | ||
|
||
--- The filepath to write test output JSON to, if using `gotestsum`. | ||
--- @type string | nil | ||
local json_filepath = nil | ||
|
||
--- The final test command to execute. | ||
--- @type table<string> | ||
local cmd = {} | ||
|
||
if runner == "go" then | ||
cmd = M.go_test(go_test_required_args) | ||
elseif runner == "gotestsum" then | ||
json_filepath = vim.fs.normalize(async.fn.tempname()) | ||
cmd = M.gotestsum(go_test_required_args, json_filepath) | ||
end | ||
|
||
return cmd, json_filepath | ||
end | ||
|
||
function M.go_test(go_test_required_args) | ||
local cmd = { "go", "test", "-json" } | ||
cmd = vim.list_extend(vim.deepcopy(cmd), options.get().go_test_args) | ||
cmd = vim.list_extend(vim.deepcopy(cmd), go_test_required_args) | ||
return cmd | ||
end | ||
|
||
function M.gotestsum(go_test_required_args, json_filepath) | ||
local cmd = { "gotestsum", "--jsonfile=" .. json_filepath } | ||
cmd = vim.list_extend(vim.deepcopy(cmd), options.get().gotestsum_args) | ||
cmd = vim.list_extend(vim.deepcopy(cmd), { "--" }) | ||
cmd = vim.list_extend(vim.deepcopy(cmd), options.get().go_test_args) | ||
cmd = vim.list_extend(vim.deepcopy(cmd), go_test_required_args) | ||
return cmd | ||
end | ||
|
||
function M.fallback_to_go_test(executable) | ||
if vim.fn.executable(executable) == 0 then | ||
vim.notify( | ||
"Runner " .. executable .. " not found. Falling back to 'go'.", | ||
vim.log.levels.WARN | ||
) | ||
options.set({ runner = "go" }) | ||
return options.get().runner | ||
end | ||
return options.get().runner | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- DAP setup related functions. | ||
|
||
local options = require("neotest-golang.options") | ||
|
||
local M = {} | ||
|
||
function M.setup_debugging(cwd) | ||
local dap_go_opts = options.get().dap_go_opts or {} | ||
local dap_go_opts_original = vim.deepcopy(dap_go_opts) | ||
if dap_go_opts.delve == nil then | ||
dap_go_opts.delve = {} | ||
end | ||
dap_go_opts.delve.cwd = cwd | ||
require("dap-go").setup(dap_go_opts) | ||
|
||
-- reset nvim-dap-go (and cwd) after debugging with nvim-dap | ||
require("dap").listeners.after.event_terminated["neotest-golang-debug"] = function() | ||
require("dap-go").setup(dap_go_opts_original) | ||
end | ||
end | ||
|
||
--- @param test_name string | ||
--- @return table | nil | ||
function M.get_dap_config(test_name) | ||
-- :help dap-configuration | ||
local dap_config = { | ||
type = "go", | ||
name = "Neotest-golang", | ||
request = "launch", | ||
mode = "test", | ||
program = "${fileDirname}", | ||
args = { "-test.run", "^" .. test_name .. "$" }, | ||
} | ||
|
||
return dap_config | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.