Skip to content

Commit 1b82a55

Browse files
committed
Update attach() function after discussion on the forums and with Stuart
1 parent 1b3bf68 commit 1b82a55

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

proposals/testing/NNNN-attachments.md

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ public struct Attachment<AttachableValue>: ~Copyable where AttachableValue: Atta
8484
sourceLocation: SourceLocation = #_sourceLocation
8585
)
8686

87-
/// Attach this instance to the current test.
87+
/// Attach an attachment to the current test.
8888
///
8989
/// - Parameters:
90+
/// - attachment: The attachment to attach.
9091
/// - sourceLocation: The source location of the call to this function.
9192
///
9293
/// 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
9899
/// disk.
99100
///
100101
/// 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)
102123

103124
/// Call a function and pass a buffer representing the value of this
104125
/// instance's ``attachableValue-2tnj5`` property to it.
@@ -134,7 +155,7 @@ conform:
134155
///
135156
/// To attach an attachable value to a test report or test run output, use it to
136157
/// 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
138159
/// once.
139160
///
140161
/// The testing library provides default conformances to this protocol for a
@@ -226,7 +247,7 @@ that refines `Attachable`:
226247
///
227248
/// To attach an attachable value to a test report or test run output, use it to
228249
/// 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
230251
/// once.
231252
///
232253
/// A type can conform to this protocol if it represents another type that
@@ -297,11 +318,12 @@ Package Manager:
297318
--attachments-path Path where attachments should be saved.
298319
```
299320

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.
305327

306328
The JSON event stream ABI will be amended correspondingly:
307329

@@ -384,6 +406,12 @@ version too.
384406
[swiftlang/swift-testing#824](https://github.com/swiftlang/swift-testing/pull/824)
385407
includes an experimental implementation of this feature.
386408
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+
387415
## Alternatives considered
388416
389417
- Doing nothing: there's sufficient demand for this feature that we know we want
@@ -423,6 +451,11 @@ version too.
423451
Instead, `Attachable` includes the function `preferredName(for:basedOn:)` that
424452
allows an implementation (such as that of `Encodable & Attachable`) to add a
425453
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.
426459
427460
## Acknowledgments
428461

0 commit comments

Comments
 (0)