-
Notifications
You must be signed in to change notification settings - Fork 146
CoreTransferable attachments
#1519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grynspan
wants to merge
10
commits into
main
Choose a base branch
from
jgrynspan/transferable-attachments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−1
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bf0b102
[WIP] `CoreTransferable` attachments
grynspan 10b5ca2
Merge branch 'main' into jgrynspan/transferable-attachments
grynspan bdb5e9a
Merge branch 'main' into jgrynspan/transferable-attachments
grynspan 3dfd55c
Merge remote-tracking branch 'origin/main' into jgrynspan/transferabl…
stmontgomery a56a4a5
Fixup Package.swift
stmontgomery 40517ac
Merge branch 'main' into jgrynspan/transferable-attachments
grynspan 2f2e448
Merge branch 'main' into jgrynspan/transferable-attachments
grynspan 87fc46e
Incorporate feedback
grynspan dfa0fff
Promote to API
grynspan b621007
API availability
grynspan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
Sources/Overlays/_Testing_CoreTransferable/Attachments/Attachment+Transferable.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| #if canImport(CoreTransferable) | ||
| public import Testing | ||
| public import CoreTransferable | ||
|
|
||
| public import UniformTypeIdentifiers | ||
|
|
||
| /// @Metadata { | ||
| /// @Available(Swift, introduced: 6.4) | ||
| /// } | ||
| @available(_transferableAPI, *) | ||
| extension Attachment { | ||
| /// Initialize an instance of this type that encloses the given transferable | ||
| /// value. | ||
| /// | ||
| /// - Parameters: | ||
| /// - transferableValue: The value that will be attached to the output of | ||
| /// the test run. | ||
| /// - contentType: The content type with which to export `transferableValue`. | ||
| /// If this argument is `nil`, the testing library calls [`exportedContentTypes(_:)`](https://developer.apple.com/documentation/coretransferable/transferable/exportedcontenttypes(_:)) | ||
| /// on `transferableValue` and uses the first type the function returns | ||
| /// that conforms to [`UTType.data`](https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/data). | ||
| /// - preferredName: The preferred name of the attachment to use when saving | ||
| /// it. If `nil`, the testing library attempts to generate a reasonable | ||
| /// filename for the attached value. | ||
| /// - sourceLocation: The source location of the call to this initializer. | ||
| /// This value is used when recording issues associated with the | ||
| /// attachment. | ||
| /// | ||
| /// - Throws: Any error that occurs while exporting `transferableValue`. | ||
| /// | ||
| /// Use this initializer to create an instance of ``Attachment`` from a value | ||
| /// that conforms to the [`Transferable`](https://developer.apple.com/documentation/coretransferable/transferable) | ||
| /// protocol. | ||
| /// | ||
| /// ```swift | ||
| /// let menu = FoodTruck.menu | ||
| /// let attachment = try await Attachment(exporting: menu, as: .pdf) | ||
| /// Attachment.record(attachment) | ||
| /// ``` | ||
| /// | ||
| /// When you call this initializer and pass it a transferable value, it | ||
| /// calls [`exported(as:)`](https://developer.apple.com/documentation/coretransferable/transferable/exported(as:)) | ||
| /// on that value. This operation may take some time, so this initializer | ||
| /// suspends the calling task until it is complete. | ||
| /// | ||
| /// @Metadata { | ||
| /// @Available(Swift, introduced: 6.4) | ||
| /// } | ||
| public init<T>( | ||
| exporting transferableValue: T, | ||
| as contentType: UTType? = nil, | ||
| named preferredName: String? = nil, | ||
| sourceLocation: SourceLocation = #_sourceLocation | ||
| ) async throws where T: Transferable, AttachableValue == _AttachableTransferableWrapper<T> { | ||
| let transferableWrapper = try await _AttachableTransferableWrapper(exporting: transferableValue, as: contentType) | ||
| self.init(transferableWrapper, named: preferredName, sourceLocation: sourceLocation) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - | ||
|
|
||
| /// A type describing errors that can occur when attaching a transferable value. | ||
| enum TransferableAttachmentError: Error { | ||
| /// The developer did not pass a content type and the value did not list any | ||
| /// that conform to `UTType.data`. | ||
| case suitableContentTypeNotFound | ||
| } | ||
|
|
||
| extension TransferableAttachmentError: CustomStringConvertible { | ||
| var description: String { | ||
| switch self { | ||
| case .suitableContentTypeNotFound: | ||
| "The value does not list any exported content types that conform to 'UTType.data'." | ||
| } | ||
| } | ||
| } | ||
| #endif |
81 changes: 81 additions & 0 deletions
81
Sources/Overlays/_Testing_CoreTransferable/Attachments/_AttachableTransferableWrapper.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| #if canImport(CoreTransferable) | ||
| public import Testing | ||
| public import CoreTransferable | ||
|
|
||
| private import Foundation | ||
| import UniformTypeIdentifiers | ||
|
|
||
| /// A wrapper type representing transferable values that can be attached | ||
| /// indirectly. | ||
| /// | ||
| /// You do not need to use this type directly. Instead, initialize an instance | ||
| /// of ``Attachment`` using an instance of a type conforming to the [`Transferable`](https://developer.apple.com/documentation/coretransferable/transferable) | ||
| /// protocol. | ||
| /// | ||
| /// @Metadata { | ||
| /// @Available(Swift, introduced: 6.4) | ||
| /// } | ||
| @available(_transferableAPI, *) | ||
| public struct _AttachableTransferableWrapper<T>: Sendable where T: Transferable { | ||
| /// The transferable value. | ||
| private var _transferableValue: T | ||
|
|
||
| /// The content type used to export the transferable value. | ||
| private var _contentType: UTType | ||
|
|
||
| /// The exported form of the transferable value. | ||
| private var _bytes: Data | ||
|
|
||
| init(exporting transferableValue: T, as contentType: UTType?) async throws { | ||
| let contentType = contentType ?? transferableValue.exportedContentTypes() | ||
| .first { $0.conforms(to: .data) } | ||
| guard let contentType else { | ||
| throw TransferableAttachmentError.suitableContentTypeNotFound | ||
| } | ||
|
|
||
| _transferableValue = transferableValue | ||
| _contentType = contentType | ||
| _bytes = try await transferableValue.exported(as: contentType) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - | ||
|
|
||
| /// @Metadata { | ||
| /// @Available(Swift, introduced: 6.4) | ||
| /// } | ||
| @available(_transferableAPI, *) | ||
| extension _AttachableTransferableWrapper: AttachableWrapper { | ||
| /// @Metadata { | ||
| /// @Available(Swift, introduced: 6.4) | ||
| /// } | ||
| public var wrappedValue: T { | ||
| _transferableValue | ||
| } | ||
|
|
||
| /// @Metadata { | ||
| /// @Available(Swift, introduced: 6.4) | ||
| /// } | ||
| public func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
| try _bytes.withUnsafeBytes(body) | ||
| } | ||
|
|
||
| /// @Metadata { | ||
| /// @Available(Swift, introduced: 6.4) | ||
| /// } | ||
| public borrowing func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String { | ||
| let baseName = _transferableValue.suggestedFilename ?? suggestedName | ||
| return (baseName as NSString).appendingPathExtension(for: _contentType) | ||
| } | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # This source file is part of the Swift.org open source project | ||
| # | ||
| # Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| # Licensed under Apache License v2.0 with Runtime Library Exception | ||
| # | ||
| # See https://swift.org/LICENSE.txt for license information | ||
| # See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
|
||
| if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") | ||
| include(ModuleABIName) | ||
| add_library(_Testing_CoreTransferable | ||
| Attachments/_AttachableTransferableWrapper.swift | ||
| Attachments/Attachment+Transferable.swift | ||
| ReexportTesting.swift) | ||
|
|
||
| target_link_libraries(_Testing_CoreTransferable PUBLIC | ||
| Testing) | ||
|
|
||
| target_compile_options(_Testing_CoreTransferable PRIVATE | ||
| -enable-library-evolution | ||
| -emit-module-interface -emit-module-interface-path $<TARGET_PROPERTY:_Testing_CoreTransferable,Swift_MODULE_DIRECTORY>/_Testing_CoreTransferable.swiftinterface) | ||
|
|
||
| _swift_testing_install_target(_Testing_CoreTransferable) | ||
| endif() |
11 changes: 11 additions & 0 deletions
11
Sources/Overlays/_Testing_CoreTransferable/ReexportTesting.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| @_exported public import Testing |
3 changes: 3 additions & 0 deletions
3
Sources/Testing/Testing.swiftcrossimport/CoreTransferable.swiftoverlay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| version: 1 | ||
| modules: | ||
| - name: _Testing_CoreTransferable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.