diff --git a/lib/console.rb b/lib/console.rb index 8a1f59c..2a2b042 100644 --- a/lib/console.rb +++ b/lib/console.rb @@ -8,7 +8,6 @@ require_relative "console/version" require_relative "console/logger" -require_relative "console/warn" module Console class << self diff --git a/lib/console/warn.rb b/lib/console/warn.rb index 70c4b4c..79ed25e 100644 --- a/lib/console/warn.rb +++ b/lib/console/warn.rb @@ -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 diff --git a/test/console/warn.rb b/test/console/warn.rb index 6e35300..583391a 100644 --- a/test/console/warn.rb +++ b/test/console/warn.rb @@ -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 @@ -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