From cb95d834cc19a45a84d8d2c6d0f25d0c44e0d813 Mon Sep 17 00:00:00 2001 From: st0012 Date: Wed, 26 Oct 2022 12:40:48 +0100 Subject: [PATCH] [ruby/irb] Don't insert new methods to Test::Unit::TestCase Ruby CI runs irb and other Ruby core/stdlib tests in the same process. So adding irb-specific helper to Test::Unit::TestCase could potentially pollute other components' tests and should be avoided. --- test/irb/test_cmd.rb | 4 +++- test/irb/test_helper.rb | 16 ++++++++++++++++ test/irb/test_input_method.rb | 4 +++- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 test/irb/test_helper.rb diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb index 6c03a9de07..6ff2afc3e3 100644 --- a/test/irb/test_cmd.rb +++ b/test/irb/test_cmd.rb @@ -3,6 +3,8 @@ require "test/unit" require "irb" require "irb/extend-command" +require_relative "test_helper" + module TestIRB class ExtendCommand < Test::Unit::TestCase class TestInputMethod < ::IRB::InputMethod @@ -443,7 +445,7 @@ module TestIRB irb = IRB::Irb.new(IRB::WorkSpace.new(self), input) IRB.conf[:MAIN_CONTEXT] = irb.context out, err = capture_output do - without_rdoc do + IRB::TestHelper.without_rdoc do irb.eval_input end end diff --git a/test/irb/test_helper.rb b/test/irb/test_helper.rb new file mode 100644 index 0000000000..19c39a4a59 --- /dev/null +++ b/test/irb/test_helper.rb @@ -0,0 +1,16 @@ +module IRB + module TestHelper + def self.without_rdoc(&block) + ::Kernel.send(:alias_method, :old_require, :require) + + ::Kernel.define_method(:require) do |name| + raise LoadError, "cannot load such file -- rdoc (test)" if name.match?("rdoc") || name.match?(/^rdoc\/.*/) + ::Kernel.send(:old_require, name) + end + + yield + ensure + EnvUtil.suppress_warning { ::Kernel.send(:alias_method, :require, :old_require) } + end + end +end diff --git a/test/irb/test_input_method.rb b/test/irb/test_input_method.rb index 0e1af7bdc7..3618fa46f1 100644 --- a/test/irb/test_input_method.rb +++ b/test/irb/test_input_method.rb @@ -3,6 +3,8 @@ require "test/unit" require "irb" +require_relative "test_helper" + module TestIRB class TestRelineInputMethod < Test::Unit::TestCase def setup @@ -76,7 +78,7 @@ module TestIRB IRB.conf[:USE_AUTOCOMPLETE] = true - without_rdoc do + IRB::TestHelper.without_rdoc do IRB::RelineInputMethod.new end