You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR introduces a new experimental trait, `.savingAttachments(if:)`,
that can be used to control whether a test's attachments are saved or
not.
XCTest has API around the
[`XCTAttachment.Lifetime`](https://developer.apple.com/documentation/xctest/xctattachment/lifetime-swift.enum)
enumeration that developers can use to control whether attachments are
saved to a test report in Xcode. This enumeration has two cases:
```objc
/*
* Attachment will be kept regardless of the outcome of the test.
*/
XCTAttachmentLifetimeKeepAlways = 0,
/*
* Attachment will only be kept when the test fails, and deleted otherwise.
*/
XCTAttachmentLifetimeDeleteOnSuccess = 1
```
I've opted to implement something a bit more granular. A developer can
specify `.savingAttachments(if: .testFails)` and `.savingAttachments(if:
.testPasses)` or can call some custom function of their own design like
`runningInCI` or `hasPlentyOfFloppyDiskSpace`. The default behaviour if
this trait is not used is to always save attachments, which is
equivalent to `XCTAttachmentLifetimeKeepAlways`.
`XCTAttachmentLifetimeDeleteOnSuccess` is, in effect, equivalent to
`.savingAttachments(if: .testFails)`, but I hope reads a bit more
clearly in context.
Here's a usage example:
```swift
@test(.savingAttachments(if: .testFails))
func `best test ever`() {
Attachment.record("...") // only saves to the test report or to disk if the
// next line is uncommented.
// Issue.record("sadness")
}
```
I've taken the opportunity to update existing documentation for
`Attachment` and `Attachable` to try to use more consistent language: a
test records an attachment and then the testing library saves it
(somewhere). I'm sure I've missed some spots, so please point them out
if you see them.
Resolves rdar://138921461.
### 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.
@@ -404,16 +416,16 @@ extension Attachment where AttachableValue: ~Copyable {
404
416
/// is derived from the value of the ``Attachment/preferredName`` property.
405
417
///
406
418
/// If you pass `--attachments-path` to `swift test`, the testing library
407
-
/// automatically uses this function to persist attachments to the directory
408
-
/// you specify.
419
+
/// automatically uses this function to save attachments to the directory you
420
+
/// specify.
409
421
///
410
422
/// This function does not get or set the value of the attachment's
411
423
/// ``fileSystemPath`` property. The caller is responsible for setting the
412
424
/// value of this property if needed.
413
425
///
414
-
/// This function is provided as a convenience to allow tools authors to write
415
-
/// attachments to persistent storage the same way that Swift Package Manager
416
-
/// does. You are not required to use this function.
426
+
/// This function is provided as a convenience to allow tools authors to save
427
+
/// attachments the same way that Swift Package Manager does. You are not
428
+
/// required to use this function.
417
429
@_spi(ForToolsIntegrationOnly)
418
430
public borrowing func write(toFileInDirectoryAtPath directoryPath:String)throws->String{
419
431
trywrite(
@@ -505,9 +517,9 @@ extension Configuration {
505
517
/// - Returns: Whether or not to continue handling the event.
506
518
///
507
519
/// This function is called automatically by ``handleEvent(_:in:)``. You do
508
-
/// not need to call it elsewhere. It automatically persists the attachment
520
+
/// not need to call it elsewhere. It automatically saves the attachment
509
521
/// associated with `event` and modifies `event` to include the path where the
510
-
/// attachment was stored.
522
+
/// attachment was saved.
511
523
func handleValueAttachedEvent(_ event:inoutEvent, in eventContext:borrowingEvent.Context)->Bool{
512
524
guardlet attachmentsPath else{
513
525
// If there is no path to which attachments should be written, there's
@@ -519,9 +531,9 @@ extension Configuration {
519
531
preconditionFailure("Passed the wrong kind of event to \(#function) (expected valueAttached, got \(event.kind)). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new")
520
532
}
521
533
if attachment.fileSystemPath !=nil{
522
-
// Somebody already persisted this attachment. This isn't necessarily a
523
-
// logic error in the testing library, but it probably means we shouldn't
524
-
// persist it again. Suppress the event.
534
+
// Somebody already saved this attachment. This isn't necessarily a logic
535
+
// error in the testing library, but it probably means we shouldn't save
0 commit comments