gitutils: add diffStrings, diffFiles, and use it in testament to compare expected vs gotten (#17892)
* gitutils: add diffStrings, diffFiles, and use it in testament to compare expected vs gotten * refactor with createTempDir * cleanup * refacotr * PRTEMP fake test spec changes to show effect of diffStrings * add runnableExamples for experimental/diff + cross-reference with gitutils * Revert "PRTEMP fake test spec changes to show effect of diffStrings" This reverts commit 57dc8d642dce6c1127c98b7cbc9edbfe747d4047.
This commit is contained in:
parent
1640508348
commit
20248a68fd
8 changed files with 99 additions and 52 deletions
|
|
@ -4,7 +4,7 @@ internal API for now, API subject to change
|
|||
|
||||
# xxx move other git utilities here; candidate for stdlib.
|
||||
|
||||
import std/[os, osproc, strutils]
|
||||
import std/[os, osproc, strutils, tempfiles]
|
||||
|
||||
const commitHead* = "HEAD"
|
||||
|
||||
|
|
@ -38,3 +38,41 @@ proc isGitRepo*(dir: string): bool =
|
|||
# usually a series of ../), so we know that it's safe to unconditionally
|
||||
# remove trailing whitespaces from the result.
|
||||
result = status == 0 and output.strip() == ""
|
||||
|
||||
proc diffFiles*(path1, path2: string): tuple[output: string, same: bool] =
|
||||
## Returns a human readable diff of files `path1`, `path2`, the exact form of
|
||||
## which is implementation defined.
|
||||
# This could be customized, e.g. non-git diff with `diff -uNdr`, or with
|
||||
# git diff options (e.g. --color-moved, --word-diff).
|
||||
# in general, `git diff` has more options than `diff`.
|
||||
var status = 0
|
||||
(result.output, status) = execCmdEx("git diff --no-index $1 $2" % [path1.quoteShell, path2.quoteShell])
|
||||
doAssert (status == 0) or (status == 1)
|
||||
result.same = status == 0
|
||||
|
||||
proc diffStrings*(a, b: string): tuple[output: string, same: bool] =
|
||||
## Returns a human readable diff of `a`, `b`, the exact form of which is
|
||||
## implementation defined.
|
||||
## See also `experimental.diff`.
|
||||
runnableExamples:
|
||||
let a = "ok1\nok2\nok3\n"
|
||||
let b = "ok1\nok2 alt\nok3\nok4\n"
|
||||
let (c, same) = diffStrings(a, b)
|
||||
doAssert not same
|
||||
let (c2, same2) = diffStrings(a, a)
|
||||
doAssert same2
|
||||
runnableExamples("-r:off"):
|
||||
let a = "ok1\nok2\nok3\n"
|
||||
let b = "ok1\nok2 alt\nok3\nok4\n"
|
||||
echo diffStrings(a, b).output
|
||||
|
||||
template tmpFileImpl(prefix, str): auto =
|
||||
let path = genTempPath(prefix, "")
|
||||
writeFile(path, str)
|
||||
path
|
||||
let patha = tmpFileImpl("diffStrings_a_", a)
|
||||
let pathb = tmpFileImpl("diffStrings_b_", b)
|
||||
defer:
|
||||
removeFile(patha)
|
||||
removeFile(pathb)
|
||||
result = diffFiles(patha, pathb)
|
||||
|
|
|
|||
|
|
@ -90,25 +90,33 @@ template randomPathName(length: Natural): string =
|
|||
res[i] = state.sample(letters)
|
||||
res
|
||||
|
||||
proc getTempDirImpl(dir: string): string {.inline.} =
|
||||
result = dir
|
||||
if result.len == 0:
|
||||
result = getTempDir()
|
||||
|
||||
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`.
|
||||
let dir = getTempDirImpl(dir)
|
||||
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, possibly after
|
||||
## retrying to ensure it doesn't already exist.
|
||||
##
|
||||
## If failing to create a temporary file, `IOError` will be raised.
|
||||
##
|
||||
## .. note:: It is the caller's responsibility to remove the file when no longer needed.
|
||||
var dir = dir
|
||||
if dir.len == 0:
|
||||
dir = getTempDir()
|
||||
|
||||
let dir = getTempDirImpl(dir)
|
||||
createDir(dir)
|
||||
|
||||
for i in 0 ..< maxRetry:
|
||||
result.path = dir / (prefix & randomPathName(nimTempPathLength) & suffix)
|
||||
result.path = genTempPath(prefix, suffix, dir)
|
||||
try:
|
||||
result.fd = safeOpen(result.path)
|
||||
except OSError:
|
||||
|
|
@ -118,25 +126,19 @@ proc createTempFile*(prefix, suffix: string, dir = ""): tuple[fd: File, path: st
|
|||
raise newException(IOError, "Failed to create a temporary file under directory " & dir)
|
||||
|
||||
proc createTempDir*(prefix, suffix: string, dir = ""): string =
|
||||
## `createTempDir` creates a new temporary directory in the directory `dir`.
|
||||
## Creates a new temporary directory 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 directory name begins with `prefix` and ends with `suffix`.
|
||||
## `createTempDir` returns the path of that temporary firectory.
|
||||
## This generates a dir name using `genTempPath(prefix, suffix, dir)`, creates
|
||||
## the directory and returns it, possibly after retrying to ensure it doesn't
|
||||
## already exist.
|
||||
##
|
||||
## If failing to create a temporary directory, `IOError` will be raised.
|
||||
##
|
||||
## .. note:: It is the caller's responsibility to remove the directory when no longer needed.
|
||||
##
|
||||
var dir = dir
|
||||
if dir.len == 0:
|
||||
dir = getTempDir()
|
||||
|
||||
let dir = getTempDirImpl(dir)
|
||||
createDir(dir)
|
||||
|
||||
for i in 0 ..< maxRetry:
|
||||
result = dir / (prefix & randomPathName(nimTempPathLength) & suffix)
|
||||
result = genTempPath(prefix, suffix, dir)
|
||||
try:
|
||||
if not existsOrCreateDir(result):
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue