Skip to content

Commit bf935c5

Browse files
committed
Add string conformances to Attachable; write attachments to system/user temporary directory by default
1 parent b5ac73a commit bf935c5

File tree

7 files changed

+61
-32
lines changed

7 files changed

+61
-32
lines changed

Sources/Testing/ABI/EntryPoints/EntryPoint.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ public func configurationForEntryPoint(from args: __CommandLineArguments_v0) thr
479479
throw _EntryPointError.invalidArgument("--experimental-attachment-path", value: attachmentPath)
480480
}
481481
configuration.attachmentDirectoryPath = attachmentPath
482+
} else {
483+
// Write attachments to the system's temporary directory.
484+
configuration.attachmentDirectoryPath = try temporaryDirectoryPath()
482485
}
483486

484487
#if canImport(Foundation)

Sources/Testing/Attachments/Test.Attachable.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,23 @@ extension UnsafeRawBufferPointer: Test.Attachable {
7070
try body(self)
7171
}
7272
}
73+
74+
@_spi(Experimental)
75+
extension String: Test.Attachable {
76+
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
77+
var selfCopy = self
78+
return try selfCopy.withUTF8 { utf8 in
79+
try body(UnsafeRawBufferPointer(utf8))
80+
}
81+
}
82+
}
83+
84+
@_spi(Experimental)
85+
extension Substring: Test.Attachable {
86+
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
87+
var selfCopy = self
88+
return try selfCopy.withUTF8 { utf8 in
89+
try body(UnsafeRawBufferPointer(utf8))
90+
}
91+
}
92+
}

Sources/Testing/Support/FileHandle.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,4 +632,33 @@ func canonicalizePath(_ path: String) -> String? {
632632
return nil
633633
#endif
634634
}
635+
636+
/// Get the path to the user's or system's temporary directory.
637+
///
638+
/// - Returns: The path to a directory suitable for storing temporary files.
639+
///
640+
/// - Throws: If the user's or system's temporary directory could not be
641+
/// determined.
642+
func temporaryDirectoryPath() throws -> String {
643+
#if SWT_TARGET_OS_APPLE
644+
try withUnsafeTemporaryAllocation(of: CChar.self, capacity: Int(PATH_MAX)) { buffer in
645+
if 0 != confstr(_CS_DARWIN_USER_TEMP_DIR, buffer.baseAddress, buffer.count) {
646+
return String(cString: buffer.baseAddress!)
647+
}
648+
return try #require(Environment.variable(named: "TMPDIR"))
649+
}
650+
#elseif os(Linux) || os(FreeBSD)
651+
"/tmp"
652+
#elseif os(Android)
653+
Environment.variable(named: "TMPDIR") ?? "/data/local/tmp"
654+
#elseif os(Windows)
655+
try withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: Int(MAX_PATH + 1)) { buffer in
656+
// NOTE: GetTempPath2W() was introduced in Windows 10 Build 20348.
657+
if 0 == GetTempPathW(DWORD(buffer.count), buffer.baseAddress) {
658+
throw Win32Error(rawValue: GetLastError())
659+
}
660+
return try #require(String.decodeCString(buffer.baseAddress, as: UTF16.self)?.result)
661+
}
662+
#endif
663+
}
635664
#endif

