[Pkg-bazaar-commits] ./bzr/unstable r301: - provide for catching output from shell commands

Martin Pool mbp at sourcefrog.net
Fri Apr 10 07:43:50 UTC 2009


------------------------------------------------------------
revno: 301
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Thu 2005-04-28 19:26:28 +1000
message:
  - provide for catching output from shell commands
modified:
  testbzr
-------------- next part --------------
=== modified file 'testbzr'
--- a/testbzr	2005-04-28 09:15:31 +0000
+++ b/testbzr	2005-04-28 09:26:28 +0000
@@ -28,7 +28,7 @@
 
 try:
     import shutil
-    from subprocess import call, Popen
+    from subprocess import call, Popen, PIPE
 except ImportError, e:
     sys.stderr.write("testbzr: sorry, this test suite requires modules from python2.4\n"
                      + '    ' + str(e))
@@ -39,9 +39,11 @@
     pass
 
 
-def runcmd(cmd, retcode=0):
+def runcmd(cmd, retcode=0, catchstdout=False):
     """Run one command and check the return code.
 
+    Returns a tuple of (stdout,stderr) strings.
+
     If a single string is based, it is split into words.
     For commands that are not simple space-separated words, please
     pass a list instead."""
@@ -52,11 +54,23 @@
     else:
         logfile.write('$ %r\n' % cmd)
     log_linenumber()
-    actual_retcode = call(cmd, stdout=logfile, stderr=logfile)
+
+    if catchstdout:
+        stdout = PIPE
+    else:
+        stdout = logfile
+        
+    child = Popen(cmd, stdout=stdout, stderr=logfile)
+    outd, errd = child.communicate()
+
+    actual_retcode = child.wait()
+    
     if retcode != actual_retcode:
         raise CommandFailed("test failed: %r returned %d, expected %d"
                             % (cmd, actual_retcode, retcode))
 
+    return outd, errd
+
 
 def progress(msg):
     print '* ' + msg
@@ -99,6 +113,7 @@
     # this should always identify something, if only "john at localhost"
     runcmd("bzr whoami")
     runcmd("bzr whoami --email")
+    assert runcmd("bzr whoami --email", catchstdout=True)[0].count('@') == 1
 
     progress("invalid commands")
     runcmd("bzr pants", retcode=1)



More information about the Pkg-bazaar-commits mailing list