Skip to content

Commit 176dbff

Browse files
authored
Extend file cloning of attachments to ABI.EncodedAttachment. (#1616)
This PR ensures that an `ABI.EncodedAttachment` instance that's wrapping an instance of `AnyAttachable` (i.e. one that has not yet been encoded to JSON) will still be able to clone its underlying file where appropriate. I haven't tried to plumb this through attachments stored solely as file paths because they've already been serialized/deserialized by that point, which implies a slow path anyway. We can revisit that in a future PR if we think it's valuable. Follow-up to #1590 and #1587. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent a6346e3 commit 176dbff

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

Sources/Testing/ABI/Encoded/ABI.EncodedAttachment.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ extension ABI.EncodedAttachment: Codable {
7979
case let .savedAtPath(path):
8080
try container.encode(path, forKey: .path)
8181
case let .abstract(attachment):
82-
if V.includesExperimentalFields {
82+
if let path = attachment.fileSystemPath {
83+
try container.encode(path, forKey: .path)
84+
} else if V.includesExperimentalFields {
8385
var errorWhileEncoding: (any Error)?
8486
do {
8587
try attachment.withUnsafeBytes { bytes in
@@ -207,6 +209,18 @@ extension ABI.EncodedAttachment: Attachable {
207209
}
208210
}
209211

212+
#if !SWT_NO_FILE_CLONING
213+
extension ABI.EncodedAttachment: FileClonable {
214+
package func clone(toFileAtPath filePath: String) -> Bool {
215+
guard case let .abstract(attachment) = kind else {
216+
return false
217+
}
218+
return attachment.attachableValue.clone(toFileAtPath: filePath)
219+
}
220+
221+
}
222+
#endif
223+
210224
extension ABI.EncodedAttachment.BytesUnavailableError: CustomStringConvertible {
211225
var description: String {
212226
"The attachment's content could not be deserialized."
@@ -221,12 +235,7 @@ extension ABI.EncodedAttachment {
221235
/// - Parameters:
222236
/// - attachment: The attachment to initialize this instance from.
223237
public init(encoding attachment: borrowing Attachment<AnyAttachable>) {
224-
if let path = attachment.fileSystemPath {
225-
kind = .savedAtPath(path)
226-
} else {
227-
kind = .abstract(copy attachment)
228-
}
229-
238+
kind = .abstract(copy attachment)
230239
if V.includesExperimentalFields {
231240
_preferredName = attachment.preferredName
232241
}

0 commit comments

Comments
 (0)