Skip to content
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
58 changes: 50 additions & 8 deletions test/snapshot_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ def assert_evaluated_snapshot(source, locals = {}, options = {}, **kwargs)

def snapshot_changed?(content, source, options = {})
if snapshot_file(source, options).exist?
previous_content = snapshot_file(source, options).read
previous_full_snapshot = snapshot_file(source, options).read
current_full_snapshot = format_snapshot_with_metadata(content, source, options)

if previous_content == content
if previous_full_snapshot == current_full_snapshot
puts "\n\nSnapshot for '#{class_name} #{name}' didn't change: \n#{snapshot_file(source, options)}\n"
false
else
puts "\n\nSnapshot for '#{class_name} #{name}' changed:\n"

puts Difftastic::Differ.new(color: :always).diff_strings(previous_content, content)
puts Difftastic::Differ.new(color: :always).diff_strings(previous_full_snapshot, current_full_snapshot)
puts "==============="
true
end
Expand All @@ -116,7 +116,7 @@ def save_failures_to_snapshot(content, source, options = {})
puts "\nUpdating Snapshot for '#{class_name} #{name}' at: \n#{snapshot_file(source, options)}\n"

FileUtils.mkdir_p(snapshot_file(source, options).dirname)
snapshot_file(source, options).write(content)
snapshot_file(source, options).write(format_snapshot_with_metadata(content, source, options))

puts "\nSnapshot for '#{class_name} #{name}' written: \n#{snapshot_file(source, options)}\n"
else
Expand All @@ -128,21 +128,27 @@ def assert_snapshot_matches(actual, source, options = {})
assert snapshot_file(source, options).exist?,
"Expected snapshot file to exist: \n#{snapshot_file(source, options).to_path}"

assert_equal snapshot_file(source, options).read, actual
expected_full_snapshot = snapshot_file(source, options).read
actual_full_snapshot = format_snapshot_with_metadata(actual, source, options)

assert_equal expected_full_snapshot, actual_full_snapshot
rescue Minitest::Assertion => e
save_failures_to_snapshot(actual, source, options) if ENV["UPDATE_SNAPSHOTS"] || ENV["FORCE_UPDATE_SNAPSHOTS"]

raise unless snapshot_file(source, options).exist?

if snapshot_file(source, options)&.read != actual
expected_full_snapshot = snapshot_file(source, options).read
actual_full_snapshot = format_snapshot_with_metadata(actual, source, options)

if expected_full_snapshot != actual_full_snapshot
puts

divider = "=" * `tput cols`.strip.to_i

flunk(<<~MESSAGE)
\e[0m
#{divider}
#{Difftastic::Differ.new(color: :always).diff_strings(snapshot_file(source, options).read, actual)}
#{Difftastic::Differ.new(color: :always).diff_strings(expected_full_snapshot, actual_full_snapshot)}
\e[31m#{divider}

Snapshots for "#{class_name} #{name}" didn't match.
Expand Down Expand Up @@ -266,6 +272,42 @@ def compare_with_erubi_evaluated(source, herb_result, locals, options, enforce_e
end
end

def format_snapshot_with_metadata(content, source, options = {})
metadata = build_snapshot_metadata(source, options)

frontmatter = "---\n"
frontmatter += "source: #{metadata["source"].inspect}\n"

# Use YAML literal block scalar for input (preserves formatting)
input_value = metadata["input"]
if input_value.include?("\n")
# Multiline: use |2- which means start content at column 0 (strip trailing newline)
frontmatter += "input: |2-\n"
frontmatter += input_value
# Ensure there's a newline after the multiline block
frontmatter += "\n" unless frontmatter.end_with?("\n")
else
# Single line: use regular quoted format
frontmatter += "input: #{input_value.inspect}\n"
end

frontmatter += "options: #{metadata["options"].inspect}\n" if metadata["options"]

frontmatter += "---\n"

frontmatter + content
end

def build_snapshot_metadata(source, options = {})
metadata = {
"source" => "#{class_name}##{name}",
"input" => source.to_s,
}

metadata["options"] = options unless options.empty?
metadata
end

private

def sanitize_name_for_filesystem(name)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading