normalizeExe (#14668)

This commit is contained in:
Timothee Cour
2020-06-15 01:57:34 -07:00
committed by GitHub
parent 3cbf59336d
commit bf604c6829
4 changed files with 28 additions and 10 deletions

View File

@@ -112,6 +112,7 @@
- new proc `heapqueue.find[T](heap: HeapQueue[T], x: T): int` to get index of element ``x``.
- Add `rstgen.rstToLatex` convenience proc for `renderRstToOut` and `initRstGenerator` with `outLatex` output.
- Add `os.normalizeExe`, eg: `koch` => `./koch`.
## Language changes

View File

@@ -1407,6 +1407,18 @@ proc absolutePath*(path: string, root = getCurrentDir()): string =
proc absolutePathInternal(path: string): string =
absolutePath(path, getCurrentDir())
proc normalizeExe*(file: var string) {.since: (1, 3, 5).} =
## on posix, prepends `./` if `file` doesn't contain `/` and is not `"", ".", ".."`.
runnableExamples:
import sugar
when defined(posix):
doAssert "foo".dup(normalizeExe) == "./foo"
doAssert "foo/../bar".dup(normalizeExe) == "foo/../bar"
doAssert "".dup(normalizeExe) == ""
when defined(posix):
if file.len > 0 and DirSep notin file and file != "." and file != "..":
file = "./" & file
proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [].} =
## Normalize a path.
##
@@ -1420,8 +1432,8 @@ proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [].} =
##
## See also:
## * `absolutePath proc <#absolutePath,string>`_
## * `normalizedPath proc <#normalizedPath,string>`_ for a version which returns
## a new string
## * `normalizedPath proc <#normalizedPath,string>`_ for outplace version
## * `normalizeExe proc <#normalizeExe,string>`_
runnableExamples:
when defined(posix):
var a = "a///b//..//c///d"

View File

@@ -13,8 +13,8 @@ import
strutils, pegs, os, osproc, streams, json,
backend, parseopt, specs, htmlgen, browsers, terminal,
algorithm, times, md5, sequtils, azure
include compiler/nodejs
from std/sugar import dup
import compiler/nodejs
var useColors = true
var backendLogging = true
@@ -450,12 +450,7 @@ proc testSpecHelper(r: var TResults, test: TTest, expected: TSpec,
exeCmd = nodejs
args = concat(@[exeFile], args)
else:
if defined(posix) and not exeFile.contains('/'):
# "security" in Posix is actually just a euphemism
# for "unproductive arbitrary shit"
exeCmd = "./" & exeFile
else:
exeCmd = exeFile
exeCmd = exeFile.dup(normalizeExe)
if expected.useValgrind:
args = @["--error-exitcode=1"] & exeCmd & args
exeCmd = "valgrind"

View File

@@ -510,3 +510,13 @@ block: # isValidFilename
doAssert isValidFilename("ux.bat")
doAssert isValidFilename("nim.nim")
doAssert isValidFilename("foo.log")
import sugar
block: # normalizeExe
doAssert "".dup(normalizeExe) == ""
when defined(posix):
doAssert "foo".dup(normalizeExe) == "./foo"
doAssert "foo/../bar".dup(normalizeExe) == "foo/../bar"
when defined(windows):
doAssert "foo".dup(normalizeExe) == "foo"