Skip to content

Instantly share code, notes, and snippets.

@daltonclaybrook
Last active June 4, 2025 19:07
Show Gist options
  • Save daltonclaybrook/38bce5fd9b8a150952c924fc1045b12c to your computer and use it in GitHub Desktop.
Save daltonclaybrook/38bce5fd9b8a150952c924fc1045b12c to your computer and use it in GitHub Desktop.
import Foundation
import SnapshotTesting
import XCTest
func assertBundledSnapshot<Value, Format>(
of value: @autoclosure () throws -> Value,
as snapshotting: Snapshotting<Value, Format>,
named name: String? = nil,
record recording: Bool? = nil,
timeout: TimeInterval = 5,
fileID: StaticString = #fileID,
file filePath: StaticString = #filePath,
testName: String = #function,
line: UInt = #line,
column: UInt = #column
) {
let snapshotDirectory = getSnapshotDirectory(filePath: filePath)?.path
let failure = verifySnapshot(
of: try value(),
as: snapshotting,
named: name,
record: recording,
snapshotDirectory: snapshotDirectory,
timeout: timeout,
fileID: fileID,
file: filePath,
testName: testName,
line: line,
column: column
)
guard let message = failure else { return }
XCTFail(message, file: filePath, line: line)
}
// MARK: - Private
private func getSnapshotDirectory(filePath: StaticString) -> URL? {
let currentBundle = Bundle(for: CurrentBundleFinder.self)
guard
let snapshotBundleURL = currentBundle.resourceURL?.appendingPathComponent("Snapshots.bundle"),
let snapshotBundle = Bundle(url: snapshotBundleURL),
let snapshotResourceURL = snapshotBundle.resourceURL
else {
return nil
}
let fileURL = URL(fileURLWithPath: "\(filePath)", isDirectory: false)
let fileName = fileURL.deletingPathExtension().lastPathComponent
let snapshotDirectory = snapshotResourceURL
.appendingPathComponent("__Snapshots__")
.appendingPathComponent(fileName)
var isDirectory: ObjCBool = false
if FileManager.default.fileExists(atPath: snapshotDirectory.path, isDirectory: &isDirectory), isDirectory.boolValue == true {
return snapshotDirectory
} else {
return nil
}
}
private class CurrentBundleFinder {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment