# HG changeset patch # User Bernhard Herzog # Date 1213972829 0 # Node ID c7ac6736649255b2dfc4209311f345839de1b7d3 # Parent a68a4e22549c5b3334b65a58a513e7647295ef89 Make treepkg.run.capture_output include the output in the SubprocessError exception raised when the command fails with an exit code != 0 diff -r a68a4e22549c -r c7ac67366492 test/test_run.py --- a/test/test_run.py Fri Jun 20 14:06:27 2008 +0000 +++ b/test/test_run.py Fri Jun 20 14:40:29 2008 +0000 @@ -11,7 +11,7 @@ import os import unittest -from treepkg.run import call, SubprocessError +from treepkg.run import call, capture_output, SubprocessError @@ -61,3 +61,29 @@ call(subprocess_cmd, inputdata=data) except SubprocessError, exc: self.assertEquals(exc.returncode, 1) + +class TestCaptureOutput(unittest.TestCase): + + def test_capture_output_stdout(self): + text = "explicit is better than implicit" + self.assertEquals(capture_output([sys.executable, "-c", + "print %r" % text]), + text + "\n") + + def test_capture_output_stderr(self): + self.assertEquals(capture_output([sys.executable, "-c", + "import sys;" + "print 'on stdout';" + " sys.stdout.flush();" + "print >>sys.stderr, 'on stderr'"]), + "on stdout\non stderr\n") + + def test_capture_output_exception(self): + try: + capture_output([sys.executable, "-c", + "import sys;" + "print 'Beautiful is better than ugly';" + " sys.exit(1)"]) + except SubprocessError, exc: + self.assertEquals(exc.returncode, 1) + self.assertEquals(exc.output, "Beautiful is better than ugly\n") diff -r a68a4e22549c -r c7ac67366492 treepkg/run.py --- a/treepkg/run.py Fri Jun 20 14:06:27 2008 +0000 +++ b/treepkg/run.py Fri Jun 20 14:40:29 2008 +0000 @@ -13,11 +13,12 @@ class SubprocessError(EnvironmentError): - def __init__(self, command, returncode): + def __init__(self, command, returncode, output=None): EnvironmentError.__init__(self, "Command %r finished with return code %d" % (command, returncode)) self.returncode = returncode + self.output = output def call(command, suppress_output=False, extra_env=None, inputdata=None, **kw): @@ -58,5 +59,5 @@ stderr=subprocess.STDOUT, **kw) output = proc.communicate()[0] if proc.returncode != 0: - raise SubprocessError(command, proc.returncode) + raise SubprocessError(command, proc.returncode, output) return output