Skip to content

Commit

Permalink
test: golist key/values used by adapter (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil authored Jul 13, 2024
1 parent 305e33b commit 86bb540
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function M.golist_data(cwd)
}
local output =
vim.fn.system("cd " .. cwd .. " && " .. table.concat(go_list_command, " "))
return json.process_golist_output(output)
return json.decode_from_string(output)
end

function M.test_command_in_package(package_or_path)
Expand Down
33 changes: 12 additions & 21 deletions lua/neotest-golang/lib/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

local M = {}

--- Process output from 'go test -json' and return an iterable table.
--- @param raw_output table
--- Decode JSON from a table of strings into a table of objects.
--- @param tbl table
--- @return table
function M.process_gotest_json_output(raw_output)
function M.decode_from_table(tbl)
local jsonlines = {}
for _, line in ipairs(raw_output) do
for _, line in ipairs(tbl) do
if string.match(line, "^%s*{") then -- must start with the `{` character
local status, json_data = pcall(vim.fn.json_decode, line)
if status then
Expand All @@ -23,31 +23,22 @@ function M.process_gotest_json_output(raw_output)
return jsonlines
end

--- Process output from 'go list -json' an iterable lua table.
--- @param raw_output string
--- Decode JSON from a string into a table of objects.
--- @param str string
--- @return table
function M.process_golist_output(raw_output)
function M.decode_from_string(str)
-- Split the input into separate JSON objects
local json_objects = {}
local tbl = {}
local current_object = ""
for line in raw_output:gmatch("[^\r\n]+") do
for line in str:gmatch("[^\r\n]+") do
if line:match("^%s*{") and current_object ~= "" then
table.insert(json_objects, current_object)
table.insert(tbl, current_object)
current_object = ""
end
current_object = current_object .. line
end
table.insert(json_objects, current_object)

-- Parse each JSON object
local objects = {}
for _, json_object in ipairs(json_objects) do
local obj = vim.fn.json_decode(json_object)
table.insert(objects, obj)
end

-- Return the table of objects
return objects
table.insert(tbl, current_object)
return M.decode_from_table(tbl)
end

return M
2 changes: 1 addition & 1 deletion lua/neotest-golang/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function M.test_results(spec, result, tree)
raw_output = async.fn.readfile(context.test_output_json_filepath)
end

local gotest_output = lib.json.process_gotest_json_output(raw_output)
local gotest_output = lib.json.decode_from_table(raw_output)

--- The 'go list -json' output, converted into a lua table.
local golist_output = context.golist_data
Expand Down
92 changes: 92 additions & 0 deletions tests/unit/golist_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
local _ = require("plenary")

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

describe("go list output", function()
it("contains expected keys/values", function()
local tests_filepath = vim.loop.cwd() .. "/tests/go"
local output = lib.cmd.golist_data(tests_filepath)
local first_entry = output[1]
local expected = {

Deps = {
"cmp",
"errors",
"fmt",
"internal/abi",
"internal/bytealg",
"internal/chacha8rand",
"internal/coverage/rtcov",
"internal/cpu",
"internal/fmtsort",
"internal/goarch",
"internal/godebugs",
"internal/goexperiment",
"internal/goos",
"internal/itoa",
"internal/oserror",
"internal/poll",
"internal/race",
"internal/reflectlite",
"internal/safefilepath",
"internal/syscall/execenv",
"internal/syscall/unix",
"internal/testlog",
"internal/unsafeheader",
"io",
"io/fs",
"math",
"math/bits",
"os",
"path",
"reflect",
"runtime",
"runtime/internal/atomic",
"runtime/internal/math",
"runtime/internal/sys",
"slices",
"sort",
"strconv",
"sync",
"sync/atomic",
"syscall",
"time",
"unicode",
"unicode/utf8",
"unsafe",
},
Dir = tests_filepath,
GoFiles = { "main.go" },
ImportPath = "github.com/fredrikaverpil/neotest-golang",
Imports = { "fmt" },
Match = { "./..." },
Module = {
Dir = tests_filepath,
GoMod = tests_filepath .. "/go.mod",
GoVersion = "1.22.2",
Main = true,
Path = "github.com/fredrikaverpil/neotest-golang",
},
Name = "main",
Root = tests_filepath,
Stale = true,
StaleReason = "build ID mismatch",
Target = "/Users/fredrik/go/bin/neotest-golang",
TestGoFiles = { "positions_test.go", "testname_test.go" },
TestImports = { "testing" },
}

-- ignored keys, as they might differ between OS/CI/platforms/too often
expected.Module.GoVersion = nil
first_entry.Module.GoVersion = nil
expected.Deps = nil
first_entry.Deps = nil
expected.StaleReason = nil
first_entry.StaleReason = nil
expected.Target = nil
first_entry.Target = nil

assert.are_same(vim.inspect(expected), vim.inspect(first_entry))
assert.are_same(expected, first_entry)
end)
end)
8 changes: 4 additions & 4 deletions tests/unit/json_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("Go list", function()
local expected = { { Dir = "foo" } }
assert.are_same(
vim.inspect(expected),
vim.inspect(lib.json.process_golist_output(input))
vim.inspect(lib.json.decode_from_string(input))
)
end)

Expand All @@ -26,7 +26,7 @@ describe("Go list", function()
local expected = { { Dir = "foo" }, { Dir = "bar" } }
assert.are_same(
vim.inspect(expected),
vim.inspect(lib.json.process_golist_output(input))
vim.inspect(lib.json.decode_from_string(input))
)
end)

Expand All @@ -45,7 +45,7 @@ describe("Go list", function()
local expected = { { Dir = "foo" }, { Dir = "bar" }, { Dir = "baz" } }
assert.are_same(
vim.inspect(expected),
vim.inspect(lib.json.process_golist_output(input))
vim.inspect(lib.json.decode_from_string(input))
)
end)
it("Returns nested entries", function()
Expand Down Expand Up @@ -77,7 +77,7 @@ describe("Go list", function()
}
assert.are_same(
vim.inspect(expected),
vim.inspect(lib.json.process_golist_output(input))
vim.inspect(lib.json.decode_from_string(input))
)
end)
end)

0 comments on commit 86bb540

Please sign in to comment.