Skip to content

Commit ae9b76d

Browse files
pftgclaude
andcommitted
fix: clean up reporter diff artifacts (diff, base.diff, heatmap) on Snap#delete!
Snap#delete! only removed .png and .base.png but left .diff.png, .base.diff.png, and .heatmap.diff.png orphaned. Also Reporters::Default #clean_tmp_files missed heatmap cleanup. Each test run leaked ~30 files into tmp/ that accumulated indefinitely across runs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c1a53c3 commit ae9b76d

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

lib/capybara/screenshot/diff/reporters/default.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def generate
3333
def clean_tmp_files
3434
annotated_base_image_path.unlink if annotated_base_image_path.exist?
3535
annotated_image_path.unlink if annotated_image_path.exist?
36+
heatmap_diff_path.unlink if heatmap_diff_path.exist?
3637
end
3738

3839
def build_error_for_different_dimensions

lib/capybara_screenshot_diff/snap.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def initialize(full_name, format, manager: SnapManager.instance)
1616
def delete!
1717
path.delete if path.exist?
1818
base_path.delete if base_path.exist?
19+
cleanup_diff_artifacts!
1920
cleanup_attempts!
2021
end
2122

@@ -51,5 +52,15 @@ def cleanup_attempts!
5152
def find_attempts_paths
5253
Dir[@manager.abs_path_for("**/#{full_name}.attempt_[0-9][0-9].#{format}")]
5354
end
55+
56+
private
57+
58+
def cleanup_diff_artifacts!
59+
[
60+
path.sub_ext(".diff.#{format}"),
61+
path.sub_ext(".heatmap.diff.#{format}"),
62+
base_path.sub_ext(".diff.#{format}")
63+
].each { |f| f.delete if f.exist? }
64+
end
5465
end
5566
end

test/unit/reporters/default_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ class Reporters::DefaultTest < ActiveSupport::TestCase
3030
assert_same_images "a-and-b.heatmap.diff.png", reporter.heatmap_diff_path
3131
end
3232

33+
test "#clean_tmp_files removes heatmap diff along with other diff artifacts" do
34+
skip "VIPS not present. Skipping VIPS driver tests." unless defined?(Vips)
35+
driver = Drivers::VipsDriver.new
36+
comparison = build_comparison_for(driver, "a.png", "b.png")
37+
reporter = Reporters::Default.new(driver.find_difference_region(comparison))
38+
reporter.generate
39+
40+
assert_predicate reporter.heatmap_diff_path, :exist?
41+
42+
reporter.clean_tmp_files
43+
44+
assert_not reporter.annotated_image_path.exist?, "diff should be cleaned"
45+
assert_not reporter.annotated_base_image_path.exist?, "base diff should be cleaned"
46+
assert_not reporter.heatmap_diff_path.exist?, "heatmap diff should be cleaned"
47+
end
48+
3349
private
3450

3551
def build_comparison_for(driver, *images)

test/unit/snap_manager_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,24 @@ class SnapManagerTest < ActiveSupport::TestCase
6060
assert_includes snap.base_path.to_s, "test_image.base.png"
6161
assert_includes snap.next_attempt_path!.to_s, "test_image.attempt_00.png"
6262
end
63+
64+
test "#cleanup! removes diff artifacts created by reporters" do
65+
snap = @manager.snapshot("test_image")
66+
source = fixture_image_path_from("a")
67+
@manager.provision_snap_with(snap, source)
68+
@manager.provision_snap_with(snap, source, version: :base)
69+
70+
# Simulate reporter artifacts
71+
diff_path = snap.path.sub_ext(".diff.png")
72+
base_diff_path = snap.path.sub_ext(".base.diff.png")
73+
heatmap_path = snap.path.sub_ext(".heatmap.diff.png")
74+
[diff_path, base_diff_path, heatmap_path].each { |p| FileUtils.cp(source, p) }
75+
76+
@manager.cleanup!
77+
78+
assert_not diff_path.exist?, "diff artifact should be cleaned up"
79+
assert_not base_diff_path.exist?, "base diff artifact should be cleaned up"
80+
assert_not heatmap_path.exist?, "heatmap diff artifact should be cleaned up"
81+
end
6382
end
6483
end

0 commit comments

Comments
 (0)