@@ -84,9 +84,10 @@ public struct Attachment<AttachableValue>: ~Copyable where AttachableValue: Atta
84
84
sourceLocation : SourceLocation = #_sourceLocation
85
85
)
86
86
87
- /// Attach this instance to the current test.
87
+ /// Attach an attachment to the current test.
88
88
///
89
89
/// - Parameters:
90
+ /// - attachment: The attachment to attach.
90
91
/// - sourceLocation: The source location of the call to this function.
91
92
///
92
93
/// When attaching a value of a type that does not conform to both
@@ -98,7 +99,27 @@ public struct Attachment<AttachableValue>: ~Copyable where AttachableValue: Atta
98
99
/// disk.
99
100
///
100
101
/// An attachment can only be attached once.
101
- public consuming func attach (sourceLocation : SourceLocation = #_sourceLocation )
102
+ public static func attach (_ attachment : consuming Self , sourceLocation : SourceLocation = #_sourceLocation )
103
+
104
+ /// Attach a value to the current test.
105
+ ///
106
+ /// - Parameters:
107
+ /// - attachableValue: The value to attach.
108
+ /// - sourceLocation: The source location of the call to this function.
109
+ ///
110
+ /// When attaching a value of a type that does not conform to both
111
+ /// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
112
+ /// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
113
+ /// the testing library encodes it as data immediately. If the value cannot be
114
+ /// encoded and an error is thrown, that error is recorded as an issue in the
115
+ /// current test and the attachment is not written to the test report or to
116
+ /// disk.
117
+ ///
118
+ /// This function creates a new instance of ``Attachment`` and immediately
119
+ /// attaches it to the current test.
120
+ ///
121
+ /// An attachment can only be attached once.
122
+ public static func attach (_ attachment : consuming AttachableValue, sourceLocation : SourceLocation = #_sourceLocation )
102
123
103
124
/// Call a function and pass a buffer representing the value of this
104
125
/// instance's ``attachableValue-2tnj5`` property to it.
@@ -134,7 +155,7 @@ conform:
134
155
///
135
156
/// To attach an attachable value to a test report or test run output, use it to
136
157
/// initialize a new instance of ``Attachment``, then call
137
- /// ``Attachment/attach(sourceLocation:)``. An attachment can only be attached
158
+ /// ``Attachment/attach(_: sourceLocation:)``. An attachment can only be attached
138
159
/// once.
139
160
///
140
161
/// The testing library provides default conformances to this protocol for a
@@ -226,7 +247,7 @@ that refines `Attachable`:
226
247
///
227
248
/// To attach an attachable value to a test report or test run output, use it to
228
249
/// initialize a new instance of ``Attachment``, then call
229
- /// ``Attachment/attach(sourceLocation:)``. An attachment can only be attached
250
+ /// ``Attachment/attach(_: sourceLocation:)``. An attachment can only be attached
230
251
/// once.
231
252
///
232
253
/// A type can conform to this protocol if it represents another type that
@@ -297,11 +318,12 @@ Package Manager:
297
318
--attachments-path Path where attachments should be saved.
298
319
```
299
320
300
- If specified, an attachment will be written to that path when its ` attach() `
301
- method is called. If not specified, attachments are not saved to disk. Tools
302
- that indirectly use Swift Testing through ` swift test ` can specify a path (e.g.
303
- to a directory created inside the system's temporary directory), then move or
304
- delete the created files as needed.
321
+ If specified, an attachment will be written to that path when the attachment is
322
+ passed to one of the ` Attachment.attach(_:sourceLocation:) ` methods. If not
323
+ specified, attachments are not saved to disk. Tools that indirectly use Swift
324
+ Testing through ` swift test ` can specify a path (e.g. to a directory created
325
+ inside the system's temporary directory), then move or delete the created files
326
+ as needed.
305
327
306
328
The JSON event stream ABI will be amended correspondingly:
307
329
@@ -384,6 +406,12 @@ version too.
384
406
[swiftlang/swift-testing#824](https://github.com/swiftlang/swift-testing/pull/824)
385
407
includes an experimental implementation of this feature.
386
408
409
+ - Attaching attachments to issues or to activities: XCTest supports attachments
410
+ on `XCTIssue`; Swift Testing does not currently allow developers to create an
411
+ issue without immediately recording it, so there is no opportunity to attach
412
+ anything to one. XCTest also supports the concept of activities as subsections
413
+ of tests; they remain a future direction for Swift Testing.
414
+
387
415
## Alternatives considered
388
416
389
417
- Doing nothing: there's sufficient demand for this feature that we know we want
@@ -423,6 +451,11 @@ version too.
423
451
Instead, `Attachable` includes the function `preferredName(for:basedOn:)` that
424
452
allows an implementation (such as that of `Encodable & Attachable`) to add a
425
453
path extension to the filename specified by the test author if needed.
454
+
455
+ - Making the `Attachment.attach(_:sourceLocation:)` methods a single instance
456
+ method of `Attachment` named `attach()`: this was in the initial pitch but the
457
+ community discussed several more ergonomic options and we chose
458
+ `Attachment.attach(_:sourceLocation:)` instead.
426
459
427
460
## Acknowledgments
428
461
0 commit comments