Skip to content

Commit

Permalink
Add Build structure; #trivial (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antondomashnev authored Jun 10, 2017
1 parent 51b2248 commit de76664
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 85 deletions.
2 changes: 2 additions & 0 deletions .codebeatignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fastlane/**
Vendor/**
4 changes: 4 additions & 0 deletions FBSnapshotsViewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
6D789CE61EC3BFA900EEF7EE /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D789CE51EC3BFA800EEF7EE /* Configuration.swift */; };
6D789CE81EC3C19F00EEF7EE /* ConfigurationStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D789CE71EC3C19F00EEF7EE /* ConfigurationStorage.swift */; };
6D7C1F921EEB35CB0045117E /* AppCodeLog.xml in Resources */ = {isa = PBXBuildFile; fileRef = 6DEFF5761EE89E010048F16B /* AppCodeLog.xml */; };
6D7C1F941EEC198A0045117E /* Build.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D7C1F931EEC198A0045117E /* Build.swift */; };
6D7D78FE1EAFF2430001C1A0 /* SnapshotTestResultFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D7D78FD1EAFF2430001C1A0 /* SnapshotTestResultFactory.swift */; };
6D7D79001EAFF2A80001C1A0 /* ApplicationLogLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D7D78FF1EAFF2A80001C1A0 /* ApplicationLogLine.swift */; };
6D7D79021EB081F50001C1A0 /* ApplicationSnapshotTestResultListenerFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D7D79011EB081F50001C1A0 /* ApplicationSnapshotTestResultListenerFactory.swift */; };
Expand Down Expand Up @@ -222,6 +223,7 @@
6D789CE31EC3BEF800EEF7EE /* PreferencesInteractorIO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesInteractorIO.swift; sourceTree = "<group>"; };
6D789CE51EC3BFA800EEF7EE /* Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = "<group>"; };
6D789CE71EC3C19F00EEF7EE /* ConfigurationStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigurationStorage.swift; sourceTree = "<group>"; };
6D7C1F931EEC198A0045117E /* Build.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Build.swift; sourceTree = "<group>"; };
6D7D78FD1EAFF2430001C1A0 /* SnapshotTestResultFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnapshotTestResultFactory.swift; sourceTree = "<group>"; };
6D7D78FF1EAFF2A80001C1A0 /* ApplicationLogLine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApplicationLogLine.swift; sourceTree = "<group>"; };
6D7D79011EB081F50001C1A0 /* ApplicationSnapshotTestResultListenerFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApplicationSnapshotTestResultListenerFactory.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -638,6 +640,7 @@
6D342CDF1EBBDF80006EEBEC /* AppleInterfaceMode.swift */,
6D789CD91EC3B38C00EEF7EE /* DerivedDataFolderType.swift */,
6D789CE51EC3BFA800EEF7EE /* Configuration.swift */,
6D7C1F931EEC198A0045117E /* Build.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -968,6 +971,7 @@
6DDFBDA41E64D14700DFC0A4 /* MenuModuleInterface.swift in Sources */,
6D789CE81EC3C19F00EEF7EE /* ConfigurationStorage.swift in Sources */,
6D4724BD1E3E9F2400F38161 /* AppDelegate.swift in Sources */,
6D7C1F941EEC198A0045117E /* Build.swift in Sources */,
6D848C591E68D3960000FC1E /* TestResultDisplayInfo.swift in Sources */,
6D5BB4C51E5915A10006BAE1 /* ApplicationTemporaryFolderEventFilter.swift in Sources */,
6D848C5D1E68D6A50000FC1E /* TestResultsInteractor.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions FBSnapshotsViewer/External Viewers/KaleidoscopeViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class KaleidoscopeViewer: ExternalViewer {
}

static func canView(snapshotTestResult: SnapshotTestResult) -> Bool {
if case SnapshotTestResult.failed(_, _, _, _, _, _) = snapshotTestResult {
if case SnapshotTestResult.failed(_, _, _, _, _) = snapshotTestResult {
return true
}
return false
}

static func view(snapshotTestResult: SnapshotTestResult, using processLauncher: ProcessLauncher = ProcessLauncher()) {
guard case let SnapshotTestResult.failed(_, referenceImagePath, _, failedImagePath, _, _) = snapshotTestResult else {
guard case let SnapshotTestResult.failed(_, referenceImagePath, _, failedImagePath, _) = snapshotTestResult else {
assertionFailure("Trying to open Kaleidoscope viewer for test result without diff")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import KZFileWatchers
typealias ApplicationSnapshotTestResultListenerOutput = (SnapshotTestResult) -> Void

class ApplicationSnapshotTestResultListener {
private var build: Build?
private var readLinesNumber: Int = 0
private var readApplicationName: String = ""
private var listeningOutput: ApplicationSnapshotTestResultListenerOutput?
private let fileWatcher: KZFileWatchers.FileWatcherProtocol
private let applicationLogReader: ApplicationLogReader
Expand Down Expand Up @@ -85,10 +85,14 @@ class ApplicationSnapshotTestResultListener {
case .unknown:
return nil
case .applicationNameMessage:
readApplicationName = try applicationNameExtractor.extractApplicationName(from: logLine)
build = Build(applicationName: try applicationNameExtractor.extractApplicationName(from: logLine))
return nil
default:
return snapshotTestResultFactory.createSnapshotTestResult(from: logLine, applicationName: readApplicationName)
guard let build = build else {
assertionFailure("Unexpected snapshot test result line \(logLine) before build information line")
return nil
}
return snapshotTestResultFactory.createSnapshotTestResult(from: logLine, build: build)
}
}
snapshotTestResults.forEach { listeningOutput($0) }
Expand All @@ -98,6 +102,7 @@ class ApplicationSnapshotTestResultListener {
private func reset() {
readLinesNumber = 0
listeningOutput = nil
build = nil
try? fileWatcher.stop()
}
}
14 changes: 7 additions & 7 deletions FBSnapshotsViewer/Managers/SnapshotTestResultFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SnapshotTestResultFactory {
return "\(testClassName) \(testName)"
}

private func createSnapshotTestResult(fromKaleidoscopeCommandLine line: String, applicationName: String) throws -> SnapshotTestResult {
private func createSnapshotTestResult(fromKaleidoscopeCommandLine line: String, build: Build) throws -> SnapshotTestResult {
let lineComponents = line.components(separatedBy: "\"")
guard lineComponents.count == 5 else {
throw SnapshotTestResultFactoryError.unexpectedKaleidoscopeCommandLineFormat
Expand All @@ -47,31 +47,31 @@ class SnapshotTestResultFactory {
let failedImagePath = lineComponents[3]
let diffImagePath = failedImagePath.replacingOccurrences(of: "/failed_", with: "/diff_")
let testName = try extractTestName(fromFailedImage: failedImagePath)
return SnapshotTestResult.failed(testName: testName, referenceImagePath: referenceImagePath, diffImagePath: diffImagePath, failedImagePath: failedImagePath, createdAt: Date(), applicationName: applicationName)
return SnapshotTestResult.failed(testName: testName, referenceImagePath: referenceImagePath, diffImagePath: diffImagePath, failedImagePath: failedImagePath, build: build)
}

private func createSnapshotTestResult(fromSavedReferenceImageLine line: String, applicationName: String) throws -> SnapshotTestResult {
private func createSnapshotTestResult(fromSavedReferenceImageLine line: String, build: Build) throws -> SnapshotTestResult {
let lineComponents = line.components(separatedBy: ApplicationLogLine.referenceImageSavedMessageIndicatorSubstring)
guard lineComponents.count == 2 else {
throw SnapshotTestResultFactoryError.unexpectedSavedReferenceImageLineFormat
}
let referenceImagePath = lineComponents[1]
let testName = try extractTestName(fromSavedReferenceImage: referenceImagePath)
return SnapshotTestResult.recorded(testName: testName, referenceImagePath: referenceImagePath, createdAt: Date(), applicationName: applicationName)
return SnapshotTestResult.recorded(testName: testName, referenceImagePath: referenceImagePath, build: build)
}

// MARK: - Interface

func createSnapshotTestResult(from logLine: ApplicationLogLine, applicationName: String = "") -> SnapshotTestResult? {
func createSnapshotTestResult(from logLine: ApplicationLogLine, build: Build) -> SnapshotTestResult? {
switch logLine {
case .unknown:
return nil
case .applicationNameMessage:
return nil
case let .kaleidoscopeCommandMessage(line):
return try? self.createSnapshotTestResult(fromKaleidoscopeCommandLine: line, applicationName: applicationName)
return try? self.createSnapshotTestResult(fromKaleidoscopeCommandLine: line, build: build)
case let .referenceImageSavedMessage(line):
return try? self.createSnapshotTestResult(fromSavedReferenceImageLine: line, applicationName: applicationName)
return try? self.createSnapshotTestResult(fromSavedReferenceImageLine: line, build: build)
}
}
}
24 changes: 24 additions & 0 deletions FBSnapshotsViewer/Models/Build.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Build.swift
// FBSnapshotsViewer
//
// Created by Anton Domashnev on 10.06.17.
// Copyright © 2017 Anton Domashnev. All rights reserved.
//

import Foundation

struct Build: AutoHashable, AutoEquatable {
let date: Date
let applicationName: String

init(date: Date, applicationName: String) {
self.applicationName = applicationName
self.date = date
}

init(applicationName: String) {
self.date = Date()
self.applicationName = applicationName
}
}
29 changes: 9 additions & 20 deletions FBSnapshotsViewer/Models/SnapshotTestResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,29 @@ import Cocoa
enum SnapshotTestResult: AutoEquatable {
var testName: String {
switch self {
case let .recorded(testName, _, _, _):
case let .recorded(testName, _, _):
return testName
case let .failed(testName, _, _, _, _, _):
case let .failed(testName, _, _, _, _):
return testName
}
}

var createdAt: Date {
var build: Build {
switch self {
case let .recorded(_, _, createdAt, _):
return createdAt
case let .failed(_, _, _, _, createdAt, _):
return createdAt
}
}

var applicationName: String {
switch self {
case let .recorded(_, _, _, applicationNamw):
return applicationNamw
case let .failed(_, _, _, _, _, applicationName):
return applicationName
case let .recorded(_, _, build):
return build
case let .failed(_, _, _, _, build):
return build
}
}

case recorded(testName: String,
referenceImagePath: String,
createdAt: Date,
applicationName: String)
build: Build)

case failed(testName: String,
referenceImagePath: String,
diffImagePath: String,
failedImagePath: String,
createdAt: Date,
applicationName: String)
build: Build)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ struct TestResultDisplayInfo: AutoEquatable {
self.testResult = testResult
self.canBeViewedInKaleidoscope = kaleidoscopeViewer.isAvailable() && kaleidoscopeViewer.canView(snapshotTestResult: testResult)
switch testResult {
case let .recorded(_, referenceImagePath, _, _):
case let .recorded(_, referenceImagePath, _):
self.referenceImageURL = URL(fileURLWithPath: referenceImagePath)
self.diffImageURL = nil
self.failedImageURL = nil
case let .failed(_, referenceImagePath, diffImagePath, failedImagePath, _, _):
case let .failed(_, referenceImagePath, diffImagePath, failedImagePath, _):
self.referenceImageURL = URL(fileURLWithPath: referenceImagePath)
self.diffImageURL = URL(fileURLWithPath: diffImagePath)
self.failedImageURL = URL(fileURLWithPath: failedImagePath)
}
self.createdAt = dateFormatter.string(from: testResult.createdAt, to: Date()) ?? "Just now"
self.createdAt = dateFormatter.string(from: testResult.build.date, to: Date()) ?? "Just now"

let testNameComponents = testResult.testName.replacingOccurrences(of: "_", with: " ").components(separatedBy: " ")
self.testContext = testNameComponents[0..<(testNameComponents.count - 1)].joined(separator: " ")
self.testName = testNameComponents[testNameComponents.count - 1]
self.applicationName = testResult.applicationName
self.applicationName = testResult.build.applicationName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class ApplicationSnapshotTestResultListener_MockApplicationLogReader: Applicatio

class ApplicationSnapshotTestResultListener_MockSnapshotTestResultFactory: SnapshotTestResultFactory {
var createdSnapshotTestResultForLogLine: [ApplicationLogLine: SnapshotTestResult] = [:]
var givenApplicationName: String!
override func createSnapshotTestResult(from logLine: ApplicationLogLine, applicationName: String) -> SnapshotTestResult? {
givenApplicationName = applicationName
var givenBuild: Build!
override func createSnapshotTestResult(from logLine: ApplicationLogLine, build: Build) -> SnapshotTestResult? {
givenBuild = build
return createdSnapshotTestResultForLogLine[logLine]
}
}
Expand Down Expand Up @@ -87,12 +87,13 @@ class ApplicationSnapshotTestResultListenerSpec: QuickSpec {

describe(".receiving new file watch event") {
var receivedSnapshotTestResults: [SnapshotTestResult] = []
let build = Build(date: Date(), applicationName: "MyApp")
let unknownLogLine = ApplicationLogLine.unknown
let applicationNameMessageLogLine = ApplicationLogLine.applicationNameMessage(line: "MyApp")
let kaleidoscopeCommandMesageLogLine = ApplicationLogLine.kaleidoscopeCommandMessage(line: "BlaBla")
let referenceImageSavedMessageLogLine = ApplicationLogLine.referenceImageSavedMessage(line: "FooFoo")
let failedSnapshotTestResult = SnapshotTestResult.failed(testName: "failedTest", referenceImagePath: "referenceTestImage.png", diffImagePath: "diffTestImage.png", failedImagePath: "failedTestImage.png", createdAt: Date(), applicationName: "MyApp")
let recordedSnapshotTestResult = SnapshotTestResult.recorded(testName: "recordedTest", referenceImagePath: "referenceTestImage.png", createdAt: Date(), applicationName: "MyApp")
let failedSnapshotTestResult = SnapshotTestResult.failed(testName: "failedTest", referenceImagePath: "referenceTestImage.png", diffImagePath: "diffTestImage.png", failedImagePath: "failedTestImage.png", build: build)
let recordedSnapshotTestResult = SnapshotTestResult.recorded(testName: "recordedTest", referenceImagePath: "referenceTestImage.png", build: build)

beforeEach {
applicationNameExtractor.extractApplicationNameReturnValue = "MyApp"
Expand Down Expand Up @@ -142,8 +143,9 @@ class ApplicationSnapshotTestResultListenerSpec: QuickSpec {
expect(receivedSnapshotTestResults).to(equal([failedSnapshotTestResult, recordedSnapshotTestResult]))
}

it("creates test results with correct application name") {
expect(snapshotTestResultFactory.givenApplicationName).to(equal(applicationNameExtractor.extractApplicationNameReturnValue))
it("creates test results with correct build") {
expect(snapshotTestResultFactory.givenBuild.applicationName) == applicationNameExtractor.extractApplicationNameReturnValue
expect(snapshotTestResultFactory.givenBuild.date.timeIntervalSince1970).to(beCloseTo(Date().timeIntervalSince1970, within: 0.01))
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions FBSnapshotsViewerTests/KaleidoscopeViewerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ class KaleidoscopeViewerSpec: QuickSpec {

describe(".canView") {
var testResult: SnapshotTestResult!
var build: Build!

context("for recorded snapshot test result") {
beforeEach {
testResult = SnapshotTestResult.recorded(testName: "testName", referenceImagePath: "foo/bar/referenceImage.png", createdAt: Date(), applicationName: "MyApp")
build = Build(applicationName: "MyApp")
testResult = SnapshotTestResult.recorded(testName: "testName", referenceImagePath: "foo/bar/referenceImage.png", build: build)
}

it("returns false") {
Expand All @@ -68,7 +70,8 @@ class KaleidoscopeViewerSpec: QuickSpec {

context("for failed snapshot test result") {
beforeEach {
testResult = SnapshotTestResult.failed(testName: "testName", referenceImagePath: "foo/bar/referenceImage.png", diffImagePath: "foo/bar/diffImage.png", failedImagePath: "foo/bar/failedImage.png", createdAt: Date(), applicationName: "MyApp")
build = Build(applicationName: "MyApp")
testResult = SnapshotTestResult.failed(testName: "testName", referenceImagePath: "foo/bar/referenceImage.png", diffImagePath: "foo/bar/diffImage.png", failedImagePath: "foo/bar/failedImage.png", build: build)
}

it("returns true") {
Expand Down Expand Up @@ -101,10 +104,12 @@ class KaleidoscopeViewerSpec: QuickSpec {

describe(".view") {
var testResult: SnapshotTestResult!
var build: Build!

context("for recorded snapshot test result") {
beforeEach {
testResult = SnapshotTestResult.recorded(testName: "testName", referenceImagePath: "foo/bar/referenceImage.png", createdAt: Date(), applicationName: "MyApp")
build = Build(date: Date(), applicationName: "MyApp")
testResult = SnapshotTestResult.recorded(testName: "testName", referenceImagePath: "foo/bar/referenceImage.png", build: build)
}

it("throws an assertion") {
Expand All @@ -114,7 +119,8 @@ class KaleidoscopeViewerSpec: QuickSpec {

context("for failed snapshot test result") {
beforeEach {
testResult = SnapshotTestResult.failed(testName: "testName", referenceImagePath: "foo/bar/referenceImage.png", diffImagePath: "foo/bar/diffImage.png", failedImagePath: "foo/bar/failedImage.png", createdAt: Date(), applicationName: "MyApp")
build = Build(date: Date(), applicationName: "MyApp")
testResult = SnapshotTestResult.failed(testName: "testName", referenceImagePath: "foo/bar/referenceImage.png", diffImagePath: "foo/bar/diffImage.png", failedImagePath: "foo/bar/failedImage.png", build: build)
}

it("launches correct process") {
Expand Down
Loading

0 comments on commit de76664

Please sign in to comment.