Skip to content

Commit d039ea0

Browse files
committed
Don't introduce a new public type for file attachments
1 parent 950fa6e commit d039ea0

File tree

1 file changed

+7
-36
lines changed

1 file changed

+7
-36
lines changed

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

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension UTType {
4242
#endif
4343

4444
@_spi(Experimental)
45-
extension Test.Attachment where AttachableValue == FileAttachment {
45+
extension Test.Attachment where AttachableValue == Data {
4646
/// Initialize an instance of this type with the contents of the given URL.
4747
///
4848
/// - Parameters:
@@ -87,37 +87,17 @@ extension Test.Attachment where AttachableValue == FileAttachment {
8787
return (preferredName as NSString).appendingPathExtension("tgz") ?? preferredName
8888
}()
8989

90-
try await self.init(FileAttachment(compressedContentsOfDirectoryAt: url), named: preferredName)
90+
try await self.init(Data(compressedContentsOfDirectoryAt: url), named: preferredName)
9191
} else {
9292
// Load the file.
93-
try self.init(FileAttachment(contentsOfFileAt: url), named: preferredName)
93+
try self.init(Data(contentsOf: url, options: [.mappedIfSafe]), named: preferredName)
9494
}
9595
}
9696
}
9797

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
12099

100+
extension Data {
121101
/// Initialize an instance of this type by compressing the contents of a
122102
/// directory.
123103
///
@@ -143,7 +123,7 @@ public struct FileAttachment: Test.Attachable, Sendable {
143123
let sourcePath = directoryURL.fileSystemPath
144124
let destinationPath = temporaryURL.fileSystemPath
145125
defer {
146-
remove(destinationPath)
126+
try? FileManager().removeItem(at: temporaryURL)
147127
}
148128

149129
try await withCheckedThrowingContinuation { continuation in
@@ -168,20 +148,11 @@ public struct FileAttachment: Test.Attachable, Sendable {
168148
}
169149
}
170150

171-
url = directoryURL
172-
_data = try Data(contentsOf: temporaryURL, options: [.mappedIfSafe])
151+
try self.init(contentsOf: temporaryURL, options: [.mappedIfSafe])
173152
#else
174153
throw CocoaError(.featureUnsupported, userInfo: [NSLocalizedDescriptionKey: "This platform does not support attaching directories to tests."])
175154
#endif
176155
}
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-
}
185156
}
186157
#endif
187158
#endif

0 commit comments

Comments
 (0)