Skip to content

Commit

Permalink
Fix handling of uplevel.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 5, 2024
1 parent 908828b commit d8a0c2e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/console/warn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def warn(*arguments, uplevel: nil, **options)
fiber = Fiber.current

# We do this to be extra pendantic about avoiding infinite recursion, i.e. if `Console.warn` some how calls `Kernel.warn` again, it would potentially cause infinite recursion. I'm not sure if this is a problem in practice, but I'd rather not find out the hard way...
return super if fiber.console_warn
return if fiber.console_warn

if uplevel
options[:backtrace] = caller(uplevel, 1)
# Add one to uplevel to skip the current frame.
options[:backtrace] = caller(uplevel+1, 1)
end

if arguments.last.is_a?(Exception)
Expand Down
22 changes: 15 additions & 7 deletions test/console/warn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@
)
end

it "supports uplevel" do
warn "It did not work as expected!", uplevel: 1
with "uplevel" do
def print_warning
warn "It did not work as expected!", uplevel: 0
end

expect(console_capture.last).to have_keys(
severity: be == :warn,
subject: be == "It did not work as expected!",
backtrace: be_a(Array)
)
it "includes appropriate backtrace" do
print_warning

expect(console_capture.last).to have_keys(
severity: be == :warn,
subject: be == "It did not work as expected!",
backtrace: be_a(Array)
)

expect(console_capture.last[:backtrace].first).to be =~ /print_warning/
end
end

it "supports exceptions" do
Expand Down

0 comments on commit d8a0c2e

Please sign in to comment.