From 69489a9a248143c663aa3d07f4d8df54f19be553 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Thu, 8 Apr 2021 19:43:39 +0100 Subject: [PATCH] Fix false positive when asserting old image with new empty image --- Sources/SnapshotTesting/AssertSnapshot.swift | 6 ++++-- .../SnapshotTesting/Snapshotting/UIImage.swift | 3 ++- .../SnapshotTestingTests.swift | 11 +++++++++++ .../testViewAgainstEmptyImage.notEmptyImage.png | Bin 0 -> 1831 bytes 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testViewAgainstEmptyImage.notEmptyImage.png diff --git a/Sources/SnapshotTesting/AssertSnapshot.swift b/Sources/SnapshotTesting/AssertSnapshot.swift index dd6ca4fb6..85acb0aba 100644 --- a/Sources/SnapshotTesting/AssertSnapshot.swift +++ b/Sources/SnapshotTesting/AssertSnapshot.swift @@ -256,8 +256,10 @@ public func verifySnapshot( let reference = snapshotting.diffing.fromData(data) #if os(iOS) || os(tvOS) - // If the image generation fails for the diffable part use the reference - if let localDiff = diffable as? UIImage, localDiff.size == .zero { + // If the image generation fails for the diffable part and the reference was empty, use the reference + if let localDiff = diffable as? UIImage, + let refImage = reference as? UIImage, + localDiff.size == .zero && refImage.size == .zero { diffable = reference } #endif diff --git a/Sources/SnapshotTesting/Snapshotting/UIImage.swift b/Sources/SnapshotTesting/Snapshotting/UIImage.swift index bf87a1c07..903e58cbf 100644 --- a/Sources/SnapshotTesting/Snapshotting/UIImage.swift +++ b/Sources/SnapshotTesting/Snapshotting/UIImage.swift @@ -30,7 +30,8 @@ extension Diffing where Value == UIImage { : "Newly-taken snapshot@\(new.size) does not match reference@\(old.size)." let oldAttachment = XCTAttachment(image: old) oldAttachment.name = "reference" - let newAttachment = XCTAttachment(image: new) + let isEmptyImage = new.size == .zero + let newAttachment = XCTAttachment(image: isEmptyImage ? emptyImage() : new) newAttachment.name = "failure" let differenceAttachment = XCTAttachment(image: difference) differenceAttachment.name = "difference" diff --git a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift index 4f7a8ba64..bd887842d 100644 --- a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift +++ b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift @@ -998,6 +998,17 @@ final class SnapshotTestingTests: XCTestCase { #endif } + func testViewAgainstEmptyImage() { + #if os(iOS) || os(tvOS) + let rect = CGRect(x: 0, y: 0, width: 0, height: 0) + let view = UIView(frame: rect) + view.backgroundColor = .blue + + let failure = verifySnapshot(matching: view, as: .image, named: "notEmptyImage") + XCTAssertNotNil(failure) + #endif + } + func testEmbeddedWebView() throws { #if os(iOS) let label = UILabel() diff --git a/Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testViewAgainstEmptyImage.notEmptyImage.png b/Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testViewAgainstEmptyImage.notEmptyImage.png new file mode 100644 index 0000000000000000000000000000000000000000..341e06ebdb1064f31cba7e1b6067216df967eb3a GIT binary patch literal 1831 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST_$yF%}28J29*~C-V}>sYs3RO!M_+ z&;qhK7+4sk7+66{fY^>v8qRiO)L>u+i!(7Wv}ZD~K-KsGX%LtI#2`Hont1^uR5eg+ z0TW#Hi3Q9EHb|T2wN?`##aZAHS