Skip to content

Commit

Permalink
fix: add custom formatter to solve nested issue (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrhughes authored Dec 2, 2024
1 parent a791001 commit 840745b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lua/neotest-rspec/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ function NeotestAdapter.discover_positions(path)
})
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.resolve(formatter_path)
end

---@param args neotest.RunArgs
---@return neotest.RunSpec | nil
function NeotestAdapter.build_spec(args)
Expand All @@ -98,9 +110,13 @@ function NeotestAdapter.build_spec(args)
if match and match ~= 0 then engine_name = string.sub(path, 0, match - 1) end
local results_path = config.results_path()

local formatter_path = get_formatter_path()

local script_args = vim.tbl_flatten({
"--require",
formatter_path,
"-f",
"json",
"NeotestFormatter",
"-o",
results_path,
"-f",
Expand Down
22 changes: 22 additions & 0 deletions neotest_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rspec/core/formatters/json_formatter"

class NeotestFormatter < RSpec::Core::Formatters::JsonFormatter
RSpec::Core::Formatters.register self, :message, :dump_summary, :dump_profile, :stop, :seed, :close

private

def get_actual_inclusion_line_number(example)
if example.metadata[:shared_group_inclusion_backtrace]&.any?
example.metadata[:shared_group_inclusion_backtrace].first.inclusion_location[/(?<=:)\d+(?=:in)/]
else
example.metadata[:line_number]
end
end

def format_example(example)
result = super(example)
result.merge(
line_number: get_actual_inclusion_line_number(example)
)
end
end

0 comments on commit 840745b

Please sign in to comment.