diff --git a/Sources/Testing/Attachments/Attachment.swift b/Sources/Testing/Attachments/Attachment.swift index 56bc2c112..d3906db35 100644 --- a/Sources/Testing/Attachments/Attachment.swift +++ b/Sources/Testing/Attachments/Attachment.swift @@ -545,7 +545,7 @@ extension Attachment where AttachableValue: ~Copyable { } 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: ".") + var preferredNameComponents = preferredName.split(separator: ".", omittingEmptySubsequences: false) let firstPreferredNameComponent = preferredNameComponents[0] while true { diff --git a/Tests/TestingTests/AttachmentTests.swift b/Tests/TestingTests/AttachmentTests.swift index 8bd7bee88..e34483a74 100644 --- a/Tests/TestingTests/AttachmentTests.swift +++ b/Tests/TestingTests/AttachmentTests.swift @@ -164,6 +164,9 @@ struct AttachmentTests { String(repeating: "a", count: maximumNameCount), String(repeating: "a", count: maximumNameCount + 1), String(repeating: "a", count: maximumNameCount + 2), + ".", + "..", + "...", ]) func writeAttachmentWithBadName(name: String) throws { let attachableValue = MySendableAttachable(string: "") let attachment = Attachment(attachableValue, named: name) @@ -176,6 +179,29 @@ struct AttachmentTests { try compare(attachableValue, toContentsOfFileAtPath: filePath) } + @Test func writeAttachmentWithEmptyPreferredName() throws { + struct MyAttachableWithEmptyName: Attachable, Sendable { + var string: String + + func withUnsafeBytes(for attachment: borrowing Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { + try Array(string.utf8).withUnsafeBytes(body) + } + + func preferredName(for attachment: borrowing Attachment, basedOn suggestedName: String) -> String { + "" + } + } + let string = "" + let attachment = Attachment(MyAttachableWithEmptyName(string: string)) + + let filePath = try attachment.write(toFileInDirectoryAtPath: temporaryDirectory()) + defer { + remove(filePath) + } + let file = try Testing.FileHandle(forReadingAtPath: filePath) + #expect(try file.readToEnd() == Array(string.utf8)) + } + @Test func fileSystemPathIsSetAfterWritingViaEventHandler() async throws { let attachableValue = MySendableAttachable(string: "") try await confirmation("Attachment detected") { valueAttached in