This commit is contained in:
Timothee Cour
2021-04-29 00:33:01 -07:00
parent 969823b8de
commit d75ebeadce
2 changed files with 17 additions and 10 deletions

View File

@@ -39,7 +39,6 @@ proc isGitRepo*(dir: string): bool =
# remove trailing whitespaces from the result.
result = status == 0 and output.strip() == ""
proc diffStrings*(a, b: string): string =
runnableExamples:
let a = "ok1\nok2\nok3"
@@ -48,10 +47,10 @@ proc diffStrings*(a, b: string): string =
echo c
template tmpFileImpl(prefix, str): auto =
# pending
let (fd, path) = createTempFile(prefix, "")
# pending https://github.com/nim-lang/Nim/pull/17889
# let (fd, path) = createTempFile(prefix, "")
let path = genTempPath(prefix, "")
defer:
close fd
removeFile(path)
writeFile(path, str)
(fd, path)

View File

@@ -90,13 +90,21 @@ template randomPathName(length: Natural): string =
res[i] = state.sample(letters)
res
proc genTempPath*(prefix, suffix: string, dir = ""): string =
## Generates a path name in `dir`.
##
## If `dir` is empty, (`getTempDir <os.html#getTempDir>`_) will be used.
## The path begins with `prefix` and ends with `suffix`.
var dir = dir
if dir.len == 0:
dir = getTempDir()
result = dir / (prefix & randomPathName(nimTempPathLength) & suffix)
proc createTempFile*(prefix, suffix: string, dir = ""): tuple[fd: File, path: string] =
## `createTempFile` creates a new temporary file in the directory `dir`.
## Creates a new temporary file in the directory `dir`.
##
## If `dir` is the empty string, the default directory for temporary files
## (`getTempDir <os.html#getTempDir>`_) will be used.
## The temporary file name begins with `prefix` and ends with `suffix`.
## `createTempFile` returns a file handle to an open file and the path of that file.
## This generates a path name using `genTempPath(prefix, suffix, dir)` and
## returns a file handle to an open file and the path of that file.
##
## If failing to create a temporary file, `IOError` will be raised.
##
@@ -108,7 +116,7 @@ proc createTempFile*(prefix, suffix: string, dir = ""): tuple[fd: File, path: st
createDir(dir)
for i in 0 ..< maxRetry:
result.path = dir / (prefix & randomPathName(nimTempPathLength) & suffix)
result.path = createTempPath(prefix, suffix, dir)
try:
result.fd = safeOpen(result.path)
except OSError: