Skip to content

Commit 366fee0

Browse files
Marc Neumannlevinmr
authored andcommitted
create sub snapshot folder if needed
1 parent 8893506 commit 366fee0

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

lib/rspec/snapshot/file_operator.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class FileOperator
1313
# @param [Hash] metadata The RSpec metadata for the current test.
1414
def initialize(snapshot_name, metadata)
1515
snapshot_dir = snapshot_dir(metadata)
16-
create_snapshot_dir(snapshot_dir)
1716
@snapshot_path = File.join(snapshot_dir, "#{snapshot_name}.snap")
17+
create_snapshot_dir(@snapshot_path)
1818
end
1919

2020
private def snapshot_dir(metadata)
@@ -26,7 +26,9 @@ def initialize(snapshot_name, metadata)
2626
end
2727

2828
private def create_snapshot_dir(snapshot_dir)
29-
FileUtils.mkdir_p(snapshot_dir)
29+
return if Dir.exist?(File.dirname(snapshot_dir))
30+
31+
FileUtils.mkdir_p(File.dirname(snapshot_dir))
3032
end
3133

3234
# @return [String] The snapshot file contents.

spec/rspec/snapshot/file_operator_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838

3939
context 'when RSpec is configured with a fixed snapshot directory' do
4040
let(:fixed_snapshot_dir) { 'spec/snapshots' }
41+
let(:snapshot_name) do
42+
'any_sub_folder/any_sub_sub_folder/descriptive_snapshot_name'
43+
end
4144
let(:fixed_snapshot_path) do
4245
"#{fixed_snapshot_dir}/#{snapshot_name}.snap"
4346
end
@@ -51,7 +54,9 @@
5154

5255
it 'creates the snapshot directory if needed' do
5356
expect(FileUtils).to(
54-
have_received(:mkdir_p).with(fixed_snapshot_dir)
57+
have_received(:mkdir_p).with(
58+
"#{fixed_snapshot_dir}/any_sub_folder/any_sub_sub_folder"
59+
)
5560
)
5661
end
5762

0 commit comments

Comments
 (0)