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

Commit 72e4fd9

Browse files
committed
Remove shared_context_metadata_behavior config option
Fixes #2832
1 parent f3c07ba commit 72e4fd9

11 files changed

+10
-236
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Breaking Changes:
66
* Extract `should` syntax (including the non-monkey-patching one liner). (Phil Pirozhkov, #2803)
77
* Remove globally-exposed DSL (example and shared group methods
88
in the root scope and on Module). (Phil Pirozhkov, #2803)
9+
* Change the default `shared_context_metadata_behavior` to `apply_to_host_groups`
10+
and remove the configuration option. (Phil Pirozhkov, #2834)
911
* Remove `run_all_when_everything_filtered` configuration option. (Phil Pirozhkov, #2845)
1012

1113
Enhancements:

features/example_groups/shared_context.feature

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,9 @@ Feature: shared context
22

33
Use `shared_context` to define a block that will be evaluated in the context of example groups either locally, using `include_context` in an example group, or globally using `config.include_context`.
44

5-
When implicitly including shared contexts via matching metadata, the normal way is to define matching metadata on an example group, in which case the context is included in the entire group. However, you also have the option to include it in an individual example instead. RSpec treats every example as having a singleton example group (analogous to Ruby's singleton classes) containing just the one example.
6-
75
Background:
86
Given a file named "shared_stuff.rb" with:
97
"""ruby
10-
RSpec.configure do |rspec|
11-
# This config option will be enabled by default on RSpec 4,
12-
# but for reasons of backwards compatibility, you have to
13-
# set it on RSpec 3.
14-
#
15-
# It causes the host group and examples to inherit metadata
16-
# from the shared context.
17-
rspec.shared_context_metadata_behavior = :apply_to_host_groups
18-
end
19-
208
RSpec.shared_context "shared stuff", :shared_context => :metadata do
219
before { @some_var = :some_value }
2210
def shared_method

features/example_groups/shared_examples.feature

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -257,23 +257,6 @@ Feature: shared examples
257257
responds to <=>
258258
"""
259259

260-
Scenario: Sharing metadata automatically includes shared example groups
261-
Given a file named "shared_example_metadata_spec.rb" with:
262-
"""ruby
263-
RSpec.shared_examples "shared stuff", :a => :b do
264-
it 'runs wherever the metadata is shared' do
265-
end
266-
end
267-
268-
RSpec.describe String, :a => :b do
269-
end
270-
"""
271-
When I run `rspec shared_example_metadata_spec.rb`
272-
Then the output should contain:
273-
"""
274-
1 example, 0 failures
275-
"""
276-
277260
Scenario: Shared examples are nestable by context
278261
Given a file named "context_specific_examples_spec.rb" with:
279262
"""Ruby

lib/rspec/core/configuration.rb

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -362,59 +362,6 @@ def treat_symbols_as_metadata_keys_with_true_values=(_value)
362362
)
363363
end
364364

365-
# @macro define_reader
366-
# Configures how RSpec treats metadata passed as part of a shared example
367-
# group definition. For example, given this shared example group definition:
368-
#
369-
# RSpec.shared_context "uses DB", :db => true do
370-
# around(:example) do |ex|
371-
# MyORM.transaction(:rollback => true, &ex)
372-
# end
373-
# end
374-
#
375-
# ...there are two ways RSpec can treat the `:db => true` metadata, each
376-
# of which has a corresponding config option:
377-
#
378-
# 1. `:trigger_inclusion`: this shared context will be implicitly included
379-
# in any groups (or examples) that have `:db => true` metadata.
380-
# 2. `:apply_to_host_groups`: the metadata will be inherited by the metadata
381-
# hash of all host groups and examples.
382-
#
383-
# `:trigger_inclusion` is the legacy behavior from before RSpec 3.5 but should
384-
# be considered deprecated. Instead, you can explicitly include a group with
385-
# `include_context`:
386-
#
387-
# RSpec.describe "My model" do
388-
# include_context "uses DB"
389-
# end
390-
#
391-
# ...or you can configure RSpec to include the context based on matching metadata
392-
# using an API that mirrors configured module inclusion:
393-
#
394-
# RSpec.configure do |rspec|
395-
# rspec.include_context "uses DB", :db => true
396-
# end
397-
#
398-
# `:apply_to_host_groups` is a new feature of RSpec 3.5 and will be the only
399-
# supported behavior in RSpec 4.
400-
#
401-
# @overload shared_context_metadata_behavior
402-
# @return [:trigger_inclusion, :apply_to_host_groups] the configured behavior
403-
# @overload shared_context_metadata_behavior=(value)
404-
# @param value [:trigger_inclusion, :apply_to_host_groups] sets the configured behavior
405-
define_reader :shared_context_metadata_behavior
406-
# @see shared_context_metadata_behavior
407-
def shared_context_metadata_behavior=(value)
408-
case value
409-
when :trigger_inclusion, :apply_to_host_groups
410-
@shared_context_metadata_behavior = value
411-
else
412-
raise ArgumentError, "Cannot set `RSpec.configuration." \
413-
"shared_context_metadata_behavior` to `#{value.inspect}`. Only " \
414-
"`:trigger_inclusion` and `:apply_to_host_groups` are valid values."
415-
end
416-
end
417-
418365
# Record the start time of the spec suite to measure load time.
419366
# return [Time]
420367
add_setting :start_time
@@ -530,7 +477,6 @@ def initialize
530477
@threadsafe = true
531478
@max_displayed_failure_line_count = 10
532479
@world = World::Null
533-
@shared_context_metadata_behavior = :trigger_inclusion
534480

535481
define_built_in_hooks
536482
end

lib/rspec/core/project_initializer/spec/spec_helper.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@
3737
mocks.verify_partial_doubles = true
3838
end
3939

40-
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41-
# have no way to turn it off -- the option exists only for backwards
42-
# compatibility in RSpec 3). It causes shared context metadata to be
43-
# inherited by the metadata hash of host groups and examples, rather than
44-
# triggering implicit auto-inclusion in groups with matching metadata.
45-
config.shared_context_metadata_behavior = :apply_to_host_groups
46-
4740
# The settings below are suggested to provide a good initial experience
4841
# with RSpec, but feel free to customize to your heart's content.
4942
=begin

lib/rspec/core/shared_example_group.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ def add(context, name, *metadata_args, &block)
125125
"block or remove the definition."
126126
end
127127

128-
if RSpec.configuration.shared_context_metadata_behavior == :trigger_inclusion
129-
return legacy_add(context, name, *metadata_args, &block)
130-
end
131-
132128
unless valid_name?(name)
133129
raise ArgumentError, "Shared example group names can only be a string, " \
134130
"symbol or module but got: #{name.inspect}"
@@ -152,24 +148,6 @@ def find(lookup_contexts, name)
152148

153149
private
154150

155-
# TODO: remove this in RSpec 4. This exists only to support
156-
# `config.shared_context_metadata_behavior == :trigger_inclusion`,
157-
# the legacy behavior of shared context metadata, which we do
158-
# not want to support in RSpec 4.
159-
def legacy_add(context, name, *metadata_args, &block)
160-
shared_module = SharedExampleGroupModule.new(name, block, {})
161-
162-
if valid_name?(name)
163-
warn_if_key_taken context, name, block
164-
shared_example_groups[context][name] = shared_module
165-
else
166-
metadata_args.unshift name
167-
end
168-
169-
return if metadata_args.empty?
170-
RSpec.configuration.include shared_module, *metadata_args
171-
end
172-
173151
def shared_example_groups
174152
@shared_example_groups ||= Hash.new { |hash, context| hash[context] = {} }
175153
end

spec/rspec/core/configuration_spec.rb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,31 +2758,6 @@ def strategy.order(list)
27582758
end
27592759
end
27602760

2761-
describe "#shared_context_metadata_behavior" do
2762-
it "defaults to :trigger_inclusion for backwards compatibility" do
2763-
expect(config.shared_context_metadata_behavior).to eq :trigger_inclusion
2764-
end
2765-
2766-
it "can be set to :apply_to_host_groups" do
2767-
config.shared_context_metadata_behavior = :apply_to_host_groups
2768-
expect(config.shared_context_metadata_behavior).to eq :apply_to_host_groups
2769-
end
2770-
2771-
it "can be set to :trigger_inclusion explicitly" do
2772-
config.shared_context_metadata_behavior = :trigger_inclusion
2773-
expect(config.shared_context_metadata_behavior).to eq :trigger_inclusion
2774-
end
2775-
2776-
it "cannot be set to any other values" do
2777-
expect {
2778-
config.shared_context_metadata_behavior = :another_value
2779-
}.to raise_error(ArgumentError, a_string_including(
2780-
"shared_context_metadata_behavior",
2781-
":another_value", ":trigger_inclusion", ":apply_to_host_groups"
2782-
))
2783-
end
2784-
end
2785-
27862761
# assigns files_or_directories_to_run and triggers post-processing
27872762
# via `files_to_run`.
27882763
def assign_files_or_directories_to_run(*value)

spec/rspec/core/example_group_spec.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,6 @@ def metadata_hash(*args)
149149
}.to raise_error(/ExampleGroups::CallingAnUndefinedMethod/)
150150
end
151151

152-
it "assigns the const before including shared contexts via metadata so error messages from eval'ing the context include the name" do
153-
RSpec.shared_context("foo", :foo) { bar }
154-
155-
expect {
156-
RSpec.describe("Including shared context via metadata", :foo)
157-
}.to raise_error(NameError,
158-
a_string_including('ExampleGroups::IncludingSharedContextViaMetadata', 'bar')
159-
)
160-
end
161-
162152
it 'does not have problems with example groups named "Core"' do
163153
RSpec.describe("Core")
164154
expect(defined?(::RSpec::ExampleGroups::Core)).to be

spec/rspec/core/metadata_spec.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,24 +310,6 @@ def metadata_for(*args)
310310
end
311311
end
312312

313-
context "generated by an unnested shared group included via metadata" do
314-
it "is an array containing an object with shared group name and inclusion location" do
315-
meta = nil
316-
317-
RSpec.shared_examples_for("some shared behavior", :include_it => true) do
318-
meta = example { }.metadata
319-
end
320-
321-
line = __LINE__ + 1
322-
RSpec.describe("Group", :include_it => true) { }
323-
324-
expect(meta[:shared_group_inclusion_backtrace]).to match [ an_object_having_attributes(
325-
:shared_group_name => "some shared behavior",
326-
:inclusion_location => a_string_including("#{Metadata.relative_path __FILE__}:#{line}")
327-
) ]
328-
end
329-
end
330-
331313
{
332314
:it_behaves_like => "generates a nested group",
333315
:include_examples => "adds the examples directly to the host group"

spec/rspec/core/shared_context_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@
3636

3737
it "runs the before each hooks in configuration before those of the shared context" do
3838
ordered_hooks = []
39-
RSpec.configure do |c|
40-
c.before(:each) { ordered_hooks << "config" }
41-
end
4239

43-
RSpec.shared_context("before each stuff", :example => :before_each_hook_order) do
40+
RSpec.shared_context("before each stuff") do
4441
before(:each) { ordered_hooks << "shared_context"}
4542
end
4643

47-
group = RSpec.describe "description", :example => :before_each_hook_order do
44+
RSpec.configure do |c|
45+
c.before(:each) { ordered_hooks << "config" }
46+
c.include_context "before each stuff"
47+
end
48+
49+
group = RSpec.describe "description" do
4850
before(:each) { ordered_hooks << "example_group" }
4951
example {}
5052
end

spec/rspec/core/shared_example_group_spec.rb

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ module Core
4040
describe shared_method_name do
4141
let(:group) { RSpec.describe('example group') }
4242

43-
before do
44-
RSpec.configuration.shared_context_metadata_behavior = :apply_to_host_groups
45-
end
46-
4743
define_method :define_shared_group do |*args, &block|
4844
group.send(shared_method_name, *args, &block)
4945
end
@@ -179,62 +175,7 @@ def find_implementation_block(registry, scope, name)
179175
end
180176
end
181177

182-
context "when `config.shared_context_metadata_behavior == :trigger_inclusion`" do
183-
before do
184-
RSpec.configuration.shared_context_metadata_behavior = :trigger_inclusion
185-
end
186-
187-
context "given a hash" do
188-
it "includes itself in matching example groups" do
189-
implementation = Proc.new { def self.bar; 'bar'; end }
190-
define_shared_group(:foo => :bar, &implementation)
191-
192-
matching_group = RSpec.describe "Group", :foo => :bar
193-
non_matching_group = RSpec.describe "Group"
194-
195-
expect(matching_group.bar).to eq("bar")
196-
expect(non_matching_group).not_to respond_to(:bar)
197-
end
198-
end
199-
200-
context "given a string and a hash" do
201-
it "captures the given string and block in the World's collection of shared example groups" do
202-
implementation = lambda { }
203-
define_shared_group("name", :foo => :bar, &implementation)
204-
expect(find_implementation_block(registry, group, "name")).to eq implementation
205-
end
206-
207-
it "delegates include on configuration" do
208-
implementation = Proc.new { def self.bar; 'bar'; end }
209-
define_shared_group("name", :foo => :bar, &implementation)
210-
211-
matching_group = RSpec.describe "Group", :foo => :bar
212-
non_matching_group = RSpec.describe "Group"
213-
214-
expect(matching_group.bar).to eq("bar")
215-
expect(non_matching_group).not_to respond_to(:bar)
216-
end
217-
end
218-
219-
it "displays a warning when adding a second shared example group with the same name" do
220-
group.send(shared_method_name, 'some shared group') {}
221-
original_declaration = [__FILE__, __LINE__ - 1].join(':')
222-
223-
warning = nil
224-
allow(::Kernel).to receive(:warn) { |msg| warning = msg }
225-
226-
group.send(shared_method_name, 'some shared group') {}
227-
second_declaration = [__FILE__, __LINE__ - 1].join(':')
228-
expect(warning).to include('some shared group', original_declaration, second_declaration)
229-
expect(warning).to_not include 'Called from'
230-
end
231-
end
232-
233-
context "when `config.shared_context_metadata_behavior == :apply_to_host_groups`" do
234-
before do
235-
RSpec.configuration.shared_context_metadata_behavior = :apply_to_host_groups
236-
end
237-
178+
describe "metadata" do
238179
it "does not auto-include the shared group based on passed metadata" do
239180
define_top_level_shared_group("name", :foo => :bar) do
240181
def self.bar; 'bar'; end
@@ -338,12 +279,6 @@ def self.bar; 'bar'; end
338279
end
339280

340281
context "when the group is included via `config.include_context` and matching metadata" do
341-
before do
342-
# To ensure we don't accidentally include shared contexts the
343-
# old way in this context, we disable the option here.
344-
RSpec.configuration.shared_context_metadata_behavior = :apply_to_host_groups
345-
end
346-
347282
describe "when it has a `let` and applies to an individual example via metadata" do
348283
it 'defines the `let` method correctly' do
349284
define_top_level_shared_group("name") do

0 commit comments

Comments
 (0)