Skip to content

Commit

Permalink
test: cleanup tests to use snap manager
Browse files Browse the repository at this point in the history
  • Loading branch information
pftg committed Jul 31, 2024
1 parent a5c30cc commit 7dbba0d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
9 changes: 7 additions & 2 deletions lib/capybara_screenshot_diff/snap_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def initialize(full_name, format, manager: SnapManager.instance)
def delete!
path.delete if path.exist?
base_path.delete if base_path.exist?
cleanup_attempts
end

def checkout_base_screenshot
Expand Down Expand Up @@ -51,6 +52,10 @@ def commit_last_attempt
def cleanup_attempts
@manager.cleanup_attempts_screenshots(path)
end

def attempts_paths
Dir[@manager.abs_path_for "**/#{full_name}.attempt_*.#{format}"]
end
end

attr_reader :root
Expand Down Expand Up @@ -82,8 +87,8 @@ def self.provision_snap_with(snap, path, version: :actual)
instance.provision_snap_with(snap, path, version: version)
end

def create_output_directory_for(path)
path.dirname.mkpath
def create_output_directory_for(path = nil)
path ? path.dirname.mkpath : root.mkpath
end

def clean!
Expand Down
39 changes: 27 additions & 12 deletions test/capybara/screenshot/diff/stable_screenshoter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ module Diff
class StableScreenshoterTest < ActionDispatch::IntegrationTest
include TestMethodsStub

setup do
@manager = CapybaraScreenshotDiff::SnapManager.new(Capybara::Screenshot.root / "stable_screenshoter_test")
@manager.create_output_directory_for
end

teardown do
@manager.delete!
end

test "#take_stable_screenshot several iterations to take stable screenshot" do
image_compare_stub = build_image_compare_stub

Expand All @@ -18,7 +27,8 @@ class StableScreenshoterTest < ActionDispatch::IntegrationTest
mock.expect(:quick_equal?, true)

ImageCompare.stub :new, mock do
take_stable_screenshot_with("tmp/02_a.png")
snap = @manager.snap_for("02_a")
take_stable_screenshot_with(snap.path)
end

mock.verify
Expand All @@ -43,26 +53,30 @@ class StableScreenshoterTest < ActionDispatch::IntegrationTest
mock.expect(:quick_equal?, false)
mock.expect(:quick_equal?, true)

assert_not (Capybara::Screenshot.root / "02_a.png").exist?
snap = @manager.snap_for("02_a")
assert_not_predicate snap.path, :exist?

ImageCompare.stub :new, mock do
StableScreenshoter
.new({stability_time_limit: 0.5, wait: 1}, image_compare_stub.driver_options)
.take_comparison_screenshot("tmp/02_a.png")
.take_comparison_screenshot(snap.path, snap)
end

mock.verify
assert_empty Dir[Capybara::Screenshot.root / "**/02_a.attempt_*.png"]
assert (Capybara::Screenshot.root / "02_a.png").exist?
assert_not_predicate (Capybara::Screenshot.root / "02_a.png").size, :zero?
assert_empty snap.attempts_paths
assert_predicate snap.path, :exist?
assert_not_predicate snap.path.size, :zero?
end

test "#take_comparison_screenshot fail on missing find stable image in time and generates annotated history screenshots" do
screenshot_path = Pathname.new("tmp/01_a.png")
snap = @manager.snap_for("01_a")

screenshot_path = snap.path

# Stub annotated files for generated comparison annotations
# We need to have different from screenshot_path name because of other stubs
annotated_screenshot_path = Pathname.new("tmp/02_a.png")
pseudo_snap_for_annotations = @manager.snap_for("02_a")
annotated_screenshot_path = pseudo_snap_for_annotations.path
annotated_attempts_paths = [
[annotated_screenshot_path.sub_ext(".attempt_01.latest.png"), annotated_screenshot_path.sub_ext(".attempt_01.committed.png")],
[annotated_screenshot_path.sub_ext(".attempt_02.latest.png"), annotated_screenshot_path.sub_ext(".attempt_02.committed.png")]
Expand All @@ -71,9 +85,9 @@ class StableScreenshoterTest < ActionDispatch::IntegrationTest
FileUtils.touch(annotated_attempts_paths)

mock = ::Minitest::Mock.new(build_image_compare_stub(equal: false))
annotated_attempts_paths.reverse_each do |(latest_path, committed_path)|
mock.reporter.expect(:annotated_image_path, latest_path.to_s)
mock.reporter.expect(:annotated_base_image_path, committed_path.to_s)
annotated_attempts_paths.reverse_each do |(actual_path, base_path)|
mock.reporter.expect(:annotated_image_path, actual_path.to_s)
mock.reporter.expect(:annotated_base_image_path, base_path.to_s)
end

assert_raises RuntimeError, "Could not get stable screenshot within 1s" do
Expand All @@ -98,7 +112,8 @@ class StableScreenshoterTest < ActionDispatch::IntegrationTest
last_annotation = screenshot_path.sub_ext(".attempt_01.png")
assert_equal 0, last_annotation.size, "#{last_annotation.to_path} should be override with annotated version"
ensure
FileUtils.rm_rf Dir["tmp/01_a*.png"]
snap&.delete!
pseudo_snap_for_annotations&.delete!
end
end
end
Expand Down

0 comments on commit 7dbba0d

Please sign in to comment.