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

Commit 312fd77

Browse files
committed
Always limit loaded files when --only-failures is used.
Before, we only did so when no file or directory arg was passed to `rspec`, but it's better to take the intersection of files matching the provided args and files with failures.
1 parent 7e50a3d commit 312fd77

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

lib/rspec/core/configuration.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ def initialize
327327
@after_suite_hooks = []
328328

329329
@mock_framework = nil
330-
@files_or_directories_to_run_defaulted = false
331330
@files_or_directories_to_run = []
332331
@loaded_spec_files = Set.new
333332
@color = false
@@ -821,10 +820,7 @@ def files_or_directories_to_run=(*files)
821820
files = files.flatten
822821

823822
if (command == 'rspec' || Runner.running_in_drb?) && default_path && files.empty?
824-
@files_or_directories_to_run_defaulted = true
825823
files << default_path
826-
else
827-
@files_or_directories_to_run_defaulted = false
828824
end
829825

830826
@files_or_directories_to_run = files
@@ -834,14 +830,7 @@ def files_or_directories_to_run=(*files)
834830
# The spec files RSpec will run.
835831
# @return [Array] specified files about to run
836832
def files_to_run
837-
@files_to_run ||= begin
838-
if @files_or_directories_to_run_defaulted && only_failures?
839-
files_with_failures = spec_files_with_failures.to_a
840-
@files_or_directories_to_run = files_with_failures if files_with_failures.any?
841-
end
842-
843-
get_files_to_run(@files_or_directories_to_run)
844-
end
833+
@files_to_run ||= get_files_to_run(@files_or_directories_to_run)
845834
end
846835

847836
# @private
@@ -1624,10 +1613,15 @@ def run_hooks_with(hooks, hook_context)
16241613
end
16251614

16261615
def get_files_to_run(paths)
1627-
FlatMap.flat_map(paths_to_check(paths)) do |path|
1616+
files = FlatMap.flat_map(paths_to_check(paths)) do |path|
16281617
path = path.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
16291618
File.directory?(path) ? gather_directories(path) : extract_location(path)
16301619
end.sort.uniq
1620+
1621+
return files unless only_failures?
1622+
relative_files = files.map { |f| Metadata.relative_path(File.expand_path f) }
1623+
with_failures = (relative_files & spec_files_with_failures.to_a)
1624+
with_failures.empty? ? files : with_failures
16311625
end
16321626

16331627
def paths_to_check(paths)

spec/rspec/core/configuration/only_failures_support_spec.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,14 @@ def allows_value_to_change_when_updated
132132
end
133133

134134
describe "#files_to_run, when `only_failures` is set" do
135-
around { |ex| Dir.chdir("spec/rspec/core", &ex) }
135+
around do |ex|
136+
handle_current_dir_change do
137+
Dir.chdir("spec/rspec/core", &ex)
138+
end
139+
end
140+
136141
let(:default_path) { "resources" }
137-
let(:files_with_failures) { ["resources/a_spec.rb"] }
142+
let(:files_with_failures) { ["./resources/a_spec.rb"] }
138143
let(:files_loaded_via_default_path) do
139144
configuration = Configuration.new
140145
configuration.default_path = default_path
@@ -167,16 +172,14 @@ def allows_value_to_change_when_updated
167172
end
168173

169174
context "and a path has been set" do
170-
it "ignores the list of files with failures, loading the configured path instead" do
171-
config.files_or_directories_to_run = ["resources/acceptance"]
172-
expect(config.files_to_run).to contain_files("resources/acceptance/foo_spec.rb")
175+
it "loads the intersection of files matching the path and files with failures" do
176+
config.files_or_directories_to_run = ["resources"]
177+
expect(config.files_to_run).to eq(files_with_failures)
173178
end
174-
end
175179

176-
context "and the default path has been explicitly set" do
177-
it "ignores the list of files with failures, loading the configured path instead" do
178-
config.files_or_directories_to_run = [default_path]
179-
expect(config.files_to_run).to eq(files_loaded_via_default_path)
180+
it "loads all files matching the path when there are no intersecting files" do
181+
config.files_or_directories_to_run = ["resources/acceptance"]
182+
expect(config.files_to_run).to contain_files("resources/acceptance/foo_spec.rb")
180183
end
181184
end
182185
end

spec/spec_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,20 @@ def without_env_vars(*vars)
5353
ENV.replace(original)
5454
end
5555
end
56+
57+
def handle_current_dir_change
58+
RSpec::Core::Metadata.instance_variable_set(:@relative_path_regex, nil)
59+
yield
60+
ensure
61+
RSpec::Core::Metadata.instance_variable_set(:@relative_path_regex, nil)
62+
end
5663
end
5764

5865
RSpec.configure do |c|
5966
c.example_status_persistence_file_path = "./spec/examples.txt"
67+
c.around(:example, :isolated_directory) do |ex|
68+
handle_current_dir_change(&ex)
69+
end
6070

6171
# structural
6272
c.alias_it_behaves_like_to 'it_has_behavior'

spec/support/aruba_support.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ def run_command(cmd)
1717

1818
temp_stdout = StringIO.new
1919
temp_stderr = StringIO.new
20-
RSpec::Core::Metadata.instance_variable_set(:@relative_path_regex, nil)
2120
cmd_parts = Shellwords.split(cmd)
2221

23-
in_current_dir do
24-
RSpec::Core::Runner.run(cmd_parts, temp_stderr, temp_stdout)
22+
handle_current_dir_change do
23+
in_current_dir do
24+
RSpec::Core::Runner.run(cmd_parts, temp_stderr, temp_stdout)
25+
end
2526
end
2627
ensure
2728
RSpec.reset
2829
RSpec.configuration.color = true
29-
RSpec::Core::Metadata.instance_variable_set(:@relative_path_regex, nil)
3030

3131
# Ensure it gets cached with a proper value -- if we leave it set to nil,
3232
# and the next spec operates in a different dir, it could get set to an

0 commit comments

Comments
 (0)