Skip to content

Commit 3cebf23

Browse files
committed
[android] Fix URL tests that use hardcoded /tmp
In Android /tmp doesn't exist, and the temporary directory is normally /data/local/tmp. It is not a symlink. The tests were mostly working, but because `resolvingSymlinksInPath` returns a trailing slash if the directory actually exists, in Android it was returning without the final trailing slash in some of the cases. The changes use the value of `NSTemporaryDirectory` in Android only, and also uses that value for the expected value. Using an existing directory matches the tests from Darwin/Linux. I didn't want to use `NSTemporaryDirectory` in every platform because at least in Darwin I get a value which is not neither /tmp nor /private/tmp.
1 parent ec5e44e commit 3cebf23

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

TestFoundation/TestURL.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,20 @@ class TestURL : XCTestCase {
394394
}
395395

396396
// tmp is special because it is symlinked to /private/tmp and this /private prefix should be dropped,
397-
// so tmp is tmp. On Linux tmp is not symlinked so it would be the same.
397+
// so tmp is tmp. On Linux tmp is not symlinked so it would be the same. In Android it doesn't exist
398+
// so we have to use NSTemporaryDirectory to be safe.
399+
#if !os(Android)
400+
let tmpDirectoryPath = "/tmp"
401+
let expectedTmpURLPrefix = "file:///tmp"
402+
#else
403+
// We don't want the trailing slash to match other platforms
404+
let tmpDirectoryPath = NSTemporaryDirectory().dropLast(1)
405+
let expectedTmpURLPrefix = "file://\(NSTemporaryDirectory())".dropLast(1)
406+
#endif
398407
do {
399-
let url = URL(fileURLWithPath: "/.//tmp/ABC/..")
408+
let url = URL(fileURLWithPath: "/./\(tmpDirectoryPath)/ABC/..")
400409
let result = url.resolvingSymlinksInPath().absoluteString
401-
XCTAssertEqual(result, "file:///tmp/", "URLByResolvingSymlinksInPath removes extraneous path components and resolve symlinks.")
410+
XCTAssertEqual(result, "\(expectedTmpURLPrefix)/", "URLByResolvingSymlinksInPath removes extraneous path components and resolve symlinks.")
402411
}
403412

404413
do {
@@ -418,22 +427,22 @@ class TestURL : XCTestCase {
418427
// tmp is symlinked on macOS only
419428
#if os(macOS)
420429
do {
421-
let url = URL(fileURLWithPath: "/tmp/..")
430+
let url = URL(fileURLWithPath: "\(tmpDirectoryPath)/..")
422431
let result = url.resolvingSymlinksInPath().absoluteString
423432
XCTAssertEqual(result, "file:///private/")
424433
}
425434
#else
426435
do {
427-
let url = URL(fileURLWithPath: "/tmp/ABC/test_URLByResolvingSymlinksInPath")
436+
let url = URL(fileURLWithPath: "\(tmpDirectoryPath)/ABC/test_URLByResolvingSymlinksInPath")
428437
let result = url.resolvingSymlinksInPath().absoluteString
429-
XCTAssertEqual(result, "file:///tmp/ABC/test_URLByResolvingSymlinksInPath", "URLByResolvingSymlinksInPath appends trailing slash for existing directories only")
438+
XCTAssertEqual(result, "\(expectedTmpURLPrefix)/ABC/test_URLByResolvingSymlinksInPath", "URLByResolvingSymlinksInPath appends trailing slash for existing directories only")
430439
}
431440
#endif
432441

433442
do {
434-
let url = URL(fileURLWithPath: "/tmp/ABC/..")
443+
let url = URL(fileURLWithPath: "\(tmpDirectoryPath)/ABC/..")
435444
let result = url.resolvingSymlinksInPath().absoluteString
436-
XCTAssertEqual(result, "file:///tmp/")
445+
XCTAssertEqual(result, "\(expectedTmpURLPrefix)/")
437446
}
438447
}
439448

0 commit comments

Comments
 (0)