Skip to content

Commit

Permalink
Merge pull request #10 from skyweb07/feature/store_images_to_disk
Browse files Browse the repository at this point in the history
Store images to disk
  • Loading branch information
skyweb07 authored Nov 29, 2017
2 parents 4f89b37 + d7a21ac commit 92c59cd
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ In order to configure the snapshot test folder, we need to add a new environment

![Project attachment](.art/xcode_project_environment_variable.png)

> Errored images and diffed one's are stored into `$PATH/Snap/Failed` and `$PATH/Snap/Diff`, if you want you can add those paths into `.gitignore` as these are not needed for `Snap.swift` to work.
### 🎯 Installation

Snap.swift is available through [CocoaPods](http://cocoapods.org). To install
Expand Down
22 changes: 14 additions & 8 deletions Snap/Core/Application/Actions/CompareImages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ struct CompareImages {

private let fileManager: FileManager
private let addAttachment: AddAttachment
private let saveImageToDisk: SaveImageToDisk

init(fileManager: FileManager,
addAttachment: AddAttachment)
addAttachment: AddAttachment,
saveImageToDisk: SaveImageToDisk)
{
self.fileManager = fileManager
self.addAttachment = addAttachment
self.saveImageToDisk = saveImageToDisk
}

func compare(with view: UIView, testTarget: TestTarget) {
Expand Down Expand Up @@ -39,29 +42,32 @@ struct CompareImages {
do {
try referenceImage.compare(with: processedImage)
} catch CompareError.notEqualSize(let referenceSize, let comparedSize) {
self.process(failedImage: processedImage, reference: referenceImage)
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("📏 Image sizes should be equals, reference image size: \(referenceSize), compared image size: \(comparedSize)")
} catch CompareError.invalidImageSize {
self.process(failedImage: processedImage, reference: referenceImage)
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("📏 One of the images has 0 size")
} catch CompareError.notEquals {
self.process(failedImage: processedImage, reference: referenceImage)
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("≠ Images are not equal")
} catch CompareError.notEqualMetadata {
self.process(failedImage: processedImage, reference: referenceImage)
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("👾 Images have different metadata information")
} catch CompareError.invalidReferenceImage {
self.process(failedImage: processedImage, reference: referenceImage)
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("👾 Invalid reference image")
} catch {
self.process(failedImage: processedImage, reference: referenceImage)
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("🚫 Unknown error")
}
}

private func process(failedImage: UIImage, reference: UIImage) {
private func process(failedImage: UIImage, reference: UIImage, testTarget: TestTarget) {
addAttachment.execute(with: failedImage, type: .failed)
saveImageToDisk.execute(with: failedImage, with: testTarget.reference(for: .failed))

guard let diffedImage = reference.diff(with: failedImage) else { return }
addAttachment.execute(with: diffedImage, type: .diff)
saveImageToDisk.execute(with: diffedImage, with: testTarget.reference(for: .diff))
}
}
11 changes: 8 additions & 3 deletions Snap/Core/Application/Actions/ExtractViewImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import XCTest
struct ExtractViewImage {

private let saveImageToDisk: SaveImageToDisk

init(saveImageToDisk: SaveImageToDisk) {
private let addAttachment: AddAttachment

init(saveImageToDisk: SaveImageToDisk,
addAttachment: AddAttachment)
{
self.saveImageToDisk = saveImageToDisk
self.addAttachment = addAttachment
}

func execute(with view: UIView, testTarget: TestTarget) {
guard let image = view.render() else {
fatalError("🖼 Can not extract image from view")
}
saveImageToDisk.execute(with: image, testTarget: testTarget)
saveImageToDisk.execute(with: image, with: testTarget.reference(for: .reference))
addAttachment.execute(with: image, type: .reference)

XCTFail("⚠️ Test ran in record mode, reference image has been saved to \(testTarget.reference(for: .reference).path), now remove `isRecording` in order to perform the snapshot comparsion.")
}
Expand Down
11 changes: 3 additions & 8 deletions Snap/Core/Application/Actions/SaveImageToDisk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ struct SaveImageToDisk {

private let environment: Environment
private let fileManager: FileManager
private let addAttachment: AddAttachment


init(environment: Environment,
fileManager: FileManager,
addAttachment: AddAttachment)
fileManager: FileManager)
{
self.environment = environment
self.fileManager = fileManager
self.addAttachment = addAttachment
}

func execute(with image: UIImage, testTarget: TestTarget) {
let reference = testTarget.reference(for: .reference)
func execute(with image: UIImage, with reference: Reference) {
let referenceImage = UIImagePNGRepresentation(image)

do {
Expand All @@ -27,6 +23,5 @@ struct SaveImageToDisk {
guard fileManager.createFile(atPath: reference.path.absoluteString, contents: referenceImage) else {
fatalError("🚨 Error saving reference image into ['\(reference.path)']")
}
addAttachment.execute(with: image, type: .reference)
}
}
13 changes: 12 additions & 1 deletion Snap/Core/Domain/Model/TestTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension TestTarget {
// There should be a better way to handle with this ⚠️
let classFile = file.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? ""
let functionName = function.replacingOccurrences(of: "()", with: "").lowercased()
let path = "Snap/\(classFile)/"
let path = "\(self.path(for: type))\(classFile)/"
let directory = url.appendingPathComponent(path)
let pathUrl = directory.appendingPathComponent("\(type.rawValue)_\(functionName)\(scale).png")

Expand Down Expand Up @@ -47,4 +47,15 @@ extension TestTarget {
}
return url
}

private func path(for type: Type) -> String {
switch type {
case .reference:
return "Snap/"
case .failed:
return "Snap/Failed/"
case .diff:
return "Snap/Diff/"
}
}
}
9 changes: 5 additions & 4 deletions Snap/Core/Infrastructure/Assembly/Assembly+Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ import Foundation
extension Assembly {
var extractViewImage: ExtractViewImage {
return ExtractViewImage(
saveImageToDisk: saveImageToDisk
saveImageToDisk: saveImageToDisk,
addAttachment: addAttachment
)
}

private var saveImageToDisk: SaveImageToDisk {
return SaveImageToDisk(
environment: environment,
fileManager: fileManager,
addAttachment: addAttachment
fileManager: fileManager
)
}

var compareImages: CompareImages {
return CompareImages(
fileManager: fileManager,
addAttachment: addAttachment
addAttachment: addAttachment,
saveImageToDisk: saveImageToDisk
)
}

Expand Down

0 comments on commit 92c59cd

Please sign in to comment.