From 03ea8e64373973a56244cc5ad9f67f91ccb4e417 Mon Sep 17 00:00:00 2001 From: Dushyant Singh Shekhawat Date: Sun, 11 Feb 2024 12:14:30 +0530 Subject: [PATCH] Add specs for prepend modifying ancestor chain --- core/module/prepend_spec.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/core/module/prepend_spec.rb b/core/module/prepend_spec.rb index f80cfbcca..c90fa9700 100644 --- a/core/module/prepend_spec.rb +++ b/core/module/prepend_spec.rb @@ -737,6 +737,39 @@ def foo(ary) klass.ancestors.take(4).should == [klass, submod, mod, Object] end + # https://bugs.ruby-lang.org/issues/17423 + describe "when module already exists in ancestor chain" do + ruby_version_is ""..."3.1" do + it "does not modify the ancestor chain" do + m = Module.new do; end + a = Module.new do; end + b = Class.new do; end + + b.include(a) + a.prepend(m) + b.ancestors.take(4).should == [b, m, a, Object] + + b.prepend(m) + b.ancestors.take(4).should == [b, m, a, Object] + end + end + + ruby_version_is "3.1" do + it "modifies the ancestor chain" do + m = Module.new do; end + a = Module.new do; end + b = Class.new do; end + + b.include(a) + a.prepend(m) + b.ancestors.take(4).should == [b, m, a, Object] + + b.prepend(m) + b.ancestors.take(5).should == [m, b, m, a, Object] + end + end + end + describe "called on a module" do describe "included into a class" it "does not obscure the module's methods from reflective access" do