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

Commit 98b8461

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 5dadbf8 commit 98b8461

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

lib/rspec/core/example_group.rb

Lines changed: 13 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,23 @@ def self.store_before_context_ivars(example_group_instance)
486488
end
487489
end
488490

491+
# @private
492+
def self.currently_executing_a_context_hook?
493+
@currently_executing_a_context_hook
494+
end
495+
489496
# @private
490497
def self.run_before_context_hooks(example_group_instance)
491498
set_ivars(example_group_instance, superclass_before_context_ivars)
492499

500+
@currently_executing_a_context_hook = true
501+
493502
ContextHookMemoized::Before.isolate_for_context_hook(example_group_instance) do
494503
hooks.run(:before, :context, example_group_instance)
495504
end
496505
ensure
497506
store_before_context_ivars(example_group_instance)
507+
@currently_executing_a_context_hook = false
498508
end
499509

500510
if RUBY_VERSION.to_f >= 1.9
@@ -525,11 +535,14 @@ def self.superclass_before_context_ivars
525535
def self.run_after_context_hooks(example_group_instance)
526536
set_ivars(example_group_instance, before_context_ivars)
527537

538+
@currently_executing_a_context_hook = true
539+
528540
ContextHookMemoized::After.isolate_for_context_hook(example_group_instance) do
529541
hooks.run(:after, :context, example_group_instance)
530542
end
531543
ensure
532544
before_context_ivars.clear
545+
@currently_executing_a_context_hook = false
533546
end
534547

535548
# 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)