Tests/TestingTests/AttachmentTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct AttachmentTests {
2424
let attachment = Test.Attachment(attachableValue, named: "loremipsum.html")
2525

2626
// Write the attachment to disk, then read it back.
27-
let filePath = try attachment.write(toFileInDirectoryAtPath: temporaryDirectory())
27+
let filePath = try attachment.write(toFileInDirectoryAtPath: temporaryDirectoryPath())
2828
defer {
2929
remove(filePath)
3030
}
@@ -59,7 +59,7 @@ struct AttachmentTests {
5959
let attachment = Test.Attachment(attachableValue, named: baseFileName)
6060

6161
// Write the attachment to disk, then read it back.
62-
let filePath = try attachment.write(toFileInDirectoryAtPath: temporaryDirectory(), appending: suffixes.next()!)
62+
let filePath = try attachment.write(toFileInDirectoryAtPath: temporaryDirectoryPath(), appending: suffixes.next()!)
6363
createdFilePaths.append(filePath)
6464
let fileName = try #require(filePath.split { $0 == "/" || $0 == #"\"# }.last)
6565
if i == 0 {
@@ -85,14 +85,14 @@ struct AttachmentTests {
8585

8686
// Write the attachment to disk once to ensure the original filename is not
8787
// available and we add a suffix.
88-
let originalFilePath = try attachment.write(toFileInDirectoryAtPath: temporaryDirectory())
88+
let originalFilePath = try attachment.write(toFileInDirectoryAtPath: temporaryDirectoryPath())
8989
defer {
9090
remove(originalFilePath)
9191
}
9292

9393
// Write the attachment to disk, then read it back.
9494
let suffix = String(UInt64.random(in: 0 ..< .max), radix: 36)
95-
let filePath = try attachment.write(toFileInDirectoryAtPath: temporaryDirectory(), appending: suffix)
95+
let filePath = try attachment.write(toFileInDirectoryAtPath: temporaryDirectoryPath(), appending: suffix)
9696
defer {
9797
remove(filePath)
9898
}

Tests/TestingTests/Support/FileHandleTests.swift

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func withTemporaryPath<R>(_ body: (_ path: String) throws -> R) throws -> R {
215215
return strnlen(buffer.baseAddress!, buffer.count)
216216
}
217217
#else
218-
let path = appendPathComponent("file_named_\(UInt64.random(in: 0 ..< .max))", to: try temporaryDirectory())
218+
let path = appendPathComponent("file_named_\(UInt64.random(in: 0 ..< .max))", to: try temporaryDirectoryPath())
219219
#endif
220220
defer {
221221
_ = remove(path)
@@ -247,29 +247,6 @@ extension FileHandle {
247247
}
248248
#endif
249249

250-
func temporaryDirectory() throws -> String {
251-
#if SWT_TARGET_OS_APPLE
252-
try withUnsafeTemporaryAllocation(of: CChar.self, capacity: Int(PATH_MAX)) { buffer in
253-
if 0 != confstr(_CS_DARWIN_USER_TEMP_DIR, buffer.baseAddress, buffer.count) {
254-
return String(cString: buffer.baseAddress!)
255-
}
256-
return try #require(Environment.variable(named: "TMPDIR"))
257-
}
258-
#elseif os(Linux) || os(FreeBSD)
259-
"/tmp"
260-
#elseif os(Android)
261-
Environment.variable(named: "TMPDIR") ?? "/data/local/tmp"
262-
#elseif os(Windows)
263-
try withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: Int(MAX_PATH + 1)) { buffer in
264-
// NOTE: GetTempPath2W() was introduced in Windows 10 Build 20348.
265-
if 0 == GetTempPathW(DWORD(buffer.count), buffer.baseAddress) {
266-
throw Win32Error(rawValue: GetLastError())
267-
}
268-
return try #require(String.decodeCString(buffer.baseAddress, as: UTF16.self)?.result)
269-
}
270-
#endif
271-
}
272-
273250
#if SWT_TARGET_OS_APPLE
274251
func fileHandleForCloseMonitoring(with confirmation: Confirmation) throws -> FileHandle {
275252
let context = Unmanaged.passRetained(confirmation as AnyObject).toOpaque()

Tests/TestingTests/SwiftPMTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ struct SwiftPMTests {
176176
func xunitOutputIsWrittenToFile() throws {
177177
// Test that a file is opened when requested. Testing of the actual output
178178
// occurs in ConsoleOutputRecorderTests.
179-
let tempDirPath = try temporaryDirectory()
179+
let tempDirPath = try temporaryDirectoryPath()
180180
let temporaryFilePath = appendPathComponent("\(UInt64.random(in: 0 ..< .max))", to: tempDirPath)
181181
defer {
182182
_ = remove(temporaryFilePath)
@@ -200,7 +200,7 @@ struct SwiftPMTests {
200200
"--configuration-path", "--experimental-configuration-path",
201201
])
202202
func configurationPath(argumentName: String) async throws {
203-
let tempDirPath = try temporaryDirectory()
203+
let tempDirPath = try temporaryDirectoryPath()
204204
let temporaryFilePath = appendPathComponent("\(UInt64.random(in: 0 ..< .max))", to: tempDirPath)
205205
defer {
206206
_ = remove(temporaryFilePath)
@@ -244,7 +244,7 @@ struct SwiftPMTests {
244244
func eventStreamOutput(outputArgumentName: String, versionArgumentName: String, version: String) async throws {
245245
// Test that JSON records are successfully streamed to a file and can be
246246
// read back into memory and decoded.
247-
let tempDirPath = try temporaryDirectory()
247+
let tempDirPath = try temporaryDirectoryPath()
248248
let temporaryFilePath = appendPathComponent("\(UInt64.random(in: 0 ..< .max))", to: tempDirPath)
249249
defer {
250250
_ = remove(temporaryFilePath)

Tests/TestingTests/Traits/TagListTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ struct TagListTests {
121121
#if !SWT_NO_FILE_IO
122122
@Test("Colors are read from disk")
123123
func tagColorsReadFromDisk() throws {
124-
let tempDirPath = try temporaryDirectory()
124+
let tempDirPath = try temporaryDirectoryPath()
125125
let jsonPath = appendPathComponent("tag-colors.json", to: tempDirPath)
126126
var jsonContent = """
127127
{

0 commit comments

Comments
 (0)