From 9e9e930a419850f8389a5c34e7841dbfd4d4e853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=88r=20Strindevall?= Date: Wed, 29 Jan 2020 10:47:19 +0100 Subject: [PATCH 1/3] Make File Manager save return URL discardable --- Sources/SnapshotFileManager.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/SnapshotFileManager.swift b/Sources/SnapshotFileManager.swift index affb808..e43b7c2 100644 --- a/Sources/SnapshotFileManager.swift +++ b/Sources/SnapshotFileManager.swift @@ -50,6 +50,7 @@ class DataHandler : DataHandling { } protocol SnapshotFileManaging { + @discardableResult func save(referenceImage: UIImage, filename: String, className: String) throws -> URL func referenceImage(filename: String, className: String) throws -> UIImage } From bf5fe1a1910b934e59d4ffb680eda3708637c634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=88r=20Strindevall?= Date: Wed, 29 Jan 2020 10:47:43 +0100 Subject: [PATCH 2/3] Save failed snapshot if it differs from reference image --- Sources/SnapshotCoordinator.swift | 8 ++++++++ .../SnapshotCoordinatorTests.swift | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Sources/SnapshotCoordinator.swift b/Sources/SnapshotCoordinator.swift index 9c38c1d..ade1f81 100644 --- a/Sources/SnapshotCoordinator.swift +++ b/Sources/SnapshotCoordinator.swift @@ -54,6 +54,7 @@ extension SnapshotCoordinator : SnapshotCoordinating { let referenceImage = try fileManager.referenceImage(filename: filename, className: className) guard snapshot.compare(withImage: referenceImage) else { + try fileManager.save(referenceImage: snapshot, filename: filename.failed, className: className) throw SnapshotError.imageMismatch(filename: filename) } } @@ -65,3 +66,10 @@ extension SnapshotCoordinator : SnapshotCoordinating { return try fileManager.save(referenceImage: referenceImage, filename: filename, className: className) } } + +fileprivate extension String { + + var failed: String { + return "\(self)_failed" + } +} diff --git a/Tests/SnapshotTestTests/SnapshotCoordinatorTests.swift b/Tests/SnapshotTestTests/SnapshotCoordinatorTests.swift index 54dbf25..b39dd16 100644 --- a/Tests/SnapshotTestTests/SnapshotCoordinatorTests.swift +++ b/Tests/SnapshotTestTests/SnapshotCoordinatorTests.swift @@ -72,6 +72,22 @@ class SnapshotCoordinatorTests: XCTestCase { XCTAssertEqual(error as? SnapshotError, SnapshotError.imageMismatch(filename: "redSquare")) } } + + func testCompareSnapshot_withViewNotEqualToReferenceImage_shouldInvokeSaveWithFailedSnapshotAndFailedFilenameOnFileManager() { + + // Given + let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) + view.backgroundColor = .blue + fileManagerMock.referenceImageReturnValue = UIImage(testFilename: "redSquare", ofType: "png") + + // When + XCTAssertThrowsError(try sut.compareSnapshot(of: view, options: [], functionName: "redSquare", line: 0)) { error in + XCTAssertEqual(fileManagerMock.saveInvokeCount, 1) + XCTAssertNotNil(fileManagerMock.saveReferenceImageArgument) + XCTAssertEqual(fileManagerMock.saveFilenameArgument, "redSquare_failed") + XCTAssertEqual(fileManagerMock.saveClassNameArgument, "CustomButtonTests") + } + } func testCompareSnapshot_shouldInvokeReferenceImageWithCorrectFilenameAndClassNameOnFileManager() { From a9429f420ac3ca9f87f5c189d464fe3c36f221fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=88r=20Strindevall?= Date: Wed, 29 Jan 2020 12:49:08 +0100 Subject: [PATCH 3/3] Bump Ruby to 2.4.3 --- .ruby-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ruby-version b/.ruby-version index a20c569..cf12b30 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1,2 +1,2 @@ -2.3.5 +2.4.3