summaryrefslogtreecommitdiff
path: root/tests/test_farm.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_farm.py')
-rw-r--r--tests/test_farm.py386
1 files changed, 197 insertions, 189 deletions
diff --git a/tests/test_farm.py b/tests/test_farm.py
index 6ed204b7..67164efe 100644
--- a/tests/test_farm.py
+++ b/tests/test_farm.py
@@ -77,12 +77,12 @@ class FarmTestCase(object):
compare contains contains_any doesnt_contain
""".split()
if self.clean_only:
- glo = dict((fn, self.noop) for fn in fns)
- glo['clean'] = self.clean
+ glo = dict((fn, noop) for fn in fns)
+ glo['clean'] = clean
else:
- glo = dict((fn, getattr(self, fn)) for fn in fns)
+ glo = dict((fn, globals()[fn]) for fn in fns)
if self.dont_clean: # pragma: not covered
- glo['clean'] = self.noop
+ glo['clean'] = noop
old_mods = dict(sys.modules)
try:
@@ -123,215 +123,223 @@ class FarmTestCase(object):
# Restore the original sys.path
sys.path = self.old_syspath
- # Functions usable inside farm run.py files
- def noop(self, *args, **kwargs):
- """A no-op function to stub out run, copy, etc, when only cleaning."""
- pass
+# Functions usable inside farm run.py files
- def copy(self, src, dst):
- """Copy a directory."""
- if os.path.exists(dst):
- shutil.rmtree(dst)
- shutil.copytree(src, dst)
+def noop(*args_unused, **kwargs_unused):
+ """A no-op function to stub out run, copy, etc, when only cleaning."""
+ pass
- def run(self, cmds, rundir="src", outfile=None):
- """Run a list of commands.
- `cmds` is a string, commands separated by newlines.
- `rundir` is the directory in which to run the commands.
- `outfile` is a filename to redirect stdout to.
+def copy(src, dst):
+ """Copy a directory."""
+ if os.path.exists(dst):
+ shutil.rmtree(dst)
+ shutil.copytree(src, dst)
- """
- cwd = change_dir(rundir)
- if outfile:
- fout = open(outfile, "a+")
- try:
- for cmd in cmds.split("\n"):
- cmd = cmd.strip()
- if not cmd:
- continue
- retcode, output = run_command(cmd)
- print(output.rstrip())
- if outfile:
- fout.write(output)
- if retcode:
- raise Exception("command exited abnormally")
- finally:
+
+def run(cmds, rundir="src", outfile=None):
+ """Run a list of commands.
+
+ `cmds` is a string, commands separated by newlines.
+ `rundir` is the directory in which to run the commands.
+ `outfile` is a filename to redirect stdout to.
+
+ """
+ cwd = change_dir(rundir)
+ if outfile:
+ fout = open(outfile, "a+")
+ try:
+ for cmd in cmds.split("\n"):
+ cmd = cmd.strip()
+ if not cmd:
+ continue
+ retcode, output = run_command(cmd)
+ print(output.rstrip())
if outfile:
- fout.close()
- change_dir(cwd)
+ fout.write(output)
+ if retcode:
+ raise Exception("command exited abnormally")
+ finally:
+ if outfile:
+ fout.close()
+ change_dir(cwd)
- def runfunc(self, fn, rundir="src", addtopath=None):
- """Run a function.
- `fn` is a callable.
- `rundir` is the directory in which to run the function.
+def runfunc(fn, rundir="src", addtopath=None):
+ """Run a function.
- """
- cwd = change_dir(rundir)
- oldpath = add_to_path(addtopath)
- try:
- fn()
- finally:
- change_dir(cwd)
- restore_path(oldpath)
+ `fn` is a callable.
+ `rundir` is the directory in which to run the function.
+
+ """
+ cwd = change_dir(rundir)
+ oldpath = add_to_path(addtopath)
+ try:
+ fn()
+ finally:
+ change_dir(cwd)
+ restore_path(oldpath)
- def compare(
- self, dir1, dir2, file_pattern=None, size_within=0,
- left_extra=False, right_extra=False, scrubs=None
- ):
- """Compare files matching `file_pattern` in `dir1` and `dir2`.
- `dir2` is interpreted as a prefix, with Python version numbers appended
- to find the actual directory to compare with. "foo" will compare
- against "foo_v241", "foo_v24", "foo_v2", or "foo", depending on which
- directory is found first.
+def compare(
+ dir1, dir2, file_pattern=None, size_within=0,
+ left_extra=False, right_extra=False, scrubs=None
+):
+ """Compare files matching `file_pattern` in `dir1` and `dir2`.
- `size_within` is a percentage delta for the file sizes. If non-zero,
- then the file contents are not compared (since they are expected to
- often be different), but the file sizes must be within this amount.
- For example, size_within=10 means that the two files' sizes must be
- within 10 percent of each other to compare equal.
+ `dir2` is interpreted as a prefix, with Python version numbers appended
+ to find the actual directory to compare with. "foo" will compare
+ against "foo_v241", "foo_v24", "foo_v2", or "foo", depending on which
+ directory is found first.
- `left_extra` true means the left directory can have extra files in it
- without triggering an assertion. `right_extra` means the right
- directory can.
+ `size_within` is a percentage delta for the file sizes. If non-zero,
+ then the file contents are not compared (since they are expected to
+ often be different), but the file sizes must be within this amount.
+ For example, size_within=10 means that the two files' sizes must be
+ within 10 percent of each other to compare equal.
- `scrubs` is a list of pairs, regexes to find and literal strings to
- replace them with to scrub the files of unimportant differences.
+ `left_extra` true means the left directory can have extra files in it
+ without triggering an assertion. `right_extra` means the right
+ directory can.
- An assertion will be raised if the directories fail one of their
- matches.
+ `scrubs` is a list of pairs, regexes to find and literal strings to
+ replace them with to scrub the files of unimportant differences.
- """
- # Search for a dir2 with a version suffix.
- version_suff = ''.join(map(str, sys.version_info[:3]))
- while version_suff:
- trydir = dir2 + '_v' + version_suff
- if os.path.exists(trydir):
- dir2 = trydir
- break
- version_suff = version_suff[:-1]
-
- assert os.path.exists(dir1), "Left directory missing: %s" % dir1
- assert os.path.exists(dir2), "Right directory missing: %s" % dir2
-
- dc = filecmp.dircmp(dir1, dir2)
- diff_files = fnmatch_list(dc.diff_files, file_pattern)
- left_only = fnmatch_list(dc.left_only, file_pattern)
- right_only = fnmatch_list(dc.right_only, file_pattern)
- show_diff = True
-
- if size_within:
- # The files were already compared, use the diff_files list as a
- # guide for size comparison.
- wrong_size = []
- for f in diff_files:
- with open(os.path.join(dir1, f), "rb") as fobj:
- left = fobj.read()
- with open(os.path.join(dir2, f), "rb") as fobj:
- right = fobj.read()
- size_l, size_r = len(left), len(right)
- big, little = max(size_l, size_r), min(size_l, size_r)
- if (big - little) / float(little) > size_within/100.0:
- # print "%d %d" % (big, little)
- # print "Left: ---\n%s\n-----\n%s" % (left, right)
- wrong_size.append("%s (%s,%s)" % (f, size_l, size_r))
- if wrong_size:
- print("File sizes differ between %s and %s: %s" % (
- dir1, dir2, ", ".join(wrong_size)
- ))
-
- # We'll show the diff iff the files differed enough in size.
- show_diff = bool(wrong_size)
-
- if show_diff:
- # filecmp only compares in binary mode, but we want text mode. So
- # look through the list of different files, and compare them
- # ourselves.
- text_diff = []
- for f in diff_files:
- with open(os.path.join(dir1, f), READ_MODE) as fobj:
- left = fobj.read()
- with open(os.path.join(dir2, f), READ_MODE) as fobj:
- right = fobj.read()
- if scrubs:
- left = scrub(left, scrubs)
- right = scrub(right, scrubs)
- if left != right:
- text_diff.append(f)
- left = left.splitlines()
- right = right.splitlines()
- print("\n".join(difflib.Differ().compare(left, right)))
- assert not text_diff, "Files differ: %s" % text_diff
-
- if not left_extra:
- assert not left_only, "Files in %s only: %s" % (dir1, left_only)
- if not right_extra:
- assert not right_only, "Files in %s only: %s" % (dir2, right_only)
-
- def contains(self, filename, *strlist):
- """Check that the file contains all of a list of strings.
-
- An assert will be raised if one of the arguments in `strlist` is
- missing in `filename`.
+ An assertion will be raised if the directories fail one of their
+ matches.
- """
- with open(filename, "r") as fobj:
- text = fobj.read()
- for s in strlist:
- assert s in text, "Missing content in %s: %r" % (filename, s)
+ """
+ # Search for a dir2 with a version suffix.
+ version_suff = ''.join(map(str, sys.version_info[:3]))
+ while version_suff:
+ trydir = dir2 + '_v' + version_suff
+ if os.path.exists(trydir):
+ dir2 = trydir
+ break
+ version_suff = version_suff[:-1]
+
+ assert os.path.exists(dir1), "Left directory missing: %s" % dir1
+ assert os.path.exists(dir2), "Right directory missing: %s" % dir2
+
+ dc = filecmp.dircmp(dir1, dir2)
+ diff_files = fnmatch_list(dc.diff_files, file_pattern)
+ left_only = fnmatch_list(dc.left_only, file_pattern)
+ right_only = fnmatch_list(dc.right_only, file_pattern)
+ show_diff = True
+
+ if size_within:
+ # The files were already compared, use the diff_files list as a
+ # guide for size comparison.
+ wrong_size = []
+ for f in diff_files:
+ with open(os.path.join(dir1, f), "rb") as fobj:
+ left = fobj.read()
+ with open(os.path.join(dir2, f), "rb") as fobj:
+ right = fobj.read()
+ size_l, size_r = len(left), len(right)
+ big, little = max(size_l, size_r), min(size_l, size_r)
+ if (big - little) / float(little) > size_within/100.0:
+ # print "%d %d" % (big, little)
+ # print "Left: ---\n%s\n-----\n%s" % (left, right)
+ wrong_size.append("%s (%s,%s)" % (f, size_l, size_r))
+ if wrong_size:
+ print("File sizes differ between %s and %s: %s" % (
+ dir1, dir2, ", ".join(wrong_size)
+ ))
+
+ # We'll show the diff iff the files differed enough in size.
+ show_diff = bool(wrong_size)
+
+ if show_diff:
+ # filecmp only compares in binary mode, but we want text mode. So
+ # look through the list of different files, and compare them
+ # ourselves.
+ text_diff = []
+ for f in diff_files:
+ with open(os.path.join(dir1, f), READ_MODE) as fobj:
+ left = fobj.read()
+ with open(os.path.join(dir2, f), READ_MODE) as fobj:
+ right = fobj.read()
+ if scrubs:
+ left = scrub(left, scrubs)
+ right = scrub(right, scrubs)
+ if left != right:
+ text_diff.append(f)
+ left = left.splitlines()
+ right = right.splitlines()
+ print("\n".join(difflib.Differ().compare(left, right)))
+ assert not text_diff, "Files differ: %s" % text_diff
+
+ if not left_extra:
+ assert not left_only, "Files in %s only: %s" % (dir1, left_only)
+ if not right_extra:
+ assert not right_only, "Files in %s only: %s" % (dir2, right_only)
+
+
+def contains(filename, *strlist):
+ """Check that the file contains all of a list of strings.
+
+ An assert will be raised if one of the arguments in `strlist` is
+ missing in `filename`.
- def contains_any(self, filename, *strlist):
- """Check that the file contains at least one of a list of strings.
+ """
+ with open(filename, "r") as fobj:
+ text = fobj.read()
+ for s in strlist:
+ assert s in text, "Missing content in %s: %r" % (filename, s)
- An assert will be raised if none of the arguments in `strlist` is in
- `filename`.
- """
- with open(filename, "r") as fobj:
- text = fobj.read()
- for s in strlist:
- if s in text:
- return
- assert False, "Missing content in %s: %r [1 of %d]" % (
- filename, strlist[0], len(strlist),
- )
+def contains_any(filename, *strlist):
+ """Check that the file contains at least one of a list of strings.
- def doesnt_contain(self, filename, *strlist):
- """Check that the file contains none of a list of strings.
+ An assert will be raised if none of the arguments in `strlist` is in
+ `filename`.
- An assert will be raised if any of the strings in `strlist` appears in
- `filename`.
+ """
+ with open(filename, "r") as fobj:
+ text = fobj.read()
+ for s in strlist:
+ if s in text:
+ return
+ assert False, "Missing content in %s: %r [1 of %d]" % (filename, strlist[0], len(strlist),)
+
+
+def doesnt_contain(filename, *strlist):
+ """Check that the file contains none of a list of strings.
+
+ An assert will be raised if any of the strings in `strlist` appears in
+ `filename`.
+
+ """
+ with open(filename, "r") as fobj:
+ text = fobj.read()
+ for s in strlist:
+ assert s not in text, "Forbidden content in %s: %r" % (filename, s)
+
+
+def clean(cleandir):
+ """Clean `cleandir` by removing it and all its children completely."""
+ # rmtree gives mysterious failures on Win7, so retry a "few" times.
+ # I've seen it take over 100 tries, so, 1000! This is probably the
+ # most unpleasant hack I've written in a long time...
+ tries = 1000
+ while tries: # pragma: part covered
+ if os.path.exists(cleandir):
+ try:
+ shutil.rmtree(cleandir)
+ except OSError: # pragma: not covered
+ if tries == 1:
+ raise
+ else:
+ tries -= 1
+ continue
+ break
- """
- with open(filename, "r") as fobj:
- text = fobj.read()
- for s in strlist:
- assert s not in text, "Forbidden content in %s: %r" % (filename, s)
-
- def clean(self, cleandir):
- """Clean `cleandir` by removing it and all its children completely."""
- # rmtree gives mysterious failures on Win7, so retry a "few" times.
- # I've seen it take over 100 tries, so, 1000! This is probably the
- # most unpleasant hack I've written in a long time...
- tries = 1000
- while tries: # pragma: part covered
- if os.path.exists(cleandir):
- try:
- shutil.rmtree(cleandir)
- except OSError: # pragma: not covered
- if tries == 1:
- raise
- else:
- tries -= 1
- continue
- break
- def skip(self, msg=None):
- """Skip the current test."""
- raise SkipTest(msg)
+def skip(msg=None):
+ """Skip the current test."""
+ raise SkipTest(msg)
# Helpers