Skip to content

Commit

Permalink
feat: core test output parsing (#82)
Browse files Browse the repository at this point in the history
fixes #4
  • Loading branch information
fredrikaverpil authored Jul 3, 2024
1 parent c9e6fe5 commit e4d8020
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 118 deletions.
2 changes: 2 additions & 0 deletions lua/neotest-golang/ast.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--- Detect test names in Go *._test.go files.

local lib = require("neotest.lib")

local M = {}
Expand Down
2 changes: 2 additions & 0 deletions lua/neotest-golang/convert.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--- Converts one form of data to another.

local M = {}

-- Converts the test name into a regexp-friendly pattern, for usage in
Expand Down
7 changes: 3 additions & 4 deletions lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ local ast = require("neotest-golang.ast")
local runspec_dir = require("neotest-golang.runspec_dir")
local runspec_file = require("neotest-golang.runspec_file")
local runspec_test = require("neotest-golang.runspec_test")
local results_dir = require("neotest-golang.results_dir")
local results_test = require("neotest-golang.results_test")
local parse = require("neotest-golang.parse")

local M = {}

Expand Down Expand Up @@ -148,13 +147,13 @@ function M.Adapter.results(spec, result, tree)
if spec.context.pos_type == "dir" then
-- A test command executed a directory of tests and the output/status must
-- now be processed.
local results = results_dir.results(spec, result, tree)
local results = parse.results(spec, result, tree)
M.workaround_neotest_issue_391(result)
return results
elseif spec.context.pos_type == "test" then
-- A test command executed a single test and the output/status must now be
-- processed.
local results = results_test.results(spec, result, tree)
local results = parse.results(spec, result, tree)
M.workaround_neotest_issue_391(result)
return results
end
Expand Down
2 changes: 2 additions & 0 deletions lua/neotest-golang/json.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--- JSON processing helpers.

local M = {}

--- Process output from 'go test -json' and return an iterable table.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--- Parse the output from the go test command.

local async = require("neotest.async")

local options = require("neotest-golang.options")
Expand Down
111 changes: 0 additions & 111 deletions lua/neotest-golang/results_test.lua

This file was deleted.

3 changes: 3 additions & 0 deletions lua/neotest-golang/runspec_dir.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
--- Helpers to build the command and context around running all tests of
--- a Go module.

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

Expand Down
2 changes: 2 additions & 0 deletions lua/neotest-golang/runspec_file.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--- Helpers to build the command and context around running all tests of a file.

local M = {}

--- Build runspec for a directory.
Expand Down
25 changes: 22 additions & 3 deletions lua/neotest-golang/runspec_test.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
--- Helpers to build the command and context around running a single test.

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

local M = {}

Expand All @@ -9,11 +12,26 @@ local M = {}
--- @return neotest.RunSpec | neotest.RunSpec[] | nil
function M.build(pos, strategy)
--- @type string
local test_name = convert.to_gotest_test_name(pos.id)
test_name = convert.to_gotest_regex_pattern(test_name)
local test_folder_absolute_path = string.match(pos.path, "(.+)/")

-- call 'go list -json ./...' to get test file data
local go_list_command = {
"go",
"list",
"-json",
"./...",
}
local go_list_command_result = vim.fn.system(
"cd "
.. test_folder_absolute_path
.. " && "
.. table.concat(go_list_command, " ")
)
local golist_output = json.process_golist_output(go_list_command_result)

--- @type string
local test_folder_absolute_path = string.match(pos.path, "(.+)/")
local test_name = convert.to_gotest_test_name(pos.id)
test_name = convert.to_gotest_regex_pattern(test_name)

local gotest = {
"go",
Expand All @@ -37,6 +55,7 @@ function M.build(pos, strategy)
context = {
id = pos.id,
test_filepath = pos.path,
golist_output = golist_output,
pos_type = "test",
},
}
Expand Down

0 comments on commit e4d8020

Please sign in to comment.