Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove accepted images from failed image path. #70

Merged
merged 4 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 7 additions & 1 deletion FBSnapshotsViewer/Extensions/FileManager+Move.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
24 changes: 24 additions & 0 deletions FBSnapshotsViewer/Managers/SnapshotTestResultAcceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,42 @@ 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)
}
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 {
try removeTestImages(testResult)
}
imageCache.invalidate()
return SnapshotTestResult.recorded(testInformation: testInformation, referenceImagePath: recordedImageURL.path, build: build)
}
catch let error {
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)
}
}
}