From d49af13454ef924f21898317ebfbc5e47660d9e9 Mon Sep 17 00:00:00 2001 From: Agis Anastasopoulos Date: Fri, 24 Apr 2020 13:44:39 +0300 Subject: [PATCH] Make World.reset also reset example group counts 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 #2721 --- lib/rspec/core/world.rb | 3 ++- spec/integration/filtering_spec.rb | 43 ++++++++++++++++++++++++++++++ spec/rspec/core/world_spec.rb | 8 ++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/rspec/core/world.rb b/lib/rspec/core/world.rb index 1edec7f487..d766424f00 100644 --- a/lib/rspec/core/world.rb +++ b/lib/rspec/core/world.rb @@ -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 @@ -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 diff --git a/spec/integration/filtering_spec.rb b/spec/integration/filtering_spec.rb index e8421f2d6b..781b4a96d1 100644 --- a/spec/integration/filtering_spec.rb +++ b/spec/integration/filtering_spec.rb @@ -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| diff --git a/spec/rspec/core/world_spec.rb b/spec/rspec/core/world_spec.rb index c8714633e4..7c0b52a3e4 100644 --- a/spec/rspec/core/world_spec.rb +++ b/spec/rspec/core/world_spec.rb @@ -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