Skip to content

Commit 8503f6c

Browse files
committed
Mark image attachments as unavailable on non-Darwin/non-Windows.
This PR explicitly marks the image attachments API bits in the core Swift Testing library as unavailable on e.g. Linux. Developers who attempt to use this API on Linux will get diagnostics such as: > 🛑 'AttachableImageFormat' is unavailable: Image attachments are not available on this platform.
1 parent a6d370e commit 8503f6c

File tree

8 files changed

+40
-1
lines changed

8 files changed

+40
-1
lines changed

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ jobs:
2424
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
2525
with:
2626
license_header_check_project_name: "Swift"
27-
docs_check_container_image: "swift:6.2-noble"
27+
docs_check_enabled: false
2828
format_check_enabled: false
2929
api_breakage_check_enabled: false

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ extension Array where Element == PackageDescription.SwiftSetting {
383383
.define("SWT_NO_DYNAMIC_LINKING", .whenEmbedded(or: .when(platforms: [.wasi]))),
384384
.define("SWT_NO_PIPES", .whenEmbedded(or: .when(platforms: [.wasi]))),
385385
.define("SWT_NO_FOUNDATION_FILE_COORDINATION", .whenEmbedded(or: .whenApple(false))),
386+
.define("SWT_NO_IMAGE_ATTACHMENTS", .whenEmbedded(or: .when(platforms: [.linux, .custom("freebsd"), .openbsd, .wasi, .android]))),
386387

387388
.define("SWT_NO_LEGACY_TEST_DISCOVERY", .whenEmbedded()),
388389
.define("SWT_NO_LIBDISPATCH", .whenEmbedded()),

Sources/Testing/Attachments/Images/AttachableImageFormat.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
/// @Metadata {
3232
/// @Available(Swift, introduced: 6.3)
3333
/// }
34+
#if SWT_NO_IMAGE_ATTACHMENTS
35+
@available(*, unavailable, message: "Image attachments are not available on this platform.")
36+
#endif
3437
@available(_uttypesAPI, *)
3538
public struct AttachableImageFormat: Sendable {
3639
/// An enumeration describing the various kinds of image format that can be
@@ -77,6 +80,9 @@ public struct AttachableImageFormat: Sendable {
7780

7881
// MARK: -
7982

83+
#if SWT_NO_IMAGE_ATTACHMENTS
84+
@available(*, unavailable, message: "Image attachments are not available on this platform.")
85+
#endif
8086
@available(_uttypesAPI, *)
8187
extension AttachableImageFormat {
8288
/// The PNG image format.

Sources/Testing/Attachments/Images/Attachment+_AttachableAsImage.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
//
1010

11+
#if SWT_NO_IMAGE_ATTACHMENTS
12+
@available(*, unavailable, message: "Image attachments are not available on this platform.")
13+
#endif
1114
@available(_uttypesAPI, *)
1215
extension Attachment {
1316
/// Initialize an instance of this type that encloses the given image.
@@ -101,6 +104,9 @@ extension Attachment {
101104
// MARK: -
102105

103106
@_spi(Experimental) // STOP: not part of ST-0014
107+
#if SWT_NO_IMAGE_ATTACHMENTS
108+
@available(*, unavailable, message: "Image attachments are not available on this platform.")
109+
#endif
104110
@available(_uttypesAPI, *)
105111
extension Attachment where AttachableValue: AttachableWrapper, AttachableValue.Wrapped: _AttachableAsImage {
106112
/// The image format to use when encoding the represented image.

Sources/Testing/Attachments/Images/ImageAttachmentError.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
private import _TestingInternals
1212

1313
/// A type representing an error that can occur when attaching an image.
14+
#if SWT_NO_IMAGE_ATTACHMENTS
15+
@available(*, unavailable, message: "Image attachments are not available on this platform.")
16+
#endif
1417
package enum ImageAttachmentError: Error {
1518
#if SWT_TARGET_OS_APPLE
1619
/// The image could not be converted to an instance of `CGImage`.
@@ -39,6 +42,9 @@ package enum ImageAttachmentError: Error {
3942
#endif
4043
}
4144

45+
#if SWT_NO_IMAGE_ATTACHMENTS
46+
@available(*, unavailable, message: "Image attachments are not available on this platform.")
47+
#endif
4248
extension ImageAttachmentError: CustomStringConvertible {
4349
package var description: String {
4450
#if SWT_TARGET_OS_APPLE

Sources/Testing/Attachments/Images/_AttachableAsImage.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
//
1010

11+
#if SWT_TARGET_OS_APPLE
12+
// Image attachments on Apple platforms conform to AttachableAsCGImage.
13+
#elseif os(Windows)
14+
// Image attachments on Windows platforms conform to AttachableAsIWICBitmapSource.
15+
#elseif !SWT_NO_IMAGE_ATTACHMENTS
16+
#error("Platform-specific misconfiguration: support for image attachments requires a platform-specific implementation")
17+
#endif
18+
1119
/// A protocol describing images that can be converted to instances of
1220
/// [`Attachment`](https://developer.apple.com/documentation/testing/attachment).
1321
///
@@ -18,6 +26,9 @@
1826
/// A future Swift Evolution proposal will promote this protocol to API so
1927
/// that we don't need to underscore its name.
2028
/// }
29+
#if SWT_NO_IMAGE_ATTACHMENTS
30+
@available(*, unavailable, message: "Image attachments are not available on this platform.")
31+
#endif
2132
@available(_uttypesAPI, *)
2233
public protocol _AttachableAsImage: SendableMetatype {
2334
/// Make a copy of this instance to pass to an attachment.

Sources/Testing/Attachments/Images/_AttachableImageWrapper.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
/// @Comment {
2121
/// | Windows | [`HBITMAP`](https://learn.microsoft.com/en-us/windows/win32/gdi/bitmaps), [`HICON`](https://learn.microsoft.com/en-us/windows/win32/menurc/icons), [`IWICBitmapSource`](https://learn.microsoft.com/en-us/windows/win32/api/wincodec/nn-wincodec-iwicbitmapsource) (including its subclasses declared by Windows Imaging Component) |
2222
/// }
23+
#if SWT_NO_IMAGE_ATTACHMENTS
24+
@available(*, unavailable, message: "Image attachments are not available on this platform.")
25+
#endif
2326
@available(_uttypesAPI, *)
2427
public final class _AttachableImageWrapper<Image>: Sendable where Image: _AttachableAsImage {
2528
/// The underlying image.
@@ -38,6 +41,9 @@ public final class _AttachableImageWrapper<Image>: Sendable where Image: _Attach
3841
}
3942
}
4043

44+
#if SWT_NO_IMAGE_ATTACHMENTS
45+
@available(*, unavailable, message: "Image attachments are not available on this platform.")
46+
#endif
4147
@available(_uttypesAPI, *)
4248
extension _AttachableImageWrapper {
4349
public var wrappedValue: Image {

cmake/modules/shared/CompilerSettings.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
4242
add_compile_definitions("SWT_NO_DYNAMIC_LINKING")
4343
add_compile_definitions("SWT_NO_PIPES")
4444
endif()
45+
if (NOT (APPLE OR CMAKE_SYSTEM_NAME STREQUAL "Windows"))
46+
add_compile_definitions("SWT_NO_IMAGE_ATTACHMENTS")
47+
endif()
4548

4649
file(STRINGS "${SWT_SOURCE_ROOT_DIR}/VERSION.txt" SWT_TESTING_LIBRARY_VERSION LIMIT_COUNT 1)
4750
if(SWT_TESTING_LIBRARY_VERSION)

0 commit comments

Comments
 (0)