Skip to content

Commit 98a028b

Browse files
author
Sam Phippen
committed
Add currently_executing_a_context_hook? to ExampleGroup
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.
1 parent 3226e51 commit 98a028b

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

lib/rspec/core/example_group.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ def self.set_it_up(description, *args, &example_group_block)
414414
)
415415
ExampleGroups.assign_const(self)
416416

417+
@currently_executing_a_context_hook = false
418+
417419
hooks.register_globals(self, RSpec.configuration.hooks)
418420
RSpec.configuration.configure_group(self)
419421
end
@@ -486,15 +488,24 @@ def self.store_before_context_ivars(example_group_instance)
486488
end
487489
end
488490

491+
# Returns true if a `before(:context)` or `after(:context)`
492+
# hook is currently executing.
493+
def self.currently_executing_a_context_hook?
494+
@currently_executing_a_context_hook
495+
end
496+
489497
# @private
490498
def self.run_before_context_hooks(example_group_instance)
491499
set_ivars(example_group_instance, superclass_before_context_ivars)
492500

501+
@currently_executing_a_context_hook = true
502+
493503
ContextHookMemoized::Before.isolate_for_context_hook(example_group_instance) do
494504
hooks.run(:before, :context, example_group_instance)
495505
end
496506
ensure
497507
store_before_context_ivars(example_group_instance)
508+
@currently_executing_a_context_hook = false
498509
end
499510

500511
if RUBY_VERSION.to_f >= 1.9
@@ -525,11 +536,14 @@ def self.superclass_before_context_ivars
525536
def self.run_after_context_hooks(example_group_instance)
526537
set_ivars(example_group_instance, before_context_ivars)
527538

539+
@currently_executing_a_context_hook = true
540+
528541
ContextHookMemoized::After.isolate_for_context_hook(example_group_instance) do
529542
hooks.run(:after, :context, example_group_instance)
530543
end
531544
ensure
532545
before_context_ivars.clear
546+
@currently_executing_a_context_hook = false
533547
end
534548

535549
# Runs all the examples in this group.

spec/rspec/core/example_group_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,51 @@ def define_and_run_group(define_outer_example = false)
713713
group.run
714714
expect(order).to eq([:example, :after_example, :example, :after_example])
715715
end
716+
717+
describe "#currently_executing_a_context_hook?" do
718+
it "sets currently_executing_a_context_hook? to false initially" do
719+
group = RSpec.describe
720+
expect(group.currently_executing_a_context_hook?).to be false
721+
end
722+
723+
it "sets currently_executing_a_context_hook? during before(:context) execution" do
724+
group = RSpec.describe
725+
hook_result = nil
726+
group.before(:context) { hook_result = group.currently_executing_a_context_hook? }
727+
group.example("") {}
728+
group.run
729+
expect(hook_result).to be true
730+
end
731+
732+
it "does not set currently_executing_a_context_hook? outside of before(:context) execution" do
733+
group = RSpec.describe
734+
hook_result = nil
735+
736+
group.before(:context) { hook_result = group.currently_executing_a_context_hook? }
737+
group.before(:each) { hook_result = group.currently_executing_a_context_hook? }
738+
group.example("") {}
739+
group.run
740+
expect(hook_result).to be false
741+
end
742+
743+
it "sets currently_executing_a_context_hook? during after(:context) execution" do
744+
group = RSpec.describe
745+
hook_result = nil
746+
747+
group.after(:context) { hook_result = group.currently_executing_a_context_hook? }
748+
group.example("") {}
749+
group.run
750+
expect(hook_result).to be true
751+
end
752+
753+
it "unsets currently_executing_a_context_hook? after an after(:context) hook is done" do
754+
group = RSpec.describe
755+
group.after(:context) { }
756+
group.example("") {}
757+
group.run
758+
expect(group.currently_executing_a_context_hook?).to be false
759+
end
760+
end
716761
end
717762

718763
it "runs the before alls in order" do

0 commit comments

Comments
 (0)