diff --git a/lib/rspec/snapshot/file_operator.rb b/lib/rspec/snapshot/file_operator.rb index 9cc5f60..a290f3c 100644 --- a/lib/rspec/snapshot/file_operator.rb +++ b/lib/rspec/snapshot/file_operator.rb @@ -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) @@ -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 diff --git a/spec/rspec/snapshot/file_operator_spec.rb b/spec/rspec/snapshot/file_operator_spec.rb index 6a0c868..ef0d136 100644 --- a/spec/rspec/snapshot/file_operator_spec.rb +++ b/spec/rspec/snapshot/file_operator_spec.rb @@ -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 diff --git a/spec/rspec/snapshot/matchers_spec.rb b/spec/rspec/snapshot/matchers_spec.rb index e54760b..6af06f9 100644 --- a/spec/rspec/snapshot/matchers_spec.rb +++ b/spec/rspec/snapshot/matchers_spec.rb @@ -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