normalizeExe (#14668)

This commit is contained in:
Timothee Cour 2020-06-15 01:57:34 -07:00 • committed by GitHub
commit bf604c6829
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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``. - 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 `rstgen.rstToLatex` convenience proc for `renderRstToOut` and `initRstGenerator` with `outLatex` output.
- Add `os.normalizeExe`, eg: `koch` => `./koch`.
## Language changes ## Language changes

View file

@ -1407,6 +1407,18 @@ proc absolutePath*(path: string, root = getCurrentDir()): string =
proc absolutePathInternal(path: string): string = proc absolutePathInternal(path: string): string =
absolutePath(path, getCurrentDir()) 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: [].} = proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [].} =
## Normalize a path. ## Normalize a path.
## ##
@ -1420,8 +1432,8 @@ proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [].} =
## ##
## See also: ## See also:
## * `absolutePath proc <#absolutePath,string>`_ ## * `absolutePath proc <#absolutePath,string>`_
## * `normalizedPath proc <#normalizedPath,string>`_ for a version which returns ## * `normalizedPath proc <#normalizedPath,string>`_ for outplace version
## a new string ## * `normalizeExe proc <#normalizeExe,string>`_
runnableExamples: runnableExamples:
when defined(posix): when defined(posix):
var a = "a///b//..//c///d" var a = "a///b//..//c///d"

View file

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

View file

@ -510,3 +510,13 @@ block: # isValidFilename
doAssert isValidFilename("ux.bat") doAssert isValidFilename("ux.bat")
doAssert isValidFilename("nim.nim") doAssert isValidFilename("nim.nim")
doAssert isValidFilename("foo.log") 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"