Skip to content

Commit d4115b7

Browse files
committed
Fix URL test failures
1 parent f71d985 commit d4115b7

File tree

4 files changed

+44
-43
lines changed

4 files changed

+44
-43
lines changed

Sources/Foundation/NSPathUtilities.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ let validPathSeps: [Character] = ["/"]
2121
#endif
2222

2323
public func NSTemporaryDirectory() -> String {
24-
FileManager.default.temporaryDirectory.path()
24+
FileManager.default.temporaryDirectory.withUnsafeFileSystemRepresentation {
25+
String(cString: $0!)
26+
}
2527
}
2628

2729
extension String {
@@ -614,12 +616,16 @@ public func NSSearchPathForDirectoriesInDomains(_ directory: FileManager.SearchP
614616
}
615617

616618
public func NSHomeDirectory() -> String {
617-
FileManager.default.homeDirectoryForCurrentUser.path
619+
FileManager.default.homeDirectoryForCurrentUser.withUnsafeFileSystemRepresentation {
620+
String(cString: $0!)
621+
}
618622
}
619623

620624
public func NSHomeDirectoryForUser(_ user: String?) -> String? {
621625
guard let user else { return NSHomeDirectory() }
622-
return FileManager.default.homeDirectory(forUser: user)?.path
626+
return FileManager.default.homeDirectory(forUser: user)?.withUnsafeFileSystemRepresentation {
627+
String(cString: $0!)
628+
}
623629
}
624630

625631
public func NSUserName() -> String {

Tests/Foundation/TestFileManager.swift

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,18 +1258,14 @@ class TestFileManager : XCTestCase {
12581258
return stdout.trimmingCharacters(in: CharacterSet.newlines)
12591259
}
12601260

1261-
func assertFetchingPath(withConfiguration config: String, identifier: String, yields path: String) {
1261+
func assertFetchingPath(withConfiguration config: String, identifier: String, yields path: String) throws {
12621262
for method in [ "NSSearchPath", "FileManagerDotURLFor", "FileManagerDotURLsFor" ] {
1263-
do {
1264-
let found = try printPathByRunningHelper(withConfiguration: config, method: method, identifier: identifier)
1265-
XCTAssertEqual(found, path)
1266-
} catch let error {
1267-
XCTFail("Failed with method \(method), configuration \(config), identifier \(identifier), equal to \(path), error \(error)")
1268-
}
1263+
let found = try printPathByRunningHelper(withConfiguration: config, method: method, identifier: identifier)
1264+
XCTAssertEqual(found, path)
12691265
}
12701266
}
12711267

1272-
func test_fetchXDGPathsFromHelper() {
1268+
func test_fetchXDGPathsFromHelper() throws {
12731269
let prefix = NSHomeDirectory() + "/_Foundation_Test_"
12741270

12751271
let configuration = """
@@ -1282,13 +1278,13 @@ class TestFileManager : XCTestCase {
12821278
VIDEOS=\(prefix)/Videos
12831279
"""
12841280

1285-
assertFetchingPath(withConfiguration: configuration, identifier: "desktop", yields: "\(prefix)/Desktop")
1286-
assertFetchingPath(withConfiguration: configuration, identifier: "download", yields: "\(prefix)/Download")
1287-
assertFetchingPath(withConfiguration: configuration, identifier: "publicShare", yields: "\(prefix)/PublicShare")
1288-
assertFetchingPath(withConfiguration: configuration, identifier: "documents", yields: "\(prefix)/Documents")
1289-
assertFetchingPath(withConfiguration: configuration, identifier: "music", yields: "\(prefix)/Music")
1290-
assertFetchingPath(withConfiguration: configuration, identifier: "pictures", yields: "\(prefix)/Pictures")
1291-
assertFetchingPath(withConfiguration: configuration, identifier: "videos", yields: "\(prefix)/Videos")
1281+
try assertFetchingPath(withConfiguration: configuration, identifier: "desktop", yields: "\(prefix)/Desktop")
1282+
try assertFetchingPath(withConfiguration: configuration, identifier: "download", yields: "\(prefix)/Download")
1283+
try assertFetchingPath(withConfiguration: configuration, identifier: "publicShare", yields: "\(prefix)/PublicShare")
1284+
try assertFetchingPath(withConfiguration: configuration, identifier: "documents", yields: "\(prefix)/Documents")
1285+
try assertFetchingPath(withConfiguration: configuration, identifier: "music", yields: "\(prefix)/Music")
1286+
try assertFetchingPath(withConfiguration: configuration, identifier: "pictures", yields: "\(prefix)/Pictures")
1287+
try assertFetchingPath(withConfiguration: configuration, identifier: "videos", yields: "\(prefix)/Videos")
12921288
}
12931289
#endif // !os(Android)
12941290
#endif // !DEPLOYMENT_RUNTIME_OBJC

Tests/Foundation/TestURL.swift

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ let kCFURLCreateAbsoluteURLWithBytesCreator = "CFURLCreateAbsoluteURLWithBytes"
2727
let kNullURLString = "<null url>"
2828
let kNullString = "<null>"
2929

30+
func XCTAssertEqualFileSystemPaths(_ lhs: String?, _ rhs: String?, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
31+
#if os(Windows)
32+
let mappedLHS = lhs?.replacingOccurrences(of: "\\", with: "/")
33+
let mappedRHS = rhs?.replacingOccurrences(of: "\\", with: "/")
34+
XCTAssertEqual(mappedLHS, mappedRHS, message(), file: file, line: line)
35+
#else
36+
XCTAssertEqual(lhs, rhs, message(), file: file, line: line)
37+
#endif
38+
}
39+
3040
/// Reads the test data plist file and returns the list of objects
3141
private func getTestData() -> [Any]? {
3242
let testFilePath = testBundle().url(forResource: "NSURLTestData", withExtension: "plist")
@@ -54,17 +64,6 @@ class TestURL : XCTestCase {
5464
let u1 = URL(fileURLWithPath: "S:\\b\\u1/")
5565
XCTAssertEqual(u1.absoluteString, "file:///S:/b/u1/")
5666

57-
// ensure that trailing slashes are compressed
58-
// e.g. NOT file:///S:/b/u2%2F%2F%2F%/
59-
let u2 = URL(fileURLWithPath: "S:\\b\\u2/////")
60-
XCTAssertEqual(u2.absoluteString, "file:///S:/b/u2/")
61-
62-
// ensure that the trailing slashes are compressed even when mixed
63-
// e.g. NOT file:///S:/b/u3%2F%/%2F%2/
64-
let u3 = URL(fileURLWithPath: "S:\\b\\u3//\\//")
65-
XCTAssertEqual(u3.absoluteString, "file:///S:/b/u3/")
66-
XCTAssertEqual(u3.path, "S:/b/u3")
67-
6867
// ensure that the regular conversion works
6968
let u4 = URL(fileURLWithPath: "S:\\b\\u4")
7069
XCTAssertEqual(u4.absoluteString, "file:///S:/b/u4")
@@ -103,9 +102,6 @@ class TestURL : XCTestCase {
103102
let u3 = URL(fileURLWithPath: "\\", isDirectory: false)
104103
XCTAssertEqual(u3.absoluteString, "file:///")
105104

106-
let u4 = URL(fileURLWithPath: "S:\\b\\u3//\\//")
107-
XCTAssertEqual(u4.absoluteString, "file:///S:/b/u3/")
108-
109105
// ensure leading slash doesn't break everything
110106
let u5 = URL(fileURLWithPath: "\\abs\\path")
111107
XCTAssertEqual(u5.absoluteString, "file:///abs/path")
@@ -124,7 +120,7 @@ class TestURL : XCTestCase {
124120
func test_fileURLWithPath_relativeTo() {
125121
let homeDirectory = NSHomeDirectory()
126122
let homeURL = URL(fileURLWithPath: homeDirectory, isDirectory: true)
127-
XCTAssertEqual(homeDirectory, homeURL.path)
123+
XCTAssertEqualFileSystemPaths(homeDirectory, homeURL.withUnsafeFileSystemRepresentation { String(cString: $0!) })
128124

129125
#if os(macOS)
130126
let baseURL = URL(fileURLWithPath: homeDirectory, isDirectory: true)
@@ -411,25 +407,25 @@ class TestURL : XCTestCase {
411407
var path = TestURL.gFileExistsPath
412408
var url = NSURL(fileURLWithPath: path)
413409
XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)")
414-
XCTAssertEqual(path, url.path, "path from file path URL is wrong")
410+
XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong")
415411

416412
// test with file that doesn't exist
417413
path = TestURL.gFileDoesNotExistPath
418414
url = NSURL(fileURLWithPath: path)
419415
XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)")
420-
XCTAssertEqual(path, url.path, "path from file path URL is wrong")
416+
XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong")
421417

422418
// test with directory that exists
423419
path = TestURL.gDirectoryExistsPath
424420
url = NSURL(fileURLWithPath: path)
425421
XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)")
426-
XCTAssertEqual(path, url.path, "path from file path URL is wrong")
422+
XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong")
427423

428424
// test with directory that doesn't exist
429425
path = TestURL.gDirectoryDoesNotExistPath
430426
url = NSURL(fileURLWithPath: path)
431427
XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)")
432-
XCTAssertEqual(path, url.path, "path from file path URL is wrong")
428+
XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong")
433429

434430
// test with name relative to current working directory
435431
path = TestURL.gFileDoesNotExistName
@@ -473,31 +469,31 @@ class TestURL : XCTestCase {
473469
XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)")
474470
url = NSURL(fileURLWithPath: path, isDirectory: false)
475471
XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)")
476-
XCTAssertEqual(path, url.path, "path from file path URL is wrong")
472+
XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong")
477473

478474
// test with file that doesn't exist
479475
path = TestURL.gFileDoesNotExistPath
480476
url = NSURL(fileURLWithPath: path, isDirectory: true)
481477
XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)")
482478
url = NSURL(fileURLWithPath: path, isDirectory: false)
483479
XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)")
484-
XCTAssertEqual(path, url.path, "path from file path URL is wrong")
480+
XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong")
485481

486482
// test with directory that exists
487483
path = TestURL.gDirectoryExistsPath
488484
url = NSURL(fileURLWithPath: path, isDirectory: false)
489485
XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)")
490486
url = NSURL(fileURLWithPath: path, isDirectory: true)
491487
XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)")
492-
XCTAssertEqual(path, url.path, "path from file path URL is wrong")
488+
XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong")
493489

494490
// test with directory that doesn't exist
495491
path = TestURL.gDirectoryDoesNotExistPath
496492
url = NSURL(fileURLWithPath: path, isDirectory: false)
497493
XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)")
498494
url = NSURL(fileURLWithPath: path, isDirectory: true)
499495
XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)")
500-
XCTAssertEqual(path, url.path, "path from file path URL is wrong")
496+
XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong")
501497

502498
// test with name relative to current working directory
503499
path = TestURL.gFileDoesNotExistName

Tests/Foundation/TestURLSession.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,8 +1022,10 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable {
10221022
}
10231023
}
10241024

1025-
// temporarily disabled (https://bugs.swift.org/browse/SR-5751)
1026-
func test_httpRedirectionTimeout() async {
1025+
func test_httpRedirectionTimeout() async throws {
1026+
#if os(Windows)
1027+
throw XCTSkip("temporarily disabled (https://bugs.swift.org/browse/SR-5751)")
1028+
#else
10271029
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates"
10281030
var req = URLRequest(url: URL(string: urlString)!)
10291031
req.timeoutInterval = 3
@@ -1041,6 +1043,7 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable {
10411043
}
10421044
task.resume()
10431045
waitForExpectations(timeout: 12)
1046+
#endif
10441047
}
10451048

10461049
func test_httpRedirectionChainInheritsTimeoutInterval() async throws {

0 commit comments

Comments
 (0)