Skip to content

Commit

Permalink
Remove accepted images from failed image path. (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
babbage authored and Antondomashnev committed Apr 9, 2018
1 parent 823a6ce commit 72b1c2e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
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)
}
}
}

0 comments on commit 72b1c2e

Please sign in to comment.