Skip to content

Commit ee99f79

Browse files
authored
fix(testables): support --nocapture when using nextest (#877)
1 parent 550b778 commit ee99f79

File tree

3 files changed

+120
-7
lines changed

3 files changed

+120
-7
lines changed

.luacheckrc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
ignore = {
2-
"631", -- max_line_length
3-
"122", -- read-only field of global variable
2+
"631", -- max_line_length
3+
"122", -- read-only field of global variable
44
}
55
read_globals = {
66
"vim",
77
"describe",
88
"it",
9-
"assert"
9+
"assert",
10+
"setup",
11+
"teardown",
1012
}

lua/rustaceanvim/overrides.lua

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ function M.maybe_nextest_transform(args)
7373
table.insert(nextest_args, '--config-file')
7474
table.insert(nextest_args, require('rustaceanvim.cache').nextest_config_path())
7575

76-
-- tranform `-- test_something --exact` into `-E 'test("test_something")'`
76+
-- tranform:
77+
-- - `-- --exact foo` -> `-- foo`
7778
for i = 1, #executable_args do
7879
if executable_args[i] == '--exact' then
7980
local test_name = executable_args[i - 1]
@@ -84,20 +85,34 @@ function M.maybe_nextest_transform(args)
8485
end
8586
end
8687

88+
-- these flags are unsupported by cargo-nextest and should be removed.
89+
---@type table<string, true>
8790
local nextest_unsupported_flags = {
88-
'--show-output',
91+
['--show-output'] = true,
92+
}
93+
-- cargo test passes these flags to the test executable,
94+
-- while cargo-nextest expects them to be passed to the test runner executable.
95+
---@type table<string, true>
96+
local move_to_nextest_args_flags = {
97+
['--nocapture'] = true,
8998
}
9099
local indexes_to_remove_reverse_order = {}
91100
for i, arg in ipairs(executable_args) do
92-
if vim.list_contains(nextest_unsupported_flags, arg) then
101+
if nextest_unsupported_flags[arg] then
102+
table.insert(indexes_to_remove_reverse_order, 1, i)
103+
end
104+
if move_to_nextest_args_flags[arg] then
93105
table.insert(indexes_to_remove_reverse_order, 1, i)
106+
table.insert(nextest_args, arg)
94107
end
95108
end
96109
for _, i in pairs(indexes_to_remove_reverse_order) do
97110
table.remove(executable_args, i)
98111
end
99112

100-
table.insert(nextest_args, '--')
113+
if #executable_args > 0 then
114+
table.insert(nextest_args, '--')
115+
end
101116
for _, v in ipairs(executable_args) do
102117
table.insert(nextest_args, v)
103118
end

spec/nextest_spec.lua

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
local overrides = require('rustaceanvim.overrides')
2+
local maybe_nextest_transform = overrides.maybe_nextest_transform
3+
4+
local orig_vim_fn_executable = vim.fn.executable
5+
6+
setup(function()
7+
---@diagnostic disable-next-line: duplicate-set-field
8+
vim.fn.executable = function(expr)
9+
if expr == 'cargo-nextest' then
10+
return 1
11+
end
12+
return orig_vim_fn_executable(expr)
13+
end
14+
end)
15+
16+
teardown(function()
17+
vim.fn.executable = orig_vim_fn_executable
18+
end)
19+
20+
describe('nextest', function()
21+
describe('`cargo test <target>` -> `cargo nextest run <target>`', function()
22+
local args = {
23+
'test',
24+
'tests::my_test_target',
25+
}
26+
local transformed_args = maybe_nextest_transform(args)
27+
assert.are.same({
28+
'nextest',
29+
'run',
30+
'tests::my_test_target',
31+
}, { unpack(transformed_args, 1, 3) })
32+
end)
33+
describe('`-- --exact <target>` -> `-- <target>`', function()
34+
local args = {
35+
'test',
36+
'tests::my_test_target',
37+
'--',
38+
'foo',
39+
'--exact',
40+
}
41+
local transformed_args = maybe_nextest_transform(args)
42+
assert.False(vim.tbl_contains(transformed_args, '--exact'))
43+
assert.True(vim.tbl_contains(transformed_args, 'foo'))
44+
end)
45+
describe('removes unsupported `--show-output`', function()
46+
local args = {
47+
'test',
48+
'tests::my_test_target',
49+
'--',
50+
'--show-output',
51+
}
52+
local transformed_args = maybe_nextest_transform(args)
53+
assert.False(vim.tbl_contains(transformed_args, '--show-output'))
54+
end)
55+
describe('`-- foo --nocapture` -> `--nocapture -- foo`', function()
56+
local args = {
57+
'test',
58+
'tests::my_test_target',
59+
'--',
60+
'foo',
61+
'--nocapture',
62+
}
63+
local transformed_args = maybe_nextest_transform(args)
64+
assert.are.same({
65+
'nextest',
66+
'run',
67+
'tests::my_test_target',
68+
'--profile',
69+
'rustaceanvim',
70+
'--config-file',
71+
require('rustaceanvim.cache').nextest_config_path(),
72+
'--nocapture',
73+
'--',
74+
'foo',
75+
}, transformed_args)
76+
end)
77+
describe('`-- --nocapture` -> `--nocapture`', function()
78+
local args = {
79+
'test',
80+
'tests::my_test_target',
81+
'--',
82+
'--nocapture',
83+
}
84+
local transformed_args = maybe_nextest_transform(args)
85+
assert.are.same({
86+
'nextest',
87+
'run',
88+
'tests::my_test_target',
89+
'--profile',
90+
'rustaceanvim',
91+
'--config-file',
92+
require('rustaceanvim.cache').nextest_config_path(),
93+
'--nocapture',
94+
}, transformed_args)
95+
end)
96+
end)

0 commit comments

Comments
 (0)