blob: 1215847bbcd45a40784e4c8c3601cac8219f99a9 [file] [log] [blame]
Guido van Rossum9a744a91999-04-08 20:27:54 +00001#! /usr/bin/env python
2"""Test script for popen2.py
3 Christian Tismer
4"""
5
Guido van Rossum8d691c82000-09-01 19:25:51 +00006import os
7
Guido van Rossum9a744a91999-04-08 20:27:54 +00008# popen2 contains its own testing routine
9# which is especially useful to see if open files
Guido van Rossum8d691c82000-09-01 19:25:51 +000010# like stdin can be read successfully by a forked
Guido van Rossum9a744a91999-04-08 20:27:54 +000011# subprocess.
12
13def main():
Guido van Rossum8d691c82000-09-01 19:25:51 +000014 print "Test popen2 module:"
15 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 Rossum9a744a91999-04-08 20:27:54 +000022 import popen2
23 popen2._test()
24
Guido van Rossum9a744a91999-04-08 20:27:54 +000025
Guido van Rossum8d691c82000-09-01 19:25:51 +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"
Guido van Rossume2ab1452000-09-05 04:49:50 +000031 teststr = "ab cd\n"
Guido van Rossum8d691c82000-09-01 19:25:51 +000032 if os.name == "nt":
33 cmd = "more"
Guido van Rossume2ab1452000-09-05 04:49:50 +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()
Guido van Rossum8d691c82000-09-01 19:25:51 +000038 print "testing popen2..."
39 w, r = os.popen2(cmd)
40 w.write(teststr)
41 w.close()
Guido van Rossume2ab1452000-09-05 04:49:50 +000042 got = r.read()
43 if got.strip() != expected:
44 raise ValueError("wrote %s read %s" % (`teststr`, `got`))
Guido van Rossum8d691c82000-09-01 19:25:51 +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()
Guido van Rossume2ab1452000-09-05 04:49:50 +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`)
Guido van Rossum8d691c82000-09-01 19:25:51 +000058 for inst in popen2._active[:]:
59 inst.wait()
Guido van Rossume2ab1452000-09-05 04:49:50 +000060 if popen2._active:
61 raise ValueError("_active not empty")
Guido van Rossum8d691c82000-09-01 19:25:51 +000062 print "All OK"
63
64main()
65_test()