Skip to content

Commit 247fe05

Browse files
committed
[WIP] Adaptions for Linux
1 parent ff255c5 commit 247fe05

File tree

1 file changed

+100
-29
lines changed

1 file changed

+100
-29
lines changed

lua/neotest-swift-testing/init.lua

Lines changed: 100 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,42 @@ local Path = require("plenary.path")
66
local logger = require("neotest-swift-testing.logging")
77
local filetype = require("plenary.filetype")
88
local files = require("neotest.lib.file")
9+
local nio = require("nio")
10+
11+
---@alias Platform
12+
---| 'Linux'
13+
---| 'macOS'
14+
---| 'Windows'
15+
16+
---Get os
17+
---@return Platform|nil
18+
local function get_os()
19+
local os_type = vim.loop.os_uname().sysname
20+
if os_type == "Linux" then
21+
return "Linux"
22+
elseif os_type == "Darwin" then
23+
return "macOS"
24+
elseif os_type == "Windows_NT" then
25+
return "Windows"
26+
else
27+
return nil
28+
end
29+
end
30+
31+
---Helper for executing external commands
32+
---@param cmd string[]
33+
---@param on_stdout fun(err: string?, data: string?)|nil
34+
---@param on_exit fun(obj: vim.SystemCompleted)
35+
---@return vim.SystemObj|nil
36+
local function run_job(cmd, on_stdout, on_exit)
37+
-- print("Running command: " .. table.concat(cmd, " "))
38+
return vim.system(cmd, {
39+
text = true,
40+
stdout = on_stdout or true,
41+
}, on_exit)
42+
end
43+
44+
local run_job_async = nio.wrap(run_job, 3)
945

1046
local M = {
1147
name = "neotest-swift-testing",
@@ -61,14 +97,27 @@ local treesitter_query = [[
6197
---@param cmd string[]
6298
---@return string|nil
6399
local function shell(cmd)
64-
local code, result = lib.process.run(cmd, { stdout = true, stderr = true })
65-
if code ~= 0 or result.stderr ~= nil or result.stdout == nil then
66-
logger.error("Failed to run command: " .. vim.inspect(cmd) .. " " .. result.stderr)
100+
local result = run_job_async(cmd, nil) -- lib.process.run(cmd, { stdout = true, stderr = true })
101+
if result == nil or result.code ~= 0 or result.stderr ~= "" or result.stdout == nil then
102+
logger.error("Failed to run command: " .. vim.inspect(cmd) .. " " .. vim.inspect(result))
67103
return nil
68104
end
69105
return result.stdout
70106
end
71107

108+
---@async
109+
---@return string|nil
110+
local function get_dyld_path()
111+
local os = get_os()
112+
if os == "Linux" then
113+
return shell({ "swiftly", "use", "-p" })
114+
elseif os == "macOS" then
115+
return shell({ "xcrun", "--show-sdk-platform-path" }) or ""
116+
else
117+
return nil
118+
end
119+
end
120+
72121
---@param kind SwiftTesting.TestType
73122
---@return neotest.PositionType
74123
local function position_type(kind)
@@ -139,6 +188,7 @@ end
139188
---@return string|nil
140189
local function get_dap_cmd()
141190
-- TODO: use swiftly
191+
-- local result = shell({"swiftly", "use", "-p"})
142192
local result = shell({ "xcode-select", "-p" })
143193
if not result then
144194
return nil
@@ -170,33 +220,54 @@ end
170220
---@param dap_args? table
171221
---@return table|nil
172222
local function get_dap_config(test_name, dap_args)
173-
local program = get_dap_cmd()
174-
if program == nil then
175-
logger.error("Failed to get the spm test helper path")
176-
return nil
177-
end
178-
local executable = get_test_executable()
179-
if not executable then
180-
logger.error("Failed to get the test executable path")
223+
local os = get_os()
224+
if os == "Linux" then
225+
local executable = get_test_executable()
226+
if not executable then
227+
logger.error("Failed to get the test executable path")
228+
return nil
229+
end
230+
return vim.tbl_extend("force", dap_args or {}, {
231+
name = "Swift Test debugger",
232+
type = "lldb",
233+
request = "attach",
234+
program = "swift-test",
235+
-- waitFor = true,
236+
cwd = "${workspaceFolder}",
237+
stopOnEntry = false,
238+
})
239+
elseif os == "macOS" then
240+
local program = get_dap_cmd()
241+
if program == nil then
242+
logger.error("Failed to get the spm test helper path")
243+
return nil
244+
end
245+
local executable = get_test_executable()
246+
if not executable then
247+
logger.error("Failed to get the test executable path")
248+
return nil
249+
end
250+
return vim.tbl_extend("force", dap_args or {}, {
251+
name = "Swift Test debugger",
252+
type = "lldb",
253+
request = "launch",
254+
program = program,
255+
args = {
256+
"--test-bundle-path",
257+
executable,
258+
"--testing-library",
259+
"swift-testing",
260+
"--enable-swift-test",
261+
"--filter",
262+
test_name,
263+
},
264+
cwd = "${workspaceFolder}",
265+
stopOnEntry = false,
266+
})
267+
else
268+
logger.debug("Unsupported OS")
181269
return nil
182270
end
183-
return vim.tbl_extend("force", dap_args or {}, {
184-
name = "Swift Test debugger",
185-
type = "lldb",
186-
request = "launch",
187-
program = program,
188-
args = {
189-
"--test-bundle-path",
190-
executable,
191-
"--testing-library",
192-
"swift-testing",
193-
"--enable-swift-test",
194-
"--filter",
195-
test_name,
196-
},
197-
cwd = "${workspaceFolder}",
198-
stopOnEntry = false,
199-
})
200271
end
201272

202273
---@async
@@ -275,7 +346,7 @@ function M.build_spec(args)
275346
logger.error("Failed to build test bundle.")
276347
return nil
277348
end
278-
local path = shell({ "xcrun", "--show-sdk-platform-path" }) or ""
349+
local path = get_dyld_path() or ""
279350
return {
280351
cwd = cwd,
281352
context = { is_dap_active = true, position_id = position.id },

0 commit comments

Comments
 (0)