Skip to content

Commit 17a6088

Browse files
authored
Merge pull request #2011 from compnerd/POSIX-is-so-1997-2017
TestFoundation: port TestURL to FileManager
2 parents 27207fe + 86cc3b6 commit 17a6088

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

TestFoundation/TestURL.swift

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class TestURL : XCTestCase {
7777
#elseif os(Linux)
7878
let baseURL = URL(fileURLWithPath: "/usr", isDirectory: true)
7979
let relativePath = "include"
80+
#elseif os(Windows)
81+
let baseURL = URL(fileURLWithPath: homeDirectory, isDirectory: true)
82+
let relativePath = "Documents"
8083
#endif
8184
// we're telling fileURLWithPath:isDirectory:relativeTo: Documents is a directory
8285
let url1 = URL(fileURLWithFileSystemRepresentation: relativePath, isDirectory: true, relativeTo: baseURL)
@@ -224,10 +227,7 @@ class TestURL : XCTestCase {
224227

225228
static let gBaseTemporaryDirectoryPath = NSTemporaryDirectory()
226229
static var gBaseCurrentWorkingDirectoryPath : String {
227-
let count = Int(1024) // MAXPATHLEN is platform specific; this is the lowest common denominator for darwin and most linuxes
228-
var buf : [Int8] = Array(repeating: 0, count: count)
229-
getcwd(&buf, count)
230-
return String(cString: buf)
230+
return FileManager.default.currentDirectoryPath
231231
}
232232
static var gRelativeOffsetFromBaseCurrentWorkingDirectory: UInt = 0
233233
static let gFileExistsName = "TestCFURL_file_exists\(ProcessInfo.processInfo.globallyUniqueString)"
@@ -240,21 +240,37 @@ class TestURL : XCTestCase {
240240
static let gDirectoryDoesNotExistPath = gBaseTemporaryDirectoryPath + gDirectoryDoesNotExistName
241241

242242
static func setup_test_paths() -> Bool {
243-
if creat(gFileExistsPath, S_IRWXU) < 0 && errno != EEXIST {
244-
return false
245-
}
246-
if unlink(gFileDoesNotExistPath) != 0 && errno != ENOENT {
243+
_ = FileManager.default.createFile(atPath: gFileExistsPath, contents: nil)
244+
245+
do {
246+
try FileManager.default.removeItem(atPath: gFileDoesNotExistPath)
247+
} catch let error as NSError {
248+
// The error code is a CocoaError
249+
if error.code != CocoaError.fileNoSuchFile.rawValue {
247250
return false
251+
}
248252
}
249-
if mkdir(gDirectoryExistsPath, S_IRWXU) != 0 && errno != EEXIST {
253+
254+
do {
255+
try FileManager.default.createDirectory(atPath: gDirectoryExistsPath, withIntermediateDirectories: false)
256+
} catch let error as NSError {
257+
// The error code is a CocoaError
258+
if error.code != CocoaError.fileWriteFileExists.rawValue {
250259
return false
260+
}
251261
}
252-
if rmdir(gDirectoryDoesNotExistPath) != 0 && errno != ENOENT {
262+
263+
do {
264+
try FileManager.default.removeItem(atPath: gDirectoryDoesNotExistPath)
265+
} catch let error as NSError {
266+
// The error code is a CocoaError
267+
if error.code != CocoaError.fileNoSuchFile.rawValue {
253268
return false
269+
}
254270
}
255-
271+
256272
#if os(Android)
257-
chdir("/data/local/tmp")
273+
FileManager.default.changeCurrentDirectoryPath("/data/local/tmp")
258274
#endif
259275

260276
let cwd = FileManager.default.currentDirectoryPath
@@ -263,8 +279,7 @@ class TestURL : XCTestCase {
263279
cwdURL.withUnsafeFileSystemRepresentation {
264280
gRelativeOffsetFromBaseCurrentWorkingDirectory = UInt(strlen($0!) + 1)
265281
}
266-
267-
282+
268283
return true
269284
}
270285

0 commit comments

Comments
 (0)