From f188fb4c51e41a597fb1f8e4fd406e673a35772a Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 7 May 2009 09:23:10 -0400 Subject: Add a failing test for running non-.py files on the command line. --- test/farm/run/run_xxx.py | 8 ++++++++ test/farm/run/src/xxx | 8 ++++++++ test/test_farm.py | 20 ++++++++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/farm/run/run_xxx.py create mode 100644 test/farm/run/src/xxx diff --git a/test/farm/run/run_xxx.py b/test/farm/run/run_xxx.py new file mode 100644 index 0000000..37a2cb5 --- /dev/null +++ b/test/farm/run/run_xxx.py @@ -0,0 +1,8 @@ +copy("src", "out") +run(""" + coverage -e -x xxx + coverage -r + """, rundir="out", outfile="stdout.txt") +contains("out/stdout.txt", "xxx: 3 4 0 7") +doesnt_contain("out/stdout.txt", "No such file or directory") +clean("out") diff --git a/test/farm/run/src/xxx b/test/farm/run/src/xxx new file mode 100644 index 0000000..6b7eb9e --- /dev/null +++ b/test/farm/run/src/xxx @@ -0,0 +1,8 @@ +# This is a python file though it doesn't look like it, like a main script. +a = b = c = d = 0 +a = 3 +b = 4 +if not b: + c = 6 +d = 7 +print "xxx:", a, b, c, d diff --git a/test/test_farm.py b/test/test_farm.py index fa45630..98f4437 100644 --- a/test/test_farm.py +++ b/test/test_farm.py @@ -58,7 +58,7 @@ class FarmTestCase(object): cwd = self.cd(self.dir) # Prepare a dictionary of globals for the run.py files to use. - fns = "copy run runfunc compare contains clean".split() + fns = "copy run runfunc compare contains doesnt_contain clean".split() if self.clean_only: glo = dict([(fn, self.noop) for fn in fns]) glo['clean'] = self.clean @@ -122,16 +122,19 @@ class FarmTestCase(object): shutil.rmtree(dst) shutil.copytree(src, dst) - def run(self, cmds, rundir="src"): + 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. """ cwd = self.cd(rundir) try: for cmd in cmds.split("\n"): + if not cmd.strip(): + continue if subprocess: proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, @@ -143,6 +146,8 @@ class FarmTestCase(object): output = stdouterr.read() retcode = 0 # Can't tell if the process failed. print output, + if outfile: + open(outfile, "a+").write(output) if retcode: raise Exception("command exited abnormally") finally: @@ -234,6 +239,17 @@ class FarmTestCase(object): for s in strlist: assert s in text, "Missing content in %s: %r" % (filename, s) + def doesnt_contain(self, 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`. + + """ + text = open(filename, "r").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.""" if os.path.exists(cleandir): -- cgit v1.2.1