Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 66850ee

Browse files
committed
Scrape out monkey patching
http://rspec.info/blog/2013/07/the-plan-for-rspec-3/#zero_monkey_patching_mode > we do want to encourage people to switch to the new syntax, so we plan to make RSpec 3 print a warning on first usage of any the old syntax methods (should, should_not, should_receive, etc) unless the should syntax has been explicitly enabled. This should nudge folks towards the new syntax while keeping RSpec friendly to new users and will pave the way for the old syntax to be disabled by default in RSpec 4. > zero-monkey-patching mode for RSpec... We plan for these config options to become the defaults in RSpec 4.0, so that RSpec 4.0 will have zero monkey patching out of the box. As for "disabled by default" vs "completely removed" and "default, out of the box" vs "impossible" I can only say that RSpec 4 was probably planned to be released earlier, as: > we'll probably be dropping support for 1.8.7 in RSpec 4 but we've also dropped 1.9, 2.0, 2.1 and 2.2 #2301 (comment) > In RSpec 4, we plan to extract all monkey patching from RSpec and move it into a separate gem, so that monkey patching is opt-in instead of opt-out and users have to explicitly install and load a gem to get it. `rspec-should` (or `rspec-monkey` as it's also about exposing example group DSL in the top-level/Module?) will be released later. Those using the monkey-patched `should` syntax are not encouraged to update to RSpec 4 until this gem is extracted. Those using the globally-exposed DSL are encouraged to use `RSpec.describe`/`RSpec.shared_examples_for` instead.
1 parent 46292a3 commit 66850ee

16 files changed

+29
-597
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Breaking Changes:
44

