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

Commit ca9ef62

Browse files
committed
Remove shared_context_metadata_behavior config option
Fixes #2832
1 parent 0c3a9d8 commit ca9ef62

File tree

10 files changed

+10
-219
lines changed

10 files changed

+10
-219
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, #????)
911

1012
Enhancements:
1113

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

lib/rspec/core/configuration.rb

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -369,59 +369,6 @@ def treat_symbols_as_metadata_keys_with_true_values=(_value)
369369
)
370370
end
371371

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

542488
define_built_in_hooks
543489
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
@@ -2769,31 +2769,6 @@ def strategy.order(list)
27692769
end
27702770
end
27712771

2772-
describe "#shared_context_metadata_behavior" do
2773-
it "defaults to :trigger_inclusion for backwards compatibility" do
2774-
expect(config.shared_context_metadata_behavior).to eq :trigger_inclusion
2775-
end
2776-
2777-
it "can be set to :apply_to_host_groups" do
2778-
config.shared_context_metadata_behavior = :apply_to_host_groups
2779-
expect(config.shared_context_metadata_behavior).to eq :apply_to_host_groups
2780-
end
2781-
2782-
it "can be set to :trigger_inclusion explicitly" do
2783-
config.shared_context_metadata_behavior = :trigger_inclusion
2784-
expect(config.shared_context_metadata_behavior).to eq :trigger_inclusion
2785-
end
2786-
2787-
it "cannot be set to any other values" do
2788-
expect {
2789-
config.shared_context_metadata_behavior = :another_value
2790-
}.to raise_error(ArgumentError, a_string_including(
2791-
"shared_context_metadata_behavior",
2792-
":another_value", ":trigger_inclusion", ":apply_to_host_groups"
2793-
))
2794-
end
2795-
end
2796-
27972772
# assigns files_or_directories_to_run and triggers post-processing
27982773
# via `files_to_run`.
27992774
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)