Skip to content

Commit

Permalink
don't fail when file won't change
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelglass committed Dec 8, 2022
1 parent 88caa84 commit 17b1567
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 66 deletions.
7 changes: 4 additions & 3 deletions lib/rspec/snapshot/matchers/match_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def matches?(actual)

@expected = read_snapshot

if should_write?
if @wrote
false
else
@actual == @expected
Expand All @@ -69,6 +69,7 @@ def matches?(actual)

private def write_snapshot
return unless should_write?
@wrote = true

RSpec.configuration.reporter.message(
"Snapshot written: #{@snapshot_path}"
Expand All @@ -79,7 +80,7 @@ def matches?(actual)
end

private def should_write?
update_snapshots? || !File.exist?(@snapshot_path)
!File.exist?(@snapshot_path) || (update_snapshots? && read_snapshot != @actual)
end

private def update_snapshots?
Expand All @@ -102,7 +103,7 @@ def diffable?
end

def failure_message
if should_write?
if @wrote
"failing because we wrote a snapshot"
else
"\nexpected: #{@expected}\n got: #{@actual}\n"
Expand Down
77 changes: 15 additions & 62 deletions spec/rspec/snapshot/matchers/match_snapshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,35 +210,20 @@ def dump(object)
expect(serializer).not_to have_received(:dump)
end

it 'opens the snapshot file for writing' do
expect(File).to have_received(:new).with(snapshot_filepath, 'w+')
end

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

it 'logs the snapshot write with the RSpec reporter' do
expect(RSpec.configuration.reporter).to(
have_received(:message)
.with("Snapshot written: #{snapshot_filepath}")
)
end

it 'opens the snapshot file for reading' do
expect(File).to have_received(:new).with(snapshot_filepath)
expect(File).to have_received(:new).with(snapshot_filepath).twice
end

it 'reads the snapshot file' do
expect(file).to have_received(:read)
expect(file).to have_received(:read).twice
end

it 'closes the snapshot file after reading and writing' do
expect(file).to have_received(:close).twice
end

it 'returns false' do
expect(@actual).to be(false)
it 'returns true' do
expect(@actual).to be(true)
end
end

Expand All @@ -258,35 +243,20 @@ def dump(object)
expect(serializer).to have_received(:dump).with(value_to_match)
end

it 'opens the snapshot file for writing' do
expect(File).to have_received(:new).with(snapshot_filepath, 'w+')
end

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

it 'logs the snapshot write with the RSpec reporter' do
expect(RSpec.configuration.reporter).to(
have_received(:message)
.with("Snapshot written: #{snapshot_filepath}")
)
end

it 'opens the snapshot file for reading' do
expect(File).to have_received(:new).with(snapshot_filepath)
expect(File).to have_received(:new).with(snapshot_filepath).twice
end

it 'reads the snapshot file' do
expect(file).to have_received(:read)
expect(file).to have_received(:read).twice
end

it 'closes the snapshot file after reading and writing' do
expect(file).to have_received(:close).twice
end

it 'returns false' do
expect(@actual).to be(false)
it 'returns true' do
expect(@actual).to be(true)
end
end
end
Expand Down Expand Up @@ -590,13 +560,6 @@ def dump(object)
expect(file).to have_received(:write).with(value_to_match)
end

it 'logs the snapshot write with the RSpec reporter' do
expect(RSpec.configuration.reporter).to(
have_received(:message)
.with("Snapshot written: #{snapshot_filepath}")
)
end

it 'opens the snapshot file for reading' do
expect(File).to have_received(:new).with(snapshot_filepath)
end
Expand Down Expand Up @@ -700,24 +663,14 @@ def dump(object)
subject.instance_variable_set(:@actual, actual)
end

context 'when should_write? is true' do
it 'returns a failure message including the actual and expected' do
expect(subject.failure_message).to(
eq("failing because we wrote a snapshot")
)
end
end

context 'when should_write? is false' do
before {
allow(subject).to receive(:should_write?).and_return(false)
}
before {
allow(subject).to receive(:should_write?).and_return(false)
}

it 'returns a failure message including the actual and expected' do
expect(subject.failure_message).to(
eq("\nexpected: #{expected}\n got: #{actual}\n")
)
end
it 'returns a failure message including the actual and expected' do
expect(subject.failure_message).to(
eq("\nexpected: #{expected}\n got: #{actual}\n")
)
end
end

Expand Down
6 changes: 5 additions & 1 deletion spec/rspec/snapshot/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ class TestClass
FileUtils.rm_rf(snapshot_dir)

# rubocop:disable RSpec/ExpectInHook
expect(expected).to match_snapshot(snapshot_name)
expect {
expect(expected).to match_snapshot(snapshot_name)
}.to raise_error
# rubocop:enable RSpec/ExpectInHook
file = File.new(snapshot_path)
@actual = file.read
Expand Down Expand Up @@ -355,7 +357,9 @@ def dump(object)
before do
File.unlink(snapshot_path) if File.exist?(snapshot_path)
# rubocop:disable RSpec/ExpectInHook
expect {
expect(snapshot_value).to match_snapshot(snapshot_name)
}.to raise_error
# rubocop:enable RSpec/ExpectInHook
file = File.new(snapshot_path)
@actual = file.read
Expand Down

0 comments on commit 17b1567

Please sign in to comment.