@@ -42,7 +42,7 @@ extension UTType {
42
42
#endif
43
43
44
44
@_spi ( Experimental)
45
- extension Test . Attachment where AttachableValue == FileAttachment {
45
+ extension Test . Attachment where AttachableValue == Data {
46
46
/// Initialize an instance of this type with the contents of the given URL.
47
47
///
48
48
/// - Parameters:
@@ -87,37 +87,17 @@ extension Test.Attachment where AttachableValue == FileAttachment {
87
87
return ( preferredName as NSString ) . appendingPathExtension ( " tgz " ) ?? preferredName
88
88
} ( )
89
89
90
- try await self . init ( FileAttachment ( compressedContentsOfDirectoryAt: url) , named: preferredName)
90
+ try await self . init ( Data ( compressedContentsOfDirectoryAt: url) , named: preferredName)
91
91
} else {
92
92
// Load the file.
93
- try self . init ( FileAttachment ( contentsOfFileAt : url) , named: preferredName)
93
+ try self . init ( Data ( contentsOf : url, options : [ . mappedIfSafe ] ) , named: preferredName)
94
94
}
95
95
}
96
96
}
97
97
98
- // MARK: - Attaching files and directories
99
-
100
- /// A type representing a file system object that has been added to an
101
- /// attachment.
102
- @_spi ( Experimental)
103
- public struct FileAttachment : Test . Attachable , Sendable {
104
- /// The file URL where the represented file system object is located.
105
- public private( set) var url : URL
106
-
107
- /// The contents of the file or directory at `url`.
108
- private var _data : Data
109
-
110
- /// Initialize an instance of this type by reading the contents of a file.
111
- ///
112
- /// - Parameters:
113
- /// - fileURL: A URL referring to the file to attach.
114
- ///
115
- /// - Throws: Any error encountered trying to read the file at `fileURL`.
116
- init ( contentsOfFileAt fileURL: URL ) throws {
117
- url = fileURL
118
- _data = try Data ( contentsOf: url, options: [ . mappedIfSafe] )
119
- }
98
+ // MARK: - Attaching directories
120
99
100
+ extension Data {
121
101
/// Initialize an instance of this type by compressing the contents of a
122
102
/// directory.
123
103
///
@@ -143,7 +123,7 @@ public struct FileAttachment: Test.Attachable, Sendable {
143
123
let sourcePath = directoryURL. fileSystemPath
144
124
let destinationPath = temporaryURL. fileSystemPath
145
125
defer {
146
- remove ( destinationPath )
126
+ try ? FileManager ( ) . removeItem ( at : temporaryURL )
147
127
}
148
128
149
129
try await withCheckedThrowingContinuation { continuation in
@@ -168,20 +148,11 @@ public struct FileAttachment: Test.Attachable, Sendable {
168
148
}
169
149
}
170
150
171
- url = directoryURL
172
- _data = try Data ( contentsOf: temporaryURL, options: [ . mappedIfSafe] )
151
+ try self . init ( contentsOf: temporaryURL, options: [ . mappedIfSafe] )
173
152
#else
174
153
throw CocoaError ( . featureUnsupported, userInfo: [ NSLocalizedDescriptionKey: " This platform does not support attaching directories to tests. " ] )
175
154
#endif
176
155
}
177
-
178
- public var estimatedAttachmentByteCount : Int ? {
179
- _data. count
180
- }
181
-
182
- public func withUnsafeBufferPointer< R> ( for attachment: borrowing Testing . Test . Attachment < FileAttachment > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
183
- try _data. withUnsafeBytes ( body)
184
- }
185
156
}
186
157
#endif
187
158
#endif
0 commit comments