Skip to content

Commit

Permalink
Merge pull request #23 from skyweb07/feature/fix_image_generation_for…
Browse files Browse the repository at this point in the history
…_directory_with_spaces

Fix failed tests for project with spaces in path
  • Loading branch information
skyweb07 authored Dec 15, 2017
2 parents 03499f6 + 38de4dd commit eececb6
Show file tree
Hide file tree
Showing 18 changed files with 412 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Pods
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ disabled_rules:

excluded:
- SnapTests
- Pods

file_length:
warning: 200
Expand Down
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
language: objective-c
cache: cocoapods
osx_image: xcode9.2

sudo: required
install:
- ./.scripts/install-swiftlint.sh
- pod install

script:
- swiftlint
- xcodebuild -scheme Snap -project ./Snap.xcodeproj -destination 'platform=iOS Simulator,name=iPhone 7' test
- xcodebuild -scheme Snap -workspace ./Snap.xcworkspace -destination 'platform=iOS Simulator,name=iPhone 7' test | xcpretty
13 changes: 13 additions & 0 deletions Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
platform :ios, '9.0'

target 'Snap' do
use_frameworks!

# Pods for Snap

target 'SnapTests' do
inherit! :search_paths
pod 'CwlPreconditionTesting', :git => 'https://github.com/mattgallagher/CwlPreconditionTesting.git'
pod 'CwlCatchException', :git => 'https://github.com/mattgallagher/CwlCatchException.git'
end
end
30 changes: 30 additions & 0 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
PODS:
- CwlCatchException (1.0.2)
- CwlPreconditionTesting (1.1.0):
- CwlCatchException

DEPENDENCIES:
- CwlCatchException (from `https://github.com/mattgallagher/CwlCatchException.git`)
- CwlPreconditionTesting (from `https://github.com/mattgallagher/CwlPreconditionTesting.git`)

EXTERNAL SOURCES:
CwlCatchException:
:git: https://github.com/mattgallagher/CwlCatchException.git
CwlPreconditionTesting:
:git: https://github.com/mattgallagher/CwlPreconditionTesting.git

CHECKOUT OPTIONS:
CwlCatchException:
:commit: b14c111e9b33cd142bd4bc75c482cfd5c3490923
:git: https://github.com/mattgallagher/CwlCatchException.git
CwlPreconditionTesting:
:commit: b2e4800abf854366d4492051069cce7b44d20ba1
:git: https://github.com/mattgallagher/CwlPreconditionTesting.git

SPEC CHECKSUMS:
CwlCatchException: 70a52ae44ea5d46db7bd385f801a94942420cd8c
CwlPreconditionTesting: d9b35890c80b34668cd90da5bef077fd817a99fc

PODFILE CHECKSUM: 540c3fa6ae55d09ebef0fe29fae919227da61157

COCOAPODS: 1.4.0.beta.2
171 changes: 171 additions & 0 deletions Snap.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Snap.xcodeproj/xcshareddata/xcschemes/Snap.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0910"
LastUpgradeVersion = "0920"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
10 changes: 10 additions & 0 deletions Snap.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Snap/Core/Application/Actions/CompareImages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ struct CompareImages {
}

