changeset 165:c7ac67366492

Make treepkg.run.capture_output include the output in the SubprocessError exception raised when the command fails with an exit code != 0
author Bernhard Herzog <bh@intevation.de>
date Fri, 20 Jun 2008 14:40:29 +0000
parents a68a4e22549c
children 61fd2892b80b
files test/test_run.py treepkg/run.py
diffstat 2 files changed, 30 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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")
--- 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
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)