Skip to content

Commit d921ab4

Browse files
committed
Merge pull request rspec#1796 from rspec/fix-all-apply
Fix all apply
2 parents 1ec3fc3 + ec0a877 commit d921ab4

File tree

11 files changed

+64
-37
lines changed

11 files changed

+64
-37
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Bug Fixes:
2828
(Myron Marston, #1771)
2929
* Don't consider expectations from `after` hooks when generating
3030
example descriptions. (Myron Marston, #1771)
31+
* Don't apply metadata-filtered config hooks to examples in groups
32+
with matching metadata when those example override the parent
33+
metadata value to not match. (Myron Marston, #1796)
3134

3235
### 3.1.8 Development
3336

features/expectation_framework_integration/configure_expectation_framework.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Feature: configure expectation framework
120120
When I run `rspec example_spec.rb`
121121
Then the examples should all pass
122122

123-
Scenario: Configure rspec/expecations AND minitest assertions
123+
Scenario: Configure rspec/expectations AND minitest assertions
124124
Given a file named "example_spec.rb" with:
125125
"""ruby
126126
RSpec.configure do |config|

lib/rspec/core/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ def extend(mod, *filters)
10921092
# `extend`.
10931093
def configure_group(group)
10941094
include_or_extend_modules.each do |include_or_extend, mod, filters|
1095-
next unless filters.empty? || group.any_apply?(filters)
1095+
next unless filters.empty? || group.apply?(:any?, filters)
10961096
__send__("safe_#{include_or_extend}", mod, group)
10971097
end
10981098
end
@@ -1359,7 +1359,7 @@ def define_derived_metadata(*filters, &block)
13591359
# @private
13601360
def apply_derived_metadata_to(metadata)
13611361
@derived_metadata_blocks.each do |filter, block|
1362-
block.call(metadata) if filter.empty? || MetadataFilter.any_apply?(filter, metadata)
1362+
block.call(metadata) if filter.empty? || MetadataFilter.apply?(:any?, filter, metadata)
13631363
end
13641364
end
13651365

lib/rspec/core/example.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,8 @@ def inspect
266266
end
267267

268268
# @private
269-
def any_apply?(filters)
270-
MetadataFilter.any_apply?(filters, metadata)
271-
end
272-
273-
# @private
274-
def all_apply?(filters)
275-
MetadataFilter.all_apply?(filters, metadata) || @example_group_class.all_apply?(filters)
269+
def apply?(predicate, filters)
270+
MetadataFilter.apply?(predicate, filters, metadata)
276271
end
277272

278273
# @private

lib/rspec/core/example_group.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,8 @@ def self.fail_fast?
536536
end
537537

538538
# @private
539-
def self.any_apply?(filters)
540-
MetadataFilter.any_apply?(filters, metadata)
541-
end
542-
543-
# @private
544-
def self.all_apply?(filters)
545-
MetadataFilter.all_apply?(filters, metadata)
539+
def self.apply?(predicate, filters)
540+
MetadataFilter.apply?(predicate, filters, metadata)
546541
end
547542

548543
# @private

lib/rspec/core/filter_manager.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def use(*args)
217217
end
218218

219219
def include_example?(example)
220-
@rules.empty? ? true : example.any_apply?(@rules)
220+
@rules.empty? ? true : example.apply?(:any?, @rules)
221221
end
222222

223223
def standalone?
@@ -252,7 +252,7 @@ class ExclusionRules < FilterRules
252252
}.freeze
253253

254254
def include_example?(example)
255-
example.any_apply?(@rules) || example.any_apply?(CONDITIONAL_FILTERS)
255+
example.apply?(:any?, @rules) || example.apply?(:any?, CONDITIONAL_FILTERS)
256256
end
257257
end
258258
end

lib/rspec/core/hooks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def initialize(block, options)
347347
end
348348

349349
def options_apply?(example_or_group)
350-
example_or_group.all_apply?(options)
350+
example_or_group.apply?(:all?, options)
351351
end
352352
end
353353

lib/rspec/core/metadata_filter.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ module Core
88
module MetadataFilter
99
class << self
1010
# @private
11-
def any_apply?(filters, metadata)
12-
filters.any? { |k, v| filter_applies?(k, v, metadata) }
13-
end
14-
15-
# @private
16-
def all_apply?(filters, metadata)
17-
filters.all? { |k, v| filter_applies?(k, v, metadata) }
11+
def apply?(predicate, filters, metadata)
12+
filters.__send__(predicate) { |k, v| filter_applies?(k, v, metadata) }
1813
end
1914

2015
# @private

spec/rspec/core/filter_manager_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ def example_with(*args)
183183
filter_manager.include_with_low_priority :foo => :bar
184184
expect(filter_manager.prune([included, excluded])).to eq([included])
185185
end
186+
187+
context "with multiple inclusion filters" do
188+
it 'includes objects that match any of them' do
189+
examples = [
190+
included_1 = example_with(:foo => true),
191+
included_2 = example_with(:bar => true),
192+
example_with(:bazz => true)
193+
]
194+
195+
filter_manager.include :foo => true, :bar => true
196+
expect(filter_manager.prune(examples)).to contain_exactly(included_1, included_2)
197+
end
198+
end
186199
end
187200

188201
describe "#inclusions#description" do

spec/rspec/core/hooks_filtering_spec.rb

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,43 @@ def filters
209209
end
210210

211211
it "does not run if some hook filters don't match the group's filters" do
212+
sequence = []
213+
214+
RSpec.configure do |c|
215+
c.before(:all, :one => 1, :four => 4) { sequence << "before all in config"}
216+
c.around(:each, :two => 2, :four => 4) {|example| sequence << "around each in config"; example.run}
217+
c.before(:each, :one => 1, :two => 2, :four => 4) { sequence << "before each in config"}
218+
c.after(:each, :one => 1, :two => 2, :three => 3, :four => 4) { sequence << "after each in config"}
219+
c.after(:all, :one => 1, :three => 3, :four => 4) { sequence << "after all in config"}
220+
end
221+
222+
RSpec.describe "group", :one => 1, :two => 2, :three => 3 do
223+
example("ex1") { sequence << "ex1" }
224+
example("ex2", :four => 4) { sequence << "ex2" }
225+
end.run
226+
227+
expect(sequence).to eq([
228+
"ex1",
229+
"around each in config",
230+
"before each in config",
231+
"ex2",
232+
"after each in config",
233+
])
234+
end
235+
236+
it "does not run for examples that do not match, even if their group matches" do
212237
filters = []
238+
213239
RSpec.configure do |c|
214-
c.before(:all, :one => 1, :four => 4) { filters << "before all in config"}
215-
c.around(:each, :two => 2, :four => 4) {|example| filters << "around each in config"; example.run}
216-
c.before(:each, :one => 1, :two => 2, :four => 4) { filters << "before each in config"}
217-
c.after(:each, :one => 1, :two => 2, :three => 3, :four => 4) { filters << "after each in config"}
218-
c.after(:all, :one => 1, :three => 3, :four => 4) { filters << "after all in config"}
240+
c.before(:each, :apply_it) { filters << :before_each }
219241
end
220-
group = ExampleGroup.describe(:one => 1, :two => 2, :three => 3)
221-
group.example("example") {}
222-
group.run
223-
expect(filters).to eq([])
242+
243+
RSpec.describe "Group", :apply_it do
244+
example("ex1") { filters << :matching_example }
245+
example("ex2", :apply_it => false) { filters << :nonmatching_example }
246+
end.run
247+
248+
expect(filters).to eq([:before_each, :matching_example, :nonmatching_example])
224249
end
225250
end
226251
end

0 commit comments

Comments
 (0)