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 4c2506f commit 6d2c7db
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 47 deletions.
52 changes: 52 additions & 0 deletions lua/neotest-golang/cmd.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--- Helper functions building the command to execute.

local async = require("neotest.async")

local options = require("neotest-golang.options")

local M = {}
Expand All @@ -17,6 +19,56 @@ function M.build_golist_cmd(cwd)
return go_list_command_result
end

function M.build_test_command_for_individual_test(cwd, test_name)
--- The runner to use for running tests.
--- @type string
local runner = options.get().runner

-- TODO: if gotestsum, check if it is on $PATH, or fall back onto `go test`

--- 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.build_gotest_cmd_for_test(cwd, test_name)
elseif runner == "gotestsum" then
json_filepath = vim.fs.normalize(async.fn.tempname())
test_cmd = M.build_gotestsum_cmd_for_test(cwd, test_name, json_filepath)
end

return test_cmd, json_filepath
end

function M.build_test_command_for_dir(module_name)
--- The runner to use for running tests.
--- @type string
local runner = options.get().runner

-- TODO: if gotestsum, check if it is on $PATH, or fall back onto `go test`

--- 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.build_gotest_cmd_for_dir(module_name)
elseif runner == "gotestsum" then
json_filepath = vim.fs.normalize(async.fn.tempname())
test_cmd = M.build_gotestsum_cmd_for_dir(module_name, json_filepath)
end

return test_cmd, json_filepath
end

function M.build_gotest_cmd_for_dir(module_name)
local gotest = {
"go",
Expand Down
8 changes: 6 additions & 2 deletions lua/neotest-golang/parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ function M.results(spec, result, tree)
--- @type neotest.Position
local pos = tree:data()

--- The runner to use for running tests.
--- @type string
local runner = options.get().runner

--- The raw output from the test command.
--- @type table
local raw_output = {}
if options.get().runner == "go" then
if runner == "go" then
raw_output = async.fn.readfile(result.output)
elseif options.get().runner == "gotestsum" then
elseif runner == "gotestsum" then
raw_output = async.fn.readfile(spec.context.jsonfile)
end

Expand Down
22 changes: 2 additions & 20 deletions lua/neotest-golang/runspec_dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,7 @@ function M.build(pos)
end
end

--- The runner to use for running tests.
--- @type string
local runner = options.get().runner

-- TODO: if gotestsum, check if it is on $PATH, or fall back onto `go test`

--- 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 = cmd.build_gotest_cmd_for_dir(module_name)
elseif runner == "gotestsum" then
json_filepath = vim.fs.normalize(async.fn.tempname())
test_cmd = cmd.build_gotestsum_cmd_for_dir(module_name, json_filepath)
end
local test_cmd, json_filepath = cmd.build_test_command_for_dir(module_name)

return M.build_runspec(
pos,
Expand Down Expand Up @@ -127,6 +108,7 @@ end
--- @param cwd string
--- @param test_cmd table<string>
--- @param golist_output table
--- @param json_filepath string | nil
--- @return neotest.RunSpec | neotest.RunSpec[] | nil
function M.build_runspec(pos, cwd, test_cmd, golist_output, json_filepath)
--- @type neotest.RunSpec
Expand Down
29 changes: 4 additions & 25 deletions lua/neotest-golang/runspec_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,10 @@ function M.build(pos, strategy)
local test_name = convert.to_gotest_test_name(pos.id)
test_name = convert.to_gotest_regex_pattern(test_name)

--- The runner to use for running tests.
--- @type string
local runner = options.get().runner

-- TODO: if gotestsum, check if it is on $PATH, or fall back onto `go test`

--- 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 =
cmd.build_gotest_cmd_for_test(test_folder_absolute_path, test_name)
elseif runner == "gotestsum" then
json_filepath = vim.fs.normalize(async.fn.tempname())
test_cmd = cmd.build_gotestsum_cmd_for_test(
test_folder_absolute_path,
test_name,
json_filepath
)
end
local test_cmd, json_filepath = cmd.build_test_command_for_individual_test(
test_folder_absolute_path,
test_name
)

local runspec_strategy = nil
if strategy == "dap" then
Expand Down

0 comments on commit 6d2c7db

Please sign in to comment.