load.c: backtrace of circular require

* load.c (load_lock): print backtrace of circular require via
  `Warning.warn` [ruby-core:80850] [Bug #13505]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58471 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-04-25 08:17:24 +00:00
parent 0adeb1c8ab
commit 5ad56e7c0b
5 changed files with 53 additions and 12 deletions

View file

@ -943,23 +943,23 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status|
end
end
def test_warning_warn
def capture_warning_warn
verbose = $VERBOSE
warning = nil
warning = []
::Warning.class_eval do
alias_method :warn2, :warn
remove_method :warn
define_method(:warn) do |str|
warning = str
warning << str
end
end
$VERBOSE = true
a = @a
yield
assert_match(/instance variable @a not initialized/, warning)
return warning
ensure
$VERBOSE = verbose
@ -970,6 +970,11 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status|
end
end
def test_warning_warn
warning = capture_warning_warn {@a}
assert_match(/instance variable @a not initialized/, warning[0])
end
def test_warning_warn_invalid_argument
assert_raise(TypeError) do
::Warning.warn nil
@ -982,6 +987,25 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status|
end
end
def test_warning_warn_circular_require_backtrace
warning = nil
path = nil
Tempfile.create(%w[circular .rb]) do |t|
path = t.path
basename = File.basename(path)
t.puts "require '#{basename}'"
t.close
$LOAD_PATH.push(File.dirname(t))
warning = capture_warning_warn {require basename}
ensure
$LOAD_PATH.pop
$LOADED_FEATURES.delete(t)
end
assert_match(/circular require/, warning.first)
warning.pop while %r[lib/rubygems/core_ext/kernel_require.rb:] =~ warning.last
assert_operator(warning.last, :start_with?, "\tfrom #{path}:1:")
end
def test_undefined_backtrace
assert_separately([], "#{<<-"begin;"}\n#{<<-"end;"}")
begin;