python-2.5.2/win32/Lib/test/test_popen2.py
changeset 0 ae805ac0140d
equal deleted inserted replaced
-1:000000000000 0:ae805ac0140d
       
     1 #! /usr/bin/env python
       
     2 """Test script for popen2.py
       
     3    Christian Tismer
       
     4 """
       
     5 
       
     6 import os
       
     7 import sys
       
     8 from test.test_support import TestSkipped, reap_children
       
     9 
       
    10 # popen2 contains its own testing routine
       
    11 # which is especially useful to see if open files
       
    12 # like stdin can be read successfully by a forked
       
    13 # subprocess.
       
    14 
       
    15 def main():
       
    16     print "Test popen2 module:"
       
    17     if (sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos') \
       
    18            and __name__ != '__main__':
       
    19         #  Locks get messed up or something.  Generally we're supposed
       
    20         #  to avoid mixing "posix" fork & exec with native threads, and
       
    21         #  they may be right about that after all.
       
    22         raise TestSkipped, "popen2() doesn't work during import on " + sys.platform
       
    23     try:
       
    24         from os import popen
       
    25     except ImportError:
       
    26         # if we don't have os.popen, check that
       
    27         # we have os.fork.  if not, skip the test
       
    28         # (by raising an ImportError)
       
    29         from os import fork
       
    30     import popen2
       
    31     popen2._test()
       
    32 
       
    33 
       
    34 def _test():
       
    35     # same test as popen2._test(), but using the os.popen*() API
       
    36     print "Testing os module:"
       
    37     import popen2
       
    38     # When the test runs, there shouldn't be any open pipes
       
    39     popen2._cleanup()
       
    40     assert not popen2._active, "Active pipes when test starts " + repr([c.cmd for c in popen2._active])
       
    41     cmd  = "cat"
       
    42     teststr = "ab cd\n"
       
    43     if os.name == "nt":
       
    44         cmd = "more"
       
    45     # "more" doesn't act the same way across Windows flavors,
       
    46     # sometimes adding an extra newline at the start or the
       
    47     # end.  So we strip whitespace off both ends for comparison.
       
    48     expected = teststr.strip()
       
    49     print "testing popen2..."
       
    50     w, r = os.popen2(cmd)
       
    51     w.write(teststr)
       
    52     w.close()
       
    53     got = r.read()
       
    54     if got.strip() != expected:
       
    55         raise ValueError("wrote %r read %r" % (teststr, got))
       
    56     print "testing popen3..."
       
    57     try:
       
    58         w, r, e = os.popen3([cmd])
       
    59     except:
       
    60         w, r, e = os.popen3(cmd)
       
    61     w.write(teststr)
       
    62     w.close()
       
    63     got = r.read()
       
    64     if got.strip() != expected:
       
    65         raise ValueError("wrote %r read %r" % (teststr, got))
       
    66     got = e.read()
       
    67     if got:
       
    68         raise ValueError("unexpected %r on stderr" % (got,))
       
    69     for inst in popen2._active[:]:
       
    70         inst.wait()
       
    71     popen2._cleanup()
       
    72     if popen2._active:
       
    73         raise ValueError("_active not empty")
       
    74     print "All OK"
       
    75 
       
    76 main()
       
    77 _test()
       
    78 reap_children()