Skip to content

Commit

Permalink
Scrape out monkey patching
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pirj committed Dec 26, 2020
1 parent 46292a3 commit f3780b3
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 596 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Breaking Changes:

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

Enhancements:

Expand Down
74 changes: 0 additions & 74 deletions features/configuration/enable_global_dsl.feature

This file was deleted.

106 changes: 0 additions & 106 deletions features/configuration/zero_monkey_patching_mode.feature

This file was deleted.

10 changes: 1 addition & 9 deletions features/subject/one_liner_syntax.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@oneliner-should
Feature: One-liner syntax

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

Notes:

Expand All @@ -29,11 +26,6 @@ Feature: One-liner syntax
"""ruby
RSpec.describe Array do
describe "when first created" do
# Rather than:
# it "should be empty" do
# subject.should be_empty
# end
it { should be_empty }
# or
it { is_expected.to be_empty }
Expand Down
21 changes: 1 addition & 20 deletions features/support/require_expect_syntax_in_aruba_specs.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
if defined?(Cucumber)
require 'shellwords'
Before('~@allow-should-syntax', '~@with-clean-spec-opts') do
Before('~@with-clean-spec-opts') do
set_environment_variable('SPEC_OPTS', "-r#{Shellwords.escape(__FILE__)}")
end

Before('@oneliner-should') do
set_environment_variable('ALLOW_ONELINER_SHOULD', 'true')
end
else
if ENV['REMOVE_OTHER_RSPEC_LIBS_FROM_LOAD_PATH']
$LOAD_PATH.reject! { |x| /rspec-mocks/ === x || /rspec-expectations/ === x }
end

module DisallowOneLinerShould
def should(*)
raise "one-liner should is not allowed"
end

def should_not(*)
raise "one-liner should_not is not allowed"
end
end

RSpec.configure do |rspec|
rspec.disable_monkey_patching!
rspec.include DisallowOneLinerShould unless ENV['ALLOW_ONELINER_SHOULD']
end
end
3 changes: 0 additions & 3 deletions lib/rspec/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,4 @@ def self.const_missing(name)
require MODULES_TO_AUTOLOAD.fetch(name) { return super }
::RSpec.const_get(name)
end

Core::DSL.expose_globally!
Core::SharedExampleGroup::TopLevelDSL.expose_globally!
end
89 changes: 0 additions & 89 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,6 @@ def default_path=(path)
# Default: `$stderr`.
add_setting :error_stream

# Indicates if the DSL has been exposed off of modules and `main`.
# Default: true
# @return [Boolean]
def expose_dsl_globally?
Core::DSL.exposed_globally?
end

# Use this to expose the core RSpec DSL via `Module` and the `main`
# object. It will be set automatically but you can override it to
# remove the DSL.
# Default: true
def expose_dsl_globally=(value)
if value
Core::DSL.expose_globally!
Core::SharedExampleGroup::TopLevelDSL.expose_globally!
else
Core::DSL.remove_globally!
Core::SharedExampleGroup::TopLevelDSL.remove_globally!
end
end

# Determines where deprecation warnings are printed.
# Defaults to `$stderr`.
# @return [IO, String] IO or filename to write to
Expand Down Expand Up @@ -1179,11 +1158,7 @@ def alias_example_to(name, *args)
# RSpec.describe User, :type => :model do
# end
#
# @note The defined aliased will also be added to the top level
# (e.g. `main` and from within modules) if
# `expose_dsl_globally` is set to true.
# @see #alias_example_to
# @see #expose_dsl_globally=
def alias_example_group_to(new_name, *args)
extra_options = Metadata.build_hash_from(args)
RSpec::Core::ExampleGroup.define_example_group_method(new_name, extra_options)
Expand Down Expand Up @@ -1586,15 +1561,13 @@ def in_project_source_dir_regex
# @private
def configure_mock_framework
RSpec::Core::ExampleGroup.include(mock_framework)
conditionally_disable_mocks_monkey_patching
end

# @private
def configure_expectation_framework
expectation_frameworks.each do |framework|
RSpec::Core::ExampleGroup.include(framework)
end
conditionally_disable_expectations_monkey_patching
end

# @private
Expand Down Expand Up @@ -1804,52 +1777,6 @@ def raise_errors_for_deprecations!
self.deprecation_stream = Formatters::DeprecationFormatter::RaiseErrorStream.new
end

# Enables zero monkey patching mode for RSpec. It removes monkey
# patching of the top-level DSL methods (`describe`,
# `shared_examples_for`, etc) onto `main` and `Module`, instead
# requiring you to prefix these methods with `RSpec.`. It enables
# expect-only syntax for rspec-mocks and rspec-expectations. It
# simply disables monkey patching on whatever pieces of RSpec
# the user is using.
#
# @note It configures rspec-mocks and rspec-expectations only
# if the user is using those (either explicitly or implicitly
# by not setting `mock_with` or `expect_with` to anything else).
#
# @note If the user uses this options with `mock_with :mocha`
# (or similiar) they will still have monkey patching active
# in their test environment from mocha.
#
# @example
#
# # It disables all monkey patching.
# RSpec.configure do |config|
# config.disable_monkey_patching!
# end
#
# # Is an equivalent to
# RSpec.configure do |config|
# config.expose_dsl_globally = false
#
# config.mock_with :rspec do |mocks|
# mocks.syntax = :expect
# mocks.patch_marshal_to_support_partial_doubles = false
# end
#
# config.expect_with :rspec do |expectations|
# expectations.syntax = :expect
# end
# end
def disable_monkey_patching!
self.expose_dsl_globally = false
self.disable_monkey_patching = true
conditionally_disable_mocks_monkey_patching
conditionally_disable_expectations_monkey_patching
end

# @private
attr_accessor :disable_monkey_patching

# Defines a callback that can assign derived metadata values.
#
# @param filters [Array<Symbol>, Hash] metadata filters that determine
Expand Down Expand Up @@ -2260,22 +2187,6 @@ def output_to_tty?(output=output_stream)
output.respond_to?(:tty?) && output.tty?
end

def conditionally_disable_mocks_monkey_patching
return unless disable_monkey_patching && rspec_mocks_loaded?

RSpec::Mocks.configuration.tap do |config|
config.syntax = :expect if config.respond_to?(:syntax)
config.patch_marshal_to_support_partial_doubles = false
end
end

def conditionally_disable_expectations_monkey_patching
return unless disable_monkey_patching && rspec_expectations_loaded?
return unless RSpec::Expectations.configuration.respond_to?(:syntax=)

RSpec::Expectations.configuration.syntax = :expect
end

def rspec_mocks_loaded?
defined?(RSpec::Mocks.configuration)
end
Expand Down
Loading

0 comments on commit f3780b3

Please sign in to comment.