Skip to content

Commit db9eb9f

Browse files
committed
[core] Merge pull request rspec/rspec-core#2234 from rspec/myron/fix-2232
Fix filtering based on `:example_group` to work for example groups. --- This commit was imported from rspec/rspec-core@faa28ac.
1 parent a497b65 commit db9eb9f

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

rspec-core/Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Bug Fixes:
66
* Use the encoded string logic for source extraction. (Jon Rowe, #2183)
77
* Fix rounding issue in duration formatting helper. (Fabersky, Jon Rowe, #2208)
88
* Fix `NoMethodError` caused by Java backtraces. (Michele Piccirillo, #2244)
9+
* Fix deprecated `:example_group`-based filtering so that it properly
10+
applies to matching example groups. (Myron Marston, #2234)
911

1012
### 3.4.4 / 2016-03-09
1113
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.4.3...v3.4.4)

rspec-core/lib/rspec/core/metadata_filter.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def filter_applies?(key, value, metadata)
3434
end
3535
end
3636

37+
# @private
38+
def silence_metadata_example_group_deprecations
39+
RSpec::Support.thread_local_data[:silence_metadata_example_group_deprecations] = true
40+
yield
41+
ensure
42+
RSpec::Support.thread_local_data.delete(:silence_metadata_example_group_deprecations)
43+
end
44+
3745
private
3846

3947
def filter_applies_to_any_value?(key, value, metadata)
@@ -72,13 +80,6 @@ def filters_apply?(key, value, metadata)
7280
return false unless Hash === subhash || HashImitatable === subhash
7381
value.all? { |k, v| filter_applies?(k, v, subhash) }
7482
end
75-
76-
def silence_metadata_example_group_deprecations
77-
RSpec::Support.thread_local_data[:silence_metadata_example_group_deprecations] = true
78-
yield
79-
ensure
80-
RSpec::Support.thread_local_data.delete(:silence_metadata_example_group_deprecations)
81-
end
8283
end
8384
end
8485

@@ -202,9 +203,20 @@ def handle_mutation(metadata)
202203
end
203204

204205
def applicable_metadata_from(metadata)
205-
@applicable_keys.inject({}) do |hash, key|
206-
hash[key] = metadata[key] if metadata.key?(key)
207-
hash
206+
MetadataFilter.silence_metadata_example_group_deprecations do
207+
@applicable_keys.inject({}) do |hash, key|
208+
# :example_group is treated special here because...
209+
# - In RSpec 2, example groups had an `:example_group` key
210+
# - In RSpec 3, that key is deprecated (it was confusing!).
211+
# - The key is not technically present in an example group metadata hash
212+
# (and thus would fail the `metadata.key?(key)` check) but a value
213+
# is provided when accessed via the hash's `default_proc`
214+
# - Thus, for backwards compatibility, we have to explicitly check
215+
# for `:example_group` here if it is one of the keys being used to
216+
# filter.
217+
hash[key] = metadata[key] if metadata.key?(key) || key == :example_group
218+
hash
219+
end
208220
end
209221
end
210222

rspec-core/spec/rspec/core/configuration_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,16 @@ def metadata_hash(*args)
938938
expect(group.new.you_call_this_a_blt?).to eq("egad man, where's the mayo?!?!?")
939939
end
940940

941+
it "includes in example groups that match a deprecated `:example_group` filter" do
942+
RSpec.configure do |c|
943+
c.include(InstanceLevelMethods, :example_group => { :file_path => /./ })
944+
end
945+
946+
group = RSpec.describe('does like, stuff and junk')
947+
expect(group).not_to respond_to(:you_call_this_a_blt?)
948+
expect(group.new.you_call_this_a_blt?).to eq("egad man, where's the mayo?!?!?")
949+
end
950+
941951
it "includes the given module into each existing matching example group" do
942952
matching_group = RSpec.describe('does like, stuff and junk', :magic_key => :include) { }
943953
non_matching_group = RSpec.describe

0 commit comments

Comments
 (0)