Skip to content

Commit 06739a3

Browse files
committed
[WIP] Refactors test discovery
1 parent bea29a3 commit 06739a3

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

lua/neotest-swift-testing/init.lua

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local lib = require("neotest.lib")
22
local async = require("neotest.async")
33
local xml = require("neotest.lib.xml")
44
local util = require("neotest-swift-testing.util")
5+
local parser = require("neotest-swift-testing.parser")
56
local Path = require("plenary.path")
67
local logger = require("neotest-swift-testing.logging")
78
local filetype = require("plenary.filetype")
@@ -54,10 +55,6 @@ local treesitter_query = [[
5455
5556
]]
5657

57-
M.discover_positions = function(file_path)
58-
return lib.treesitter.parse_positions(file_path, treesitter_query, { nested_tests = true, require_namespaces = true })
59-
end
60-
6158
---@async
6259
---@param cmd string[]
6360
---@return string|nil
@@ -70,6 +67,65 @@ local function shell(cmd)
7067
return result.stdout
7168
end
7269

70+
---@param kind SwiftTesting.TestType
71+
---@return neotest.PositionType
72+
local function position_type(kind)
73+
if kind == "suite" then
74+
return "namespace"
75+
else
76+
return "test"
77+
end
78+
end
79+
80+
---@param record SwiftTesting.TestRecord
81+
---@return neotest.Position|nil
82+
local function position_from_record(record)
83+
if record.kind == "test" and record.payload ~= nil then
84+
---@type neotest.Position
85+
local pos = {
86+
id = record.payload.id,
87+
name = record.payload.name,
88+
path = record.payload.sourceLocation._filePath,
89+
type = position_type(record.payload.kind),
90+
range = {
91+
record.payload.sourceLocation.line,
92+
record.payload.sourceLocation.column,
93+
record.payload.sourceLocation.line,
94+
record.payload.sourceLocation.column,
95+
},
96+
}
97+
return pos
98+
end
99+
return nil
100+
end
101+
102+
---@async
103+
---@param file_path string
104+
---@return neotest.Tree
105+
M.discover_positions = function(file_path)
106+
local output = async.fn.tempname() .. "test-events.jsonl"
107+
shell({ "swift", "test", "list", "--enable-swift-testing", "--event-stream-output-path", output })
108+
local lines = lib.files.read_lines(output)
109+
110+
---@type neotest.Position[]
111+
local positions = {}
112+
for _, line in ipairs(lines) do
113+
local parsed = parser.parse(line)
114+
if parsed ~= nil and parsed.kind == "test" then
115+
local pos = position_from_record(parsed)
116+
if pos ~= nil then
117+
table.insert(positions, pos)
118+
end
119+
end
120+
end
121+
local Tree = require("neotest.types").Tree
122+
logger.debug("Discovered positions: " .. vim.inspect(positions))
123+
return Tree.from_list(positions, function(p)
124+
--return lib.treesitter.parse_positions(file_path, treesitter_query, { nested_tests = true, require_namespaces = true })
125+
return p.id
126+
end)
127+
end
128+
73129
---Removes new line characters
74130
---@param str string
75131
---@return string

spec/plugin_spec.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,18 @@ describe("Swift testing adapter", function()
425425
}, sut.results(spec, given_strategy_result(0, "/outputpath/log"), tree))
426426
end)
427427
end)
428+
429+
describe("Test discovery", function()
430+
it("discovers tests", function()
431+
local path = vim.fn.getcwd() .. "/spec/fixtures/Tests/TargetTests/TargetTests.swift"
432+
local output = "/temporary/path/test-events.jsonl"
433+
local entry = [[
434+
{"kind":"test","payload":{"id":"MyPackageTests.example()\/MyPackageTests.swift:4:2","isParameterized":false,"kind":"function","name":"example()","sourceLocation":{"_filePath":"\/Users\/user\/MyPackage\/Tests\/MyPackageTests\/MyPackageTests.swift","column":2,"fileID":"MyPackageTests\/MyPackageTests.swift","line":4}},"version":0}
435+
]]
436+
given("swift test list --enable-swift-testing --event-stream-output-path " .. output, "", 0)
437+
given_file(output, entry)
438+
439+
assert.are.same({}, sut.discover_positions(path))
440+
end)
441+
end)
428442
end)

0 commit comments

Comments
 (0)