1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
private import _TestingInternals
/// A type describing values that can be attached to the output of a test run
/// and inspected later by the user.
///
/// Attachments are included in test reports in Xcode or written to disk when
/// tests are run at the command line. To create an attachment, you need a value
/// of some type that conforms to ``Attachable``. Initialize an instance of
/// ``Attachment`` with that value and, optionally, a preferred filename to use
/// when writing to disk.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public struct Attachment<AttachableValue>: ~Copyable where AttachableValue: Attachable & ~Copyable {
/// Storage for ``attachableValue-7dyjv``.
fileprivate var _attachableValue: AttachableValue
/// The path to which the this attachment was written, if any.
///
/// If a developer sets the ``Configuration/attachmentsPath`` property of the
/// current configuration before running tests, or if a developer passes
/// `--attachments-path` on the command line, then attachments will be
/// automatically written to disk when they are attached and the value of this
/// property will describe the path where they were written.
///
/// If no destination path is set, or if an error occurred while writing this
/// attachment to disk, the value of this property is `nil`.
@_spi(ForToolsIntegrationOnly)
public var fileSystemPath: String?
/// The default preferred name to use if the developer does not supply one.
package static var defaultPreferredName: String {
"untitled"
}
/// Storage for ``preferredName``.
fileprivate var _preferredName: String?
/// A filename to use when writing this attachment to a test report or to a
/// file on disk.
///
/// The value of this property is used as a hint to the testing library. The
/// testing library may substitute a different filename as needed. If the
/// value of this property has not been explicitly set, the testing library
/// will attempt to generate its own value.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public var preferredName: String {
let suggestedName = if let _preferredName, !_preferredName.isEmpty {
_preferredName
} else {
Self.defaultPreferredName
}
return attachableValue.preferredName(for: self, basedOn: suggestedName)
}
/// The source location of this instance.
///
/// This property is not part of the public interface of the testing library.
/// It is initially set when the attachment is created and is updated later
/// when the attachment is attached to something.
///
/// The value of this property is used when recording issues associated with
/// the attachment.
var sourceLocation: SourceLocation
}
extension Attachment: Copyable where AttachableValue: Copyable {}
extension Attachment: Sendable where AttachableValue: Sendable {}
// MARK: - Initializing an attachment
#if !SWT_NO_LAZY_ATTACHMENTS
extension Attachment where AttachableValue: ~Copyable {
/// Initialize an instance of this type that encloses the given attachable
/// value.
///
/// - Parameters:
/// - attachableValue: The value that will be attached to the output of the
/// test run.
/// - preferredName: The preferred name of the attachment when writing it to
/// a test report or to disk. If `nil`, the testing library attempts to
/// derive a reasonable filename for the attached value.
/// - sourceLocation: The source location of the call to this initializer.
/// This value is used when recording issues associated with the
/// attachment.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public init(_ attachableValue: consuming AttachableValue, named preferredName: String? = nil, sourceLocation: SourceLocation = #_sourceLocation) {
self._attachableValue = attachableValue
self._preferredName = preferredName
self.sourceLocation = sourceLocation
}
}
@_spi(ForToolsIntegrationOnly)
extension Attachment where AttachableValue == AnyAttachable {
/// Create a type-erased attachment from an instance of ``Attachment``.
///
/// - Parameters:
/// - attachment: The attachment to type-erase.
fileprivate init(_ attachment: Attachment<some Attachable & Sendable & Copyable>) {
self.init(
_attachableValue: AnyAttachable(wrappedValue: attachment.attachableValue),
fileSystemPath: attachment.fileSystemPath,
_preferredName: attachment._preferredName,
sourceLocation: attachment.sourceLocation
)
}
}
#endif
/// A type-erased wrapper type that represents any attachable value.
///
/// This type is not generally visible to developers. It is used when posting
/// events of kind ``Event/Kind/valueAttached(_:)``. Test tools authors who use
/// `@_spi(ForToolsIntegrationOnly)` will see instances of this type when
/// handling those events.
///
/// @Comment {
/// Swift's type system requires that this type be at least as visible as
/// `Event.Kind.valueAttached(_:)`, otherwise it would be declared private.
/// }
@_spi(ForToolsIntegrationOnly)
public struct AnyAttachable: AttachableWrapper, Copyable, Sendable {
#if !SWT_NO_LAZY_ATTACHMENTS
public typealias Wrapped = any Attachable & Sendable /* & Copyable rdar://137614425 */
#else
public typealias Wrapped = [UInt8]
#endif
public var wrappedValue: Wrapped
init(wrappedValue: Wrapped) {
self.wrappedValue = wrappedValue
}
public var estimatedAttachmentByteCount: Int? {
wrappedValue.estimatedAttachmentByteCount
}
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
func open<T>(_ wrappedValue: T, for attachment: borrowing Attachment<Self>) throws -> R where T: Attachable & Sendable & Copyable {
let temporaryAttachment = Attachment<T>(
_attachableValue: wrappedValue,
fileSystemPath: attachment.fileSystemPath,
_preferredName: attachment._preferredName,
sourceLocation: attachment.sourceLocation
)
return try temporaryAttachment.withUnsafeBytes(body)
}
return try open(wrappedValue, for: attachment)
}
public borrowing func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String {
func open<T>(_ wrappedValue: T, for attachment: borrowing Attachment<Self>) -> String where T: Attachable & Sendable & Copyable {
let temporaryAttachment = Attachment<T>(
_attachableValue: wrappedValue,
fileSystemPath: attachment.fileSystemPath,
_preferredName: attachment._preferredName,
sourceLocation: attachment.sourceLocation
)
return temporaryAttachment.preferredName
}
return open(wrappedValue, for: attachment)
}
}
// MARK: - Describing an attachment
extension Attachment where AttachableValue: ~Copyable {
@_documentation(visibility: private)
public var description: String {
let typeInfo = TypeInfo(describing: AttachableValue.self)
return #""\#(preferredName)": instance of '\#(typeInfo.unqualifiedName)'"#
}
}
extension Attachment: CustomStringConvertible {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public var description: String {
#""\#(preferredName)": \#(String(describingForTest: attachableValue))"#
}
}
// MARK: - Getting an attachable value from an attachment
extension Attachment where AttachableValue: ~Copyable {
/// The value of this attachment.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
@_disfavoredOverload public var attachableValue: AttachableValue {
_read {
yield _attachableValue
}
}
}
extension Attachment where AttachableValue: AttachableWrapper & ~Copyable {
/// The value of this attachment.
///
/// When the attachable value's type conforms to ``AttachableWrapper``, the
/// value of this property equals the wrapper's underlying attachable value.
/// To access the attachable value as an instance of `T` (where `T` conforms
/// to ``AttachableWrapper``), specify the type explicitly:
///
/// ```swift
/// let attachableValue = attachment.attachableValue as T
/// ```
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public var attachableValue: AttachableValue.Wrapped {
_read {
yield attachableValue.wrappedValue
}
}
}
// MARK: - Attaching an attachment to a test (etc.)
#if !SWT_NO_LAZY_ATTACHMENTS
extension Attachment where AttachableValue: Sendable & Copyable {
/// Attach an attachment to the current test.
///
/// - Parameters:
/// - attachment: The attachment to attach.
/// - sourceLocation: The source location of the call to this function.
///
/// When attaching a value of a type that does not conform to both
/// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
/// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
/// the testing library encodes it as data immediately. If the value cannot be
/// encoded and an error is thrown, that error is recorded as an issue in the
/// current test and the attachment is not written to the test report or to
/// disk.
///
/// An attachment can only be attached once.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
@_documentation(visibility: private)
public static func record(_ attachment: consuming Self, sourceLocation: SourceLocation = #_sourceLocation) {
var attachmentCopy = Attachment<AnyAttachable>(attachment)
attachmentCopy.sourceLocation = sourceLocation
Event.post(.valueAttached(attachmentCopy))
}
/// Attach a value to the current test.
///
/// - Parameters:
/// - attachableValue: The value to attach.
/// - preferredName: The preferred name of the attachment when writing it to
/// a test report or to disk. If `nil`, the testing library attempts to
/// derive a reasonable filename for the attached value.
/// - sourceLocation: The source location of the call to this function.
///
/// When attaching a value of a type that does not conform to both
/// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
/// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
/// the testing library encodes it as data immediately. If the value cannot be
/// encoded and an error is thrown, that error is recorded as an issue in the
/// current test and the attachment is not written to the test report or to
/// disk.
///
/// This function creates a new instance of ``Attachment`` and immediately
/// attaches it to the current test.
///
/// An attachment can only be attached once.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
@_documentation(visibility: private)
public static func record(_ attachableValue: consuming AttachableValue, named preferredName: String? = nil, sourceLocation: SourceLocation = #_sourceLocation) {
record(Self(attachableValue, named: preferredName, sourceLocation: sourceLocation), sourceLocation: sourceLocation)
}
}
#endif
extension Attachment where AttachableValue: ~Copyable {
/// Attach an attachment to the current test.
///
/// - Parameters:
/// - attachment: The attachment to attach.
/// - sourceLocation: The source location of the call to this function.
///
/// When attaching a value of a type that does not conform to both
/// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
/// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
/// the testing library encodes it as data immediately. If the value cannot be
/// encoded and an error is thrown, that error is recorded as an issue in the
/// current test and the attachment is not written to the test report or to
/// disk.
///
/// An attachment can only be attached once.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public static func record(_ attachment: consuming Self, sourceLocation: SourceLocation = #_sourceLocation) {
do {
let attachmentCopy = try attachment.withUnsafeBytes { buffer in
let attachableWrapper = AnyAttachable(wrappedValue: Array(buffer))
return Attachment<AnyAttachable>(
_attachableValue: attachableWrapper,
fileSystemPath: attachment.fileSystemPath,
_preferredName: attachment.preferredName, // invokes preferredName(for:basedOn:)
sourceLocation: sourceLocation
)
}
Event.post(.valueAttached(attachmentCopy))
} catch {
let sourceContext = SourceContext(backtrace: .current(), sourceLocation: sourceLocation)
Issue(kind: .valueAttachmentFailed(error), comments: [], sourceContext: sourceContext).record()
}
}
/// Attach a value to the current test.
///
/// - Parameters:
/// - attachableValue: The value to attach.
/// - preferredName: The preferred name of the attachment when writing it to
/// a test report or to disk. If `nil`, the testing library attempts to
/// derive a reasonable filename for the attached value.
/// - sourceLocation: The source location of the call to this function.
///
/// When attaching a value of a type that does not conform to both
/// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
/// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
/// the testing library encodes it as data immediately. If the value cannot be
/// encoded and an error is thrown, that error is recorded as an issue in the
/// current test and the attachment is not written to the test report or to
/// disk.
///
/// This function creates a new instance of ``Attachment`` and immediately
/// attaches it to the current test.
///
/// An attachment can only be attached once.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public static func record(_ attachableValue: consuming AttachableValue, named preferredName: String? = nil, sourceLocation: SourceLocation = #_sourceLocation) {
record(Self(attachableValue, named: preferredName, sourceLocation: sourceLocation), sourceLocation: sourceLocation)
}
}
// MARK: - Getting the serialized form of an attachable value (generically)
extension Attachment where AttachableValue: ~Copyable {
/// Call a function and pass a buffer representing the value of this
/// instance's ``attachableValue-2tnj5`` property to it.
///
/// - Parameters:
/// - body: A function to call. A temporary buffer containing a data
/// representation of this instance is passed to it.
///
/// - Returns: Whatever is returned by `body`.
///
/// - Throws: Whatever is thrown by `body`, or any error that prevented the
/// creation of the buffer.
///
/// The testing library uses this function when writing an attachment to a
/// test report or to a file on disk. This function calls the
/// ``Attachable/withUnsafeBytes(for:_:)`` function on this attachment's
/// ``attachableValue-2tnj5`` property.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
@inlinable public borrowing func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
try attachableValue.withUnsafeBytes(for: self, body)
}
}
#if !SWT_NO_FILE_IO
// MARK: - Writing
extension Attachment where AttachableValue: ~Copyable {
/// Write the attachment's contents to a file in the specified directory.
///
/// - Parameters:
/// - directoryPath: The directory that should contain the attachment when
/// written.
///
/// - Throws: Any error preventing writing the attachment.
///
/// - Returns: The path to the file that was written.
///
/// The attachment is written to a file _within_ `directoryPath`, whose name
/// is derived from the value of the ``Attachment/preferredName`` property.
///
/// If you pass `--attachments-path` to `swift test`, the testing library
/// automatically uses this function to persist attachments to the directory
/// you specify.
///
/// This function does not get or set the value of the attachment's
/// ``fileSystemPath`` property. The caller is responsible for setting the
/// value of this property if needed.
///
/// This function is provided as a convenience to allow tools authors to write
/// attachments to persistent storage the same way that Swift Package Manager
/// does. You are not required to use this function.
@_spi(ForToolsIntegrationOnly)
public borrowing func write(toFileInDirectoryAtPath directoryPath: String) throws -> String {
try write(
toFileInDirectoryAtPath: directoryPath,
appending: String(UInt64.random(in: 0 ..< .max), radix: 36)
)
}
/// Write the attachment's contents to a file in the specified directory.
///
/// - Parameters:
/// - directoryPath: The directory to which the attachment should be
/// written.
/// - usingPreferredName: Whether or not to use the attachment's preferred
/// name. If `false`, ``defaultPreferredName`` is used instead.
/// - suffix: A suffix to attach to the file name (instead of randomly
/// generating one.) This value may be evaluated multiple times.
///
/// - Throws: Any error preventing writing the attachment.
///
/// - Returns: The path to the file that was written.
///
/// The attachment is written to a file _within_ `directoryPath`, whose name
/// is derived from the value of the ``Attachment/preferredName`` property and
/// the value of `suffix`.
///
/// If the argument `suffix` always produces the same string, the result of
/// this function is undefined.
borrowing func write(toFileInDirectoryAtPath directoryPath: String, usingPreferredName: Bool = true, appending suffix: @autoclosure () -> String) throws -> String {
let result: String
let preferredName = usingPreferredName ? preferredName : Self.defaultPreferredName
var file: FileHandle?
do {
// First, attempt to create the file with the exact preferred name. If a
// file exists at this path (note "x" in the mode string), an error will
// be thrown and we'll try again by adding a suffix.
let preferredPath = appendPathComponent(preferredName, to: directoryPath)
file = try FileHandle(atPath: preferredPath, mode: "wxeb")
result = preferredPath
} catch {
// Split the extension(s) off the preferred name. The first component in
// the resulting array is our base name.
var preferredNameComponents = preferredName.split(separator: ".")
let firstPreferredNameComponent = preferredNameComponents[0]
while true {
preferredNameComponents[0] = "\(firstPreferredNameComponent)-\(suffix())"
let preferredName = preferredNameComponents.joined(separator: ".")
let preferredPath = appendPathComponent(preferredName, to: directoryPath)
// Propagate any error *except* EEXIST, which would indicate that the
// name was already in use (so we should try again with a new suffix.)
do {
file = try FileHandle(atPath: preferredPath, mode: "wxeb")
result = preferredPath
break
} catch let error as CError where error.rawValue == swt_EEXIST() {
// Try again with a new suffix.
continue
} catch where usingPreferredName {
// Try again with the default name before giving up.
return try write(toFileInDirectoryAtPath: directoryPath, usingPreferredName: false, appending: suffix())
}
}
}
// There should be no code path that leads to this call where the attachable
// value is nil.
try withUnsafeBytes { buffer in
try file!.write(buffer)
}
return result
}
}
extension Configuration {
/// Handle the given "value attached" event.
///
/// - Parameters:
/// - event: The event to handle. This event must be of kind
/// ``Event/Kind/valueAttached(_:)``. If the associated attachment's
/// ``Attachment/fileSystemPath`` property is not `nil`, this function
/// does nothing.
/// - context: The context associated with the event.
///
/// - Returns: Whether or not to continue handling the event.
///
/// This function is called automatically by ``handleEvent(_:in:)``. You do
/// not need to call it elsewhere. It automatically persists the attachment
/// associated with `event` and modifies `event` to include the path where the
/// attachment was stored.
func handleValueAttachedEvent(_ event: inout Event, in eventContext: borrowing Event.Context) -> Bool {
guard let attachmentsPath else {
// If there is no path to which attachments should be written, there's
// nothing to do here. The event handler may still want to handle it.
return true
}
guard case let .valueAttached(attachment) = event.kind else {
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")
}
if attachment.fileSystemPath != nil {
// Somebody already persisted this attachment. This isn't necessarily a
// logic error in the testing library, but it probably means we shouldn't
// persist it again. Suppress the event.
return false
}
do {
// Write the attachment.
var attachment = attachment
attachment.fileSystemPath = try attachment.write(toFileInDirectoryAtPath: attachmentsPath)
// Update the event before returning and continuing to handle it.
event.kind = .valueAttached(attachment)
return true
} catch {
// Record the error as an issue and suppress the event.
let sourceContext = SourceContext(backtrace: .current(), sourceLocation: attachment.sourceLocation)
Issue(kind: .valueAttachmentFailed(error), comments: [], sourceContext: sourceContext).record(configuration: self)
return false
}
}
}
#endif
|