ruby-changes:52605
From: nobu <ko1@a...>
Date: Mon, 24 Sep 2018 12:14:49 +0900 (JST)
Subject: [ruby-changes:52605] nobu:r64817 (trunk): kernel_warn.rb: skip kernel_require.rb
nobu 2018-09-24 12:14:43 +0900 (Mon, 24 Sep 2018) New Revision: 64817 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64817 Log: kernel_warn.rb: skip kernel_require.rb * lib/rubygems/core_ext/kernel_warn.rb (Kernel#warn): skip kernel_require.rb's frames when `uplevel` option is given. Added files: trunk/lib/rubygems/core_ext/kernel_warn.rb Modified files: trunk/lib/rubygems.rb trunk/test/rubygems/test_require.rb Index: lib/rubygems.rb =================================================================== --- lib/rubygems.rb (revision 64816) +++ lib/rubygems.rb (revision 64817) @@ -1392,5 +1392,6 @@ Gem::Specification.load_defaults https://github.com/ruby/ruby/blob/trunk/lib/rubygems.rb#L1392 require 'rubygems/core_ext/kernel_gem' require 'rubygems/core_ext/kernel_require' +require 'rubygems/core_ext/kernel_warn' Gem.use_gemdeps Index: lib/rubygems/core_ext/kernel_warn.rb =================================================================== --- lib/rubygems/core_ext/kernel_warn.rb (nonexistent) +++ lib/rubygems/core_ext/kernel_warn.rb (revision 64817) @@ -0,0 +1,21 @@ https://github.com/ruby/ruby/blob/trunk/lib/rubygems/core_ext/kernel_warn.rb#L1 +# frozen_string_literal: true + +if RUBY_VERSION >= "2.5" + module Kernel + path = "#{__dir__}/" + original_warn = instance_method(:warn) + Module.new {define_method(:warn, original_warn)} + original_warn = method(:warn) + + module_function define_method(:warn) {|message, uplevel: nil| + if uplevel + while (loc, = caller_locations(uplevel, 1); loc && loc.path.start_with?(path)) + uplevel += 1 + end + original_warn.call(message, uplevel: uplevel + 1) + else + original_warn.call(message) + end + } + end +end Property changes on: lib/rubygems/core_ext/kernel_warn.rb ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property Index: test/rubygems/test_require.rb =================================================================== --- test/rubygems/test_require.rb (revision 64816) +++ test/rubygems/test_require.rb (revision 64817) @@ -412,6 +412,24 @@ class TestGemRequire < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_require.rb#L412 end end + if RUBY_VERSION >= "2.5" + def test_no_kernel_require_in_warn_with_uplevel + lib = File.realpath("../../../lib", __FILE__) + Dir.mktmpdir("warn_test") do |dir| + File.write(dir + "/sub.rb", "warn 'uplevel test', uplevel: 2\n") + File.write(dir + "/main.rb", "require 'sub'\n") + _, err = capture_subprocess_io do + system(@@ruby, "-w", "--disable=gems", "-I", lib, "-C", dir, "-I.", "main.rb") + end + assert_equal "main.rb:1: warning: uplevel test\n", err + _, err = capture_subprocess_io do + system(@@ruby, "-w", "--enable=gems", "-I", lib, "-C", dir, "-I.", "main.rb") + end + assert_equal "main.rb:1: warning: uplevel test\n", err + end + end + end + def silence_warnings old_verbose, $VERBOSE = $VERBOSE, false yield -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/