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

Apply config hooks to previously defined groups. #2189

Merged
merged 1 commit into from
Mar 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Enhancements:

* Remove unneeded `:execution_result` example groups metadata, saving a
bit of memory. (Myron Marston, #2172)
* Apply hooks registered with `config` to previously defined groups.
(Myron Marston, #2189)

### 3.5.0.beta1 / 2016-02-06
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.4.3...v3.5.0.beta1)
Expand Down
17 changes: 17 additions & 0 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,8 @@ def apply_derived_metadata_to(metadata)
# @see #after
# @see #append_after
def before(scope=nil, *meta, &block)
on_existing_matching_groups({}) { |g| g.before(scope, *meta, &block) }

handle_suite_hook(scope, meta) do
@before_suite_hooks << Hooks::BeforeHook.new(block, {})
end || super(scope, *meta, &block)
Expand All @@ -1590,6 +1592,8 @@ def before(scope=nil, *meta, &block)
# @see #after
# @see #append_after
def prepend_before(scope=nil, *meta, &block)
on_existing_matching_groups({}) { |g| g.prepend_before(scope, *meta, &block) }

handle_suite_hook(scope, meta) do
@before_suite_hooks.unshift Hooks::BeforeHook.new(block, {})
end || super(scope, *meta, &block)
Expand All @@ -1605,6 +1609,8 @@ def prepend_before(scope=nil, *meta, &block)
# @see #before
# @see #prepend_before
def after(scope=nil, *meta, &block)
on_existing_matching_groups({}) { |g| g.after(scope, *meta, &block) }

handle_suite_hook(scope, meta) do
@after_suite_hooks.unshift Hooks::AfterHook.new(block, {})
end || super(scope, *meta, &block)
Expand All @@ -1625,11 +1631,22 @@ def after(scope=nil, *meta, &block)
# @see #before
# @see #prepend_before
def append_after(scope=nil, *meta, &block)
on_existing_matching_groups({}) { |g| g.append_after(scope, *meta, &block) }

handle_suite_hook(scope, meta) do
@after_suite_hooks << Hooks::AfterHook.new(block, {})
end || super(scope, *meta, &block)
end

# Registers `block` as an `around` hook.
#
# See {Hooks#around} for full `around` hook docs.
def around(scope=nil, *meta, &block)
on_existing_matching_groups({}) { |g| g.around(scope, *meta, &block) }

super(scope, *meta, &block)
end

# @private
def with_suite_hooks
return yield if dry_run?
Expand Down
9 changes: 1 addition & 8 deletions lib/rspec/core/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,7 @@ def hooks
private

# @private
class Hook
attr_reader :block, :options

def initialize(block, options)
@block = block
@options = options
end
end
Hook = Struct.new(:block, :options)

# @private
class BeforeHook < Hook
Expand Down
113 changes: 113 additions & 0 deletions spec/rspec/core/hooks_filtering_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,118 @@
module RSpec::Core
RSpec.describe "config block hook filtering" do
context "when hooks are defined after a group has been defined" do
it "still applies" do
sequence = []

group = RSpec.describe do
example { sequence << :ex_1 }
example { sequence << :ex_2 }
end

RSpec.configure do |c|
c.before(:context) { sequence << :before_cont_2 }
c.prepend_before(:context) { sequence << :before_cont_1 }

c.before(:example) { sequence << :before_ex_2 }
c.prepend_before(:example) { sequence << :before_ex_1 }

c.after(:context) { sequence << :after_cont_1 }
c.append_after(:context) { sequence << :after_cont_2 }

c.after(:example) { sequence << :after_ex_1 }
c.append_after(:example) { sequence << :after_ex_2 }

c.around(:example) do |ex|
sequence << :around_before_ex
ex.run
sequence << :around_after_ex
end
end

group.run

expect(sequence).to eq [
:before_cont_1, :before_cont_2,
:around_before_ex, :before_ex_1, :before_ex_2, :ex_1, :after_ex_1, :after_ex_2, :around_after_ex,
:around_before_ex, :before_ex_1, :before_ex_2, :ex_2, :after_ex_1, :after_ex_2, :around_after_ex,
:after_cont_1, :after_cont_2
]
end

it "applies only to groups with matching metadata" do
sequence = []

unmatching_group = RSpec.describe do
example { }
example { }
end

matching_group = RSpec.describe "", :run_hooks do
example { sequence << :ex_1 }
example { sequence << :ex_2 }
end

RSpec.configure do |c|
c.before(:context, :run_hooks) { sequence << :before_cont_2 }
c.prepend_before(:context, :run_hooks) { sequence << :before_cont_1 }

c.before(:example, :run_hooks) { sequence << :before_ex_2 }
c.prepend_before(:example, :run_hooks) { sequence << :before_ex_1 }

c.after(:context, :run_hooks) { sequence << :after_cont_1 }
c.append_after(:context, :run_hooks) { sequence << :after_cont_2 }

c.after(:example, :run_hooks) { sequence << :after_ex_1 }
c.append_after(:example, :run_hooks) { sequence << :after_ex_2 }

c.around(:example, :run_hooks) do |ex|
sequence << :around_before_ex
ex.run
sequence << :around_after_ex
end
end

expect { unmatching_group.run }.not_to change { sequence }.from([])

matching_group.run
expect(sequence).to eq [
:before_cont_1, :before_cont_2,
:around_before_ex, :before_ex_1, :before_ex_2, :ex_1, :after_ex_1, :after_ex_2, :around_after_ex,
:around_before_ex, :before_ex_1, :before_ex_2, :ex_2, :after_ex_1, :after_ex_2, :around_after_ex,
:after_cont_1, :after_cont_2
]
end

it "applies only to examples with matching metadata" do
sequence = []

group = RSpec.describe do
example("") { sequence << :ex_1 }
example("", :run_hooks) { sequence << :ex_2 }
end

RSpec.configure do |c|
c.before(:example, :run_hooks) { sequence << :before_ex_2 }
c.prepend_before(:example, :run_hooks) { sequence << :before_ex_1 }

c.after(:example, :run_hooks) { sequence << :after_ex_1 }
c.append_after(:example, :run_hooks) { sequence << :after_ex_2 }

c.around(:example, :run_hooks) do |ex|
sequence << :around_before_ex
ex.run
sequence << :around_after_ex
end
end

group.run
expect(sequence).to eq [
:ex_1,
:around_before_ex, :before_ex_1, :before_ex_2, :ex_2, :after_ex_1, :after_ex_2, :around_after_ex,
]
end
end

describe "unfiltered hooks" do
it "is run" do
filters = []
Expand Down