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

Commit 6a6a7f1

Browse files
committed
Adding ability to write diff artifacts to disk
1 parent f2e4fda commit 6a6a7f1

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
@@ -144,7 +144,13 @@ public func _verifyInlineSnapshot<Value>(
144144
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") {
145145
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
146146
attachments.forEach {
147-
activity.add($0)
147+
let data = snapshotting.diffing.toData($0.value)
148+
let attachment = XCTAttachment(
149+
data: data,
150+
uniformTypeIdentifier: $0.uniformTypeIdentifier
151+
)
152+
attachment.name = $0.name
153+
activity.add(attachment)
148154
}
149155
}
150156
}

Sources/SnapshotTesting/AssertSnapshot.swift

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

192192
let testName = sanitizePathComponent(testName)
193+
let snapshotFileName = "\(testName).\(identifier)"
193194
let snapshotFileUrl = snapshotDirectoryUrl
194-
.appendingPathComponent("\(testName).\(identifier)")
195+
.appendingPathComponent(snapshotFileName)
195196
.appendingPathExtension(snapshotting.pathExtension ?? "")
196197
let fileManager = FileManager.default
197198
try fileManager.createDirectory(at: snapshotDirectoryUrl, withIntermediateDirectories: true)
@@ -248,20 +249,37 @@ public func verifySnapshot<Value, Format>(
248249
fileURLWithPath: ProcessInfo.processInfo.environment["SNAPSHOT_ARTIFACTS"] ?? NSTemporaryDirectory(), isDirectory: true
249250
)
250251
let artifactsSubUrl = artifactsUrl.appendingPathComponent(fileName)
251-
try fileManager.createDirectory(at: artifactsSubUrl, withIntermediateDirectories: true)
252252
let failedSnapshotFileUrl = artifactsSubUrl.appendingPathComponent(snapshotFileUrl.lastPathComponent)
253-
try snapshotting.diffing.toData(diffable).write(to: failedSnapshotFileUrl)
253+
try fileManager.createDirectory(at: artifactsSubUrl, withIntermediateDirectories: true)
254254

255255
if !attachments.isEmpty {
256256
#if !os(Linux)
257257
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") {
258258
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
259259
attachments.forEach {
260-
activity.add($0)
260+
let data = snapshotting.diffing.toData($0.value)
261+
let attachment = XCTAttachment(
262+
data: data,
263+
uniformTypeIdentifier: $0.uniformTypeIdentifier
264+
)
265+
attachment.name = $0.name
266+
activity.add(attachment)
261267
}
262268
}
263269
}
264270
#endif
271+
272+
for (index, attachment) in attachments.enumerated() {
273+
let snapshotArtifactUrl = artifactsSubUrl.appendingPathComponent(snapshotFileName)
274+
try fileManager.createDirectory(at: snapshotArtifactUrl, withIntermediateDirectories: true)
275+
276+
let snapshotArtifactFileUrl = snapshotArtifactUrl
277+
.appendingPathComponent(attachment.name ?? String(describing: index))
278+
.appendingPathExtension(snapshotting.pathExtension ?? "")
279+
try snapshotting.diffing.toData(attachment.value).write(to: snapshotArtifactFileUrl)
280+
}
281+
} else {
282+
try snapshotting.diffing.toData(diffable).write(to: failedSnapshotFileUrl)
265283
}
266284

267285
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
@@ -20,15 +20,24 @@ extension Diffing where Value == UIImage {
2020
let message = new.size == old.size
2121
? "Newly-taken snapshot does not match reference."
2222
: "Newly-taken snapshot@\(new.size) does not match reference@\(old.size)."
23-
let oldAttachment = XCTAttachment(image: old)
24-
oldAttachment.name = "reference"
25-
let newAttachment = XCTAttachment(image: new)
26-
newAttachment.name = "failure"
27-
let differenceAttachment = XCTAttachment(image: difference)
28-
differenceAttachment.name = "difference"
23+
let oldArtifact = DiffingArtifact(
24+
name: "reference",
25+
uniformTypeIdentifier: "public.png",
26+
value: old
27+
)
28+
let newArtifact = DiffingArtifact(
29+
name: "failure",
30+
uniformTypeIdentifier: "public.png",
31+
value: new
32+
)
33+
let diffArtifact = DiffingArtifact(
34+
name: "difference",
35+
uniformTypeIdentifier: "public.png",
36+
value: difference
37+
)
2938
return (
3039
message,
31-
[oldAttachment, newAttachment, differenceAttachment]
40+
[oldArtifact, newArtifact, diffArtifact]
3241
)
3342
}
3443
}

0 commit comments

Comments
 (0)