Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Commit cfe06e7

Browse files
committed
Adding ability to write diff artifacts to disk
1 parent 88f6e2c commit cfe06e7

File tree

5 files changed

+80
-16
lines changed

5 files changed

+80
-16
lines changed

Sources/SnapshotTesting/AssertInlineSnapshot.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ public func _verifyInlineSnapshot<Value>(
151151
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") {
152152
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
153153
attachments.forEach {
154-
activity.add($0)
154+
let data = snapshotting.diffing.toData($0.value)
155+
let attachment = XCTAttachment(
156+
data: data,
157+
uniformTypeIdentifier: $0.uniformTypeIdentifier
158+
)
159+
attachment.name = $0.name
160+
activity.add(attachment)
155161
}
156162
}
157163
}

Sources/SnapshotTesting/AssertSnapshot.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ public func verifySnapshot<Value, Format>(
198198
}
199199

200200
let testName = sanitizePathComponent(testName)
201+
let snapshotFileName = "\(testName).\(identifier)"
201202
let snapshotFileUrl = snapshotDirectoryUrl
202-
.appendingPathComponent("\(testName).\(identifier)")
203+
.appendingPathComponent(snapshotFileName)
203204
.appendingPathExtension(snapshotting.pathExtension ?? "")
204205
let fileManager = FileManager.default
205206
try fileManager.createDirectory(at: snapshotDirectoryUrl, withIntermediateDirectories: true)
@@ -270,20 +271,37 @@ public func verifySnapshot<Value, Format>(
270271
fileURLWithPath: ProcessInfo.processInfo.environment["SNAPSHOT_ARTIFACTS"] ?? NSTemporaryDirectory(), isDirectory: true
271272
)
272273
let artifactsSubUrl = artifactsUrl.appendingPathComponent(fileName)
273-
try fileManager.createDirectory(at: artifactsSubUrl, withIntermediateDirectories: true)
274274
let failedSnapshotFileUrl = artifactsSubUrl.appendingPathComponent(snapshotFileUrl.lastPathComponent)
275-
try snapshotting.diffing.toData(diffable).write(to: failedSnapshotFileUrl)
275+
try fileManager.createDirectory(at: artifactsSubUrl, withIntermediateDirectories: true)
276276

277277
if !attachments.isEmpty {
278278
#if !os(Linux) && !os(Windows)
279279
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") {
280280
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
281281
attachments.forEach {
282-
activity.add($0)
282+
let data = snapshotting.diffing.toData($0.value)
283+
let attachment = XCTAttachment(
284+
data: data,
285+
uniformTypeIdentifier: $0.uniformTypeIdentifier
286+
)
287+
attachment.name = $0.name
288+
activity.add(attachment)
283289
}
284290
}
285291
}
286292
#endif
293+
294+
for (index, attachment) in attachments.enumerated() {
295+
let snapshotArtifactUrl = artifactsSubUrl.appendingPathComponent(snapshotFileName)
296+
try fileManager.createDirectory(at: snapshotArtifactUrl, withIntermediateDirectories: true)
297+
298+
let snapshotArtifactFileUrl = snapshotArtifactUrl
299+
.appendingPathComponent(attachment.name ?? String(describing: index))
300+
.appendingPathExtension(snapshotting.pathExtension ?? "")
301+
try snapshotting.diffing.toData(attachment.value).write(to: snapshotArtifactFileUrl)
302+
}
303+
} else {
304+
try snapshotting.diffing.toData(diffable).write(to: failedSnapshotFileUrl)
287305
}
288306

289307
let diffMessage = diffTool

Sources/SnapshotTesting/Diffing.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public struct Diffing<Value> {
1010
public var fromData: (Data) -> Value
1111

1212
/// Compares two values. If the values do not match, returns a failure message and artifacts describing the failure.
13-
public var diff: (Value, Value) -> (String, [XCTAttachment])?
13+
public var diff: (Value, Value) -> (String, [DiffingArtifact<Value>])?
1414

1515
/// Creates a new `Diffing` on `Value`.
1616
///
@@ -25,10 +25,38 @@ public struct Diffing<Value> {
2525
public init(
2626
toData: @escaping (_ value: Value) -> Data,
2727
fromData: @escaping (_ data: Data) -> Value,
28-
diff: @escaping (_ lhs: Value, _ rhs: Value) -> (String, [XCTAttachment])?
28+
diff: @escaping (_ lhs: Value, _ rhs: Value) -> (String, [DiffingArtifact<Value>])?
2929
) {
3030
self.toData = toData
3131
self.fromData = fromData
3232
self.diff = diff
3333
}
3434
}
35+
36+
public struct DiffingArtifact<Value> {
37+
/// The name of artifact type, i.e. "reference", "failure", "patch", etc.
38+
public var name: String?
39+
40+
/// UTI (Uniform Type Identifier) of the payload data for an `XCTAttachment`.
41+
public var uniformTypeIdentifier: String
42+
43+
/// The value of the artifact.
44+
public var value: Value
45+
46+
/// Creates a new `DiffingArtifact` on `Value`.
47+
///
48+
/// - Parameters:
49+
/// - name: The name of the artifact, to be written to disk and included
50+
/// as an `XCTAttachment`.
51+
/// - uniformTypeIdentifier: UTI for `XCTAttachment` payload representing the artifact.
52+
/// - value: The value to be written to disk and included as an `XCTAttachment` payload.
53+
public init(
54+
name: String? = nil,
55+
uniformTypeIdentifier: String,
56+
value: Value
57+
) {
58+
self.name = name
59+
self.uniformTypeIdentifier = uniformTypeIdentifier
60+
self.value = value
61+
}
62+
}

Sources/SnapshotTesting/Snapshotting/String.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ extension Diffing where Value == String {
2020
let failure = hunks
2121
.flatMap { [$0.patchMark] + $0.lines }
2222
.joined(separator: "\n")
23-
let attachment = XCTAttachment(data: Data(failure.utf8), uniformTypeIdentifier: "public.patch-file")
24-
return (failure, [attachment])
23+
let artifact = DiffingArtifact(
24+
uniformTypeIdentifier: "public.patch-file",
25+
value: failure
26+
)
27+
return (failure, [artifact])
2528
}
2629
}

Sources/SnapshotTesting/Snapshotting/UIImage.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,24 @@ extension Diffing where Value == UIImage {
2828
let message = new.size == old.size
2929
? "Newly-taken snapshot does not match reference."
3030
: "Newly-taken snapshot@\(new.size) does not match reference@\(old.size)."
31-
let oldAttachment = XCTAttachment(image: old)
32-
oldAttachment.name = "reference"
33-
let newAttachment = XCTAttachment(image: new)
34-
newAttachment.name = "failure"
35-
let differenceAttachment = XCTAttachment(image: difference)
36-
differenceAttachment.name = "difference"
31+
let oldArtifact = DiffingArtifact(
32+
name: "reference",
33+
uniformTypeIdentifier: "public.png",
34+
value: old
35+
)
36+
let newArtifact = DiffingArtifact(
37+
name: "failure",
38+
uniformTypeIdentifier: "public.png",
39+
value: new
40+
)
41+
let diffArtifact = DiffingArtifact(
42+
name: "difference",
43+
uniformTypeIdentifier: "public.png",
44+
value: difference
45+
)
3746
return (
3847
message,
39-
[oldAttachment, newAttachment, differenceAttachment]
48+
[oldArtifact, newArtifact, diffArtifact]
4049
)
4150
}
4251
}

0 commit comments

Comments
 (0)