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

Commit eee9511

Browse files
committed
Add RSpec 4 deprecation warnings
See rspec/rspec#61
1 parent 3f0e6b1 commit eee9511

12 files changed

+121
-7
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Bug fixes:
1010

1111
* Ensure bisect communication uses consistent encoding. (Mike Jarema, #2852)
1212

13+
Deprecations:
14+
15+
* Add RSpec 4 deprecation warnings. (Phil Pirozhkov, #2880)
16+
1317
### 3.10.1 / 2020-12-27
1418
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.10.0...v3.10.1)
1519

lib/rspec/core/configuration.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,11 @@ def exclude_pattern=(value)
327327
# (default: `false`).
328328
# @deprecated Use {#filter_run_when_matching} instead for the specific
329329
# filters that you want to be ignored if none match.
330-
add_setting :run_all_when_everything_filtered
330+
def run_all_when_everything_filtered=(value)
331+
RSpec.deprecate("`run_all_when_everything_filtered` setting", :replacement => "`filter_run_when_matching :focus`")
332+
@run_all_when_everything_filtered = value
333+
end
334+
add_read_only_setting :run_all_when_everything_filtered
331335

332336
# @macro add_setting
333337
# Color to use to indicate success. Defaults to `:green` but can be set
@@ -440,6 +444,11 @@ def shared_context_metadata_behavior=(value)
440444
"shared_context_metadata_behavior` to `#{value.inspect}`. Only " \
441445
"`:trigger_inclusion` and `:apply_to_host_groups` are valid values."
442446
end
447+
448+
if value == :trigger_inclusion
449+
RSpec.deprecate("`shared_context_metadata_behavior` setting",
450+
:message => "`:apply_to_host_groups` will become the default and only option.")
451+
end
443452
end
444453

445454
# Record the start time of the spec suite to measure load time.
@@ -492,7 +501,12 @@ def bisect_runner=(value)
492501

493502
# @private
494503
# @deprecated Use {#color_mode} = :on, instead of {#color} with {#tty}
495-
add_setting :tty
504+
def tty=(value)
505+
RSpec.deprecate("`tty` setting", :replacement => "`color_mode`")
506+
@tty = value
507+
end
508+
add_read_only_setting :tty
509+
496510
# @private
497511
attr_writer :files_to_run
498512
# @private
@@ -933,8 +947,11 @@ def color_enabled?(output=output_stream)
933947
#
934948
# @deprecated No longer recommended because of complex behavior. Instead,
935949
# rely on the fact that TTYs will display color by default, or set
936-
# {:color_mode} to :on to display color on a non-TTY output.
937-
attr_writer :color
950+
# {#color_mode} to :on to display color on a non-TTY output.
951+
def color=(value)
952+
RSpec.deprecate("`color` setting", :replacement => "`color_mode`")
953+
@color = value
954+
end
938955

939956
# @private
940957
def libs=(libs)
@@ -1217,7 +1234,13 @@ def alias_example_group_to(new_name, *args)
12171234
def alias_it_behaves_like_to(new_name, report_label='')
12181235
RSpec::Core::ExampleGroup.define_nested_shared_group_method(new_name, report_label)
12191236
end
1220-
alias_method :alias_it_should_behave_like_to, :alias_it_behaves_like_to
1237+
1238+
# Alias for `alias_it_behaves_like_to`.
1239+
# @deprecated Use {#alias_it_behaves_like_to} instead.
1240+
def alias_it_should_behave_like_to(new_name, report_label='')
1241+
RSpec.deprecate("`alias_it_should_behave_like_to`", :replacement => "`alias_it_behaves_like_to`")
1242+
alias_it_behaves_like_to(new_name, report_label)
1243+
end
12211244

12221245
# Adds key/value pairs to the `inclusion_filter`. If `args`
12231246
# includes any symbols that are not part of the hash, each symbol

lib/rspec/core/dsl.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def self.remove_globally!
8181
def self.expose_example_group_alias_globally(method_name)
8282
change_global_dsl do
8383
remove_method(method_name) if method_defined?(method_name)
84-
define_method(method_name) { |*a, &b| ::RSpec.__send__(method_name, *a, &b) }
84+
define_method(method_name) do |*a, &b|
85+
RSpec.deprecate("Globally-exposed DSL (`#{method_name}`)", :replacement => "`RSpec.#{method_name}`")
86+
::RSpec.__send__(method_name, *a, &b)
87+
end
8588
end
8689
end
8790

lib/rspec/core/example.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def location_rerun_argument
109109
# @note If there are multiple examples identified by this location, they will use {#id}
110110
# to rerun instead, but this method will still return the location (that's why it is deprecated!).
111111
def rerun_argument
112+
RSpec.deprecate("`rerun_argument`", :replacement => "`location_rerun_argument`")
112113
location_rerun_argument
113114
end
114115

lib/rspec/core/example_group.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ def self.define_example_group_method(name, metadata={})
316316
# @see SharedExampleGroup
317317
def self.define_nested_shared_group_method(new_name, report_label="it should behave like")
318318
idempotently_define_singleton_method(new_name) do |name, *args, &customization_block|
319+
yield if block_given? # to print a deprecation warning for it_should_behave_like usage
320+
319321
# Pass :caller so the :location metadata is set properly.
320322
# Otherwise, it'll be set to the next line because that's
321323
# the block's source_location.
@@ -332,7 +334,9 @@ def self.define_nested_shared_group_method(new_name, report_label="it should beh
332334
define_nested_shared_group_method :it_behaves_like, "behaves like"
333335
# Generates a nested example group and includes the shared content
334336
# mapped to `name` in the nested group.
335-
define_nested_shared_group_method :it_should_behave_like
337+
define_nested_shared_group_method(:it_should_behave_like) do
338+
RSpec.deprecate("`it_should_behave_like`", :replacement => "`it_behaves_like`")
339+
end
336340

337341
# Includes shared content mapped to `name` directly in the group in which
338342
# it is declared, as opposed to `it_behaves_like`, which creates a nested
@@ -517,6 +521,9 @@ def self.top_level?
517521
# @private
518522
def self.ensure_example_groups_are_configured
519523
unless defined?(@@example_groups_configured)
524+
unless RSpec.configuration.disable_monkey_patching
525+
RSpec.deprecate("Monkey-patching mode", :call_site => nil)
526+
end
520527
RSpec.configuration.configure_mock_framework
521528
RSpec.configuration.configure_expectation_framework
522529
# rubocop:disable Style/ClassVars

lib/rspec/core/filter_manager.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ def add_path_to_arrays_filter(filter_key, path, values)
8989
def prune_conditionally_filtered_examples(examples)
9090
examples.reject do |ex|
9191
meta = ex.metadata
92+
if meta.key?(:if)
93+
RSpec.deprecate("`:if` metadata will have no special meaning in RSpec 4 and",
94+
:replacement => "`:skip` with a negated condition",
95+
:call_site => meta[:location])
96+
end
97+
if meta.key?(:unless)
98+
RSpec.deprecate("`:unless` metadata will have no special meaning in RSpec 4 and",
99+
:replacement => "`:skip`")
100+
end
92101
!meta.fetch(:if, true) || meta[:unless]
93102
end
94103
end

lib/rspec/core/memoized_helpers.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def subject
7878
# @note If you are using RSpec's newer expect-based syntax you may
7979
# want to use `is_expected.to` instead of `should`.
8080
def should(matcher=nil, message=nil)
81+
RSpec.deprecate("Monkey-patching `should`", :replacement => "RSpec Expectations' `is_expected.to`")
8182
enforce_value_expectation(matcher, 'should')
8283
RSpec::Expectations::PositiveExpectationHandler.handle_matcher(subject, matcher, message)
8384
end
@@ -98,6 +99,7 @@ def should(matcher=nil, message=nil)
9899
# @note If you are using RSpec's newer expect-based syntax you may
99100
# want to use `is_expected.to_not` instead of `should_not`.
100101
def should_not(matcher=nil, message=nil)
102+
RSpec.deprecate("Monkey-patching `should_not`", :replacement => "RSpec Expectations' `is_expected.not_to`")
101103
enforce_value_expectation(matcher, 'should_not')
102104
RSpec::Expectations::NegativeExpectationHandler.handle_matcher(subject, matcher, message)
103105
end

spec/rspec/core/configuration_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,18 @@ def metadata_hash(*args)
13061306
config.run_all_when_everything_filtered = true
13071307
expect(config.run_all_when_everything_filtered?).to be(true)
13081308
end
1309+
1310+
it "emits a deprecation message when set" do
1311+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /run_all_when_everything_filtered/)
1312+
config.run_all_when_everything_filtered = true
1313+
end
1314+
end
1315+
1316+
describe "#tty=" do
1317+
it "emits a deprecation message when set" do
1318+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /tty/)
1319+
config.tty = true
1320+
end
13091321
end
13101322

