Skip to content

Commit

Permalink
refact: replace file names with paths and other cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
pftg committed Dec 10, 2023
1 parent 216eb43 commit 045d4fb
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 99 deletions.
6 changes: 6 additions & 0 deletions lib/capybara/screenshot/diff/comparison.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Capybara::Screenshot::Diff
class Comparison < Struct.new(:new_image, :base_image, :options, :driver, :new_image_path, :base_image_path)
end
end
10 changes: 8 additions & 2 deletions lib/capybara/screenshot/diff/drivers/base_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ def dimension(image)
[width_for(image), height_for(image)]
end

def inscribed?(dimensions, i)
width_for(i) < dimensions[0] || height_for(i) < dimensions[1]
# Checks if the given image fits within the specified dimensions.
#
# @param dimensions [Array<Integer>] An array containing the width and height to check against.
# @param image [ChunkyPNG::Image] The image to check.
#
# @return [Boolean] Returns `true` if the image's width and height are both less than the corresponding dimensions, and `false` otherwise.
def inscribed?(dimensions, image)
width_for(image) < dimensions[0] || height_for(image) < dimensions[1]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/screenshot/diff/drivers/chunky_png_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def resize_image_to(image, new_width, new_height)
end

def load_image_files(old_file_name, file_name)
[File.binread(old_file_name), File.binread(file_name)]
[old_file_name.binread, file_name.binread]
end

def draw_rectangles(images, region, (r, g, b), offset: 0)
Expand Down
91 changes: 45 additions & 46 deletions lib/capybara/screenshot/diff/image_compare.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "capybara/screenshot/diff/comparison"

module Capybara
module Screenshot
module Diff
Expand All @@ -12,7 +14,7 @@ class ImageCompare

attr_reader :driver, :driver_options

attr_reader :image_path, :base_image_path, :new_file_name, :old_file_name
attr_reader :image_path, :base_image_path

attr_reader :difference

Expand All @@ -21,15 +23,8 @@ def initialize(image_path, base_image_path, options = {})
@error_message = nil

@image_path = Pathname.new(image_path)

@new_file_name = @image_path.to_s
@annotated_image_path = @image_path.sub_ext(".diff.png")

@base_image_path = Pathname.new(base_image_path)

@old_file_name = @base_image_path.to_s
@annotated_base_image_path = @base_image_path.sub_ext(".diff.png")

@driver_options = options.dup

@driver = Drivers.for(@driver_options)
Expand All @@ -38,16 +33,28 @@ def initialize(image_path, base_image_path, options = {})
# Compare the two image files and return `true` or `false` as quickly as possible.
# Return falsely if the old file does not exist or the image dimensions do not match.
def quick_equal?

# TODO: What to do with this? Raise Argument Error?
return false unless image_files_exist?
# TODO: Confirm this change. There are screenshots with the same size, but there is a big difference
return true if new_file_size == old_file_size

comparison = load_and_process_images

return false unless driver.same_dimension?(comparison)
unless driver.same_dimension?(comparison)
@difference = failed_difference(comparison, { different_dimensions: true })
@reporter = nil
@error_message = nil

return true if driver.same_pixels?(comparison)
return false
end

if driver.same_pixels?(comparison)
@difference = no_difference(comparison)
@reporter = nil
@error_message = nil

return true
end

# Could not make any difference to be tolerable, so skip and return as not equal
return false if without_tolerable_options?
Expand All @@ -67,39 +74,28 @@ def different?

@difference = find_difference unless processed?

@error_message = report(@difference)
@error_message = report

@difference.different?
end

def image_files_exist?
@base_image_path.exist? && @image_path.exist?
end

attr_reader :error_message

def reporter(difference = @difference)
@reporter ||= Capybara::Screenshot::Diff::Reporters::Default.new(difference || no_difference)
end

# TODO: Delete me
def annotated_image_path
return unless different?

reporter.annotated_image_path
def reporter
@reporter ||= begin
current_difference = difference || no_difference(nil)
Capybara::Screenshot::Diff::Reporters::Default.new(current_difference)
end
end

# TODO: Delete me
def annotated_base_image_path
return unless different?
private

reporter.annotated_base_image_path
def image_files_exist?
@base_image_path.exist? && @image_path.exist?
end

private

def report(difference)
reporter(difference).generate
def report
reporter.generate
end

def processed?
Expand Down Expand Up @@ -137,7 +133,7 @@ def failed_difference(comparison, failed_by)
end

def load_and_process_images
images = driver.load_images(old_file_name, new_file_name)
images = driver.load_images(base_image_path, image_path)
base_image, new_image = preprocess_images(images)
Comparison.new(new_image, base_image, @driver_options, driver, image_path, base_image_path)
end
Expand All @@ -150,10 +146,6 @@ def median_filter_window_size
@driver_options[:median_filter_window_size]
end

def dimensions
@driver_options[:dimensions]
end

