Skip to content

Commit 3811d5b

Browse files
Fix build failures on iOS (#363)
Make `temporaryDirectory()` available on non-macOS, non-Linux and non-Windows platforms too. ### Motivation: Currently our test fail to build for non-macOS, non-Linux and non-Windows platforms. ### Modifications: I moved `temporaryDirectory()` outside the conditional that made it only available to macOS, Linux and Windows, but still inside the `#if !SWT_NO_FILE_IO` condition. ### Result: Our unit tests compile. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent e92bee7 commit 3811d5b

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

Tests/TestingTests/Support/FileHandleTests.swift

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,6 @@ struct FileHandleTests {
164164

165165
// MARK: - Fixtures
166166

167-
func temporaryDirectory() throws -> String {
168-
#if SWT_TARGET_OS_APPLE
169-
try withUnsafeTemporaryAllocation(of: CChar.self, capacity: Int(PATH_MAX)) { buffer in
170-
if 0 != confstr(_CS_DARWIN_USER_TEMP_DIR, buffer.baseAddress, buffer.count) {
171-
return String(cString: buffer.baseAddress!)
172-
}
173-
return try #require(Environment.variable(named: "TMPDIR"))
174-
}
175-
#elseif os(Linux)
176-
"/tmp"
177-
#elseif os(Windows)
178-
try withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: Int(MAX_PATH + 1)) { buffer in
179-
// NOTE: GetTempPath2W() was introduced in Windows 10 Build 20348.
180-
if 0 == GetTempPathW(DWORD(buffer.count), buffer.baseAddress) {
181-
throw Win32Error(rawValue: GetLastError())
182-
}
183-
return try #require(String.decodeCString(buffer.baseAddress, as: UTF16.self)?.result)
184-
}
185-
#endif
186-
}
187-
188167
func withTemporaryPath<R>(_ body: (_ path: String) throws -> R) throws -> R {
189168
// NOTE: we are not trying to test mkstemp() here. We are trying to test the
190169
// capacity of FileHandle to open a file for reading or writing and we need a
@@ -226,4 +205,26 @@ extension FileHandle {
226205
}
227206
}
228207
#endif
208+
209+
func temporaryDirectory() throws -> String {
210+
#if SWT_TARGET_OS_APPLE
211+
try withUnsafeTemporaryAllocation(of: CChar.self, capacity: Int(PATH_MAX)) { buffer in
212+
if 0 != confstr(_CS_DARWIN_USER_TEMP_DIR, buffer.baseAddress, buffer.count) {
213+
return String(cString: buffer.baseAddress!)
214+
}
215+
return try #require(Environment.variable(named: "TMPDIR"))
216+
}
217+
#elseif os(Linux)
218+
"/tmp"
219+
#elseif os(Windows)
220+
try withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: Int(MAX_PATH + 1)) { buffer in
221+
// NOTE: GetTempPath2W() was introduced in Windows 10 Build 20348.
222+
if 0 == GetTempPathW(DWORD(buffer.count), buffer.baseAddress) {
223+
throw Win32Error(rawValue: GetLastError())
224+
}
225+
return try #require(String.decodeCString(buffer.baseAddress, as: UTF16.self)?.result)
226+
}
227+
#endif
228+
}
229+
229230
#endif

0 commit comments

Comments
 (0)