Skip to content

Commit 351ed85

Browse files
committed
Expand ~/ on Windows
1 parent 3862b89 commit 351ed85

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ 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+
// On Windows also allow /. Windows seems to work fine with mixed slashes
35+
// like c:\users\coder/my/path/here.
36+
val os = getOS()
37+
if (path.startsWith("~" + File.separator) || (os == OS.WINDOWS && path.startsWith("~/"))) {
3538
return Path.of(System.getProperty("user.home"), path.substring(1)).toString()
3639
}
37-
if (path.startsWith("\$HOME" + File.separator)) {
40+
if (path.startsWith("\$HOME" + File.separator) || (os == OS.WINDOWS && path.startsWith("\$HOME/"))) {
3841
return Path.of(System.getProperty("user.home"), path.substring(5)).toString()
3942
}
40-
if (path.startsWith("\${user.home}" + File.separator)) {
43+
if (path.startsWith("\${user.home}" + File.separator) || (os == OS.WINDOWS && path.startsWith("\${user.home}/"))) {
4144
return Path.of(System.getProperty("user.home"), path.substring(12)).toString()
4245
}
4346
return path

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

+7
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,14 @@ internal class PathExtensionsTest {
108108
// Do not replace if part of a larger string.
109109
assertEquals(home, expand(it))
110110
assertEquals(home, expand(it + File.separator))
111+
if (isWindows) {
112+
assertEquals(home, expand(it + "/"))
113+
} else {
114+
assertEquals(it + "\\", expand(it + "\\"))
115+
}
111116
assertEquals(it + "hello", expand(it + "hello"))
117+
assertEquals(it + "hello/foo", expand(it + "hello/foo"))
118+
assertEquals(it + "hello\\foo", expand(it + "hello\\foo"))
112119
}
113120
}
114121
}

0 commit comments

Comments
 (0)