def preprocess_images(images)
images.map { |image| preprocess_image(image) }
end
Expand All @@ -166,7 +158,14 @@ def preprocess_image(image)
end

if median_filter_window_size
result = blur_image_by(image, median_filter_window_size)
if driver.is_a?(Drivers::VipsDriver)
result = blur_image_by(image, median_filter_window_size)
else
warn(
"[capybara-screenshot-diff] Median filter has been skipped for #{image_path} " \
"because it is not supported by #{driver.class.name}"
)
end
end

result
Expand All @@ -177,28 +176,28 @@ def blur_image_by(image, size)
end

def ignore_skipped_area(image)
skip_area.reduce(image) { |memo, region| driver.add_black_box(memo, region) }
skip_area&.reduce(image) { |memo, region| driver.add_black_box(memo, region) }
end

def old_file_size
@old_file_size ||= image_files_exist? && File.size(@old_file_name)
base_image_path.size
end

def new_file_size
File.size(@new_file_name)
image_path.size
end

def no_difference(comparison = nil)
Difference.new(
nil,
{ difference_level: nil, max_color_distance: 0 },
comparison || Comparison.new(nil, nil, driver_options, driver, image_path, base_image_path)
comparison || build_comparison
).freeze
end

end

class Comparison < Struct.new(:new_image, :base_image, :options, :driver, :new_image_path, :base_image_path)
def build_comparison
Capybara::Screenshot::Diff::Comparison.new(nil, nil, driver_options, driver, image_path, base_image_path)
end
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/capybara/screenshot/diff/reporters/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ def generate
end

if difference.failed? && difference.failed_by[:different_dimensions]
return build_error_for_different_dimensions(difference.failed_by[:different_dimensions])
return build_error_for_different_dimensions
end

annotate_and_save_images(difference)
build_error_message(difference)
annotate_and_save_images
build_error_message
end

def clean_tmp_files
annotated_base_image_path.unlink if annotated_base_image_path.exist?
annotated_image_path.unlink if annotated_image_path.exist?
end

def build_error_for_different_dimensions(failed_by = nil)
def build_error_for_different_dimensions
change_msg = [comparison.base_image, comparison.new_image]
.map { |i| driver.dimension(i).join("x") }
.map { |image| driver.dimension(image).join("x") }
.join(" => ")

"Screenshot dimension has been changed for #{new_file_name}: #{change_msg}"
end

def annotate_and_save_images(difference)
annotate_and_save_image(difference, difference.comparison.new_image, annotated_image_path)
annotate_and_save_image(difference, difference.comparison.base_image, annotated_base_image_path)
def annotate_and_save_images
annotate_and_save_image(difference.comparison.new_image, annotated_image_path)
annotate_and_save_image(difference.comparison.base_image, annotated_base_image_path)
end

def annotate_and_save_image(difference, image, image_path)
def annotate_and_save_image(image, image_path)
image = annotate_difference(image, difference.region)
image = annotate_skip_areas(image, difference.skip_area) if difference.skip_area

Expand All @@ -72,7 +72,7 @@ def save(image, image_path)

NEW_LINE = "\n".freeze

def build_error_message(difference)
def build_error_message
[
"(#{difference.inspect})",
new_image_path.to_path,
Expand Down
14 changes: 14 additions & 0 deletions sig/capybara/screenshot/diff/comparison.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Capybara
module Screenshot
module Diff
class Comparison
attr_accessor new_image: ImageCompare::image_entity
attr_accessor base_image: ImageCompare::image_entity
attr_accessor options: Drivers::BaseDriver::options_entity
attr_accessor driver: ImageCompare::driver_entity
attr_accessor new_image_path: Pathname
attr_accessor base_image_path: Pathname
end
end
end
end
2 changes: 1 addition & 1 deletion sig/capybara/screenshot/diff/difference.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Capybara
module Screenshot
module Diff
class Difference
attr_reader comparison: ImageCompare::Comparison
attr_reader comparison: Comparison

def different?: () -> bool

Expand Down
4 changes: 2 additions & 2 deletions sig/capybara/screenshot/diff/drivers/base_driver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Capybara

type color = [Integer, Integer, Integer, Integer]

def find_difference_region: (ImageEntity new_image, ImageEntity old_image, Numeric color_distance_limit, Numeric _shift_distance_limit, Integer _area_size_limit, ?fast_fail: bool) -> Difference
def find_difference_region: (Comparison) -> Difference

def inscribed?: (dimension_entity dimensions, ImageEntity i) -> boolish

Expand All @@ -45,7 +45,7 @@ module Capybara

def resize_image_to: (ImageEntity image, Integer new_width, Integer new_height) -> ImageEntity

def load_images: (String old_file_name, String new_file_name) -> images_entity[ImageEntity]
def load_images: (TestMethods::path_entity old_file_name, TestMethods::path_entity new_file_name) -> images_entity[ImageEntity]

def from_file: (TestMethods::path_entity filename) -> ImageEntity

Expand Down
Loading

0 comments on commit 045d4fb

Please sign in to comment.