|
8 | 8 | // See https://swift.org/CONTRIBUTORS.txt for Swift project authors
|
9 | 9 | //
|
10 | 10 |
|
| 11 | +private import _TestingInternals |
| 12 | + |
| 13 | +#if canImport(Foundation) |
| 14 | +private import Foundation |
| 15 | +#endif |
| 16 | + |
| 17 | +/// The maximum size, in bytes, of an attachment that will be stored inline in |
| 18 | +/// an encoded attachment. |
| 19 | +private let _maximumInlineAttachmentByteCount: Int = { |
| 20 | + let pageSize: Int |
| 21 | +#if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) |
| 22 | + pageSize = Int(clamping: sysconf(_SC_PAGESIZE)) |
| 23 | +#elseif os(WASI) |
| 24 | + // sysconf(_SC_PAGESIZE) is a complex macro in wasi-libc. |
| 25 | + pageSize = Int(clamping: getpagesize()) |
| 26 | +#elseif os(Windows) |
| 27 | + var systemInfo = SYSTEM_INFO() |
| 28 | + GetSystemInfo(&systemInfo) |
| 29 | + pageSize = Int(clamping: systemInfo.dwPageSize) |
| 30 | +#else |
| 31 | +#warning("Platform-specific implementation missing: page size unknown (assuming 4KB)") |
| 32 | + pageSize = 4 * 1024 |
| 33 | +#endif |
| 34 | + |
| 35 | + return pageSize // i.e. we'll store up to a page-sized attachment |
| 36 | +}() |
| 37 | + |
11 | 38 | extension ABI {
|
12 | 39 | /// A type implementing the JSON encoding of ``Attachment`` for the ABI entry
|
13 | 40 | /// point and event stream output.
|
14 | 41 | ///
|
15 | 42 | /// This type is not part of the public interface of the testing library. It
|
16 | 43 | /// assists in converting values to JSON; clients that consume this JSON are
|
17 | 44 | /// expected to write their own decoders.
|
18 |
| - /// |
19 |
| - /// - Warning: Attachments are not yet part of the JSON schema. |
20 | 45 | struct EncodedAttachment<V>: Sendable where V: ABI.Version {
|
21 | 46 | /// The path where the attachment was written.
|
22 | 47 | var path: String?
|
23 | 48 |
|
| 49 | + /// The preferred name of the attachment. |
| 50 | + /// |
| 51 | + /// - Warning: Attachments' preferred names are not yet part of the JSON |
| 52 | + /// schema. |
| 53 | + var _preferredName: String? |
| 54 | + |
| 55 | + /// The raw content of the attachment, if available. |
| 56 | + /// |
| 57 | + /// If the value of this property is `nil`, the attachment can instead be |
| 58 | + /// read from ``path``. |
| 59 | + /// |
| 60 | + /// - Warning: Inline attachment content is not yet part of the JSON schema. |
| 61 | + var _bytes: Bytes? |
| 62 | + |
24 | 63 | init(encoding attachment: borrowing Attachment<AnyAttachable>, in eventContext: borrowing Event.Context) {
|
25 | 64 | path = attachment.fileSystemPath
|
| 65 | + _preferredName = attachment.preferredName |
| 66 | + |
| 67 | + if let estimatedByteCount = attachment.attachableValue.estimatedAttachmentByteCount, |
| 68 | + estimatedByteCount <= _maximumInlineAttachmentByteCount { |
| 69 | + _bytes = try? attachment.withUnsafeBytes { bytes in |
| 70 | + if bytes.count > 0 && bytes.count < _maximumInlineAttachmentByteCount { |
| 71 | + return Bytes(rawValue: [UInt8](bytes)) |
| 72 | + } |
| 73 | + return nil |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// A structure representing the bytes of an attachment. |
| 79 | + struct Bytes: Sendable, RawRepresentable { |
| 80 | + var rawValue: [UInt8] |
26 | 81 | }
|
27 | 82 | }
|
28 | 83 | }
|
29 | 84 |
|
30 | 85 | // MARK: - Codable
|
31 | 86 |
|
32 | 87 | extension ABI.EncodedAttachment: Codable {}
|
| 88 | + |
| 89 | +extension ABI.EncodedAttachment.Bytes: Codable { |
| 90 | + func encode(to encoder: any Encoder) throws { |
| 91 | +#if canImport(Foundation) |
| 92 | + // If possible, encode this structure as Base64 data. |
| 93 | + try rawValue.withUnsafeBytes { rawValue in |
| 94 | + let data = Data(bytesNoCopy: .init(mutating: rawValue.baseAddress!), count: rawValue.count, deallocator: .none) |
| 95 | + var container = encoder.singleValueContainer() |
| 96 | + try container.encode(data) |
| 97 | + } |
| 98 | +#else |
| 99 | + // Otherwise, it's an array of integers. |
| 100 | + var container = encoder.singleValueContainer() |
| 101 | + try container.encode(rawValue) |
| 102 | +#endif |
| 103 | + } |
| 104 | + |
| 105 | + init(from decoder: any Decoder) throws { |
| 106 | + let container = try decoder.singleValueContainer() |
| 107 | + |
| 108 | +#if canImport(Foundation) |
| 109 | + // If possible, decode a whole Foundation Data object. |
| 110 | + if let data = try? container.decode(Data.self) { |
| 111 | + self.init(rawValue: [UInt8](data)) |
| 112 | + return |
| 113 | + } |
| 114 | +#endif |
| 115 | + |
| 116 | + // Fall back to trying to decode an array of integers. |
| 117 | + let bytes = try container.decode([UInt8].self) |
| 118 | + self.init(rawValue: bytes) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// MARK: - Attachable |
| 123 | + |
| 124 | +extension ABI.EncodedAttachment: Attachable { |
| 125 | + var estimatedAttachmentByteCount: Int? { |
| 126 | + _bytes?.rawValue.count |
| 127 | + } |
| 128 | + |
| 129 | + fileprivate struct BytesUnavailableError: Error {} |
| 130 | + |
| 131 | + borrowing func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { |
| 132 | + if let bytes = _bytes?.rawValue { |
| 133 | + return try bytes.withUnsafeBytes(body) |
| 134 | + } |
| 135 | + |
| 136 | + guard let path else { |
| 137 | + throw BytesUnavailableError() |
| 138 | + } |
| 139 | + let fileHandle = try FileHandle(forReadingAtPath: path) |
| 140 | + // TODO: map the attachment back into memory |
| 141 | + let bytes = try fileHandle.readToEnd() |
| 142 | + return try bytes.withUnsafeBytes(body) |
| 143 | + } |
| 144 | + |
| 145 | + borrowing func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String { |
| 146 | + _preferredName ?? suggestedName |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +extension ABI.EncodedAttachment.BytesUnavailableError: CustomStringConvertible { |
| 151 | + var description: String { |
| 152 | + "The attachment's content could not be deserialized." |
| 153 | + } |
| 154 | +} |
0 commit comments