Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error backtrace formatting on Ruby 3.4+ #1771

Merged
merged 6 commits into from
Feb 25, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix error backtrace formatting on Ruby 3.4+
Accomodate the new Ruby 3.4+ backtrace display format
orien committed Jan 13, 2025
commit 1a6077d5bec47d0a09d9b970c473e18c3e179460
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ that need to rely on procedural loading / reloading of files should use method i

### Fixed
- Fixed an issue where a change to one example in compatibility testing wasn't fully adhered to ([luke-hill](https://github.com/luke-hill))
- Fixed Ruby 3.4+ issue where error backtraces weren't being formatted. ([#1771](https://github.com/cucumber/cucumber-ruby/pull/1771) [orien](https://github.com/orien))

### Removed
- `StepDefinitionLight` associated methods. The class itself is present but deprecated
4 changes: 3 additions & 1 deletion lib/cucumber/formatter/backtrace_filter.rb
Original file line number Diff line number Diff line change
@@ -43,8 +43,10 @@ def exception

if ::ENV['CUCUMBER_TRUNCATE_OUTPUT']
# Strip off file locations
regexp = RUBY_VERSION >= '3.4' ? /(.*):in '/ : /(.*):in `/
filtered = filtered.map do |line|
line =~ /(.*):in `/ ? Regexp.last_match(1) : line
match = regexp.match(line)
match ? match[1] : line
end
end

13 changes: 11 additions & 2 deletions lib/cucumber/glue/invoke_in_world.rb
Original file line number Diff line number Diff line change
@@ -14,7 +14,10 @@ def self.replace_instance_exec_invocation_line!(backtrace, instance_exec_invocat
return unless instance_exec_pos

replacement_line = instance_exec_pos + INSTANCE_EXEC_OFFSET
backtrace[replacement_line].gsub!(/`.*'/, "`#{pseudo_method}'") if pseudo_method
if pseudo_method
pattern = RUBY_VERSION >= '3.4' ? /'.*'/ : /`.*'/
backtrace[replacement_line].gsub!(pattern, "`#{pseudo_method}'")
end

depth = backtrace.count { |line| line == instance_exec_invocation_line }
end_pos = depth > 1 ? instance_exec_pos : -1
@@ -49,7 +52,13 @@ def self.cucumber_compatible_arity?(args, block)
def self.cucumber_run_with_backtrace_filtering(pseudo_method)
yield
rescue Exception => e
instance_exec_invocation_line = "#{__FILE__}:#{__LINE__ - 2}:in `cucumber_run_with_backtrace_filtering'"
yield_line_number = __LINE__ - 2
instance_exec_invocation_line =
if RUBY_VERSION >= '3.4'
"#{__FILE__}:#{yield_line_number}:in '#{name}.#{__method__}'"
else
"#{__FILE__}:#{yield_line_number}:in `#{__method__}'"
end
replace_instance_exec_invocation_line!((e.backtrace || []), instance_exec_invocation_line, pseudo_method)
raise e
end