Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SnapshotTesting.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Pod::Spec.new do |s|
:tag => s.version
}

s.swift_versions = "5.0", "5.1.2"
s.swift_version = "5.0"

s.ios.deployment_target = "10.0"
s.osx.deployment_target = "10.10"
Expand Down
47 changes: 32 additions & 15 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public func assertSnapshot<Value, Format>(
timeout: TimeInterval = 5,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
line: UInt = #line,
snapshotPath: String? = nil
) {

let failure = verifySnapshot(
Expand All @@ -38,7 +39,8 @@ public func assertSnapshot<Value, Format>(
timeout: timeout,
file: file,
testName: testName,
line: line
line: line,
snapshotPath: snapshotPath
)
guard let message = failure else { return }
XCTFail(message, file: file, line: line)
Expand Down Expand Up @@ -161,7 +163,8 @@ public func verifySnapshot<Value, Format>(
timeout: TimeInterval = 5,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
line: UInt = #line,
snapshotPath: String? = nil
)
-> String? {

Expand Down Expand Up @@ -190,9 +193,15 @@ public func verifySnapshot<Value, Format>(
}

let testName = sanitizePathComponent(testName)
let snapshotFileUrl = snapshotDirectoryUrl
.appendingPathComponent("\(testName).\(identifier)")
.appendingPathExtension(snapshotting.pathExtension ?? "")
let snapshotFileUrl: URL
if let providedPath = snapshotPath {
snapshotFileUrl = URL(fileURLWithPath: providedPath, isDirectory: true)
}
else {
snapshotFileUrl = snapshotDirectoryUrl
.appendingPathComponent("\(testName).\(identifier)")
.appendingPathExtension(snapshotting.pathExtension ?? "")
}
let fileManager = FileManager.default
try fileManager.createDirectory(at: snapshotDirectoryUrl, withIntermediateDirectories: true)

Expand All @@ -218,20 +227,34 @@ public func verifySnapshot<Value, Format>(
return "Couldn't snapshot value"
}

let artifactsUrl = URL(
fileURLWithPath: ProcessInfo.processInfo.environment["SNAPSHOT_ARTIFACTS"] ?? NSTemporaryDirectory(), isDirectory: true
)
let artifactsSubUrl = artifactsUrl.appendingPathComponent(fileName)
try fileManager.createDirectory(at: artifactsSubUrl, withIntermediateDirectories: true)
let failedSnapshotFileUrl = artifactsSubUrl.appendingPathComponent(snapshotFileUrl.lastPathComponent)

guard !recording, fileManager.fileExists(atPath: snapshotFileUrl.path) else {
try snapshotting.diffing.toData(diffable).write(to: snapshotFileUrl)
var url = snapshotFileUrl

#if BAZEL
url = failedSnapshotFileUrl
#endif

try snapshotting.diffing.toData(diffable).write(to: url)

return recording
? """
Record mode is on. Turn record mode off and re-run "\(testName)" to test against the newly-recorded snapshot.

open "\(snapshotFileUrl.path)"
open "\(url.path)"

Recorded snapshot: …
"""
: """
No reference was found on disk. Automatically recorded snapshot: …

open "\(snapshotFileUrl.path)"
open "\(url.path)"

Re-run "\(testName)" to test against the newly-recorded snapshot.
"""
Expand All @@ -244,12 +267,6 @@ public func verifySnapshot<Value, Format>(
return nil
}

let artifactsUrl = URL(
fileURLWithPath: ProcessInfo.processInfo.environment["SNAPSHOT_ARTIFACTS"] ?? NSTemporaryDirectory(), isDirectory: true
)
let artifactsSubUrl = artifactsUrl.appendingPathComponent(fileName)
try fileManager.createDirectory(at: artifactsSubUrl, withIntermediateDirectories: true)
let failedSnapshotFileUrl = artifactsSubUrl.appendingPathComponent(snapshotFileUrl.lastPathComponent)
try snapshotting.diffing.toData(diffable).write(to: failedSnapshotFileUrl)

if !attachments.isEmpty {
Expand Down