add os.absolutePath; fixes #8174 (#8213)

* add os.absolutePath

* fixup

* fixup

* Fixes absolutePath error message.
This commit is contained in:
Timothee Cour
2018-07-10 12:31:13 -07:00
committed by Dominik Picheta
parent f805018461
commit 25bf0d1683

View File

@@ -296,6 +296,26 @@ proc setCurrentDir*(newDir: string) {.inline, tags: [].} =
else:
if chdir(newDir) != 0'i32: raiseOSError(osLastError())
proc absolutePath*(path: string, root = getCurrentDir()): string =
## Returns the absolute path of `path`, rooted at `root` (which must be absolute)
## if `path` is absolute, return it, ignoring `root`
runnableExamples:
doAssert absolutePath("a") == getCurrentDir() / "a"
if isAbsolute(path): path
else:
if not root.isAbsolute:
raise newException(ValueError, "The specified root is not absolute: " & root)
joinPath(root, path)
when isMainModule:
doAssertRaises(ValueError): discard absolutePath("a", "b")
doAssert absolutePath("a") == getCurrentDir() / "a"
doAssert absolutePath("a", "/b") == "/b" / "a"
when defined(Posix):
doAssert absolutePath("a", "/b/") == "/b" / "a"
doAssert absolutePath("a", "/b/c") == "/b/c" / "a"
doAssert absolutePath("/a", "b/") == "/a"
proc expandFilename*(filename: string): string {.rtl, extern: "nos$1",
tags: [ReadDirEffect].} =
## Returns the full (`absolute`:idx:) path of an existing file `filename`,