[Treepkg-commits] r90 - in trunk: test treepkg

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Jun 20 16:40:29 CEST 2008


Author: bh
Date: 2008-06-20 16:40:29 +0200 (Fri, 20 Jun 2008)
New Revision: 90

Modified:
   trunk/test/test_run.py
   trunk/treepkg/run.py
Log:
Make treepkg.run.capture_output include the output in the
SubprocessError exception raised when the command fails with an exit
code != 0


Modified: trunk/test/test_run.py
===================================================================
--- trunk/test/test_run.py	2008-06-20 14:06:27 UTC (rev 89)
+++ trunk/test/test_run.py	2008-06-20 14:40:29 UTC (rev 90)
@@ -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")

Modified: trunk/treepkg/run.py
===================================================================
--- trunk/treepkg/run.py	2008-06-20 14:06:27 UTC (rev 89)
+++ trunk/treepkg/run.py	2008-06-20 14:40:29 UTC (rev 90)
@@ -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



More information about the Treepkg-commits mailing list