Skip to content

Commit

Permalink
Add currently_executing_a_context_hook? to ExampleGroup
Browse files Browse the repository at this point in the history
This is useful because at the moment the only way to determine if an
example is currently executing a context hook is to look at it's inspect
string. Here, we add a simple boolean flag which is triggered in before
and after context hook execution.

This is needed by rspec/rspec-rails#1501.
  • Loading branch information
Sam Phippen committed Dec 10, 2015
1 parent 5dadbf8 commit f8e555d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/rspec/core/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ def self.set_it_up(description, *args, &example_group_block)
)
ExampleGroups.assign_const(self)

@currently_executing_a_context_hook = false

hooks.register_globals(self, RSpec.configuration.hooks)
RSpec.configuration.configure_group(self)
end
Expand Down Expand Up @@ -486,15 +488,24 @@ def self.store_before_context_ivars(example_group_instance)
end
end

# Returns true if a `before(:context)` or `after(:context)`
# hook is currently executing.
def self.currently_executing_a_context_hook?
@currently_executing_a_context_hook
end

# @private
def self.run_before_context_hooks(example_group_instance)
set_ivars(example_group_instance, superclass_before_context_ivars)

@currently_executing_a_context_hook = true

ContextHookMemoized::Before.isolate_for_context_hook(example_group_instance) do
hooks.run(:before, :context, example_group_instance)
end
ensure
store_before_context_ivars(example_group_instance)
@currently_executing_a_context_hook = false
end

if RUBY_VERSION.to_f >= 1.9
Expand Down Expand Up @@ -525,11 +536,14 @@ def self.superclass_before_context_ivars
def self.run_after_context_hooks(example_group_instance)
set_ivars(example_group_instance, before_context_ivars)

@currently_executing_a_context_hook = true

ContextHookMemoized::After.isolate_for_context_hook(example_group_instance) do
hooks.run(:after, :context, example_group_instance)
end
ensure
before_context_ivars.clear
@currently_executing_a_context_hook = false
end

# Runs all the examples in this group.
Expand Down
45 changes: 45 additions & 0 deletions spec/rspec/core/example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,51 @@ def define_and_run_group(define_outer_example = false)
group.run
expect(order).to eq([:example, :after_example, :example, :after_example])
end

describe "#currently_executing_a_context_hook?" do
it "sets currently_executing_a_context_hook? to false initially" do
group = RSpec.describe
expect(group.currently_executing_a_context_hook?).to be false
end

it "sets currently_executing_a_context_hook? during before(:context) execution" do
group = RSpec.describe
hook_result = nil
group.before(:context) { hook_result = group.currently_executing_a_context_hook? }
group.example("") {}
group.run
expect(hook_result).to be true
end

it "does not set currently_executing_a_context_hook? outside of before(:context) execution" do
group = RSpec.describe
hook_result = nil

group.before(:context) { hook_result = group.currently_executing_a_context_hook? }
group.before(:each) { hook_result = group.currently_executing_a_context_hook? }
group.example("") {}
group.run
expect(hook_result).to be false
end

it "sets currently_executing_a_context_hook? during after(:context) execution" do
group = RSpec.describe
hook_result = nil

group.after(:context) { hook_result = group.currently_executing_a_context_hook? }
group.example("") {}
group.run
expect(hook_result).to be true
end

it "unsets currently_executing_a_context_hook? after an after(:context) hook is done" do
group = RSpec.describe
group.after(:context) { }
group.example("") {}
group.run
expect(group.currently_executing_a_context_hook?).to be false
end
end
end

it "runs the before alls in order" do
Expand Down

0 comments on commit f8e555d

Please sign in to comment.