Skip to content

Commit d1eb384

Browse files
committed
Expand both ~/ and ~\ regardless of OS
1 parent 3862b89 commit d1eb384

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/main/kotlin/com/coder/gateway/util/PathExtensions.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ fun expand(path: String): String {
3131
if (path == "~" || path == "\$HOME" || path == "\${user.home}") {
3232
return System.getProperty("user.home")
3333
}
34-
if (path.startsWith("~" + File.separator)) {
34+
if (path.startsWith("~/") || path.startsWith("~\\")) {
3535
return Path.of(System.getProperty("user.home"), path.substring(1)).toString()
3636
}
37-
if (path.startsWith("\$HOME" + File.separator)) {
37+
if (path.startsWith("\$HOME/") || path.startsWith("\$HOME\\")) {
3838
return Path.of(System.getProperty("user.home"), path.substring(5)).toString()
3939
}
40-
if (path.startsWith("\${user.home}" + File.separator)) {
40+
if (path.startsWith("\${user.home}/" || path.startsWith("\${user.home}\\"))) {
4141
return Path.of(System.getProperty("user.home"), path.substring(12)).toString()
4242
}
4343
return path

src/test/kotlin/com/coder/gateway/util/PathExtensionsTest.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ internal class PathExtensionsTest {
107107

108108
// Do not replace if part of a larger string.
109109
assertEquals(home, expand(it))
110-
assertEquals(home, expand(it + File.separator))
110+
assertEquals(home, expand(it + "/"))
111+
assertEquals(home, expand(it + "\\"))
111112
assertEquals(it + "hello", expand(it + "hello"))
113+
assertEquals(it + "hello/foo", expand(it + "hello/foo"))
114+
assertEquals(it + "hello\\foo", expand(it + "hello\\foo"))
112115
}
113116
}
114117
}

0 commit comments

Comments
 (0)