Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

before/after(:all) hook filtering fix #161

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 32 additions & 1 deletion features/filtering/exclusion_filters.feature
Expand Up @@ -76,4 +76,35 @@ Feature: exclusion filters
Then the output should contain "No examples were matched. Perhaps {:broken=>true} is excluding everything?"
And the output should contain "0 examples, 0 failures"
And the output should not contain "group 1"
And the output should not contain "group 2"
And the output should not contain "group 2"

Scenario: before/after(:all) hook in excluded example group
Given a file named "spec/before_after_all_exclusion_filter_spec.rb" with:
"""
RSpec.configure do |c|
c.filter_run_excluding :broken => true
end

describe "group 1" do
before(:all) { puts "before all in focused group" }
after(:all) { puts "after all in focused group" }

it "group 1 example" do
end
end

describe "group 2", :broken => true do
before(:all) { puts "before all in unfocused group" }
after(:all) { puts "after all in unfocused group" }

context "context 1" do
it "group 2 context 1 example 1" do
end
end
end
"""
When I run "rspec ./spec/before_after_all_exclusion_filter_spec.rb"
Then the output should contain "before all in focused group"
And the output should contain "after all in focused group"
And the output should not contain "before all in unfocused group"
And the output should not contain "after all in unfocused group"
30 changes: 30 additions & 0 deletions features/filtering/inclusion_filters.feature
Expand Up @@ -72,3 +72,33 @@ Feature: inclusion filters
And the output should contain "group 2 example 1"
And the output should contain "3 examples, 0 failures"

Scenario: before/after(:all) hook in unmatched example group
Given a file named "spec/before_after_all_inclusion_filter_spec.rb" with:
"""
RSpec.configure do |c|
c.filter_run :focus => true
end

describe "group 1", :focus => true do
before(:all) { puts "before all in focused group" }
after(:all) { puts "after all in focused group" }

it "group 1 example" do
end
end

describe "group 2" do
before(:all) { puts "before all in unfocused group" }
after(:all) { puts "after all in unfocused group" }

context "context 1" do
it "group 2 context 1 example 1" do
end
end
end
"""
When I run "rspec ./spec/before_after_all_inclusion_filter_spec.rb"
Then the output should contain "before all in focused group"
And the output should contain "after all in focused group"
And the output should not contain "before all in unfocused group"
And the output should not contain "after all in unfocused group"
8 changes: 6 additions & 2 deletions lib/rspec/core/example_group.rb
Expand Up @@ -97,6 +97,10 @@ def self.descendant_filtered_examples
@descendant_filtered_examples ||= filtered_examples + children.collect{|c| c.descendant_filtered_examples}
end

def self.any_descendant_filtered_examples?
descendant_filtered_examples.flatten.any?
end

def self.metadata
@metadata if defined?(@metadata)
end
Expand Down Expand Up @@ -168,7 +172,7 @@ def self.assign_before_all_ivars(ivars, example_group_instance)
end

def self.eval_before_alls(example_group_instance)
return if descendant_filtered_examples.empty?
return unless any_descendant_filtered_examples?
assign_before_all_ivars(superclass.before_all_ivars, example_group_instance)
world.run_hook_filtered(:before, :all, self, example_group_instance) if top_level?
run_hook!(:before, :all, example_group_instance)
Expand All @@ -193,7 +197,7 @@ def self.eval_after_eachs(example_group_instance)
end

def self.eval_after_alls(example_group_instance)
return if descendant_filtered_examples.empty?
return unless any_descendant_filtered_examples?
assign_before_all_ivars(before_all_ivars, example_group_instance)
run_hook!(:after, :all, example_group_instance)
world.run_hook_filtered(:after, :all, self, example_group_instance) if top_level?
Expand Down
31 changes: 31 additions & 0 deletions spec/rspec/core/example_group_spec.rb
Expand Up @@ -260,6 +260,37 @@ module RSpec::Core
order.should == [3,2,1]
end

it "only runs before/after(:all) hooks from example groups that have specs that run" do
hooks_run = []

RSpec.configure do |c|
c.filter_run :focus => true
end

unfiltered_group = ExampleGroup.describe "unfiltered" do
before(:all) { hooks_run << :unfiltered_before_all }
after(:all) { hooks_run << :unfiltered_after_all }

context "a subcontext" do
it("has an example") { }
end
end

filtered_group = ExampleGroup.describe "filtered", :focus => true do
before(:all) { hooks_run << :filtered_before_all }
after(:all) { hooks_run << :filtered_after_all }

context "a subcontext" do
it("has an example") { }
end
end

unfiltered_group.run_all
filtered_group.run_all

hooks_run.should == [:filtered_before_all, :filtered_after_all]
end

it "runs before_all_defined_in_config, before all, before each, example, after each, after all, after_all_defined_in_config in that order" do
order = []

Expand Down