This commit is contained in:
Mamoru TASAKA 2022-10-26 17:06:42 +09:00
parent 2d7218ef19
commit ae84f53d6d
3 changed files with 7 additions and 80 deletions

View File

@ -1,71 +0,0 @@
From 363c1e4a56f53ba3dbd00d50889250ab24f005a8 Mon Sep 17 00:00:00 2001
From: Benoit Daloze <eregontp@gmail.com>
Date: Thu, 21 Apr 2022 16:10:58 +0200
Subject: [PATCH] Use a better and more reliable check for whether a method is
the same as Class#new
* See https://bugs.ruby-lang.org/issues/18729#note-5
---
lib/rspec/mocks/method_reference.rb | 16 ++++++++++++++--
spec/rspec/mocks/partial_double_spec.rb | 18 ++++++++++++++++++
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/lib/rspec/mocks/method_reference.rb b/lib/rspec/mocks/method_reference.rb
index 026c2c07d..276202563 100644
--- a/lib/rspec/mocks/method_reference.rb
+++ b/lib/rspec/mocks/method_reference.rb
@@ -185,11 +185,23 @@ class ClassNewMethodReference < ObjectMethodReference
def self.applies_to?(method_name)
return false unless method_name == :new
klass = yield
- return false unless klass.respond_to?(:new, true)
+ return false unless ::Class === klass && klass.respond_to?(:new, true)
# We only want to apply our special logic to normal `new` methods.
# Methods that the user has monkeyed with should be left as-is.
- ::RSpec::Support.method_handle_for(klass, :new).owner == ::Class
+ uses_class_new?(klass)
+ end
+
+ if RUBY_VERSION.to_i >= 3
+ CLASS_NEW = ::Class.singleton_class.instance_method(:new)
+
+ def self.uses_class_new?(klass)
+ ::RSpec::Support.method_handle_for(klass, :new) == CLASS_NEW.bind(klass)
+ end
+ else # Ruby 2's Method#== is too strict
+ def self.uses_class_new?(klass)
+ ::RSpec::Support.method_handle_for(klass, :new).owner == ::Class
+ end
end
def with_signature
diff --git a/spec/rspec/mocks/partial_double_spec.rb b/spec/rspec/mocks/partial_double_spec.rb
index 099b15517..ea327e101 100644
--- a/spec/rspec/mocks/partial_double_spec.rb
+++ b/spec/rspec/mocks/partial_double_spec.rb
@@ -622,6 +622,24 @@ class << self
end
end
+ context "on a class with a twice-aliased `new`" do
+ it 'uses the method signature from `#initialize` for arg verification' do
+ if RUBY_VERSION.to_i < 3
+ pending "Failing due to Ruby 2's Method#== being too strict"
+ end
+
+ subclass = Class.new(klass) do
+ class << self
+ alias_method :_new, :new
+ alias_method :new, :_new
+ end
+ end
+
+ prevents(/arguments/) { allow(subclass).to receive(:new).with(1) }
+ allow(subclass).to receive(:new).with(1, 2)
+ end
+ end
+
context 'on a class that has redefined `self.method`' do
it 'allows the stubbing of :new' do
subclass = Class.new(klass) do

View File

@ -1,9 +1,9 @@
%global majorver 3.11.1
%global majorver 3.11.2
#%%global preminorver .rc6
%global rpmminorver .%(echo %preminorver | sed -e 's|^\\.\\.*||')
%global fullver %{majorver}%{?preminorver}
%global fedorarel 2
%global fedorarel 1
%global gem_name rspec-mocks
@ -23,10 +23,6 @@ Source0: https://rubygems.org/gems/%{gem_name}-%{fullver}.gem
Source1: rubygem-%{gem_name}-%{version}-full.tar.gz
Source2: rspec-related-create-full-tarball.sh
# https://bugs.ruby-lang.org/issues/18729#note-5
# https://github.com/rspec/rspec-mocks/pull/1470
Patch1: %{name}-3.11.1-check_method_is_same_as_class_new.patch
#BuildRequires: ruby(release)
BuildRequires: rubygems-devel
%if %{without bootstrap}
@ -58,7 +54,6 @@ This package contains documentation for %{name}.
gem unpack %{SOURCE0}
%setup -q -D -T -n %{gem_name}-%{version} -b 1
%patch1 -p1 -b .ruby32_new
# Cucumber 7 syntax change
sed -i cucumber.yml -e "s|~@wip|not @wip|"
@ -104,6 +99,9 @@ cucumber
%{gem_docdir}
%changelog
* Wed Oct 26 2022 Mamoru TASAKA <mtasaka@fedoraproject.org> - 3.11.2-1
- 3.11.2
* Mon Oct 3 2022 Mamoru TASAKA <mtasaka@fedoraproject.org> - 3.11.1-2
- Backport upstream patch for ruby32 wrt method reference changes

View File

@ -1,2 +1,2 @@
SHA512 (rspec-mocks-3.11.1.gem) = 5b19b17ac7f8c24196018c70651f71693b250789af38ef5b0541032e77b93bc49ed94ad985ab4e3744b5493b03b5cc21730a5bd8c292958e7d2263f7d44bdea6
SHA512 (rubygem-rspec-mocks-3.11.1-full.tar.gz) = dd11d0e36d4e1e0e572af7fed666accdc5511b7cea87b0695efa4fe8776f4f9916316f9945fc8f362be17dc79ff33748b7ef1edebd626d6e214534eb31e35347
SHA512 (rspec-mocks-3.11.2.gem) = 1578ab2112ab45db105c2ee072f087e80148c22156e99f660fdd9ca0adbe6a1ab201a24877335ce6dbddde0e2a4d1fbc68204091a9fea2ad6a5e76542e2c2412
SHA512 (rubygem-rspec-mocks-3.11.2-full.tar.gz) = 76b4110f0aea5c947e6dc9eb130d7cecbfcaf0e8fd86de9bb892c9270543c305dd905629167edf9f852994c719a841c278841288ee491965576dbb4c392e8b5e