Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/rspec/snapshot/file_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ def read
# Writes the value to file, overwriting the file contents if either of the
# following is true:
# * The snapshot file does not already exist.
# * If it does not exist and the RAISE_ON_SNAPSHOT_CREATE environment variable is set throw
# * The UPDATE_SNAPSHOTS environment variable is set.
#
# TODO: Do not write to file if running in CI mode.
#
# @param [String] snapshot_name The snapshot name.
# @param [String] value The value to write to file.
def write(value)
Expand All @@ -68,7 +67,13 @@ def write(value)
end

private def file_does_not_exist?
!File.exist?(@snapshot_path)
return false if File.exist?(@snapshot_path)

if ENV.fetch('RAISE_ON_SNAPSHOT_CREATE', nil) == 'true'
raise "Snapshot file does not exist #{@snapshot_path}, " \
'set to raise when missing'
end
true
end
end
end
Expand Down
44 changes: 33 additions & 11 deletions spec/rspec/snapshot/file_operator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,45 @@
context 'when the snapshot does not exist' do
before do
allow(File).to receive(:exist?).and_return(false)
subject.write(value)
end

it 'checks for file existence' do
expect(File).to have_received(:exist?).with(relative_snapshot_path)
end
context 'RAISE_ON_SNAPSHOT_CREATE env var is not set' do
before do
subject.write(value)
end

it 'creates a new file instance' do
expect(File).to have_received(:new).with(relative_snapshot_path, 'w+')
end
it 'checks for file existence' do
expect(File).to have_received(:exist?).with(relative_snapshot_path)
end

it 'creates a new file instance' do
expect(File).to have_received(:new).with(relative_snapshot_path, 'w+')
end

it 'writes the value to the file' do
expect(file).to have_received(:write).with(value)
it 'writes the value to the file' do
expect(file).to have_received(:write).with(value)
end

it 'closes the file' do
expect(file).to have_received(:close)
end
end

it 'closes the file' do
expect(file).to have_received(:close)
context 'and the RAISE_ON_SNAPSHOT_CREATE env var is set' do
before do
allow(ENV).to receive(:fetch).and_call_original

allow(ENV).to receive(:fetch).with('RAISE_ON_SNAPSHOT_CREATE', nil)
.and_return('true')
end

it 'raises an error' do
expect { subject.write(value) }.to raise_error(
RuntimeError,
"Snapshot file does not exist #{relative_snapshot_path}, set to raise when missing"
)
expect(File).not_to have_received(:new).with(relative_snapshot_path, 'w+')
end
end
end

Expand Down
76 changes: 76 additions & 0 deletions spec/rspec/snapshot/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,82 @@ def dump(object)
end
end

context 'when RAISE_ON_SNAPSHOT_CREATE environment variable is set' do
before do
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with('RAISE_ON_SNAPSHOT_CREATE',
nil).and_return('true')
end

context 'and a snapshot file exists' do
let(:original_snapshot_value) { 'foo' }
let(:updated_snapshot_value) { 'bar' }
let(:snapshot_name) { 'update_existing_snapshot' }
let(:snapshot_path) do
current_directory_path.join('__snapshots__',
"#{snapshot_name}.snap")
end

before do
allow(ENV).to receive(:fetch).with('UPDATE_SNAPSHOTS',
nil).and_return(true)
end

let!(:actual) do
file = File.new(snapshot_path, 'w+')
file.write(original_snapshot_value)
file.close

expect(updated_snapshot_value).to match_snapshot(snapshot_name)

file = File.new(snapshot_path)
actual = file.read
file.close
actual
end

it 'does not throw and updates' do
expect(actual).to eq(updated_snapshot_value)
end
end

context 'and a snapshot file does not exist' do
let(:snapshot_value) { 'foo' }
let(:snapshot_name) { 'update_non_existing_snapshot' }
let(:snapshot_path) do
current_directory_path.join('__snapshots__',
"#{snapshot_name}.snap")
end

before do
file = instance_double(File)

allow(File).to receive(:new).and_return(file)
allow(file).to receive(:write)
allow(file).to receive(:close)
end

let(:actual) do
FileUtils.rm_f(snapshot_path)

expect(snapshot_value).to match_snapshot(snapshot_name)

file = File.new(snapshot_path)
actual = file.read
file.close
actual
end

it 'writes the snapshot with the current value' do
expect { actual }.to raise_error(
RuntimeError,
a_string_including("Snapshot file does not exist")
)
expect(File).not_to have_received(:new).with(snapshot_path, 'w+')
end
end
end

context 'when UPDATE_SNAPSHOTS environment variable is not set' do
before do
allow(ENV).to receive(:fetch).and_call_original
Expand Down