13111323
describe "#color_mode" do
@@ -1400,6 +1412,11 @@ def metadata_hash(*args)
14001412
describe "#color=" do
14011413
before { config.color_mode = :automatic }
14021414

1415+
it "emits a deprecation message when set" do
1416+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /color/)
1417+
config.color = true
1418+
end
1419+
14031420
context "given false" do
14041421
before { config.color = false }
14051422

@@ -2369,6 +2386,13 @@ class << self
23692386
end
23702387
end
23712388

2389+
describe '#alias_it_should_behave_like_to' do
2390+
it "emits a deprecation message when used" do
2391+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /alias_it_should_behave_like_to/)
2392+
config.alias_it_should_behave_like_to :it_should_have_behaved_like
2393+
end
2394+
end
2395+
23722396
it_behaves_like "metadata hash builder" do
23732397
def metadata_hash(*args)
23742398
config.alias_example_group_to :my_group_method, *args
@@ -2904,6 +2928,16 @@ def emulate_not_configured_expectation_framework
29042928
":another_value", ":trigger_inclusion", ":apply_to_host_groups"
29052929
))
29062930
end
2931+
2932+
it "emits a deprecation message when set to :trigger_inclusion" do
2933+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /shared_context_metadata_behavior/)
2934+
config.shared_context_metadata_behavior = :trigger_inclusion
2935+
end
2936+
2937+
it "does not emit a deprecation message when set to :apply_to_host_groups" do
2938+
expect_no_deprecation
2939+
config.shared_context_metadata_behavior = :apply_to_host_groups
2940+
end
29072941
end
29082942

