Skip to content

Commit

Permalink
Make World.reset also reset example group counts
Browse files Browse the repository at this point in the history
If we don't reset example group counts in custom runners that run from
the same process, example metadata end up being published with incorrect
scoped IDs.

World#example_group_counts_by_spec_file is merely added for testability.

Fixes rspec#2721
  • Loading branch information
agis committed May 2, 2020
1 parent b80f43c commit 2229c98
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rspec/core/world.rb
Expand Up @@ -5,7 +5,7 @@ module Core
# Internal container for global non-configuration data.
class World
# @private
attr_reader :example_groups, :filtered_examples
attr_reader :example_groups, :filtered_examples, :example_group_counts_by_spec_file

# Used internally to determine what to do when a SIGINT is received.
attr_accessor :wants_to_quit
Expand Down Expand Up @@ -53,6 +53,7 @@ def reset
example_groups.clear
@sources_by_path.clear if defined?(@sources_by_path)
@syntax_highlighter = nil
@example_group_counts_by_spec_file = Hash.new(0)
end

# @private
Expand Down
43 changes: 43 additions & 0 deletions spec/integration/filtering_spec.rb
Expand Up @@ -66,6 +66,49 @@ def run_rerun_command_for_failing_spec
end

context "passing a line-number filter" do
it "works with different custom runners used in the same process" do
result_counter = Class.new do
RSpec::Core::Formatters.register(self, :example_passed)

attr_accessor :passed_examples

def initialize(*)
@passed_examples = 0
end

def example_passed(notification)
@passed_examples += 1
end
end

spec_file = "spec/filtering_custom_runner_spec.rb"

write_file_formatted spec_file, "
RSpec.describe 'A group' do
example('ex 1') { }
example('ex 2') { }
end
"

spec_file_path = expand_path(spec_file)

formatter = result_counter.new
RSpec.configuration.add_formatter(formatter)
opts = RSpec::Core::ConfigurationOptions.new(["#{spec_file_path}[1:1]"])
RSpec::Core::Runner.new(opts).run(StringIO.new, StringIO.new)

expect(formatter.passed_examples).to eq 1

RSpec.clear_examples

formatter = result_counter.new
RSpec.configuration.add_formatter(formatter)
opts = RSpec::Core::ConfigurationOptions.new(["#{spec_file_path}[1:2]"])
RSpec::Core::Runner.new(opts).run(StringIO.new, StringIO.new)

expect(formatter.passed_examples).to eq 1
end

it "trumps exclusions, except for :if/:unless (which are absolute exclusions)" do
write_file_formatted 'spec/a_spec.rb', "
RSpec.configure do |c|
Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/core/world_spec.rb
Expand Up @@ -36,6 +36,14 @@ module RSpec::Core
RSpec.world.reset
}.to change(RSpec::ExampleGroups, :constants).to([])
end

it 'clears #example_group_counts_by_spec_file' do
RSpec.describe "group"

expect {
RSpec.world.reset
}.to change { world.example_group_counts_by_spec_file }.to be_empty
end
end

describe "#example_groups" do
Expand Down

0 comments on commit 2229c98

Please sign in to comment.