-
Notifications
You must be signed in to change notification settings - Fork 108
Attachments! #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Attachments! #770
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3078640
Attachments!
grynspan 745d665
Fix a filesystem-sensitive test
grynspan e913c94
Add string conformances to Attachable; write attachments to system/us…
grynspan 92ff89f
Log attachment path at default verbosity
grynspan 4af729e
Don't set attachments path if no file I/O
grynspan f80e1e1
Don't change JSON schema until the feature is approved (experimental …
grynspan 80b9763
Increase test coverage, add estimatedAttachmentByteCount property
grynspan 604e83d
Add mutable buffer conformances
grynspan 5f480a8
Some comment tweaks; change how we go about writing files during even…
grynspan a7cefae
Don't automatically write to /tmp (yet)
grynspan 86eeb29
Work around compiler crash
grynspan 8453452
Add Attachable conformances for ArraySlice and ContiguousArray
grynspan 4a3764d
Make the attachment symbol underscored in JSON
grynspan 83c10cf
Amend some comments
grynspan 253eb53
Incorporate feedback
grynspan 0ab059f
Fix typos
grynspan b0b4142
Correct estimatedAttachmentByteCount after proxying
grynspan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Sources/Testing/ABI/v0/Encoded/ABIv0.EncodedAttachment.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// 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 | ||
// | ||
|
||
extension ABIv0 { | ||
/// A type implementing the JSON encoding of ``Test/Attachment`` for the ABI | ||
/// entry point and event stream output. | ||
/// | ||
/// This type is not part of the public interface of the testing library. It | ||
/// assists in converting values to JSON; clients that consume this JSON are | ||
/// expected to write their own decoders. | ||
/// | ||
/// - Warning: Attachments are not yet part of the JSON schema. | ||
struct EncodedAttachment: Sendable { | ||
/// The path where the attachment was written. | ||
var path: String? | ||
|
||
init(encoding attachment: borrowing Test.Attachment, in eventContext: borrowing Event.Context) { | ||
path = attachment.fileSystemPath | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Codable | ||
|
||
extension ABIv0.EncodedAttachment: Codable {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// | ||
// 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 | ||
// | ||
|
||
@_spi(Experimental) | ||
extension Test { | ||
/// A protocol describing a type that can be attached to a test report or | ||
/// written to disk when a test is run. | ||
/// | ||
/// To attach an attachable value to a test report or test run output, use it | ||
/// to initialize a new instance of ``Test/Attachment``, then call | ||
/// ``Test/Attachment/attach()``. An attachment can only be attached once. | ||
/// | ||
/// The testing library provides default conformances to this protocol for a | ||
/// variety of standard library types. Most user-defined types do not need to | ||
/// conform to this protocol. | ||
/// | ||
/// A type should conform to this protocol if it can be represented as a | ||
/// sequence of bytes that would be diagnostically useful if a test fails. | ||
public protocol Attachable: ~Copyable { | ||
/// An estimate of the number of bytes of memory needed to store this value | ||
/// as an attachment. | ||
/// | ||
/// The testing library uses this property to determine if an attachment | ||
/// should be held in memory or should be immediately persisted to storage. | ||
/// Larger attachments are more likely to be persisted, but the algorithm | ||
/// the testing library uses is an implementation detail and is subject to | ||
/// change. | ||
/// | ||
/// The value of this property is approximately equal to the number of bytes | ||
/// that will actually be needed, or `nil` if the value cannot be computed | ||
/// efficiently. The default implementation of this property returns `nil`. | ||
/// | ||
/// - Complexity: O(1) unless `Self` conforms to `Collection`, in which case | ||
/// up to O(_n_) where _n_ is the length of the collection. | ||
var estimatedAttachmentByteCount: Int? { get } | ||
grynspan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Call a function and pass a buffer representing this instance to it. | ||
/// | ||
/// - Parameters: | ||
/// - attachment: The attachment that is requesting a buffer (that is, the | ||
/// attachment containing this instance.) | ||
/// - 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. The format of the buffer is | ||
/// implementation-defined, but should be "idiomatic" for this type: for | ||
/// example, if this type represents an image, it would be appropriate for | ||
/// the buffer to contain an image in PNG format, JPEG format, etc., but it | ||
/// would not be idiomatic for the buffer to contain a textual description | ||
/// of the image. | ||
borrowing func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R | ||
} | ||
} | ||
|
||
// MARK: - Default implementations | ||
|
||
extension Test.Attachable where Self: ~Copyable { | ||
public var estimatedAttachmentByteCount: Int? { | ||
nil | ||
} | ||
} | ||
|
||
extension Test.Attachable where Self: Collection, Element == UInt8 { | ||
public var estimatedAttachmentByteCount: Int? { | ||
count | ||
} | ||
|
||
// We do not provide an implementation of withUnsafeBufferPointer(for:_:) here | ||
// because there is no way in the standard library to statically detect if a | ||
// collection can provide contiguous storage (_HasContiguousBytes is not API.) | ||
// If withContiguousBytesIfAvailable(_:) fails, we don't want to make a | ||
// (potentially expensive!) copy of the collection. | ||
// | ||
// The planned Foundation cross-import overlay can provide a default | ||
// implementation for collection types that conform to Foundation's | ||
// ContiguousBytes protocol. | ||
} | ||
|
||
extension Test.Attachable where Self: StringProtocol { | ||
public var estimatedAttachmentByteCount: Int? { | ||
// NOTE: utf8.count may be O(n) for foreign strings. | ||
// SEE: https://github.com/swiftlang/swift/blob/main/stdlib/public/core/StringUTF8View.swift | ||
utf8.count | ||
} | ||
} | ||
|
||
// MARK: - Default conformances | ||
|
||
// Implement the protocol requirements for byte arrays and buffers so that | ||
// developers can attach raw data when needed. | ||
@_spi(Experimental) | ||
extension Array<UInt8>: Test.Attachable { | ||
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
try withUnsafeBytes(body) | ||
} | ||
} | ||
|
||
@_spi(Experimental) | ||
extension ContiguousArray<UInt8>: Test.Attachable { | ||
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
try withUnsafeBytes(body) | ||
} | ||
} | ||
|
||
@_spi(Experimental) | ||
extension ArraySlice<UInt8>: Test.Attachable { | ||
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
try withUnsafeBytes(body) | ||
} | ||
} | ||
|
||
@_spi(Experimental) | ||
extension UnsafeBufferPointer<UInt8>: Test.Attachable { | ||
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
try body(.init(self)) | ||
} | ||
} | ||
|
||
@_spi(Experimental) | ||
extension UnsafeMutableBufferPointer<UInt8>: Test.Attachable { | ||
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
try body(.init(self)) | ||
} | ||
} | ||
|
||
@_spi(Experimental) | ||
extension UnsafeRawBufferPointer: Test.Attachable { | ||
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
try body(self) | ||
} | ||
} | ||
|
||
@_spi(Experimental) | ||
extension UnsafeMutableRawBufferPointer: Test.Attachable { | ||
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
try body(.init(self)) | ||
} | ||
} | ||
|
||
@_spi(Experimental) | ||
extension String: Test.Attachable { | ||
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
var selfCopy = self | ||
return try selfCopy.withUTF8 { utf8 in | ||
try body(UnsafeRawBufferPointer(utf8)) | ||
} | ||
} | ||
} | ||
|
||
@_spi(Experimental) | ||
extension Substring: Test.Attachable { | ||
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { | ||
var selfCopy = self | ||
return try selfCopy.withUTF8 { utf8 in | ||
try body(UnsafeRawBufferPointer(utf8)) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.