Skip to content

Commit 6841f4b

Browse files
committed
[android] Fix tilde tests in Android.
In Android, the home directory is the root directory. Foundation returns the right one, but the tests where checking against having a final trailing slash, which is not true if the home directory is the root directory. The change adds an extra check to avoid the failure in case the resulting tilde expansion is just the root directory. The comment explains why the change is needed. Additionally, instead of using `+` to add two strings, `appendingPathComponent` is used to build an expected value from the result of `NSHomeDirectory()`, in order to deal with cases where the `NSHomeDirectory()` has a trailing slash.
1 parent ec5e44e commit 6841f4b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

TestFoundation/TestNSString.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,25 +1014,30 @@ class TestNSString: LoopbackServerTest {
10141014
}
10151015

10161016
func test_expandingTildeInPath() {
1017+
// Android home directory is the root directory, so the result of ~ may
1018+
// actually have a trailing path separator, but only if it is the root
1019+
// directory itself.
1020+
let rootDirectory = "/"
1021+
10171022
do {
10181023
let path: NSString = "~"
10191024
let result = path.expandingTildeInPath
10201025
XCTAssert(result == NSHomeDirectory(), "Could resolve home directory for current user")
1021-
XCTAssertFalse(result.hasSuffix("/"), "Result have no trailing path separator")
1026+
XCTAssertFalse(result.hasSuffix("/") && result != rootDirectory, "Result should not have a trailing path separator")
10221027
}
10231028

10241029
do {
10251030
let path: NSString = "~/"
10261031
let result = path.expandingTildeInPath
10271032
XCTAssert(result == NSHomeDirectory(), "Could resolve home directory for current user")
1028-
XCTAssertFalse(result.hasSuffix("/"), "Result have no trailing path separator")
1033+
XCTAssertFalse(result.hasSuffix("/") && result != rootDirectory, "Result should not have a trailing path separator")
10291034
}
10301035

10311036
do {
10321037
let path = NSString(string: "~\(NSUserName())")
10331038
let result = path.expandingTildeInPath
10341039
XCTAssert(result == NSHomeDirectory(), "Could resolve home directory for specific user")
1035-
XCTAssertFalse(result.hasSuffix("/"), "Result have no trailing path separator")
1040+
XCTAssertFalse(result.hasSuffix("/") && result != rootDirectory, "Result should not have a trailing path separator")
10361041
}
10371042

10381043
do {
@@ -1064,7 +1069,7 @@ class TestNSString: LoopbackServerTest {
10641069
do {
10651070
let path: NSString = "~/foo/bar/"
10661071
let result = path.standardizingPath
1067-
let expected = NSHomeDirectory() + "/foo/bar"
1072+
let expected = NSHomeDirectory().appendingPathComponent("foo/bar")
10681073
XCTAssertEqual(result, expected, "standardizingPath expanding initial tilde.")
10691074
}
10701075

0 commit comments

Comments
 (0)