Skip to content

Commit 9bc82e9

Browse files
committed
Add DIFF_COMPARE_OPTIONS to pxr_register_test()
1 parent d1ebe6f commit 9bc82e9

File tree

3 files changed

+135
-52
lines changed

3 files changed

+135
-52
lines changed

cmake/macros/Public.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ function(pxr_register_test TEST_NAME)
733733
EXPECTED_RETURN_CODE
734734
TESTENV
735735
WARN WARN_PERCENT HARD_WARN FAIL FAIL_PERCENT HARD_FAIL)
736-
set(MULTI_VALUE_ARGS DIFF_COMPARE IMAGE_DIFF_COMPARE ENV PRE_PATH POST_PATH)
736+
set(MULTI_VALUE_ARGS DIFF_COMPARE DIFF_COMPARE_OPTIONS IMAGE_DIFF_COMPARE ENV PRE_PATH POST_PATH)
737737

738738
cmake_parse_arguments(bt
739739
"${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}"
@@ -805,8 +805,10 @@ function(pxr_register_test TEST_NAME)
805805
set(testWrapperCmd ${testWrapperCmd} --testenv-dir=${testenvDir})
806806

807807
if (bt_DIFF_COMPARE)
808+
set(compareOptions ${bt_DIFF_COMPARE_OPTIONS})
809+
list(TRANSFORM compareOptions PREPEND --diff-compare-options=)
808810
foreach(compareFile ${bt_DIFF_COMPARE})
809-
set(testWrapperCmd ${testWrapperCmd} --diff-compare=${compareFile})
811+
set(testWrapperCmd ${testWrapperCmd} --diff-compare=${compareFile} ${compareOptions})
810812
endforeach()
811813
endif()
812814

cmake/macros/testWrapper.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import argparse
2525
import glob
2626
import os
27+
import itertools
2728
import platform
2829
import shlex
2930
import shutil
@@ -41,6 +42,9 @@ def _parseArgs():
4142
action='append',
4243
help=('Compare output file with a file in the baseline-dir of the '
4344
'same name'))
45+
parser.add_argument('--diff-compare-options', default=[], type=str,
46+
action='append',
47+
help='Options for the diff tool')
4448
parser.add_argument('--image-diff-compare', default=[], type=str,
4549
action='append',
4650
help=('Compare output image with an image in the baseline '
@@ -153,25 +157,45 @@ def _cleanOutput(pathPattern, fileName, verbose):
153157
_stripPath(fileName, pathPattern)
154158
return True
155159

156-
def _diff(fileName, baselineDir, verbose, failuresDir=None):
160+
def _diff(fileName, baselineDir, diffOptions, verbose, failuresDir=None):
157161
# Use the diff program or equivalent, rather than filecmp or similar
158162
# because it's possible we might want to specify other diff programs
159163
# in the future.
164+
160165
import platform
161-
if platform.system() == 'Windows':
162-
diff = 'fc.exe'
163-
else:
164-
diff = '/usr/bin/diff'
166+
isWindows = platform.system() == 'Windows';
167+
168+
diffTool = shutil.which('diff')
169+
if not diffTool and isWindows:
170+
gitExe = shutil.which('git')
171+
if gitExe:
172+
# On Windows, diff is bundled with Git.
173+
# git.exe should be found here: <prefix>/Git/cmd/git.exe
174+
# And diff.exe should be here: <prefix>/Git/usr/bin/diff.exe
175+
gitToolsPath = os.path.normpath(os.path.join(os.path.dirname(gitExe),
176+
'..', 'usr', 'bin'))
177+
diffTool = shutil.which('diff', path=gitToolsPath)
178+
179+
if not diffTool:
180+
sys.stderr.write(
181+
"Error: could not find \"diff\" tool. Make sure it's in your PATH.\n")
182+
return False
165183

166184
filesToDiff = glob.glob(fileName)
167185
if not filesToDiff:
168186
sys.stderr.write(
169187
"Error: could not files matching {0} to diff".format(fileName))
170188
return False
171189

190+
191+
options = list(itertools.chain.from_iterable([shlex.split(option)
192+
for option in diffOptions]))
193+
if isWindows:
194+
options.append('--strip-trailing-cr')
195+
172196
for fileToDiff in filesToDiff:
173197
baselineFile = _resolvePath(baselineDir, fileToDiff)
174-
cmd = [diff, baselineFile, fileToDiff]
198+
cmd = [diffTool, *options, baselineFile, fileToDiff]
175199
if verbose:
176200
print("diffing with {0}".format(cmd))
177201

@@ -413,8 +437,8 @@ def _runCommand(raw_command, stdout_redir, stderr_redir, env,
413437
os.environ.get('PXR_CTEST_RUN_ID', 'NOT_RUN_FROM_CTEST'))
414438
if args.diff_compare:
415439
for diff in args.diff_compare:
416-
if not _diff(diff, args.baseline_dir, args.verbose,
417-
failuresDir=failuresDir):
440+
if not _diff(diff, args.baseline_dir, args.diff_compare_options,
441+
args.verbose, failuresDir=failuresDir):
418442
sys.stderr.write('Error: diff for {0} failed '
419443
'(DIFF_COMPARE).'.format(diff))
420444
sys.exit(1)

0 commit comments

Comments
 (0)