Commit cfee3df8 authored by Tim Peters's avatar Tim Peters

The "more" cmd varies across Windows flavors, sometimes adding stray

newlines at the start or end.  Fiddle test_popen2 and popen2._test() to
tolerate this.  Also change all "assert"s in these tests to raise
explicit exceptions, so that python -O doesn't render them useless.
Also, in case of error, make the msg display the reprs of what we
wrote and what we read, so we can tell exactly why it's failing.
parent fdd02ebe
...@@ -143,16 +143,20 @@ else: ...@@ -143,16 +143,20 @@ else:
def _test(): def _test():
cmd = "cat" cmd = "cat"
teststr = "abc\n" teststr = "ab cd\n"
resultstr = teststr
if os.name == "nt": if os.name == "nt":
cmd = "more" cmd = "more"
resultstr = "\n" + resultstr # "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()
print "testing popen2..." print "testing popen2..."
r, w = popen2(cmd) r, w = popen2(cmd)
w.write(teststr) w.write(teststr)
w.close() w.close()
assert r.read() == resultstr got = r.read()
if got.strip() != expected:
raise ValueError("wrote %s read %s" % (`teststr`, `got`))
print "testing popen3..." print "testing popen3..."
try: try:
r, w, e = popen3([cmd]) r, w, e = popen3([cmd])
...@@ -160,11 +164,16 @@ def _test(): ...@@ -160,11 +164,16 @@ def _test():
r, w, e = popen3(cmd) r, w, e = popen3(cmd)
w.write(teststr) w.write(teststr)
w.close() w.close()
assert r.read() == resultstr got = r.read()
assert e.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`)
for inst in _active[:]: for inst in _active[:]:
inst.wait() inst.wait()
assert not _active if _active:
raise ValueError("_active not empty")
print "All OK" print "All OK"
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -28,16 +28,20 @@ def _test(): ...@@ -28,16 +28,20 @@ def _test():
print "Testing os module:" print "Testing os module:"
import popen2 import popen2
cmd = "cat" cmd = "cat"
teststr = "abc\n" teststr = "ab cd\n"
resultstr = teststr
if os.name == "nt": if os.name == "nt":
cmd = "more" cmd = "more"
resultstr = "\n" + resultstr # "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()
print "testing popen2..." print "testing popen2..."
w, r = os.popen2(cmd) w, r = os.popen2(cmd)
w.write(teststr) w.write(teststr)
w.close() w.close()
assert r.read() == resultstr got = r.read()
if got.strip() != expected:
raise ValueError("wrote %s read %s" % (`teststr`, `got`))
print "testing popen3..." print "testing popen3..."
try: try:
w, r, e = os.popen3([cmd]) w, r, e = os.popen3([cmd])
...@@ -45,11 +49,16 @@ def _test(): ...@@ -45,11 +49,16 @@ def _test():
w, r, e = os.popen3(cmd) w, r, e = os.popen3(cmd)
w.write(teststr) w.write(teststr)
w.close() w.close()
assert r.read() == resultstr got = r.read()
assert e.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`)
for inst in popen2._active[:]: for inst in popen2._active[:]:
inst.wait() inst.wait()
assert not popen2._active if popen2._active:
raise ValueError("_active not empty")
print "All OK" print "All OK"
main() main()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment