Skip to content

Commit

Permalink
Replace Swap with Accept and Accept All. (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
babbage authored and Antondomashnev committed Apr 8, 2018
1 parent 65ab907 commit 823a6ce
Show file tree
Hide file tree
Showing 22 changed files with 206 additions and 205 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 0.8.1 (Next)

* Your contribution here.
* [#69](https://github.com/Antondomashnev/FBSnapshotsViewer/pull/69): Replace Swap with Accept and Accept All. - [@babbage](https://github.com/babbage).

### 0.8.0 (11.10.2017)

Expand Down
16 changes: 8 additions & 8 deletions FBSnapshotsViewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@
6DBD6C541EAD4B2B006F14DC /* ApplicationTestLogFilesListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBD6C531EAD4B2B006F14DC /* ApplicationTestLogFilesListener.swift */; };
6DBD99D91E7AB31B00E1714E /* NonRecursiveFolderEventsListenerSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBD99D81E7AB31B00E1714E /* NonRecursiveFolderEventsListenerSpec.swift */; };
6DBD99DB1E7AB3D400E1714E /* FileWatcherFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBD99DA1E7AB3D400E1714E /* FileWatcherFactory.swift */; };
6DCCEF0C1F02F60700523B47 /* SnapshotTestResultSwapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DCCEF0B1F02F60700523B47 /* SnapshotTestResultSwapper.swift */; };
6DCCEF0E1F02F64800523B47 /* SnapshotTestResultSwapperSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DCCEF0D1F02F64800523B47 /* SnapshotTestResultSwapperSpec.swift */; };
6DCCEF0C1F02F60700523B47 /* SnapshotTestResultAcceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DCCEF0B1F02F60700523B47 /* SnapshotTestResultAcceptor.swift */; };
6DCCEF0E1F02F64800523B47 /* SnapshotTestResultAcceptorSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DCCEF0D1F02F64800523B47 /* SnapshotTestResultAcceptorSpec.swift */; };
6DD1AEA51F08093E002FCA82 /* ImageCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DD1AEA41F08093E002FCA82 /* ImageCache.swift */; };
6DD1AEA71F080968002FCA82 /* Nuke+ImageCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DD1AEA61F080968002FCA82 /* Nuke+ImageCache.swift */; };
6DDFBD8D1E63268300DFC0A4 /* FolderEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DDFBD8C1E63268300DFC0A4 /* FolderEvent.swift */; };
Expand Down Expand Up @@ -328,8 +328,8 @@
6DBD6C531EAD4B2B006F14DC /* ApplicationTestLogFilesListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApplicationTestLogFilesListener.swift; sourceTree = "<group>"; };
6DBD99D81E7AB31B00E1714E /* NonRecursiveFolderEventsListenerSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NonRecursiveFolderEventsListenerSpec.swift; sourceTree = "<group>"; };
6DBD99DA1E7AB3D400E1714E /* FileWatcherFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileWatcherFactory.swift; sourceTree = "<group>"; };
6DCCEF0B1F02F60700523B47 /* SnapshotTestResultSwapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnapshotTestResultSwapper.swift; sourceTree = "<group>"; };
6DCCEF0D1F02F64800523B47 /* SnapshotTestResultSwapperSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnapshotTestResultSwapperSpec.swift; sourceTree = "<group>"; };
6DCCEF0B1F02F60700523B47 /* SnapshotTestResultAcceptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnapshotTestResultAcceptor.swift; sourceTree = "<group>"; };
6DCCEF0D1F02F64800523B47 /* SnapshotTestResultAcceptorSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnapshotTestResultAcceptorSpec.swift; sourceTree = "<group>"; };
6DD1AEA41F08093E002FCA82 /* ImageCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageCache.swift; sourceTree = "<group>"; };
6DD1AEA61F080968002FCA82 /* Nuke+ImageCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Nuke+ImageCache.swift"; sourceTree = "<group>"; };
6DDFBD8C1E63268300DFC0A4 /* FolderEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FolderEvent.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -577,7 +577,7 @@
6D14776C1EFED78D0070EFEC /* BuildCreatorSpec.swift */,
6D1477721EFEE7D00070EFEC /* FBReferenceImageDirectoryURLExtractorSpec.swift */,
6D1477741EFEEDB30070EFEC /* ApplicationSnapshotTestResultFileWatcherUpdateHandlerSpec.swift */,
6DCCEF0D1F02F64800523B47 /* SnapshotTestResultSwapperSpec.swift */,
6DCCEF0D1F02F64800523B47 /* SnapshotTestResultAcceptorSpec.swift */,
6D57D3E31F31A9CD007FDBAE /* StringStripSpec.swift */,
6D942D521F40E544009984C4 /* TestClassPathExtractorpecSpec.swift */,
6D942D561F40FE1D009984C4 /* TestLineNumberExtractorSpec.swift */,
Expand Down Expand Up @@ -644,7 +644,7 @@
6D942D541F40FC2E009984C4 /* TestLineNumberExtractor.swift */,
6D14776A1EFED5500070EFEC /* BuildCreator.swift */,
6D14776E1EFEDF5A0070EFEC /* ApplicationSnapshotTestResultFileWatcherUpdateHandler.swift */,
6DCCEF0B1F02F60700523B47 /* SnapshotTestResultSwapper.swift */,
6DCCEF0B1F02F60700523B47 /* SnapshotTestResultAcceptor.swift */,
6D5EB5421F43999A0055C356 /* TestNameExtractor.swift */,
6D5EB5441F4399A70055C356 /* TestClassNameExtractor.swift */,
);
Expand Down Expand Up @@ -1158,7 +1158,7 @@
6D3000431EBFBE14005B6103 /* PreferencesController.swift in Sources */,
6D5F913C1E4F6D4700C6E5BF /* FolderEventsListenerFactory.swift in Sources */,
6D89B9541EF088B600865C5F /* TestResultsDiffMode.swift in Sources */,
6DCCEF0C1F02F60700523B47 /* SnapshotTestResultSwapper.swift in Sources */,
6DCCEF0C1F02F60700523B47 /* SnapshotTestResultAcceptor.swift in Sources */,
6DA1BF991E7755110017D47B /* Storyboards.swift in Sources */,
6DE3477E1E47D9DF004147DF /* MenuInteractorIO.swift in Sources */,
6DBD6C501EACF1B0006F14DC /* DerivedDataFolder.swift in Sources */,
Expand Down Expand Up @@ -1216,7 +1216,7 @@
6D1477731EFEE7D00070EFEC /* FBReferenceImageDirectoryURLExtractorSpec.swift in Sources */,
6D130B6D1EC8E82B00E9642A /* ConfigurationSpec.swift in Sources */,
6D3BA5651E6E07AA00CAD4EE /* MenuPresenterSpec.swift in Sources */,
6DCCEF0E1F02F64800523B47 /* SnapshotTestResultSwapperSpec.swift in Sources */,
6DCCEF0E1F02F64800523B47 /* SnapshotTestResultAcceptorSpec.swift in Sources */,
6D42C93E1EB275B6003744BF /* MenuWireframeSpec.swift in Sources */,
6D342CE21EBBDFDF006EEBEC /* AppleInterfaceModeSpec.swift in Sources */,
6D12F4721E81758500AA4727 /* TestResultsControllerSpec.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SnapshotTestResultSwapper.swift
// SnapshotTestResultAcceptor.swift
// FBSnapshotsViewer
//
// Created by Anton Domashnev on 27.06.17.
Expand All @@ -9,14 +9,14 @@
import Foundation
import Nuke

enum SnapshotTestResultSwapperError: Error {
case canNotBeSwapped(testResult: SnapshotTestResult)
enum SnapshotTestResultAcceptorError: Error {
case canNotBeAccepted(testResult: SnapshotTestResult)
case nonRetinaImages(testResult: SnapshotTestResult)
case notExistedRecordedImage(testResult: SnapshotTestResult)
case canNotPerformFileManagerOperation(testResult: SnapshotTestResult, underlyingError: Error)
}

class SnapshotTestResultSwapper {
class SnapshotTestResultAcceptor {
private let fileManager: FileManager
private let imageCache: ImageCache

Expand All @@ -29,31 +29,31 @@ class SnapshotTestResultSwapper {

private func buildRecordedImageURL(from imagePath: String, of testResult: SnapshotTestResult) throws -> URL {
guard let failedImageSizeSuffixRange = imagePath.range(of: "@\\d{1}x", options: .regularExpression) else {
throw SnapshotTestResultSwapperError.nonRetinaImages(testResult: testResult)
throw SnapshotTestResultAcceptorError.nonRetinaImages(testResult: testResult)
}
let failedImageSizeSuffix = imagePath.substring(with: failedImageSizeSuffixRange)
let recordedImageURLs = testResult.build.fbReferenceImageDirectoryURLs.flatMap { fbReferenceImageDirectoryURL -> URL? in
let url = fbReferenceImageDirectoryURL.appendingPathComponent(testResult.testClassName).appendingPathComponent("\(testResult.testName)\(failedImageSizeSuffix)").appendingPathExtension("png")
return fileManager.fileExists(atPath: url.path) ? url : nil
}
guard let url = recordedImageURLs.first else {
throw SnapshotTestResultSwapperError.notExistedRecordedImage(testResult: testResult)
throw SnapshotTestResultAcceptorError.notExistedRecordedImage(testResult: testResult)
}
return url
}

// MARK: - Interface

func canSwap(_ testResult: SnapshotTestResult) -> Bool {
func canAccept(_ testResult: SnapshotTestResult) -> Bool {
if case SnapshotTestResult.failed(_, _, _, _, _) = testResult {
return true
}
return false
}

func swap(_ testResult: SnapshotTestResult) throws -> SnapshotTestResult {
guard case let SnapshotTestResult.failed(testInformation, _, _, failedImagePath, build) = testResult, canSwap(testResult) else {
throw SnapshotTestResultSwapperError.canNotBeSwapped(testResult: testResult)
func accept(_ testResult: SnapshotTestResult) throws -> SnapshotTestResult {
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 {
Expand All @@ -63,7 +63,7 @@ class SnapshotTestResultSwapper {
return SnapshotTestResult.recorded(testInformation: testInformation, referenceImagePath: recordedImageURL.path, build: build)
}
catch let error {
throw SnapshotTestResultSwapperError.canNotPerformFileManagerOperation(testResult: testResult, underlyingError: error)
throw SnapshotTestResultAcceptorError.canNotPerformFileManagerOperation(testResult: testResult, underlyingError: error)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import Foundation
import Cocoa

enum TestResultsInteractorError: Error {
case canNotSwapNotExistedTestResult
case canNotAcceptNotExistedTestResult
}

class TestResultsInteractorBuilder {
var externalViewers: ExternalViewers = ExternalViewers()
var processLauncher: ProcessLauncher = ProcessLauncher()
var swapper: SnapshotTestResultSwapper = SnapshotTestResultSwapper()
var acceptor: SnapshotTestResultAcceptor = SnapshotTestResultAcceptor()
var pasteboard: Pasteboard = NSPasteboard.general()
var testResults: [SnapshotTestResult] = []

Expand All @@ -31,7 +31,7 @@ class TestResultsInteractor {
fileprivate let xcodeViewer: ExternalViewer.Type
fileprivate let kaleidoscopeViewer: ExternalViewer.Type
fileprivate let processLauncher: ProcessLauncher
fileprivate let swapper: SnapshotTestResultSwapper
fileprivate let acceptor: SnapshotTestResultAcceptor
fileprivate let pasteboard: Pasteboard
var testResults: [SnapshotTestResult]

Expand All @@ -42,7 +42,7 @@ class TestResultsInteractor {
self.kaleidoscopeViewer = builder.externalViewers.kaleidoscope
self.xcodeViewer = builder.externalViewers.xcode
self.processLauncher = builder.processLauncher
self.swapper = builder.swapper
self.acceptor = builder.acceptor
self.pasteboard = builder.pasteboard
}
}
Expand All @@ -52,7 +52,7 @@ extension TestResultsInteractor: TestResultsInteractorInput {

private func replace(testResult: SnapshotTestResult, with newTestResult: SnapshotTestResult) throws {
guard let indexOfTestResult = testResults.index(of: testResult) else {
throw TestResultsInteractorError.canNotSwapNotExistedTestResult
throw TestResultsInteractorError.canNotAcceptNotExistedTestResult
}
testResults[indexOfTestResult] = newTestResult
}
Expand All @@ -75,16 +75,16 @@ extension TestResultsInteractor: TestResultsInteractorInput {
xcodeViewer.view(snapshotTestResult: testResult, using: processLauncher)
}

func swap(testResult: SnapshotTestResult) {
if !swapper.canSwap(testResult) {
func accept(testResult: SnapshotTestResult) {
if !acceptor.canAccept(testResult) {
return
}
do {
let swappedTestResult = try swapper.swap(testResult)
try replace(testResult: testResult, with: swappedTestResult)
let acceptedTestResult = try acceptor.accept(testResult)
try replace(testResult: testResult, with: acceptedTestResult)
}
catch let error {
output?.didFailToSwap(testResult: testResult, with: error)
output?.didFailToAccept(testResult: testResult, with: error)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ protocol TestResultsInteractorInput: class, AutoMockable {
var testResults: [SnapshotTestResult] { get }
func openInKaleidoscope(testResult: SnapshotTestResult)
func openInXcode(testResult: SnapshotTestResult)
func swap(testResult: SnapshotTestResult)
func accept(testResult: SnapshotTestResult)
func copy(testResult: SnapshotTestResult)
}

protocol TestResultsInteractorOutput: class, AutoMockable {
func didFailToSwap(testResult: SnapshotTestResult, with error: Error)
func didFailToAccept(testResult: SnapshotTestResult, with error: Error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ struct TestResultInformationDisplayInfo: AutoEquatable {

struct TestResultDisplayInfoOptions: OptionSet {
let rawValue: Int
static let canBeSwapped = TestResultDisplayInfoOptions(rawValue: 1 << 0)
static let canBeAccepted = TestResultDisplayInfoOptions(rawValue: 1 << 0)
static let canBeViewedInKaleidoscope = TestResultDisplayInfoOptions(rawValue: 1 << 1)
static let canBeViewedInXcode = TestResultDisplayInfoOptions(rawValue: 1 << 2)
static let canBeCopied = TestResultDisplayInfoOptions(rawValue: 1 << 3)
}

extension TestResultDisplayInfoOptions {
static func availableOptions(for testResult: SnapshotTestResult, externalViewers: ExternalViewers = ExternalViewers(), swapper: SnapshotTestResultSwapper = SnapshotTestResultSwapper()) -> TestResultDisplayInfoOptions {
static func availableOptions(for testResult: SnapshotTestResult, externalViewers: ExternalViewers = ExternalViewers(), acceptor: SnapshotTestResultAcceptor = SnapshotTestResultAcceptor()) -> TestResultDisplayInfoOptions {
var availableOptions: TestResultDisplayInfoOptions = [TestResultDisplayInfoOptions.canBeCopied]
if swapper.canSwap(testResult) { availableOptions = availableOptions.union(.canBeSwapped) }
if acceptor.canAccept(testResult) { availableOptions = availableOptions.union(.canBeAccepted) }
if externalViewers.xcode.isAvailable() && externalViewers.xcode.canView(snapshotTestResult: testResult) { availableOptions = availableOptions.union(.canBeViewedInXcode) }
if externalViewers.kaleidoscope.isAvailable() && externalViewers.kaleidoscope.canView(snapshotTestResult: testResult) { availableOptions = availableOptions.union(.canBeViewedInKaleidoscope) }
return availableOptions
Expand All @@ -55,8 +55,8 @@ struct TestResultDisplayInfo: AutoEquatable {
return testInformation.testContext
}

var canBeSwapped: Bool {
return options.contains(.canBeSwapped)
var canBeAccepted: Bool {
return options.contains(.canBeAccepted)
}

var canBeViewedInKaleidoscope: Bool {
Expand All @@ -71,10 +71,10 @@ struct TestResultDisplayInfo: AutoEquatable {
return options.contains(.canBeCopied)
}

init(testResult: SnapshotTestResult, swapper: SnapshotTestResultSwapper = SnapshotTestResultSwapper(), externalViewers: ExternalViewers = ExternalViewers()) {
init(testResult: SnapshotTestResult, acceptor: SnapshotTestResultAcceptor = SnapshotTestResultAcceptor(), externalViewers: ExternalViewers = ExternalViewers()) {
self.testResult = testResult
self.testInformation = TestResultInformationDisplayInfo(testResultInformation: testResult.testInformation)
self.options = TestResultDisplayInfoOptions.availableOptions(for: testResult, externalViewers: externalViewers, swapper: swapper)
self.options = TestResultDisplayInfoOptions.availableOptions(for: testResult, externalViewers: externalViewers, acceptor: acceptor)
switch testResult {
case let .recorded(_, referenceImagePath, _):
self.referenceImageURL = URL(fileURLWithPath: referenceImagePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ extension TestResultsPresenter: TestResultsModuleInterface {
updateUserInterface()
}

func swap(_ testResults: [TestResultDisplayInfo]) {
func accept(_ testResults: [TestResultDisplayInfo]) {
for testResultInfo in testResults {
interactor?.swap(testResult: testResultInfo.testResult)
interactor?.accept(testResult: testResultInfo.testResult)
}
updateUserInterface()
}
Expand All @@ -55,11 +55,11 @@ extension TestResultsPresenter: TestResultsModuleInterface {
}

extension TestResultsPresenter: TestResultsInteractorOutput {
func didFailToSwap(testResult: SnapshotTestResult, with error: Error) {
let failToSwapAlert = NSAlert()
failToSwapAlert.addButton(withTitle: "Ok")
failToSwapAlert.alertStyle = .critical
failToSwapAlert.messageText = "Ops, swap can not be done. \(error.localizedDescription). Please report an issue https://github.com/Antondomashnev/FBSnapshotsViewer/issues/new"
failToSwapAlert.runModal()
func didFailToAccept(testResult: SnapshotTestResult, with error: Error) {
let failToAcceptAlert = NSAlert()
failToAcceptAlert.addButton(withTitle: "Ok")
failToAcceptAlert.alertStyle = .critical
failToAcceptAlert.messageText = "Ops, accept can not be done. \(error.localizedDescription). Please report an issue https://github.com/Antondomashnev/FBSnapshotsViewer/issues/new"
failToAcceptAlert.runModal()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ struct TestResultsSectionDisplayInfo: AutoEquatable {
let titleInfo: TestResultsSectionTitleDisplayInfo
let itemInfos: [TestResultDisplayInfo]

var hasItemsToSwap: Bool {
return itemInfos.index { $0.canBeSwapped } != nil ? true : false
var hasItemsToAccept: Bool {
return itemInfos.index { $0.canBeAccepted } != nil ? true : false
}

init(title: TestResultsSectionTitleDisplayInfo, items: [TestResultDisplayInfo] = []) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ protocol TestResultsModuleInterface: class, AutoMockable {
func openInKaleidoscope(testResultDisplayInfo: TestResultDisplayInfo)
func openInXcode(testResultDisplayInfo: TestResultDisplayInfo)
func selectDiffMode(_ diffMode: TestResultsDiffMode)
func swap(_ testResults: [TestResultDisplayInfo])
func accept(_ testResults: [TestResultDisplayInfo])
func copy(testResultDisplayInfo: TestResultDisplayInfo)
}
Loading

0 comments on commit 823a6ce

Please sign in to comment.