summaryrefslogtreecommitdiff
path: root/Test
diff options
context:
space:
mode:
authorGravatar Dan Liew <daniel.liew@imperial.ac.uk>2014-05-27 19:04:58 +0100
committerGravatar Dan Liew <daniel.liew@imperial.ac.uk>2014-05-27 19:04:58 +0100
commit451c92fde0945e18e12fbcce4f07bc6843118ce3 (patch)
tree8234984a484d818106985d38b16ddf5b513591a0 /Test
parent25168d775055ca464bb5cc57120f7caff8924779 (diff)
Added simple python implementation of diff to replace using the
``fc`` tool on windows because it is buggy when tests are run concurrently.
Diffstat (limited to 'Test')
-rw-r--r--Test/README.md5
-rw-r--r--Test/lit.site.cfg7
-rwxr-xr-xTest/pydiff.py100
3 files changed, 108 insertions, 4 deletions
diff --git a/Test/README.md b/Test/README.md
index 98013581..4446aa4f 100644
--- a/Test/README.md
+++ b/Test/README.md
@@ -110,8 +110,9 @@ The RUN lines may use several substitutions
- ``%boogie`` expands to the absolute path to the Boogie executable with any set
options and prefixed by ``mono`` on non Windows platforms
-- ``%diff`` expands to the diff tool being used. This is ``diff`` on non Windows
- platforms and ``fc`` on Windows.
+- ``%diff`` expands to the diff tool being used. This is ``diff`` on non
+ Windows platforms and ``pydiff`` on Windows. Do not use the ``fc`` tool
+ because it is buggy when tests are run concurrently.
- ``%OutputCheck`` expands to the absolute path to the OutputCheck tool
diff --git a/Test/lit.site.cfg b/Test/lit.site.cfg
index 9cd85ef6..e103edde 100644
--- a/Test/lit.site.cfg
+++ b/Test/lit.site.cfg
@@ -111,13 +111,16 @@ else:
lit_config.warning('Skipping solver sanity check on Windows')
# Add diff tool substitution
+commonDiffFlags=' --unified=3 --strip-trailing-cr --ignore-all-space'
diffExecutable = None
if os.name == 'posix':
- diffExecutable = 'diff --unified=3 --strip-trailing-cr --ignore-all-space'
+ diffExecutable = 'diff' + commonDiffFlags
elif os.name == 'nt':
- diffExecutable = 'fc /W'
+ pydiff = os.path.join(config.test_source_root, 'pydiff.py')
+ diffExecutable = sys.executable + ' ' + pydiff + commonDiffFlags
else:
lit_config.fatal('Unsupported platform')
+lit_config.note("Using diff tool '{}'".format(diffExecutable))
config.substitutions.append( ('%diff', diffExecutable ))
diff --git a/Test/pydiff.py b/Test/pydiff.py
new file mode 100755
index 00000000..407fe251
--- /dev/null
+++ b/Test/pydiff.py
@@ -0,0 +1,100 @@
+#!/usr/bin/env python
+"""
+Simple driver around python's difflib that implements a basic ``diff`` like
+tool. This exists to provide a simple ``diff`` like tool that will run on
+Windows where only the ``fc`` tool is available.
+"""
+import argparse
+import difflib
+import os
+import sys
+
+
+def main(args):
+ parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
+ description=__doc__
+ )
+ # Open in binary mode so python doesn't try and do
+ # universal line endings for us.
+ parser.add_argument('from-file',
+ type=argparse.FileType('rb'),
+ )
+ parser.add_argument('to-file',
+ type=argparse.FileType('rb'),
+ )
+ parser.add_argument('-U','--unified=',
+ type=int,
+ default=3,
+ help='Number of context lines to show. '
+ 'Default %(default)s'
+ )
+ parser.add_argument('--strip-trailing-cr',
+ action='store_true',
+ help='strip trailing carriage return when comparing'
+ )
+ parser.add_argument('--ignore-all-space','-w',
+ action='store_true',
+ help='Ignore all whitespace characters when comparing'
+ )
+
+ parsedArgs = parser.parse_args(args)
+ fromFile, fromFileName = preProcess(getattr(parsedArgs,'from-file'),
+ parsedArgs.strip_trailing_cr,
+ parsedArgs.ignore_all_space
+ )
+ toFile, toFileName = preProcess(getattr(parsedArgs,'to-file'),
+ parsedArgs.strip_trailing_cr,
+ parsedArgs.ignore_all_space
+ )
+
+ result = difflib.unified_diff(fromFile,
+ toFile,
+ fromFileName,
+ toFileName,
+ n=getattr(parsedArgs,'unified='),
+ )
+ # Force lazy computation to happen now
+ result = list(result)
+
+ if len(result) == 0:
+ # Files are identical
+ return 0
+ else:
+ for l in result:
+ sys.stdout.write(l)
+ return 1
+
+def preProcess(openFile, stripTrailingCR=False, ignoreAllSpace=False):
+ """
+ Helper function to read lines in a file and do any necessary
+ pre-processing
+ """
+ original = openFile.readlines()
+
+ # Translation table deletes white space characters Note we don't remove
+ # newline characters because this will create a mess when outputting the
+ # diff. Is this the right behaviour?
+ deleteChars=' \t'
+ translationTable = str.maketrans('','', deleteChars)
+
+ copy = [ ]
+ for line in original:
+ newLine = str(line.decode())
+
+ if stripTrailingCR:
+ if newLine[-2:] == '\r\n' or newLine[-2:] == '\n\r':
+ newLine = newLine[:-2] + '\n'
+
+ if ignoreAllSpace:
+ newLine = newLine.translate(translationTable)
+
+ copy.append(newLine)
+
+ return (copy, openFile.name)
+
+def getFileName(openFile):
+ return openFile.name
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv[1:]))
+