From 224b575fc7f19b89c64b06b69054bfbbe2f7c800 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 4 May 2019 20:18:45 +0300 Subject: warning: print file and line of the calling frame We want to print the calling frame, so we can point out who exactly triggered it. The current behaviour is less useful (we print the line that called the warning). --- lib/pry/warning.rb | 4 ++-- spec/warning_spec.rb | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/pry/warning.rb b/lib/pry/warning.rb index 6f7bee76..5585ce20 100644 --- a/lib/pry/warning.rb +++ b/lib/pry/warning.rb @@ -9,12 +9,12 @@ class Pry # @return [void] def self.warn(message) if Kernel.respond_to?(:caller_locations) - location = caller_locations(1..1).first + location = caller_locations(2..2).first path = location.path lineno = location.lineno else # Ruby 1.9.3 support. - frame = caller.first.split(':') # rubocop:disable Performance/Caller + frame = caller[1].split(':') # rubocop:disable Performance/Caller path = frame.first lineno = frame[1] end diff --git a/spec/warning_spec.rb b/spec/warning_spec.rb index 3f7328ce..18bfa219 100644 --- a/spec/warning_spec.rb +++ b/spec/warning_spec.rb @@ -1,9 +1,7 @@ RSpec.describe Pry::Warning do describe "#warn" do - it "prints a warning with file and line" do - expect(Kernel).to receive(:warn).with( - "#{__FILE__}:#{__LINE__ + 2}: warning: foo bar" - ) + it "prints message with file and line of the calling frame" do + expect(Kernel).to receive(:warn).with(/.+\.rb:\d+: warning: foo bar/) described_class.warn('foo bar') end end -- cgit v1.2.1