Skip to content

Commit

Permalink
Fix image loading crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
lunij committed Jun 23, 2024
1 parent a70067a commit 2b1ce84
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public func verifySnapshot<Value, Format>(
}

let data = try Data(contentsOf: snapshotFileUrl)
let reference = snapshotting.diffing.fromData(data)
let reference = try snapshotting.diffing.fromData(data)

guard let (failureMessage, attachments) = snapshotting.diffing.diff(reference, diffable) else {
return nil
Expand Down
4 changes: 2 additions & 2 deletions Sources/SnapshotTesting/Diffing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public struct Diffing<Value> {
public var toData: (Value) throws -> Data

/// Produces a value _from_ data.
public var fromData: (Data) -> Value
public var fromData: (Data) throws -> Value

/// Compares two values. If the values do not match, returns a failure message and artifacts
/// describing the failure.
Expand All @@ -26,7 +26,7 @@ public struct Diffing<Value> {
/// - rhs: Another value to compare.
public init(
toData: @escaping (_ value: Value) throws -> Data,
fromData: @escaping (_ data: Data) -> Value,
fromData: @escaping (_ data: Data) throws -> Value,
diff: @escaping (_ lhs: Value, _ rhs: Value) -> (String, [XCTAttachment])?
) {
self.toData = toData
Expand Down
21 changes: 21 additions & 0 deletions Sources/SnapshotTesting/Extensions/Data+XImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

extension Data {
#if os(iOS) || os(tvOS)
func image(scale: CGFloat) throws -> XImage {
let image = XImage(data: self, scale: scale)
guard let image else {
throw ImageConversionError.invalidImageData
}
return image
}
#elseif os(macOS)
func image() throws -> XImage {
let image = XImage(data: self)
guard let image else {
throw ImageConversionError.invalidImageData
}
return image
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation

enum ImageConversionError: Error {
case cgImageConversionFailed
case invalidImageData
case pngDataConversionFailed
case zeroHeight
case zeroSize
Expand All @@ -14,6 +15,8 @@ extension ImageConversionError: LocalizedError {
switch self {
case .cgImageConversionFailed, .pngDataConversionFailed:
return "Snapshot could not be processed"
case .invalidImageData:
return "Invalid image data"
case .zeroHeight:
return "Snapshot has a height of zero"
case .zeroSize:
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Snapshotting/NSImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public static func image(precision: Float = 1, perceptualPrecision: Float = 1) -> Diffing {
return .init(
toData: convertToData,
fromData: { NSImage(data: $0)! }
fromData: { try $0.image() }
) { old, new in
do {
let result = try compare(old, new, precision: precision, perceptualPrecision: perceptualPrecision)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Snapshotting/UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
) -> Diffing {
Diffing(
toData: convertToData,
fromData: { UIImage(data: $0, scale: scale)! }
fromData: { try $0.image(scale: scale) }
) { old, new in
do {
let result = try compare(old, new, precision: precision, perceptualPrecision: perceptualPrecision)
Expand Down
7 changes: 7 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,13 @@ final class SnapshotTestingTests: XCTestCase {
XCTAssertEqual(firstLine, "[macos] Snapshot size (123.0, 123.0) is unequal to expected size (100.0, 100.0)")
#endif
}

func testImageSnapshot_whenReferenceImageLoadingFails() {
let size = CGSize(width: 100, height: 100)
let view = XView(frame: .init(origin: .zero, size: size))
let message = verifySnapshot(of: view, as: .image)
XCTAssertEqual(message, "Invalid image data")
}
#endif

#if canImport(SwiftUI)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2b1ce84

Please sign in to comment.