1999-04-08 17:27:54 -03:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test script for popen2.py
|
|
|
|
Christian Tismer
|
|
|
|
"""
|
|
|
|
|
2000-09-01 16:25:51 -03:00
|
|
|
import os
|
|
|
|
|
1999-04-08 17:27:54 -03:00
|
|
|
# popen2 contains its own testing routine
|
|
|
|
# which is especially useful to see if open files
|
2000-09-01 16:25:51 -03:00
|
|
|
# like stdin can be read successfully by a forked
|
1999-04-08 17:27:54 -03:00
|
|
|
# subprocess.
|
|
|
|
|
|
|
|
def main():
|
2000-09-01 16:25:51 -03:00
|
|
|
print "Test popen2 module:"
|
|
|
|
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-04-08 17:27:54 -03:00
|
|
|
import popen2
|
|
|
|
popen2._test()
|
|
|
|
|
|
|
|
|
2000-09-01 16:25:51 -03:00
|
|
|
def _test():
|
|
|
|
# same test as popen2._test(), but using the os.popen*() API
|
|
|
|
print "Testing os module:"
|
|
|
|
import popen2
|
|
|
|
cmd = "cat"
|
|
|
|
teststr = "abc\n"
|
|
|
|
resultstr = teststr
|
|
|
|
if os.name == "nt":
|
|
|
|
cmd = "more"
|
|
|
|
resultstr = "\n" + resultstr
|
|
|
|
print "testing popen2..."
|
|
|
|
w, r = os.popen2(cmd)
|
|
|
|
w.write(teststr)
|
|
|
|
w.close()
|
|
|
|
assert r.read() == resultstr
|
|
|
|
print "testing popen3..."
|
|
|
|
try:
|
|
|
|
w, r, e = os.popen3([cmd])
|
|
|
|
except:
|
|
|
|
w, r, e = os.popen3(cmd)
|
|
|
|
w.write(teststr)
|
|
|
|
w.close()
|
|
|
|
assert r.read() == resultstr
|
|
|
|
assert e.read() == ""
|
|
|
|
for inst in popen2._active[:]:
|
|
|
|
inst.wait()
|
|
|
|
assert not popen2._active
|
|
|
|
print "All OK"
|
|
|
|
|
|
|
|
main()
|
|
|
|
_test()
|