-"""Run tests in the farm subdirectory.
+"""Run tests in the farm subdirectory. nose."""
import filecmp, fnmatch, glob, os, shutil, sys
from coverage.files import FileLocator
"""A test-generating function for nose to find and run."""
for fname in glob.glob("test/farm/*/*.py"):
case = FarmTestCase(fname, clean_only)
+ """A test case from the farm tree.
+ Tests are short Python script files, often called run.py:
+ compare("out", "gold", "*,cover")
+ Verbs (copy, run, compare, clean) are methods in this class. FarmTestCase
+ has options to allow various uses of the test cases (normal execution,
+ cleaning-only, or run and leave the results for debugging).
def __init__(self, runpy, clean_only=False, dont_clean=False):
+ """Create a test case from a run.py file.
+ `clean_only` means that only the clean() action is executed.
+ `dont_clean` means that the clean() action is not executed.
self.dir, self.runpy = os.path.split(runpy)
self.clean_only = clean_only
self.dont_clean = dont_clean
+ """Change the current directory, and return the old one."""
+ """Execute the test from the run.py file.
# Prepare a dictionary of globals for the run.py files to use.
glo = dict([(fn, getattr(self, fn)) for fn in fns])
- execfile(self.runpy, glo)
+ execfile(self.runpy, glo)
def fnmatch_list(self, files, filepattern):
"""Filter the list of `files` to only those that match `filepattern`.
files = [f for f in files if fnmatch.fnmatch(f, filepattern)]
+ """Test set up, run by nose before __call__."""
+ """Test tear down, run by nose after __call__."""
+ # Make sure no matter what, the test is cleaned up.
# Functions usable inside farm run.py files
def noop(self, *args, **kwargs):
- for cmd in cmds.split("\n"):
- proc = subprocess.Popen(cmd, shell=True,
- stdin=subprocess.PIPE, stdout=subprocess.PIPE,
- output = proc.stdout.read()
- stdin, stdouterr = os.popen4(cmd)
- output = stdouterr.read()
- retcode = 0 # Can't tell if the process failed.
- raise Exception("command exited abnormally")
+ for cmd in cmds.split("\n"):
+ proc = subprocess.Popen(cmd, shell=True,
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ output = proc.stdout.read()
+ stdin, stdouterr = os.popen4(cmd)
+ output = stdouterr.read()
+ retcode = 0 # Can't tell if the process failed.
+ raise Exception("command exited abnormally")
def compare(self, dir1, dir2, filepattern=None):
+ """Compare files matching `filepattern` in `dir1` and `dir2`.
+ An assertion will be raised if the directories don't match in some way.
dc = filecmp.dircmp(dir1, dir2)
diff_files = self.fnmatch_list(dc.diff_files, filepattern)
left_only = self.fnmatch_list(dc.left_only, filepattern)
assert not right_only, "Files in %s only: %s" % (dir2, right_only)
def clean(self, cleandir):
+ """Clean `cleandir` by removing it and all its children completely."""
case = FarmTestCase(sys.argv)
# Run the test, but don't clean up, so we can examine the output.
case = FarmTestCase(sys.argv, dont_clean=True)
# Run all the tests, but just clean.
for test in test_farm(clean_only=True):