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..4627f71 100644 --- a/FBSnapshotsViewer/Extensions/FileManager+Move.swift +++ b/FBSnapshotsViewer/Extensions/FileManager+Move.swift @@ -10,7 +10,13 @@ import Foundation extension FileManager { func moveItem(at fromURL: URL, to toURL: URL) throws { - try self.removeItem(at: toURL) + try deleteIfExists(at: toURL) try self.copyItem(at: fromURL, to: toURL) } + + 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 883c1f4..321e3e4 100644 --- a/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift +++ b/FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift @@ -52,6 +52,7 @@ class SnapshotTestResultAcceptor { } func accept(_ testResult: SnapshotTestResult) throws -> SnapshotTestResult { + let removeAcceptedImages = true guard case let SnapshotTestResult.failed(testInformation, _, _, failedImagePath, build) = testResult, canAccept(testResult) else { throw SnapshotTestResultAcceptorError.canNotBeAccepted(testResult: testResult) } @@ -59,6 +60,10 @@ class SnapshotTestResultAcceptor { do { let recordedImageURL = try buildRecordedImageURL(from: failedImagePath, of: testResult) try fileManager.moveItem(at: failedImageURL, to: recordedImageURL) + + if removeAcceptedImages { + try removeTestImages(testResult) + } imageCache.invalidate() return SnapshotTestResult.recorded(testInformation: testInformation, referenceImagePath: recordedImageURL.path, build: build) } @@ -66,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.deleteIfExists(at: referenceImageURL) + try fileManager.deleteIfExists(at: diffImageURL) + try fileManager.deleteIfExists(at: failedImageURL) + } + catch let error { + throw SnapshotTestResultAcceptorError.canNotPerformFileManagerOperation(testResult: testResult, underlyingError: error) + } + } }