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

Commit aa650db

Browse files
committed
Provide friendly warnings when we can’t access status file.
1 parent 85bcc85 commit aa650db

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

lib/rspec/core/configuration.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,17 @@ def files_to_run
841841
def last_run_statuses
842842
@last_run_statuses ||= Hash.new(UNKNOWN_STATUS).tap do |statuses|
843843
if (path = example_status_persistence_file_path)
844-
ExampleStatusPersister.load_from(path).inject(statuses) do |hash, example|
845-
hash[example.fetch(:example_id)] = example.fetch(:status)
846-
hash
844+
begin
845+
ExampleStatusPersister.load_from(path).inject(statuses) do |hash, example|
846+
hash[example.fetch(:example_id)] = example.fetch(:status)
847+
hash
848+
end
849+
rescue SystemCallError => e
850+
RSpec.warning "Could not read from #{path.inspect} (configured as " \
851+
"`config.example_status_persistence_file_path`) due " \
852+
"to a system error: #{e.inspect}. Please check that " \
853+
"the config option is set to an accessible, valid " \
854+
"file path", :call_site => nil
847855
end
848856
end
849857
end

lib/rspec/core/runner.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,14 @@ def run_specs(example_groups)
117117
private
118118

119119
def persist_example_statuses
120-
return unless @configuration.example_status_persistence_file_path
121-
122-
ExampleStatusPersister.persist(
123-
@world.all_examples,
124-
@configuration.example_status_persistence_file_path
125-
)
120+
return unless (path = @configuration.example_status_persistence_file_path)
121+
122+
ExampleStatusPersister.persist(@world.all_examples, path)
123+
rescue SystemCallError => e
124+
RSpec.warning "Could not write example statuses to #{path} (configured as " \
125+
"`config.example_status_persistence_file_path`) due to a " \
126+
"system error: #{e.inspect}. Please check that the config " \
127+
"option is set to an accessible, valid file path", :call_site => nil
126128
end
127129

128130
# @private

spec/integration/filtering_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,58 @@ def run_rerun_command_for_failing_spec
203203
expect(last_cmd_stdout).to match(/2 examples, 0 failures/)
204204
end
205205
end
206+
207+
context "when `config.example_status_persistence_file_path` is configured" do
208+
context "to an invalid file path (e.g. spec/spec_helper.rb/examples.txt)" do
209+
before do
210+
write_file_formatted "spec/1_spec.rb", "
211+
RSpec.configure do |c|
212+
c.example_status_persistence_file_path = 'spec/1_spec.rb/examples.txt'
213+
end
214+
RSpec.describe { example { } }
215+
"
216+
end
217+
218+
it 'emits a helpful warning to the user, and still runs the spec suite' do
219+
run_command "spec/1_spec.rb"
220+
221+
expect(last_cmd_stderr).to include(
222+
"WARNING: Could not write",
223+
"spec/1_spec.rb/examples.txt",
224+
"config.example_status_persistence_file_path",
225+
"Errno:"
226+
)
227+
expect(last_cmd_stdout).to include("1 example")
228+
end
229+
end
230+
231+
context "to a file path for which we lack permissions" do
232+
before do
233+
write_file_formatted "spec/1_spec.rb", "
234+
RSpec.configure do |c|
235+
c.example_status_persistence_file_path = 'spec/examples.txt'
236+
end
237+
RSpec.describe { example { } }
238+
"
239+
240+
write_file_formatted "spec/examples.txt", ""
241+
in_current_dir do
242+
FileUtils.chmod 0000, "spec/examples.txt"
243+
end
244+
end
245+
246+
247+
it 'emits a helpful warning to the user, and still runs the spec suite' do
248+
run_command "spec/1_spec.rb"
249+
250+
expect(last_cmd_stderr).to include(
251+
"WARNING: Could not read",
252+
"spec/examples.txt",
253+
"config.example_status_persistence_file_path",
254+
"Errno:"
255+
)
256+
expect(last_cmd_stdout).to include("1 example")
257+
end
258+
end
259+
end
206260
end

spec/support/aruba_support.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def run_command(cmd)
1717

1818
temp_stdout = StringIO.new
1919
temp_stderr = StringIO.new
20+
21+
# So that `RSpec.warning` will go to temp_stderr.
22+
allow(::Kernel).to receive(:warn) { |msg| temp_stderr.puts(msg) }
2023
cmd_parts = Shellwords.split(cmd)
2124

2225
handle_current_dir_change do

0 commit comments

Comments
 (0)