Skip to content

Commit

Permalink
Merge pull request #638 from sparklemotion/flavorjones-prefer-caller-…
Browse files Browse the repository at this point in the history
…location

prefer `caller_locations` and `Thread::Backtrace::Location` methods to `Kernel#caller`
  • Loading branch information
flavorjones committed Feb 21, 2024
2 parents 8c47e35 + 73b8a7e commit 2d4b1af
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/mechanize.rb
Expand Up @@ -717,10 +717,10 @@ class << self
# authentication.

def auth user, password, domain = nil
caller.first =~ /(.*?):(\d+).*?$/
c = caller_locations(1,1).first

warn <<-WARNING
At #{$1} line #{$2}
At #{c.absolute_path} line #{c.lineno}
Use of #auth and #basic_auth are deprecated due to a security vulnerability.
Expand Down
4 changes: 2 additions & 2 deletions lib/mechanize/cookie.rb
Expand Up @@ -7,7 +7,7 @@ class Mechanize
module CookieDeprecated
def __deprecated__(to = nil)
$VERBOSE or return
method = caller[0][/([^`]+)(?='$)/]
method = caller_locations(1,1).first.base_label
to ||= method
case self
when Class
Expand All @@ -21,7 +21,7 @@ def __deprecated__(to = nil)
this = '%s#%s' % [klass, method]
that = 'HTTP::%s#%s' % [lname, to]
end
warn '%s: The call of %s needs to be fixed to follow the new API (%s).' % [caller[1], this, that]
warn '%s: The call of %s needs to be fixed to follow the new API (%s).' % [caller_locations(2,1).first, this, that]
end
private :__deprecated__
end
Expand Down
13 changes: 12 additions & 1 deletion test/test_mechanize_cookie.rb
Expand Up @@ -502,12 +502,23 @@ def test_domain=
cookie.domain = 'Dom.example.com'
assert 'dom.example.com', cookie.domain

cookie.domain = Object.new.tap { |o|
new_domain = Object.new.tap { |o|
def o.to_str
'Example.com'
end
}
cookie.domain = new_domain
assert 'example.com', cookie.domain

new_domain = Object.new.tap { |o|
def o.to_str
'Example2.com'
end
}
assert_output nil, /The call of Mechanize::Cookie#set_domain/ do
cookie.set_domain(new_domain)
end
assert 'example2.com', cookie.domain
end

def test_cookie_httponly
Expand Down
2 changes: 1 addition & 1 deletion test/test_mechanize_http_agent.rb
Expand Up @@ -29,7 +29,7 @@ def auth_realm uri, scheme, type

def skip_if_jruby_zlib
if RUBY_ENGINE == 'jruby'
meth = caller[0][/`(\w+)/, 1]
meth = caller_locations(1,1).first.base_label
skip "#{meth}: skipped because how Zlib handles error is different in JRuby"
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_mechanize_page_link.rb
Expand Up @@ -55,7 +55,7 @@ def util_page body = @body, res = @res

def skip_if_nkf_dependency
if RUBY_ENGINE == 'jruby'
meth = caller[0][/`(\w+)/, 1]
meth = caller_locations(1,1).first.base_label
skip "#{meth}: skipped because this feature currently depends on NKF"
end
end
Expand Down

0 comments on commit 2d4b1af

Please sign in to comment.