Skip to content

Commit 6057594

Browse files
grynspanjgmcnutt
andauthored
Add prose documentation for file/directory attachments. (#1440)
Document how to attach a file or directory to a test. ### 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. --------- Co-authored-by: jgmcnutt <[email protected]>
1 parent 1d3961a commit 6057594

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Sources/Overlays/_Testing_Foundation/Attachments/Attachment+URL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension Attachment where AttachableValue == _AttachableURLWrapper {
4747
///
4848
/// When you call this initializer and pass it the URL of a file, it reads or
4949
/// maps the contents of that file into memory. When you call this initializer
50-
/// and pass it the URL of a directory, it creates a temporary zip file of the
50+
/// and pass it the URL of a directory, it creates a temporary ZIP file of the
5151
/// directory before reading or mapping it into memory. These operations may
5252
/// take some time, so this initializer suspends the calling task until they
5353
/// are complete.

Sources/Testing/Testing.docc/Attachments.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,29 @@ extension SalesReport: Encodable, Attachable {}
7676
your test target imports the [Foundation](https://developer.apple.com/documentation/foundation)
7777
module.
7878

79+
### Attach files and directories
80+
81+
If you have a file you want to save as an attachment, you can attach it using
82+
its file URL. The testing library needs to read or map the file before attaching it
83+
to your test, and those operations can fail, so you need to explicitly create an
84+
instance of ``Attachment`` before you record it.
85+
86+
```swift
87+
import Foundation
88+
89+
@Test func `sales report adds up`() async throws {
90+
let salesReport = await generateSalesReport()
91+
try salesReport.validate()
92+
let salesReportURL = try salesReport.save()
93+
let attachment = try await Attachment(contentsOf: salesReportURL)
94+
Attachment.record(attachment)
95+
}
96+
```
97+
98+
You can also attach a directory to a test using its file URL. When you attach a
99+
directory to a test, the testing library creates a ZIP file containing the
100+
directory's contents, then attaches that ZIP file in place of the directory.
101+
79102
### Attach images
80103

81104
You can attach instances of the following system-provided image types to a test:

0 commit comments

Comments
 (0)