Skip to content

Commit

Permalink
feat(poc): gotestsum for readable output when attached
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 3, 2024
1 parent d9b0bb2 commit b7d3cb1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 29 deletions.
8 changes: 3 additions & 5 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
local M = {}

local opts = {
go_test_args = {
"-v",
"-race",
"-count=1",
},
runner = "go", -- or "gotestsum"
go_test_args = { "-v", "-race", "-count=1" },
gotestsum_args = { "--format=standard-verbose" },
dap_go_enabled = false,
dap_go_opts = {},
warn_test_name_dupes = true,
Expand Down
9 changes: 7 additions & 2 deletions lua/neotest-golang/parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ function M.results(spec, result, tree)
--- @type neotest.Position
local pos = tree:data()

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

--- The 'go test' JSON output, converted into a lua table.
--- @type table
Expand Down
50 changes: 39 additions & 11 deletions lua/neotest-golang/runspec_dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

local M = {}

Expand Down Expand Up @@ -114,22 +115,45 @@ end
--- @param module_name string
--- @return neotest.RunSpec | neotest.RunSpec[] | nil
function M.build_dir_test_runspec(pos, cwd, golist_output, module_name)
local gotest = {
"go",
"test",
"-json",
}

--- @type table
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 gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args)
local gotest_command = {}
local jsonfile = ""

if options.get().runner == "go" then
local gotest = {
"go",
"test",
"-json",
}

local combined_args = vim.list_extend(
vim.deepcopy(options.get().go_test_args),
required_go_test_args
)
gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args)
elseif options.get().runner == "gotestsum" then
jsonfile = vim.fs.normalize(async.fn.tempname())
local gotest = { "gotestsum" }
local gotestsum_json = {
"--jsonfile=" .. jsonfile,
"--",
}
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)

gotest_command = cmd
end

--- @type neotest.RunSpec
local run_spec = {
Expand All @@ -143,6 +167,10 @@ function M.build_dir_test_runspec(pos, cwd, golist_output, module_name)
},
}

if jsonfile ~= nil then
run_spec.context.jsonfile = jsonfile
end

return run_spec
end

Expand Down
50 changes: 39 additions & 11 deletions lua/neotest-golang/runspec_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
local convert = require("neotest-golang.convert")
local options = require("neotest-golang.options")
local json = require("neotest-golang.json")
local async = require("neotest.async")

local M = {}

Expand Down Expand Up @@ -33,20 +34,43 @@ function M.build(pos, strategy)
local test_name = convert.to_gotest_test_name(pos.id)
test_name = convert.to_gotest_regex_pattern(test_name)

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

--- @type table
local required_go_test_args = { test_folder_absolute_path, "-run", test_name }

local combined_args = vim.list_extend(
vim.deepcopy(options.get().go_test_args),
required_go_test_args
)
local gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args)
local gotest_command = {}
local jsonfile = ""

if options.get().runner == "go" then
local gotest = {
"go",
"test",
"-json",
}

local combined_args = vim.list_extend(
vim.deepcopy(options.get().go_test_args),
required_go_test_args
)
gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args)
elseif options.get().runner == "gotestsum" then
jsonfile = vim.fs.normalize(async.fn.tempname())
local gotest = { "gotestsum" }
local gotestsum_json = {
"--jsonfile=" .. jsonfile,
"--",
}
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)

gotest_command = cmd
end

--- @type neotest.RunSpec
local run_spec = {
Expand All @@ -60,6 +84,10 @@ function M.build(pos, strategy)
},
}

if jsonfile ~= nil then
run_spec.context.jsonfile = jsonfile
end

-- set up for debugging of test
if strategy == "dap" then
run_spec.strategy = M.get_dap_config(test_name)
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ describe("Options are set up", function()
local expected_options = {
dap_go_enabled = false,
dap_go_opts = {},
runner = "go",
go_test_args = {
"-v",
"-race",
"-count=1",
},
gotestsum_args = { "--format=standard-verbose" },
warn_test_name_dupes = true,
warn_test_not_executed = true,
dev_notifications = false,
Expand All @@ -22,12 +24,14 @@ describe("Options are set up", function()
local expected_options = {
dap_go_enabled = false,
dap_go_opts = {},
runner = "go",
go_test_args = {
"-v",
"-race",
"-count=1",
"-parallel=1", -- non-default
},
gotestsum_args = { "--format=standard-verbose" },
warn_test_name_dupes = true,
warn_test_not_executed = true,
dev_notifications = false,
Expand Down

0 comments on commit b7d3cb1

Please sign in to comment.