forked from olimorris/neotest-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
277 lines (238 loc) · 7.74 KB
/
init.lua
File metadata and controls
277 lines (238 loc) · 7.74 KB
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
local lib = require("neotest.lib")
local async = require("neotest.async")
local logger = require("neotest.logging")
local utils = require("neotest-rspec.utils")
local config = require("neotest-rspec.config")
---@class neotest.Adapter
---@field name string
local NeotestAdapter = { name = "neotest-rspec" }
---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 NeotestAdapter.root(dir)
local result = nil
for _, root_file in ipairs(config.get_root_files()) do
result = lib.files.match_root_pattern(root_file)(dir)
if result then break end
end
return result
end
---@async
---@param file_path string
---@return boolean
function NeotestAdapter.is_test_file(file_path)
return vim.endswith(file_path, "_spec.rb")
end
---Filter directories when searching for test files
---@async
---@param name string Name of directory
---@return boolean
function NeotestAdapter.filter_dir(name)
for _, filter_dir in ipairs(config.get_filter_dirs()) do
if name == filter_dir then return false end
end
return true
end
---Given a file path, parse all the tests within it.
---@async
---@param path string Absolute file path
---@return neotest.Tree | nil
function NeotestAdapter.discover_positions(path)
local query = [[
((call
method: (identifier) @func_name (#match? @func_name "^(describe|shared_examples|context|feature)$")
arguments: (argument_list (_) @namespace.name)
)) @namespace.definition
((call
method: (identifier) @namespace.name (#match? @namespace.name "^(describe|shared_examples|context|feature)$")
.
block: (_)
)) @namespace.definition
((call
method: (identifier) @func_name (#match? @func_name "^(it|its|specify)$")
block: (block (_) @test.name)
)) @test.definition
((call
method: (identifier) @func_name (#match? @func_name "^(it|its|specify)$")
block: (do_block (_) @test.name)
!arguments
)) @test.definition
((call
method: (identifier) @func_name (#match? @func_name "^(it|its|scenario|include_examples|it_behaves_like)$")
arguments: (argument_list (_) @test.name)
)) @test.definition
]]
return lib.treesitter.parse_positions(path, query, {
nested_tests = true,
require_namespaces = true,
position_id = "require('neotest-rspec.utils').generate_treesitter_id",
})
end
local function get_formatter_path()
-- Get the directory of the current init.lua file
local plugin_root =
vim.fn.fnamemodify(vim.api.nvim_get_runtime_file("lua/neotest-rspec/init.lua", false)[1], ":h:h:h")
-- Construct the path to formatter.rb
local formatter_path = plugin_root .. "/neotest_formatter.rb"
-- Return the absolute path
return vim.fn.fnamemodify(formatter_path, ":p")
end
---@param args neotest.RunArgs
---@return neotest.RunSpec | nil
function NeotestAdapter.build_spec(args)
local position = args.tree:data()
local engine_name = nil
local spec_path = config.transform_spec_path(position.path)
local path = vim.fn.fnamemodify(async.fn.expand("%"), ":.")
if config.engine_support then
-- if the path starts with spec, it's a normal test. Otherwise, it's an engine test
local match = vim.regex("spec/"):match_str(path)
if match and match ~= 0 then engine_name = string.sub(path, 0, match - 1) end
end
local results_path = config.results_path()
local formatter_path = get_formatter_path()
local formatter = config.formatter()
local script_args = {
"-f",
formatter,
"-o",
results_path,
"-f",
"progress",
}
if formatter == "NeotestFormatter" then
script_args = vim.tbl_flatten({
"--require",
formatter_path,
script_args,
})
end
local function run_by_filename()
table.insert(script_args, spec_path)
end
local function run_by_line_number()
table.insert(
script_args,
vim.tbl_flatten({
spec_path .. ":" .. tonumber(position.range[1] + 1),
})
)
end
local function get_strategy_config(strategy, command, cwd)
local strategy_config = {
dap = function()
return {
name = "Debug RSpec Tests",
type = "ruby",
args = { unpack(command, 2) },
command = command[1],
cwd = cwd or "${workspaceFolder}",
current_line = true,
random_port = true,
request = "attach",
error_on_failure = false, -- prevent nvim-dap-ruby from notifying user of exit code 1 from a test with failures.
localfs = true,
waiting = 1000,
}
end,
}
if strategy_config[strategy] then return strategy_config[strategy]() end
end
if position.type == "file" then run_by_filename() end
if position.type == "test" or position.type == "namespace" then run_by_line_number() end
if position.type == "dir" and vim.bo.filetype == "neotest-summary" then run_by_filename() end
local command = vim.tbl_flatten({
config.get_rspec_cmd(position.type),
script_args,
})
return {
cwd = engine_name,
command = command,
context = {
results_path = results_path,
engine_name = engine_name,
},
strategy = get_strategy_config(args.strategy, command, engine_name),
}
end
---@async
---@param spec neotest.RunSpec
---@param result neotest.StrategyResult
---@param tree neotest.Tree
---@return neotest.Result[]
function NeotestAdapter.results(spec, result, tree)
local output_file = spec.context.results_path
local engine_name = spec.context.engine_name
local ok, data = pcall(lib.files.read, output_file)
if not ok then
logger.error("No test output file found:", output_file)
return {}
end
local ok, parsed_data = pcall(vim.json.decode, data, { luanil = { object = true } })
if not ok then
logger.error("Failed to parse test output:", output_file)
return {}
end
local ok, results = pcall(utils.parse_json_output, parsed_data, output_file, engine_name)
if not ok then
logger.error("Failed to get test results:", output_file)
return {}
end
return results
end
local is_callable = function(obj)
return type(obj) == "function" or (type(obj) == "table" and obj.__call)
end
setmetatable(NeotestAdapter, {
__call = function(_, opts)
if is_callable(opts.rspec_cmd) then
config.get_rspec_cmd = opts.rspec_cmd
elseif opts.rspec_cmd then
config.get_rspec_cmd = function()
return opts.rspec_cmd
end
end
if is_callable(opts.root_files) then
config.get_root_files = opts.root_files
elseif opts.root_files then
config.get_root_files = function()
return opts.root_files
end
end
if is_callable(opts.filter_dirs) then
config.get_filter_dirs = opts.filter_dirs
elseif opts.filter_dirs then
config.get_filter_dirs = function()
return opts.filter_dirs
end
end
if is_callable(opts.transform_spec_path) then
config.transform_spec_path = opts.transform_spec_path
elseif opts.transform_spec_path then
config.transform_spec_path = function()
return opts.transform_spec_path
end
end
if is_callable(opts.results_path) then
config.results_path = opts.results_path
elseif opts.results_path then
config.results_path = function()
return opts.results_path
end
end
if opts.engine_support ~= nil then
config.engine_support = opts.engine_support
end
if is_callable(opts.formatter) then
config.formatter = opts.formatter
elseif opts.formatter then
config.formatter = function()
return opts.formatter
end
end
return NeotestAdapter
end,
})
return NeotestAdapter