Skip to content

Commit

Permalink
Extend Warning instead of Kernel and don't load by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 5, 2024
1 parent d8a0c2e commit 75abf04
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 54 deletions.
1 change: 0 additions & 1 deletion lib/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

require_relative "console/version"
require_relative "console/logger"
require_relative "console/warn"

module Console
class << self
Expand Down
33 changes: 14 additions & 19 deletions lib/console/warn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,31 @@
# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require_relative "logger"

module Console
# Whether the current fiber is emitting a warning.
Fiber.attr_accessor :console_warn

# Redirect warnings to Console.warn.
module Warn
def warn(*arguments, uplevel: nil, **options)
# Redirect warnings to {Console.warn}.
def warn(message, **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 if fiber.console_warn

if uplevel
# Add one to uplevel to skip the current frame.
options[:backtrace] = caller(uplevel+1, 1)
end
# We do this to be extra pendantic about avoiding infinite recursion.
return super if fiber.console_warn

if arguments.last.is_a?(Exception)
exception = arguments.pop
begin
fiber.console_warn = true
message.chomp!

Console::Event::Failure.for(exception).emit(*arguments, severity: :warn)
else
begin
fiber.console_warn = true
Console.warn(*arguments, **options)
ensure
fiber.console_warn = false
end
Console::Logger.instance.warn(message, **options)
ensure
fiber.console_warn = false
end
end
end

::Kernel.prepend(Warn)
::Warning.extend(Warn)
end
36 changes: 2 additions & 34 deletions test/console/warn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Copyright, 2024, by Samuel Williams.

require "sus/fixtures/console"
require "console/warn"

describe "Kernel#warn" do
include_context Sus::Fixtures::Console::CapturedLogger
Expand All @@ -13,40 +14,7 @@

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

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

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
warn "An error occured:", StandardError.new("It did not work as expected!")

expect(console_capture.last).to have_keys(
severity: be == :warn,
subject: be == "An error occured:",
event: have_keys(
type: be == :failure,
root: be_a(String),
class: be =~ /StandardError/,
message: be =~ /It did not work as expected!/
)
subject: be =~ /It did not work as expected/
)
end
end

0 comments on commit 75abf04

Please sign in to comment.