Created
May 20, 2026 19:29
-
-
Save gohanlon/c248dd4b1ec179ee832d9a2ccc8805a8 to your computer and use it in GitHub Desktop.
Verification: Attachment.record(Array(data)) works correctly (swift-snapshot-testing #1085)
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
| #!/bin/bash | |
| set -e | |
| rm -rf /tmp/AttachmentVerification | |
| mkdir -p /tmp/AttachmentVerification/Tests/AttachmentVerificationTests | |
| cat > /tmp/AttachmentVerification/Package.swift << 'EOF' | |
| // swift-tools-version: 6.0 | |
| import PackageDescription | |
| let package = Package( | |
| name: "AttachmentVerification", | |
| platforms: [.macOS(.v13), .iOS(.v16), .tvOS(.v16)], | |
| targets: [ | |
| .testTarget(name: "AttachmentVerificationTests") | |
| ] | |
| ) | |
| EOF | |
| cat > /tmp/AttachmentVerification/Tests/AttachmentVerificationTests/AttachmentVerificationTests.swift << 'SWIFT' | |
| import Foundation | |
| import Testing | |
| #if canImport(AppKit) | |
| import AppKit | |
| #endif | |
| #if canImport(UIKit) | |
| import UIKit | |
| #endif | |
| @Suite("Attachment.record with Array(data)") | |
| struct AttachmentVerificationTests { | |
| @Test("Array(data) preserves bytes exactly") | |
| func arrayFromDataPreservesBytes() { | |
| let original: [UInt8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] | |
| #expect(Array(Data(original)) == original) | |
| } | |
| @Test("Attachment value is byte-identical to source Data") | |
| func attachmentValue() { | |
| let bytes: [UInt8] = [0xFF, 0xD8, 0xFF, 0xE0] | |
| let attachment = Attachment(Array(Data(bytes)), named: "test.jpg") | |
| #expect(attachment.attachableValue == bytes) | |
| } | |
| @Test("Record text data") | |
| func recordTextData() { | |
| Attachment.record(Array(Data("Hello from Array(data)".utf8)), named: "hello.txt") | |
| } | |
| @Test("Record empty data") | |
| func recordEmptyData() { | |
| Attachment.record(Array(Data()), named: "empty.bin") | |
| } | |
| @Test("Record large data (1 MB)") | |
| func recordLargeData() { | |
| let data = Data(repeating: 0xAB, count: 1_000_000) | |
| let attachment = Attachment(Array(data), named: "large.bin") | |
| #expect(attachment.attachableValue.count == 1_000_000) | |
| Attachment.record(Array(data), named: "large.bin") | |
| } | |
| private static var isAppKit: Bool { | |
| #if canImport(AppKit) | |
| true | |
| #else | |
| false | |
| #endif | |
| } | |
| private static var isUIKit: Bool { | |
| #if canImport(UIKit) && !os(watchOS) | |
| true | |
| #else | |
| false | |
| #endif | |
| } | |
| @Test("Record PNG via AppKit", | |
| .enabled(if: isAppKit, "AppKit not available on this platform")) | |
| func recordPNGViaAppKit() throws { | |
| #if canImport(AppKit) | |
| let image = NSImage(size: NSSize(width: 100, height: 100), flipped: false) { rect in | |
| NSColor.red.setFill() | |
| rect.fill() | |
| return true | |
| } | |
| let cgImage = try #require(image.cgImage(forProposedRect: nil, context: nil, hints: nil)) | |
| let pngData = try #require( | |
| NSBitmapImageRep(cgImage: cgImage).representation(using: .png, properties: [:]) | |
| ) | |
| Attachment.record(Array(pngData), named: "red-square.png") | |
| #endif | |
| } | |
| @Test("Record PNG via UIKit", | |
| .enabled(if: isUIKit, "UIKit not available on this platform")) | |
| func recordPNGViaUIKit() throws { | |
| #if canImport(UIKit) && !os(watchOS) | |
| let pngData = UIGraphicsImageRenderer(size: CGSize(width: 100, height: 100)).pngData { | |
| UIColor.red.setFill() | |
| $0.fill(CGRect(x: 0, y: 0, width: 100, height: 100)) | |
| } | |
| Attachment.record(Array(pngData), named: "red-square.png") | |
| #endif | |
| } | |
| } | |
| SWIFT | |
| cd /tmp/AttachmentVerification | |
| for v in 6.2.4 6.3.0; do | |
| echo "=== Swift $v ===" | |
| swiftly use "$v" | |
| swift package clean | |
| swift test 2>&1 \ | |
| | grep -E "passed|failed|skipped|error:|Attached" | |
| done | |
| sleep 3 | |
| open /tmp/AttachmentVerification/Package.swift |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment