Skip to content

Commit

Permalink
refactor: cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 3, 2024
1 parent 0c15ff3 commit bad873e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 122 deletions.
157 changes: 36 additions & 121 deletions lua/neotest-golang/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,22 @@ function M.golist_output(cwd)
return json.process_golist_output(output)
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
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_individual_test(cwd, test_name)
--- 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 test_cmd = {}

if runner == "go" then
test_cmd = M.gotest_with_args_for_individual_test(cwd, test_name)
elseif runner == "gotestsum" then
json_filepath = vim.fs.normalize(async.fn.tempname())
test_cmd =
M.gotestsum_with_args_for_individual_test(cwd, test_name, json_filepath)
end

return test_cmd, json_filepath
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_for_dir(module_name)
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)
Expand All @@ -67,108 +46,44 @@ function M.test_command_for_dir(module_name)

--- The final test command to execute.
--- @type table<string>
local test_cmd = {}
local cmd = {}

if runner == "go" then
test_cmd = M.gotest_with_args_for_dir(module_name)
cmd = M.go_test(go_test_required_args)
elseif runner == "gotestsum" then
json_filepath = vim.fs.normalize(async.fn.tempname())
test_cmd = M.gotestsum_with_args_cmd_for_dir(module_name, json_filepath)
cmd = M.gotestsum(go_test_required_args, json_filepath)
end

return test_cmd, json_filepath
return cmd, json_filepath
end

function M.gotest_with_args_for_dir(module_name)
local gotest = {
"go",
"test",
"-json",
}

local required_go_test_args = {
module_name,
}

local combined_args = vim.list_extend(
vim.deepcopy(options.get().go_test_args),
required_go_test_args
)
local cmd = vim.list_extend(vim.deepcopy(gotest), combined_args)

return cmd
end

function M.gotestsum_with_args_cmd_for_dir(module_name, json_filepath)
local gotest = { "gotestsum" }
local gotestsum_json = {
"--jsonfile=" .. json_filepath,
"--",
}

local required_go_test_args = {
module_name,
}

local gotest_args = vim.list_extend(
vim.deepcopy(options.get().go_test_args),
required_go_test_args
)

local cmd =
vim.list_extend(vim.deepcopy(gotest), options.get().gotestsum_args)
cmd = vim.list_extend(vim.deepcopy(cmd), gotestsum_json)
cmd = vim.list_extend(vim.deepcopy(cmd), gotest_args)

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.gotest_with_args_for_individual_test(
test_folder_absolute_path,
test_name
)
--- @type table
local required_go_test_args = { test_folder_absolute_path, "-run", test_name }

local gotest = {
"go",
"test",
"-json",
}

local combined_args = vim.list_extend(
vim.deepcopy(options.get().go_test_args),
required_go_test_args
)
local cmd = vim.list_extend(vim.deepcopy(gotest), combined_args)
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.gotestsum_with_args_for_individual_test(
test_folder_absolute_path,
test_name,
json_filepath
)
--- @type table
local required_go_test_args = { test_folder_absolute_path, "-run", test_name }

local gotest = { "gotestsum" }
local gotestsum_json = {
"--jsonfile=" .. json_filepath,
"--",
}

local gotest_args = vim.list_extend(
vim.deepcopy(options.get().go_test_args),
required_go_test_args
)

local cmd =
vim.list_extend(vim.deepcopy(gotest), options.get().gotestsum_args)
cmd = vim.list_extend(vim.deepcopy(cmd), gotestsum_json)
cmd = vim.list_extend(vim.deepcopy(cmd), gotest_args)

return cmd
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
1 change: 0 additions & 1 deletion lua/neotest-golang/runspec_dir.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
--- Helpers to build the command and context around running all tests of
--- a Go module.

local json = require("neotest-golang.json")
local cmd = require("neotest-golang.cmd")

local M = {}
Expand Down

0 comments on commit bad873e

Please sign in to comment.