diff --git a/SnapshotTesting.podspec b/SnapshotTesting.podspec index fa47df170..3f9dba128 100644 --- a/SnapshotTesting.podspec +++ b/SnapshotTesting.podspec @@ -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" diff --git a/Sources/SnapshotTesting/AssertSnapshot.swift b/Sources/SnapshotTesting/AssertSnapshot.swift index 39e1337a1..fa7242087 100644 --- a/Sources/SnapshotTesting/AssertSnapshot.swift +++ b/Sources/SnapshotTesting/AssertSnapshot.swift @@ -27,7 +27,8 @@ public func assertSnapshot( timeout: TimeInterval = 5, file: StaticString = #file, testName: String = #function, - line: UInt = #line + line: UInt = #line, + snapshotPath: String? = nil ) { let failure = verifySnapshot( @@ -38,7 +39,8 @@ public func assertSnapshot( timeout: timeout, file: file, testName: testName, - line: line + line: line, + snapshotPath: snapshotPath ) guard let message = failure else { return } XCTFail(message, file: file, line: line) @@ -161,7 +163,8 @@ public func verifySnapshot( timeout: TimeInterval = 5, file: StaticString = #file, testName: String = #function, - line: UInt = #line + line: UInt = #line, + snapshotPath: String? = nil ) -> String? { @@ -190,9 +193,15 @@ public func verifySnapshot( } 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) @@ -218,20 +227,34 @@ public func verifySnapshot( 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. """ @@ -244,12 +267,6 @@ public func verifySnapshot( 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 {