summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2009-09-26 09:45:43 -0400
committerNed Batchelder <ned@nedbatchelder.com>2009-09-26 09:45:43 -0400
commit19e525685ab858089c05580f90b79da7ff91ccd8 (patch)
treeceb8fc0a2bdd301d899c9c047303dcf22283d263
parent17e6ca309e07186eed46242b1903a84c9bc5e167 (diff)
downloadpython-coveragepy-git-19e525685ab858089c05580f90b79da7ff91ccd8.tar.gz
File comparison can include regex scrubbing to ignore certain parts.
-rw-r--r--test/test_farm.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/test/test_farm.py b/test/test_farm.py
index 01934dfd..a20a9a2d 100644
--- a/test/test_farm.py
+++ b/test/test_farm.py
@@ -1,6 +1,6 @@
"""Run tests in the farm subdirectory. Designed for nose."""
-import difflib, filecmp, fnmatch, glob, os, shutil, sys
+import difflib, filecmp, fnmatch, glob, os, re, shutil, sys
sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
from backtest import run_command, execfile # pylint: disable-msg=W0622
@@ -158,7 +158,7 @@ class FarmTestCase(object):
self.cd(cwd)
def compare(self, dir1, dir2, filepattern=None, size_within=0,
- left_extra=False, right_extra=False
+ left_extra=False, right_extra=False, scrubs=None
):
"""Compare files matching `filepattern` in `dir1` and `dir2`.
@@ -177,6 +177,9 @@ class FarmTestCase(object):
without triggering an assertion. `right_extra` means the right
directory can.
+ `scrubs` is a list of pairs, regex find and replace patterns to use to
+ scrub the files of unimportant differences.
+
An assertion will be raised if the directories fail one of their
matches.
@@ -220,6 +223,9 @@ class FarmTestCase(object):
for f in diff_files:
left = open(os.path.join(dir1, f), "r").readlines()
right = open(os.path.join(dir2, f), "r").readlines()
+ if scrubs:
+ left = self._scrub(left, scrubs)
+ right = self._scrub(right, scrubs)
if left != right:
text_diff.append(f)
print("".join(list(difflib.Differ().compare(left, right))))
@@ -230,6 +236,14 @@ class FarmTestCase(object):
if not right_extra:
assert not right_only, "Files in %s only: %s" % (dir2, right_only)
+ def _scrub(self, strlist, scrubs):
+ scrubbed = []
+ for s in strlist:
+ for rgx_find, rgx_replace in scrubs:
+ s = re.sub(rgx_find, rgx_replace, s)
+ scrubbed.append(s)
+ return scrubbed
+
def contains(self, filename, *strlist):
"""Check that the file contains all of a list of strings.