From ae640ec8460f6b22f866a5ba40ba2fc5af6ed787 Mon Sep 17 00:00:00 2001 From: Oscar Yuandinata Date: Mon, 27 Apr 2020 14:48:49 +0700 Subject: [PATCH 1/3] provide filepath --- Sources/SnapshotTesting/AssertSnapshot.swift | 54 ++++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/Sources/SnapshotTesting/AssertSnapshot.swift b/Sources/SnapshotTesting/AssertSnapshot.swift index 39e1337a1..ac51beba9 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,41 @@ 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) + var suffix = snapshotFileUrl.lastPathComponent + + // if running in bazel, add -bazel to filename to differentiate from xcode test + #if BAZEL + suffix = snapshotFileUrl.deletingPathExtension().lastPathComponent + "-bazel." + (snapshotting.pathExtension ?? "") + #endif + + let failedSnapshotFileUrl = artifactsSubUrl.appendingPathComponent(suffix) + 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 +274,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 { From a8ef7d1e7bbc08e2944c50816f6bd93e011b8f92 Mon Sep 17 00:00:00 2001 From: Oscar Yuandinata Date: Tue, 28 Apr 2020 16:24:15 +0700 Subject: [PATCH 2/3] fix swift version --- SnapshotTesting.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" From 861bc408fd3070acb490310b4bb413cec278f15e Mon Sep 17 00:00:00 2001 From: Oscar Yuandinata Date: Wed, 29 Apr 2020 15:10:52 +0700 Subject: [PATCH 3/3] fix something --- Sources/SnapshotTesting/AssertSnapshot.swift | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Sources/SnapshotTesting/AssertSnapshot.swift b/Sources/SnapshotTesting/AssertSnapshot.swift index ac51beba9..fa7242087 100644 --- a/Sources/SnapshotTesting/AssertSnapshot.swift +++ b/Sources/SnapshotTesting/AssertSnapshot.swift @@ -232,14 +232,7 @@ public func verifySnapshot( ) let artifactsSubUrl = artifactsUrl.appendingPathComponent(fileName) try fileManager.createDirectory(at: artifactsSubUrl, withIntermediateDirectories: true) - var suffix = snapshotFileUrl.lastPathComponent - - // if running in bazel, add -bazel to filename to differentiate from xcode test - #if BAZEL - suffix = snapshotFileUrl.deletingPathExtension().lastPathComponent + "-bazel." + (snapshotting.pathExtension ?? "") - #endif - - let failedSnapshotFileUrl = artifactsSubUrl.appendingPathComponent(suffix) + let failedSnapshotFileUrl = artifactsSubUrl.appendingPathComponent(snapshotFileUrl.lastPathComponent) guard !recording, fileManager.fileExists(atPath: snapshotFileUrl.path) else { var url = snapshotFileUrl