Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Testing/Attachments/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions Tests/TestingTests/AttachmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<!doctype html>")
let attachment = Attachment(attachableValue, named: name)
Expand All @@ -176,6 +179,29 @@ struct AttachmentTests {
try compare(attachableValue, toContentsOfFileAtPath: filePath)
}

@Test func writeAttachmentWithEmptyPreferredName() throws {
struct MyAttachableWithEmptyName: Attachable, Sendable {
var string: String

func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
try Array(string.utf8).withUnsafeBytes(body)
}

func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String {
""
}
}
let string = "<!doctype html>"
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: "<!doctype html>")
try await confirmation("Attachment detected") { valueAttached in
Expand Down
Loading