This commit is contained in:
Timothee Cour
2021-04-29 02:17:28 -07:00
parent 6525d9d592
commit a192bbfd7e
6 changed files with 32 additions and 18 deletions

View File

@@ -39,14 +39,27 @@ proc isGitRepo*(dir: string): bool =
# remove trailing whitespaces from the result.
result = status == 0 and output.strip() == ""
proc diffStrings*(a, b: string): string =
proc diffFiles*(path1, path2: string): tuple[output: string, same: bool] =
# 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.
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] =
runnableExamples:
let a = "ok1\nok2\nok3"
let b = "ok1\nok2 alt\nok3"
let c = diffStrings(a, b)
echo c
let c2 = diffStrings(a, a)
echo c2
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 =
# pending https://github.com/nim-lang/Nim/pull/17889
@@ -59,7 +72,4 @@ proc diffStrings*(a, b: string): string =
defer:
removeFile(patha)
removeFile(pathb)
# could be customized, e.g. non-git diff with `diff -uNdr`, or with git diff options.
var status = 0
(result, status) = execCmdEx("git diff --no-index $1 $2" % [patha.quoteShell, pathb.quoteShell])
echo status
result = diffFiles(patha, pathb)

View File

@@ -1,4 +1,5 @@
import os, strutils
from std/private/gitutils import diffFiles
const
baseDir = "nimdoc/rst2html"
@@ -19,7 +20,7 @@ proc testRst2Html(fixup = false) =
exec("$1 rst2html $2" % [nimExe, sourceFile])
let producedHtml = expectedHtml.replace('\\', '/').replace("/expected/", "/source/htmldocs/")
if readFile(expectedHtml) != readFile(producedHtml):
discard execShellCmd("diff -uNdr " & expectedHtml & " " & producedHtml)
echo diffFiles(expectedHtml, producedHtml).output
inc failures
if fixup:
copyFile(producedHtml, expectedHtml)

View File

@@ -3,6 +3,7 @@
# to change expected results (after carefully verifying everything), use -d:fixup
import strutils, os
from std/private/gitutils import diffFiles
var
failures = 0
@@ -40,7 +41,7 @@ proc testNimDoc(prjDir, docsDir: string; switches: NimSwitches; fixup = false) =
inc failures
elif readFile(expected) != readFile(produced):
echo "FAILURE: files differ: ", produced
discard execShellCmd("diff -uNdr " & expected & " " & produced)
echo diffFiles(expected, produced).output
inc failures
if fixup:
copyFile(produced, expected)

View File

@@ -1,6 +1,7 @@
# Small program that runs the test cases
import strutils, os, sequtils
from std/private/gitutils import diffFiles
const
dir = "nimpretty/tests"
@@ -26,7 +27,7 @@ proc test(infile, ext: string) =
let produced = dir / nimFile.changeFileExt(ext)
if readFile(expected) != readFile(produced):
echo "FAILURE: files differ: ", nimFile
discard execShellCmd("diff -uNdr " & expected & " " & produced)
echo diffFiles(expected, produced).output
failures += 1
else:
echo "SUCCESS: files identical: ", nimFile
@@ -43,7 +44,7 @@ proc testTogether(infiles: seq[string]) =
let produced = dir / "outputdir" / infile
if readFile(expected) != readFile(produced):
echo "FAILURE: files differ: ", nimFile
discard execShellCmd("diff -uNdr " & expected & " " & produced)
echo diffFiles(expected, produced).output
failures += 1
else:
echo "SUCCESS: files identical: ", nimFile

View File

@@ -665,8 +665,8 @@ proc runJoinedTest(r: var TResults, cat: Category, testsDir: string, options: st
if buf != outputExpected:
writeFile(outputExceptedFile, outputExpected)
discard execShellCmd("diff -uNdr $1 $2" % [outputExceptedFile, outputGottenFile])
echo failString & "megatest output different!"
echo diffFiles(outputGottenFile, outputExceptedFile).output
echo failString & "megatest output different, see $1 vs $2" % [outputGottenFile, outputExceptedFile]
# outputGottenFile, outputExceptedFile not removed on purpose for debugging.
quit 1
else:

View File

@@ -17,6 +17,7 @@ from std/sugar import dup
import compiler/nodejs
import lib/stdtest/testutils
from lib/stdtest/specialpaths import splitTestFile
from std/private/gitutils import diffStrings
proc trimUnitSep(x: var string) =
let L = x.len
@@ -307,7 +308,7 @@ proc addResult(r: var TResults, test: TTest, target: TTarget,
maybeStyledEcho styleBright, expected, "\n"
maybeStyledEcho fgYellow, "Gotten:"
maybeStyledEcho styleBright, given, "\n"
echo diffStrings(expected, given).output
if backendLogging and (isAppVeyor or isAzure):
let (outcome, msg) =