blob: 1215847bbcd45a40784e4c8c3601cac8219f99a9 [file] [log] [blame]
Guido van Rossum59e4f371999-03-11 13:26:23 +00001#! /usr/bin/env python
2"""Test script for popen2.py
3 Christian Tismer
4"""
5
Fred Drake31f182e2000-08-28 17:20:05 +00006import os
7
Guido van Rossum59e4f371999-03-11 13:26:23 +00008# popen2 contains its own testing routine
9# which is especially useful to see if open files
Fredrik Lundh9407e552000-07-27 07:42:43 +000010# like stdin can be read successfully by a forked
Guido van Rossum59e4f371999-03-11 13:26:23 +000011# subprocess.
12
13def main():
Fred Drake31f182e2000-08-28 17:20:05 +000014 print "Test popen2 module:"
Fredrik Lundh9407e552000-07-27 07:42:43 +000015 try:
16 from os import popen
17 except ImportError:
18 # if we don't have os.popen, check that
19 # we have os.fork. if not, skip the test
20 # (by raising an ImportError)
21 from os import fork
Guido van Rossum59e4f371999-03-11 13:26:23 +000022 import popen2
23 popen2._test()
24
Guido van Rossum59e4f371999-03-11 13:26:23 +000025
Fred Drake31f182e2000-08-28 17:20:05 +000026def _test():
27 # same test as popen2._test(), but using the os.popen*() API
28 print "Testing os module:"
29 import popen2
30 cmd = "cat"
Tim Peters36208572000-09-01 20:38:55 +000031 teststr = "ab cd\n"
Fred Drake31f182e2000-08-28 17:20:05 +000032 if os.name == "nt":
33 cmd = "more"
Tim Peters36208572000-09-01 20:38:55 +000034 # "more" doesn't act the same way across Windows flavors,
35 # sometimes adding an extra newline at the start or the
36 # end. So we strip whitespace off both ends for comparison.
37 expected = teststr.strip()
Fred Drake31f182e2000-08-28 17:20:05 +000038 print "testing popen2..."
39 w, r = os.popen2(cmd)
40 w.write(teststr)
41 w.close()
Tim Peters36208572000-09-01 20:38:55 +000042 got = r.read()
43 if got.strip() != expected:
44 raise ValueError("wrote %s read %s" % (`teststr`, `got`))
Fred Drake31f182e2000-08-28 17:20:05 +000045 print "testing popen3..."
46 try:
47 w, r, e = os.popen3([cmd])
48 except:
49 w, r, e = os.popen3(cmd)
50 w.write(teststr)
51 w.close()
Tim Peters36208572000-09-01 20:38:55 +000052 got = r.read()
53 if got.strip() != expected:
54 raise ValueError("wrote %s read %s" % (`teststr`, `got`))
55 got = e.read()
56 if got:
57 raise ValueError("unexected %s on stderr" % `got`)
Fred Drake31f182e2000-08-28 17:20:05 +000058 for inst in popen2._active[:]:
59 inst.wait()
Tim Peters36208572000-09-01 20:38:55 +000060 if popen2._active:
61 raise ValueError("_active not empty")
Fred Drake31f182e2000-08-28 17:20:05 +000062 print "All OK"
63
64main()
65_test()