1999-03-11 09:26:23 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test script for popen2.py
|
|
|
|
Christian Tismer
|
|
|
|
"""
|
|
|
|
|
2000-08-28 14:20:05 -03:00
|
|
|
import os
|
|
|
|
|
1999-03-11 09:26:23 -04:00
|
|
|
# popen2 contains its own testing routine
|
|
|
|
# which is especially useful to see if open files
|
2000-07-27 04:42:43 -03:00
|
|
|
# like stdin can be read successfully by a forked
|
1999-03-11 09:26:23 -04:00
|
|
|
# subprocess.
|
|
|
|
|
|
|
|
def main():
|
2000-08-28 14:20:05 -03:00
|
|
|
print "Test popen2 module:"
|
2000-07-27 04:42:43 -03:00
|
|
|
try:
|
|
|
|
from os import popen
|
|
|
|
except ImportError:
|
|
|
|
# if we don't have os.popen, check that
|
|
|
|
# we have os.fork. if not, skip the test
|
|
|
|
# (by raising an ImportError)
|
|
|
|
from os import fork
|
1999-03-11 09:26:23 -04:00
|
|
|
import popen2
|
|
|
|
popen2._test()
|
|
|
|
|
|
|
|
|
2000-08-28 14:20:05 -03:00
|
|
|
def _test():
|
|
|
|
# same test as popen2._test(), but using the os.popen*() API
|
|
|
|
print "Testing os module:"
|
|
|
|
import popen2
|
|
|
|
cmd = "cat"
|
2000-09-01 17:38:55 -03:00
|
|
|
teststr = "ab cd\n"
|
2000-08-28 14:20:05 -03:00
|
|
|
if os.name == "nt":
|
|
|
|
cmd = "more"
|
2000-09-01 17:38:55 -03:00
|
|
|
# "more" doesn't act the same way across Windows flavors,
|
|
|
|
# sometimes adding an extra newline at the start or the
|
|
|
|
# end. So we strip whitespace off both ends for comparison.
|
|
|
|
expected = teststr.strip()
|
2000-08-28 14:20:05 -03:00
|
|
|
print "testing popen2..."
|
|
|
|
w, r = os.popen2(cmd)
|
|
|
|
w.write(teststr)
|
|
|
|
w.close()
|
2000-09-01 17:38:55 -03:00
|
|
|
got = r.read()
|
|
|
|
if got.strip() != expected:
|
|
|
|
raise ValueError("wrote %s read %s" % (`teststr`, `got`))
|
2000-08-28 14:20:05 -03:00
|
|
|
print "testing popen3..."
|
|
|
|
try:
|
|
|
|
w, r, e = os.popen3([cmd])
|
|
|
|
except:
|
|
|
|
w, r, e = os.popen3(cmd)
|
|
|
|
w.write(teststr)
|
|
|
|
w.close()
|
2000-09-01 17:38:55 -03:00
|
|
|
got = r.read()
|
|
|
|
if got.strip() != expected:
|
|
|
|
raise ValueError("wrote %s read %s" % (`teststr`, `got`))
|
|
|
|
got = e.read()
|
|
|
|
if got:
|
|
|
|
raise ValueError("unexected %s on stderr" % `got`)
|
2000-08-28 14:20:05 -03:00
|
|
|
for inst in popen2._active[:]:
|
|
|
|
inst.wait()
|
2000-09-01 17:38:55 -03:00
|
|
|
if popen2._active:
|
|
|
|
raise ValueError("_active not empty")
|
2000-08-28 14:20:05 -03:00
|
|
|
print "All OK"
|
|
|
|
|
|
|
|
main()
|
|
|
|
_test()
|