-
Notifications
You must be signed in to change notification settings - Fork 27
/
init.lua
436 lines (379 loc) · 12.8 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
local async = require("neotest.async")
local context_manager = require("plenary.context_manager")
local dap = require("neotest-rust.dap")
local util = require("neotest-rust.util")
local errors = require("neotest-rust.errors")
local Job = require("plenary.job")
local open = context_manager.open
local Path = require("plenary.path")
local lib = require("neotest.lib")
local with = context_manager.with
local xml = require("neotest.lib.xml")
local adapter = { name = "neotest-rust" }
local cargo_metadata = setmetatable({}, {
__call = function(self, cwd)
local metadata = self[cwd]
if metadata ~= nil then
return metadata
else
Job:new({
command = "cargo",
args = { "metadata", "--no-deps" },
cwd = cwd,
on_exit = function(j, return_val)
metadata = vim.json.decode(j:result()[1])
end,
}):sync()
self[cwd] = metadata
return metadata
end
end,
})
---Find the project root directory given a current directory to work from.
---Should no root be found, the adapter can still be used in a non-project context if a test file matches.
---@async
---@param dir string @Directory to treat as cwd
---@return string | nil @Absolute root dir of test suite
function adapter.root(dir)
local cwd = lib.files.match_root_pattern("Cargo.toml")(dir)
if cwd == nil then
return
end
return cargo_metadata(cwd).workspace_root
end
local package_name_by_root = function(package_root)
local manifest_path = package_root .. "Cargo.toml"
local metadata = cargo_metadata(package_root)
return vim.tbl_filter(function(p)
return p.manifest_path == manifest_path
end, metadata.packages)[1].name
end
local get_args = function()
return {}
end
local get_dap_adapter = function()
return "codelldb"
end
local is_callable = function(obj)
return type(obj) == "function" or (type(obj) == "table" and obj.__call)
end
---@async
---@param file_path string
---@return boolean
function adapter.is_test_file(file_path)
return vim.endswith(file_path, ".rs") and #adapter.discover_positions(file_path):to_list() ~= 1
end
---Filter directories when searching for test files
---@async
---@param name string Name of directory
---@param rel_path string Path to directory, relative to root
---@param root string Root directory of project
---@return boolean
function adapter.filter_dir(name, rel_path, root)
return root .. Path.path.sep .. rel_path ~= cargo_metadata(root).target_directory
end
local get_package_root = lib.files.match_root_pattern("Cargo.toml")
local function is_unit_test(path)
return vim.startswith(path, get_package_root(path) .. Path.path.sep .. "src" .. Path.path.sep)
end
local function is_integration_test(path)
return vim.startswith(path, get_package_root(path) .. Path.path.sep .. "tests" .. Path.path.sep)
end
local function is_alternate_binary(path)
return vim.startswith(
path,
get_package_root(path) .. Path.path.sep .. "src" .. Path.path.sep .. "bin" .. Path.path.sep
)
end
local function path_to_test_path(path)
local root = get_package_root(path)
-- main.rs, lib.rs, and mod.rs aren't part of the test name
for _, filename in ipairs({ "main", "lib", "mod" }) do
path = path:gsub(filename .. ".rs$", "")
end
-- Trim '.rs'
path = path:gsub(".rs$", "")
if is_unit_test(path) then
if is_alternate_binary(path) then
return nil
end
path = Path:new(path)
path = path:make_relative(root .. Path.path.sep .. "src")
else
path = Path:new(path)
path = path:make_relative(root .. Path.path.sep .. "tests")
-- Remove the first component of the path of an integration test in a
-- subdirectory, e.g. 'testsuite/foo/bar.rs' becomes 'foo/bar.rs'
if path:find(Path.path.sep) then
path = path:gsub("^.+" .. Path.path.sep, "")
else
return nil
end
end
-- Replace separators with '::'
path = path:gsub(Path.path.sep, "::")
-- If the file was main.rs, lib.rs, or mod.rs, the relative path will
-- be "." after we strip the filename.
if path == "." then
return nil
else
return path
end
end
local function integration_test_name(path)
local package_root = get_package_root(path)
path = Path:new(path)
path = path:make_relative(package_root .. Path.path.sep .. "tests")
path = path:gsub(".rs$", "")
return vim.split(path, "/")[1]
end
local function binary_name(path)
local package_root = get_package_root(path)
path = Path:new(path)
path = path:make_relative(package_root .. Path.path.sep .. "src" .. Path.path.sep .. "bin")
path = path:gsub(".rs$", "")
return vim.split(path, "/")[1]
end
---Given a file path, parse all the tests within it.
---@async
---@param path string Absolute file path
---@return neotest.Tree | nil
function adapter.discover_positions(path)
local query = [[;; query
(
(attribute_item
[
(attribute
(identifier) @macro_name
)
(attribute
[
(identifier) @macro_name
(scoped_identifier
name: (identifier) @macro_name
)
]
)
]
)
[
(attribute_item
(attribute
(identifier)
)
)
(line_comment)
]*
.
(function_item
name: (identifier) @test.name
) @test.definition
(#any-of? @macro_name "test" "rstest" "case")
)
(mod_item name: (identifier) @namespace.name)? @namespace.definition
]]
return lib.treesitter.parse_positions(path, query, {
require_namespaces = false,
position_id = function(position, namespaces)
return table.concat(
vim.tbl_flatten({
path_to_test_path(path),
vim.tbl_map(function(pos)
return pos.name
end, namespaces),
position.name,
}),
"::"
)
end,
})
end
---@param args neotest.RunArgs
---@return nil | neotest.RunSpec | neotest.RunSpec[]
function adapter.build_spec(args)
local tmp_nextest_config = async.fn.tempname() .. ".nextest.toml"
local junit_path = async.fn.tempname() .. ".junit.xml"
local position = args.tree:data()
local cwd = adapter.root(position.path)
local nextest_config = Path:new(cwd .. ".config/nextest.toml")
if nextest_config:exists() then
nextest_config:copy({ destination = tmp_nextest_config })
end
with(open(tmp_nextest_config, "a"), function(writer)
writer:write("[profile.neotest.junit]\npath = '" .. junit_path .. "'")
end)
local command = vim.tbl_flatten({
"cargo",
"nextest",
"run",
"--workspace",
"--no-fail-fast",
"--config-file",
tmp_nextest_config,
"--profile",
"neotest",
vim.list_extend(get_args(), args.extra_args or {}),
})
if is_integration_test(position.path) then
vim.list_extend(command, { "--test", integration_test_name(position.path) })
end
if is_alternate_binary(position.path) then
vim.list_extend(command, { "--bin", binary_name(position.path) })
end
-- Determine the package name if we're in a workspace
local workspace_root = adapter.root(position.path) .. Path.path.sep
local package_root = lib.files.match_root_pattern("Cargo.toml")(position.path)
local belongs_to_workspace = (package_root:sub(0, #workspace_root) == workspace_root)
local package_name = belongs_to_workspace and package_name_by_root(package_root .. Path.path.sep)
local package_filter = ""
if package_name then
package_filter = "package(" .. package_name .. ") & "
end
local position_id
local test_filter
if position.type == "test" then
position_id = position.id
-- TODO: Support rstest parametrized tests
test_filter = "-E " .. vim.fn.shellescape(package_filter .. "test(/^" .. position_id .. "$/)")
elseif position.type == "file" then
if package_name then
-- A basic filter to run tests within the package that will be
-- overridden later if 'position_id' is not nil
test_filter = "-E " .. vim.fn.shellescape("package(" .. package_name .. ")")
end
position_id = path_to_test_path(position.path)
if is_unit_test(position.path) and position_id == nil then
-- main.rs or lib.rs
position_id = "tests"
end
if position_id then
-- Either a unit test or an integration test in a subdirectory
test_filter = "-E " .. vim.fn.shellescape(package_filter .. "test(/^" .. position_id .. "::/)")
end
end
table.insert(command, test_filter)
local context = {
junit_path = junit_path,
file = position.path,
test_filter = test_filter,
position_id = position_id,
strategy = args.strategy,
}
-- Debug
if args.strategy == "dap" then
local dap_args = { "--nocapture" }
if position.type == "test" then
context.test_filter = position.id
table.insert(dap_args, "--exact")
else
position_id = path_to_test_path(position.path)
if position_id == nil then
context.test_filter = "tests"
else
context.test_filter = position_id
end
end
table.insert(dap_args, context.test_filter)
local strategy = {
name = "Debug Rust Tests",
type = get_dap_adapter(),
request = "launch",
cwd = cwd or "${workspaceFolder}",
stopOnEntry = false,
args = dap_args,
program = dap.get_test_binary(cwd, position.path),
}
-- codelldb must be provided with a file for stdout in its launch parameters.
-- https://github.com/vadimcn/codelldb/blob/v1.9.0/MANUAL.md#stdio-redirection
if get_dap_adapter() == "codelldb" then
strategy["stdio"] = { nil, async.fn.tempname() }
end
return {
cwd = cwd,
context = context,
strategy = strategy,
}
end
-- Run
return {
command = table.concat(command, " "),
cwd = cwd,
context = context,
}
end
---@async
---@param spec neotest.RunSpec
---@param result neotest.StrategyResult
---@param tree neotest.Tree
---@return table<string, neotest.Result>
function adapter.results(spec, result, tree)
---@type table<string, neotest.Result>
local results = {}
local output_path = spec.strategy.stdio and spec.strategy.stdio[2] or result.output
if util.file_exists(spec.context.junit_path) then
local data
with(open(spec.context.junit_path, "r"), function(reader)
data = reader:read("*a")
end)
local root = xml.parse(data)
local testsuites
if root.testsuites.testsuite == nil then
testsuites = {}
elseif #root.testsuites.testsuite == 0 then
testsuites = { root.testsuites.testsuite }
else
testsuites = root.testsuites.testsuite
end
for _, testsuite in pairs(testsuites) do
local testcases
if #testsuite.testcase == 0 then
testcases = { testsuite.testcase }
else
testcases = testsuite.testcase
end
for _, testcase in pairs(testcases) do
if testcase.failure then
local output = testcase.failure[1]
results[testcase._attr.name] = {
status = "failed",
short = output,
errors = errors.parse_errors(output),
}
else
results[testcase._attr.name] = {
status = "passed",
}
end
end
end
elseif spec.context.strategy == "dap" and util.file_exists(output_path) then
results = dap.translate_results(output_path)
else
local output = result.output
results[spec.context.position_id] = {
status = "failed",
output = output,
}
end
return results
end
setmetatable(adapter, {
__call = function(_, opts)
if is_callable(opts.args) then
get_args = opts.args
elseif opts.args then
get_args = function()
return opts.args
end
end
if is_callable(opts.dap_adapter) then
get_dap_adapter = opts.dap_adapter
elseif opts.dap_adapter then
get_dap_adapter = function()
return opts.dap_adapter
end
end
return adapter
end,
})
return adapter