summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-03-17 00:25:28 -0700
committerGitHub <noreply@github.com>2021-03-17 08:25:28 +0100
commitbebf2ce24a43bef4cde5c90c4010631a1e4a7927 (patch)
tree0cb8664e6e99ea983f787dd0d943bd9eb6f541eb /lib
parent4d3f3513e2695d4bcb8ffef2521ff7abcce70bdf (diff)
downloadNim-bebf2ce24a43bef4cde5c90c4010631a1e4a7927.tar.gz
fix #17393 getHomeDir and expandTilde should not include trailing `/` (#17398)
* fix #17393 getHomeDir and expandTilde should not include trailing `/`

* changelog
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/os.nim14
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index 54239e858..00a5f9342 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -901,9 +901,14 @@ proc getHomeDir*(): string {.rtl, extern: "nos$1",
   ## * `setCurrentDir proc <#setCurrentDir,string>`_
   runnableExamples:
     assert getHomeDir() == expandTilde("~")
+    # `getHomeDir()` doesn't end in `DirSep` even if `$HOME` (on posix) or
+    # `$USERPROFILE` (on windows) does, unless `-d:nimLegacyHomeDir` is specified.
+    from std/strutils import endsWith
+    assert not getHomeDir().endsWith DirSep
 
-  when defined(windows): return getEnv("USERPROFILE") & "\\"
-  else: return getEnv("HOME") & "/"
+  when defined(windows): result = getEnv("USERPROFILE")
+  else: result = getEnv("HOME")
+  result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir))
 
 proc getConfigDir*(): string {.rtl, extern: "nos$1",
   tags: [ReadEnvEffect, ReadIOEffect].} =
@@ -999,6 +1004,8 @@ proc expandTilde*(path: string): string {.
   ##
   ## Windows: this is still supported despite Windows platform not having this
   ## convention; also, both ``~/`` and ``~\`` are handled.
+  ## 
+  ## .. warning:: `~bob` and `~bob/` are not yet handled correctly.
   ##
   ## See also:
   ## * `getHomeDir proc <#getHomeDir>`_
@@ -1010,6 +1017,9 @@ proc expandTilde*(path: string): string {.
     assert expandTilde("~" / "appname.cfg") == getHomeDir() / "appname.cfg"
     assert expandTilde("~/foo/bar") == getHomeDir() / "foo/bar"
     assert expandTilde("/foo/bar") == "/foo/bar"
+    assert expandTilde("~") == getHomeDir()
+    from std/strutils import endsWith
+    assert not expandTilde("~").endsWith(DirSep)
 
   if len(path) == 0 or path[0] != '~':
     result = path