let reference = testTarget.reference(for: .reference).path
guard fileManager.fileExists(atPath: reference.absoluteString) else {
XCTFail("🚫 Reference image not found [`\(reference)`]")
guard fileManager.fileExists(atPath: reference.path) else {
XCTFail("🚫 Reference image not found [`\(reference.path)`]")
return
}

guard let referenceImage = UIImage(contentsOfFile: reference.standardizedFileURL.absoluteString) else {
XCTFail("🚫 Error loading reference image [`\(reference)`]")
guard let referenceImage = UIImage(contentsOfFile: reference.path) else {
XCTFail("🚫 Error loading reference image [`\(reference.path)`]")
return
}

Expand All @@ -43,13 +43,13 @@ struct CompareImages {
try referenceImage.compare(with: processedImage)
} catch CompareError.notEqualSize(let referenceSize, let comparedSize) {
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("📏 Image sizes should be equals, reference image size: \(referenceSize), compared image size: \(comparedSize)")
XCTFail("📏 Image sizes should be equals, reference image size: \(referenceSize), compared image size: \(comparedSize)")
} catch CompareError.invalidImageSize {
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("📏 One of the images has 0 size")
XCTFail("📏 One of the images has 0 size")
} catch CompareError.notEquals {
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("≠ Images are not equal")
XCTFail("≠ Images are not equal")
} catch CompareError.notEqualMetadata {
self.process(failedImage: processedImage, reference: referenceImage, testTarget: testTarget)
XCTFail("👾 Images have different metadata information")
Expand Down
4 changes: 2 additions & 2 deletions Snap/Core/Application/Actions/ExtractViewImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit
import XCTest

struct ExtractViewImage {

private let saveImageToDisk: SaveImageToDisk
private let addAttachment: AddAttachment

Expand All @@ -20,6 +20,6 @@ struct ExtractViewImage {
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.")
XCTFail("⚠️ Test ran in record mode, reference image has been saved to \(testTarget.reference(for: .reference).path.path), now remove `isRecording` in order to perform the snapshot comparsion.")
}
}
6 changes: 3 additions & 3 deletions Snap/Core/Application/Actions/SaveImageToDisk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct SaveImageToDisk {

private let environment: Environment
private let fileManager: FileManager

init(environment: Environment,
fileManager: FileManager)
{
Expand All @@ -16,11 +16,11 @@ struct SaveImageToDisk {
let referenceImage = UIImagePNGRepresentation(image)

do {
try fileManager.createDirectory(atPath: reference.directory.absoluteString, withIntermediateDirectories: true)
try fileManager.createDirectory(atPath: reference.directory.path, withIntermediateDirectories: true)
} catch {
fatalError("🚨 Error creating reference image directory ['\(reference.directory)']")
}
guard fileManager.createFile(atPath: reference.path.absoluteString, contents: referenceImage) else {
guard fileManager.createFile(atPath: reference.path.path, contents: referenceImage) else {
fatalError("🚨 Error saving reference image into ['\(reference.path)']")
}
}
Expand Down
10 changes: 5 additions & 5 deletions Snap/Core/Domain/Model/TestTarget+Reference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension TestTarget {
let functionName = function.replacingOccurrences(of: "()", with: "").lowercased()
let path = "\(self.path(for: type))\(classFile)/"
let directory = url.appendingPathComponent(path)
let name = named ?? functionName
let name = (named ?? functionName).replacingOccurrences(of: " ", with: "_")
let pathUrl = directory.appendingPathComponent("\(type.rawValue)_\(name)\(scale).png")

return Reference(
Expand All @@ -32,14 +32,14 @@ extension TestTarget {
guard let referenceImagePath = environment.get(Path.referenceImage) else {
fatalError("🚧 You need to configure the reference image path environment variable `\(Path.referenceImage)`")
}

let fileURL = URL(fileURLWithPath: referenceImagePath)
var isDir: ObjCBool = true
let referenceImagePathExists = fileManager.fileExists(atPath: referenceImagePath, isDirectory: &isDir)
let referenceImagePathExists = fileManager.fileExists(atPath: fileURL.path, isDirectory: &isDir)

guard referenceImagePathExists, let url = URL(string: referenceImagePath) else {
guard referenceImagePathExists else {
fatalError("🚫 Provided path ['\(referenceImagePath)'] for `\(Path.referenceImage)` is invalid")
}
return url
return fileURL
}

private func path(for type: Type) -> String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

final class EnvironmentStub: Environment {
var keys = [String: String]()

func get(_ key: String) -> String? {
return keys[key]
}
}
133 changes: 133 additions & 0 deletions SnapTests/Core/Domain/Model/TestTargetSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import XCTest

final class TestTargetSpec: XCTestCase {

private var environment: EnvironmentStub!

override func setUp() {
super.setUp()
environment = EnvironmentStub()
}

override func tearDown() {
environment = nil
super.tearDown()
}

func test_should_throw_assertion_if_environment_path_is_not_set() {
givenEnviromentIsNotSet()

let reference = givenTestTarget()

assertThrowException() {
let _ = reference.reference(for: .reference).path
}
}

func test_should_no_throw_if_environment_path_is_set() {
givenEnvironment()

let reference = givenTestTarget()

assertNoThrowException() {
let _ = reference.reference(for: .reference).path
}
}

func test_should_throw_assertion_if_given_path_is_invalid() {
givenEnvironment(with: "invalid/path/")

let reference = givenTestTarget()

assertThrowException() {
let _ = reference.reference(for: .reference).path
}
}

func test_should_return_correct_path_for_test_target() {
givenEnvironment()

let reference = givenTestTarget()

let referencePath = reference.reference(for: .reference).path
let expectedPath = "\(currentPath)/Snap/file/[email protected]"

XCTAssertEqual(referencePath.path, expectedPath)
}

func test_should_return_correct_path_for_test_target_with_reference_path_with_spaces() {
givenEnvironment()

let reference = givenTestTarget(
with: "function name"
)

let referencePath = reference.reference(for: .reference).path
let expectedPath = "\(currentPath)/Snap/file/[email protected]"

XCTAssertEqual(referencePath.path, expectedPath)
}

func test_should_return_correct_path_for_test_target_with_custom_name() {
givenEnvironment()

let reference = givenTestTarget(
with: "function name",
named: "custom name"
)

let referencePath = reference.reference(for: .reference).path
let expectedPath = "\(currentPath)/Snap/file/[email protected]"

XCTAssertEqual(referencePath.path, expectedPath)
}

func test_should_return_correct_path_for_different_test_targets() {
givenEnvironment()

let reference = givenTestTarget(
with: "function name",
named: "custom name"
)

let referencePath = reference.reference(for: .reference).path
let failedPath = reference.reference(for: .failed).path
let diffPath = reference.reference(for: .diff).path

let expectedReferencePath = "\(currentPath)/Snap/file/[email protected]"
let expectedFailedPath = "\(currentPath)/Snap/Failed/file/[email protected]"
let expectedDiffPath = "\(currentPath)/Snap/Diff/file/[email protected]"

XCTAssertEqual(referencePath.path, expectedReferencePath)
XCTAssertEqual(failedPath.path, expectedFailedPath)
XCTAssertEqual(diffPath.path, expectedDiffPath)
}
}

// MARK: - Helpers

extension TestTargetSpec {
private func givenEnviromentIsNotSet() {
environment.keys[Path.referenceImage] = nil
}

private func givenEnvironment(with path: String? = FileManager.default.currentDirectoryPath) {
environment.keys[Path.referenceImage] = path
}

private func givenTestTarget(with function: String = "function",
file: String = "file",
named: String? = nil) -> TestTarget
{
return TestTarget(
function: function,
file: file,
named: named,
fileManager: .default,
environment: environment
)
}
var currentPath: String {
return FileManager.default.currentDirectoryPath
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import XCTest
import UIKit

class CALayerImageSpec: XCTestCase {
final class CALayerImageSpec: XCTestCase {

func test_should_render_image_from_calayer() {
let layer = CALayer()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import XCTest

class UIImageCompareSpec: XCTestCase {
final class UIImageCompareSpec: XCTestCase {

func test_should_throw_if_image_sizes_are_not_the_equals() throws {
let image1 = Image.fixture(.camera, from: self)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import XCTest
import UIKit

class UIImageNormalizedSpec: XCTestCase {
final class UIImageNormalizedSpec: XCTestCase {

func test_should_normalize_image_into_data() {
let image = Image.fixture(from: self)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import XCTest
import CwlCatchException
import CwlPreconditionTesting

extension XCTestCase {
func assertThrowException(f: @escaping () -> Void) {
let instruction = catchBadInstruction {
f()
}

guard let _ = instruction else {
XCTFail("No exceptions were thrown")
return
}
}

func assertNoThrowException(f: @escaping () -> Void) {
XCTAssertNil(catchBadInstruction { f() })
}
}

0 comments on commit eececb6

Please sign in to comment.