55
* Ruby < 2.3 is no longer supported. (Phil Pirozhkov, #2787)
6+
* Remove monkey-patching syntax. (Phil Pirozhkov, #2803)
67

78
Enhancements:
89

features/configuration/enable_global_dsl.feature

Lines changed: 0 additions & 74 deletions
This file was deleted.

features/configuration/zero_monkey_patching_mode.feature

Lines changed: 0 additions & 106 deletions
This file was deleted.

features/subject/one_liner_syntax.feature

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@oneliner-should
21
Feature: One-liner syntax
32

43
RSpec supports a one-liner syntax for setting an expectation on the
@@ -14,9 +13,7 @@ Feature: One-liner syntax
1413
* `is_expected` is defined simply as `expect(subject)` and is designed for
1514
when you are using rspec-expectations with its newer expect-based syntax.
1615
* `should` was designed back when rspec-expectations only had a should-based
17-
syntax. However, it continues to be available and work even if the
18-
`:should` syntax is disabled (since that merely removes `Object#should`
19-
but this is `RSpec::Core::ExampleGroup#should`).
16+
syntax.
2017

2118
Notes:
2219

@@ -29,11 +26,6 @@ Feature: One-liner syntax
2926
"""ruby
3027
RSpec.describe Array do
3128
describe "when first created" do
32-
# Rather than:
33-
# it "should be empty" do
34-
# subject.should be_empty
35-
# end
36-
3729
it { should be_empty }
3830
# or
3931
it { is_expected.to be_empty }
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
if defined?(Cucumber)
22
require 'shellwords'
3-
Before('~@allow-should-syntax', '~@with-clean-spec-opts') do
3+
Before('~@with-clean-spec-opts') do
44
set_environment_variable('SPEC_OPTS', "-r#{Shellwords.escape(__FILE__)}")
55
end
6-
7-
Before('@oneliner-should') do
8-
set_environment_variable('ALLOW_ONELINER_SHOULD', 'true')
9-
end
106
else
117
if ENV['REMOVE_OTHER_RSPEC_LIBS_FROM_LOAD_PATH']
128
$LOAD_PATH.reject! { |x| /rspec-mocks/ === x || /rspec-expectations/ === x }
139
end
14-
15-
module DisallowOneLinerShould
16-
def should(*)
17-
raise "one-liner should is not allowed"
18-
end
19-
20-
def should_not(*)
21-
raise "one-liner should_not is not allowed"
22-
end
23-
end
24-
25-
RSpec.configure do |rspec|
26-
rspec.disable_monkey_patching!
27-
rspec.include DisallowOneLinerShould unless ENV['ALLOW_ONELINER_SHOULD']
28-
end
2910
end

lib/rspec/core.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,4 @@ def self.const_missing(name)
178178
require MODULES_TO_AUTOLOAD.fetch(name) { return super }
179179
::RSpec.const_get(name)
180180
end
181-
182-
Core::DSL.expose_globally!
183-
Core::SharedExampleGroup::TopLevelDSL.expose_globally!
184181
end

lib/rspec/core/configuration.rb

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -138,27 +138,6 @@ def default_path=(path)
138138
# Default: `$stderr`.
139139
add_setting :error_stream
140140

141-
# Indicates if the DSL has been exposed off of modules and `main`.
142-
# Default: true
143-
# @return [Boolean]
144-
def expose_dsl_globally?
145-
Core::DSL.exposed_globally?
146-
end
147-
148-
# Use this to expose the core RSpec DSL via `Module` and the `main`
149-
# object. It will be set automatically but you can override it to
150-
# remove the DSL.
151-
# Default: true
152-
def expose_dsl_globally=(value)
153-
if value
154-
Core::DSL.expose_globally!
155-
Core::SharedExampleGroup::TopLevelDSL.expose_globally!
156-
else
157-
Core::DSL.remove_globally!
158-
Core::SharedExampleGroup::TopLevelDSL.remove_globally!
159-
end
160-
end
161-
162141
# Determines where deprecation warnings are printed.
163142
# Defaults to `$stderr`.
164143
# @return [IO, String] IO or filename to write to
@@ -1179,11 +1158,7 @@ def alias_example_to(name, *args)
11791158
# RSpec.describe User, :type => :model do
11801159
# end
11811160
#
1182-
# @note The defined aliased will also be added to the top level
1183-
# (e.g. `main` and from within modules) if
1184-
# `expose_dsl_globally` is set to true.
11851161
# @see #alias_example_to
1186-
# @see #expose_dsl_globally=
11871162
def alias_example_group_to(new_name, *args)
11881163
extra_options = Metadata.build_hash_from(args)
11891164
RSpec::Core::ExampleGroup.define_example_group_method(new_name, extra_options)
@@ -1586,15 +1561,13 @@ def in_project_source_dir_regex
15861561
# @private
15871562
def configure_mock_framework
15881563
RSpec::Core::ExampleGroup.include(mock_framework)
1589-
conditionally_disable_mocks_monkey_patching
15901564
end
15911565

15921566
# @private
15931567
def configure_expectation_framework
15941568
expectation_frameworks.each do |framework|
15951569
RSpec::Core::ExampleGroup.include(framework)
15961570
end
1597-
conditionally_disable_expectations_monkey_patching
15981571
end
15991572

16001573
# @private
@@ -1804,52 +1777,6 @@ def raise_errors_for_deprecations!
18041777
self.deprecation_stream = Formatters::DeprecationFormatter::RaiseErrorStream.new
18051778
end
18061779

1807-
# Enables zero monkey patching mode for RSpec. It removes monkey
1808-
# patching of the top-level DSL methods (`describe`,
1809-
# `shared_examples_for`, etc) onto `main` and `Module`, instead
1810-
# requiring you to prefix these methods with `RSpec.`. It enables
1811-
# expect-only syntax for rspec-mocks and rspec-expectations. It
1812-
# simply disables monkey patching on whatever pieces of RSpec
1813-
# the user is using.
1814-
#
1815-
# @note It configures rspec-mocks and rspec-expectations only
1816-
# if the user is using those (either explicitly or implicitly
1817-
# by not setting `mock_with` or `expect_with` to anything else).
1818-
#
1819-
# @note If the user uses this options with `mock_with :mocha`
1820-
# (or similiar) they will still have monkey patching active
1821-
# in their test environment from mocha.
1822-
#
1823-
# @example
1824-
#
1825-
# # It disables all monkey patching.
1826-
# RSpec.configure do |config|
1827-
# config.disable_monkey_patching!
1828-
# end
1829-
#
1830-
# # Is an equivalent to
1831-
# RSpec.configure do |config|
1832-
# config.expose_dsl_globally = false
1833-
#
1834-
# config.mock_with :rspec do |mocks|
1835-
# mocks.syntax = :expect
1836-
# mocks.patch_marshal_to_support_partial_doubles = false
1837-
# end
1838-
#
1839-
# config.expect_with :rspec do |expectations|
1840-
# expectations.syntax = :expect
1841-
# end
1842-
# end
1843-
def disable_monkey_patching!
1844-
self.expose_dsl_globally = false
1845-
self.disable_monkey_patching = true
1846-
conditionally_disable_mocks_monkey_patching
1847-
conditionally_disable_expectations_monkey_patching
1848-
end
1849-
1850-
# @private
1851-
attr_accessor :disable_monkey_patching
1852-
18531780
# Defines a callback that can assign derived metadata values.
18541781
#
18551782
# @param filters [Array<Symbol>, Hash] metadata filters that determine
@@ -2260,22 +2187,6 @@ def output_to_tty?(output=output_stream)
22602187
output.respond_to?(:tty?) && output.tty?
22612188
end
22622189

2263-
def conditionally_disable_mocks_monkey_patching
2264-
return unless disable_monkey_patching && rspec_mocks_loaded?
2265-
2266-
RSpec::Mocks.configuration.tap do |config|
2267-
config.syntax = :expect if config.respond_to?(:syntax)
2268-
config.patch_marshal_to_support_partial_doubles = false
2269-
end
2270-
end
2271-
2272-
def conditionally_disable_expectations_monkey_patching
2273-
return unless disable_monkey_patching && rspec_expectations_loaded?
2274-
return unless RSpec::Expectations.configuration.respond_to?(:syntax=)
2275-
2276-
RSpec::Expectations.configuration.syntax = :expect
2277-
end
2278-
22792190
def rspec_mocks_loaded?
22802191
defined?(RSpec::Mocks.configuration)
22812192
end

0 commit comments

Comments
 (0)