29092943
# assigns files_or_directories_to_run and triggers post-processing

spec/rspec/core/dsl_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ def enable
2727
expect(Object.new).not_to respond_to(*method_names)
2828
end
2929
end
30+
31+
it 'emits a deprecation warning' do
32+
in_sub_process do
33+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 6, /Globally-exposed DSL \(`describe`\)/)
34+
changing_expose_dsl_globally do
35+
RSpec.configuration.expose_dsl_globally = true
36+
expect(RSpec.configuration.expose_dsl_globally?).to eq true
37+
38+
Module.new do
39+
describe 'monkey' do
40+
end
41+
end
42+
end
43+
end
44+
end
3045
end
3146

3247
context "when expose_dsl_globally is disabled" do

spec/rspec/core/example_group_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,14 @@ def foo; end
18831883
end
18841884
}.to raise_error("boom").and avoid_changing(RSpec::Support, :thread_local_data)
18851885
end
1886+
1887+
it "emits a deprecation warning when used" do
1888+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 3, /it_should_behave_like/)
1889+
RSpec.describe do
1890+
shared_examples_for("stuff") { }
1891+
it_should_behave_like "stuff"
1892+
end
1893+
end
18861894
end
18871895

18881896
it 'minimizes the number of methods that users could inadvertantly overwrite' do

spec/rspec/core/example_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def metadata_hash(*args)
3838
example = RSpec.describe.example
3939
expect(example.rerun_argument).to eq("#{RSpec::Core::Metadata.relative_path(__FILE__)}:#{__LINE__ - 1}")
4040
end
41+
42+
it "emits a deprecation warning when used" do
43+
example = RSpec.describe.example
44+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /rerun_argument/)
45+
example.rerun_argument
46+
end
4147
end
4248

4349
describe "#update_inherited_metadata" do

spec/rspec/core/memoized_helpers_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,13 @@ def supports_value_expectations?
642642
subject { 'value or a Proc' }
643643

644644
it '`should` prints a deprecation warning when given a value' do
645+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 2, /Monkey-patching `should`/)
645646
expect_warn_deprecation(/The implicit block expectation syntax is deprecated, you should pass/)
646647
expect { should block_matcher }.not_to raise_error
647648
end
648649

649650
it '`should_not` prints a deprecation warning when given a value' do
651+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 2, /Monkey-patching `should_not`/)
650652
expect_warn_deprecation(/The implicit block expectation syntax is deprecated, you should pass/)
651653
expect { should_not block_matcher }.to raise_error(Exception)
652654
end

0 commit comments

Comments
 (0)