From 3cc7c67fee70c19aef775ffe8fee408518705f13 Mon Sep 17 00:00:00 2001 From: Duncan Babbage Date: Sat, 7 Apr 2018 09:29:07 +1200 Subject: [PATCH 1/4] Remove accepted images from failed image path. When a new image is accepted, remove the reference image, failed image, and diff image from the failed image path. --- CHANGELOG.md | 1 + FBSnapshotsViewer/Extensions/FileManager+Move.swift | 4 ++++ .../Managers/SnapshotTestResultAcceptor.swift | 11 ++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db64678..fcbf936 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Your contribution here. * [#69](https://github.com/Antondomashnev/FBSnapshotsViewer/pull/69): Replace Swap with Accept and Accept All. - [@babbage](https://github.com/babbage). +* [#70](https://github.com/Antondomashnev/FBSnapshotsViewer/pull/70): Remove accepted images from failed image path. - [@babbage](https://github.com/babbage). ### 0.8.0 (11.10.2017) diff --git a/FBSnapshotsViewer/Extensions/FileManager+Move.swift b/FBSnapshotsViewer/Extensions/FileManager+Move.swift index 76f8080..3a29620 100644 --- a/FBSnapshotsViewer/Extensions/FileManager+Move.swift +++ b/FBSnapshotsViewer/Extensions/FileManager+Move.swift @@ -13,4 +13,8 @@ extension FileManager { try self.removeItem(at: toURL) try self.copyItem(at: fromURL, to: toURL) } + + func deleteItem(at url: URL) throws { + try self.removeItem(at: url) + } } diff --git a/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift b/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift index 883c1f4..af507dc 100644 --- a/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift +++ b/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift @@ -52,13 +52,22 @@ class SnapshotTestResultAcceptor { } func accept(_ testResult: SnapshotTestResult) throws -> SnapshotTestResult { - guard case let SnapshotTestResult.failed(testInformation, _, _, failedImagePath, build) = testResult, canAccept(testResult) else { + let removeAcceptedImages = true + guard case let SnapshotTestResult.failed(testInformation, referenceImagePath, diffImagePath, failedImagePath, build) = testResult, canAccept(testResult) else { throw SnapshotTestResultAcceptorError.canNotBeAccepted(testResult: testResult) } let failedImageURL = URL(fileURLWithPath: failedImagePath, isDirectory: false) do { let recordedImageURL = try buildRecordedImageURL(from: failedImagePath, of: testResult) try fileManager.moveItem(at: failedImageURL, to: recordedImageURL) + + if removeAcceptedImages { + let referenceImageURL = URL(fileURLWithPath: referenceImagePath, isDirectory: false) + let diffImageURL = URL(fileURLWithPath: diffImagePath, isDirectory: false) + try fileManager.deleteItem(at: failedImageURL) + try fileManager.deleteItem(at: referenceImageURL) + try fileManager.deleteItem(at: diffImageURL) + } imageCache.invalidate() return SnapshotTestResult.recorded(testInformation: testInformation, referenceImagePath: recordedImageURL.path, build: build) } From c13d059b90ac19e2b3536777d3e5848695f43fc0 Mon Sep 17 00:00:00 2001 From: Duncan Babbage Date: Sat, 7 Apr 2018 11:43:40 +1200 Subject: [PATCH 2/4] Remove test images in separate function. --- .../Managers/SnapshotTestResultAcceptor.swift | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift b/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift index af507dc..dda11b2 100644 --- a/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift +++ b/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift @@ -53,7 +53,7 @@ class SnapshotTestResultAcceptor { func accept(_ testResult: SnapshotTestResult) throws -> SnapshotTestResult { let removeAcceptedImages = true - guard case let SnapshotTestResult.failed(testInformation, referenceImagePath, diffImagePath, failedImagePath, build) = testResult, canAccept(testResult) else { + guard case let SnapshotTestResult.failed(testInformation, _, _, failedImagePath, build) = testResult, canAccept(testResult) else { throw SnapshotTestResultAcceptorError.canNotBeAccepted(testResult: testResult) } let failedImageURL = URL(fileURLWithPath: failedImagePath, isDirectory: false) @@ -62,11 +62,7 @@ class SnapshotTestResultAcceptor { try fileManager.moveItem(at: failedImageURL, to: recordedImageURL) if removeAcceptedImages { - let referenceImageURL = URL(fileURLWithPath: referenceImagePath, isDirectory: false) - let diffImageURL = URL(fileURLWithPath: diffImagePath, isDirectory: false) - try fileManager.deleteItem(at: failedImageURL) - try fileManager.deleteItem(at: referenceImageURL) - try fileManager.deleteItem(at: diffImageURL) + try removeTestImages(testResult) } imageCache.invalidate() return SnapshotTestResult.recorded(testInformation: testInformation, referenceImagePath: recordedImageURL.path, build: build) @@ -75,4 +71,23 @@ class SnapshotTestResultAcceptor { throw SnapshotTestResultAcceptorError.canNotPerformFileManagerOperation(testResult: testResult, underlyingError: error) } } + + func removeTestImages(_ testResult: SnapshotTestResult) throws { + guard case let SnapshotTestResult.failed(_, referenceImagePath, diffImagePath, failedImagePath, _) = testResult else { + throw SnapshotTestResultAcceptorError.canNotBeAccepted(testResult: testResult) + } + + let referenceImageURL = URL(fileURLWithPath: referenceImagePath, isDirectory: false) + let diffImageURL = URL(fileURLWithPath: diffImagePath, isDirectory: false) + let failedImageURL = URL(fileURLWithPath: failedImagePath, isDirectory: false) + + do { + try fileManager.deleteItem(at: referenceImageURL) + try fileManager.deleteItem(at: diffImageURL) + try fileManager.deleteItem(at: failedImageURL) + } + catch let error { + throw SnapshotTestResultAcceptorError.canNotPerformFileManagerOperation(testResult: testResult, underlyingError: error) + } + } } From 751477b5aa0653d49d7e399dd10daf5632bc4024 Mon Sep 17 00:00:00 2001 From: Duncan Babbage Date: Sat, 7 Apr 2018 21:21:53 +1200 Subject: [PATCH 3/4] Only deleteItem if file exists at path. Implemented here partly to ease merge with proposed changes in other pull requests. --- FBSnapshotsViewer/Extensions/FileManager+Move.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/FBSnapshotsViewer/Extensions/FileManager+Move.swift b/FBSnapshotsViewer/Extensions/FileManager+Move.swift index 3a29620..1a98128 100644 --- a/FBSnapshotsViewer/Extensions/FileManager+Move.swift +++ b/FBSnapshotsViewer/Extensions/FileManager+Move.swift @@ -10,11 +10,13 @@ import Foundation extension FileManager { func moveItem(at fromURL: URL, to toURL: URL) throws { - try self.removeItem(at: toURL) + try deleteItem(at: toURL) try self.copyItem(at: fromURL, to: toURL) } func deleteItem(at url: URL) throws { - try self.removeItem(at: url) + if fileExists(atPath: url.path) { + try self.removeItem(at: url) + } } } From 260635f9b4a3e22190b39551fb2b56c6a726b96f Mon Sep 17 00:00:00 2001 From: Duncan Babbage Date: Mon, 9 Apr 2018 12:57:07 +1200 Subject: [PATCH 4/4] Rename deleteItem to deleteIfExists. --- FBSnapshotsViewer/Extensions/FileManager+Move.swift | 4 ++-- FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/FBSnapshotsViewer/Extensions/FileManager+Move.swift b/FBSnapshotsViewer/Extensions/FileManager+Move.swift index 1a98128..4627f71 100644 --- a/FBSnapshotsViewer/Extensions/FileManager+Move.swift +++ b/FBSnapshotsViewer/Extensions/FileManager+Move.swift @@ -10,11 +10,11 @@ import Foundation extension FileManager { func moveItem(at fromURL: URL, to toURL: URL) throws { - try deleteItem(at: toURL) + try deleteIfExists(at: toURL) try self.copyItem(at: fromURL, to: toURL) } - func deleteItem(at url: URL) throws { + func deleteIfExists(at url: URL) throws { if fileExists(atPath: url.path) { try self.removeItem(at: url) } diff --git a/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift b/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift index dda11b2..321e3e4 100644 --- a/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift +++ b/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift @@ -82,9 +82,9 @@ class SnapshotTestResultAcceptor { let failedImageURL = URL(fileURLWithPath: failedImagePath, isDirectory: false) do { - try fileManager.deleteItem(at: referenceImageURL) - try fileManager.deleteItem(at: diffImageURL) - try fileManager.deleteItem(at: failedImageURL) + try fileManager.deleteIfExists(at: referenceImageURL) + try fileManager.deleteIfExists(at: diffImageURL) + try fileManager.deleteIfExists(at: failedImageURL) } catch let error { throw SnapshotTestResultAcceptorError.canNotPerformFileManagerOperation(testResult: testResult, underlyingError: error)