blob: 18142ecc13ca31418c7935d3057df4658c09fd7e [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
Moshe Zadkafc3fc332001-01-30 18:35:32 +00007import sys
Barry Warsaw04f357c2002-07-23 19:04:11 +00008from test.test_support import TestSkipped
Fred Drake31f182e2000-08-28 17:20:05 +00009
Guido van Rossum59e4f371999-03-11 13:26:23 +000010# popen2 contains its own testing routine
11# which is especially useful to see if open files
Fredrik Lundh9407e552000-07-27 07:42:43 +000012# like stdin can be read successfully by a forked
Guido van Rossum59e4f371999-03-11 13:26:23 +000013# subprocess.
14
15def main():
Fred Drake31f182e2000-08-28 17:20:05 +000016 print "Test popen2 module:"
Martin v. Löwisf90ae202002-06-11 06:22:31 +000017 if (sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos') \
18 and __name__ != '__main__':
Moshe Zadkafc3fc332001-01-30 18:35:32 +000019 # Locks get messed up or something. Generally we're supposed
20 # to avoid mixing "posix" fork & exec with native threads, and
21 # they may be right about that after all.
Martin v. Löwisf90ae202002-06-11 06:22:31 +000022 raise TestSkipped, "popen2() doesn't work during import on " + sys.platform
Fredrik Lundh9407e552000-07-27 07:42:43 +000023 try:
24 from os import popen
25 except ImportError:
26 # if we don't have os.popen, check that
27 # we have os.fork. if not, skip the test
28 # (by raising an ImportError)
29 from os import fork
Guido van Rossum59e4f371999-03-11 13:26:23 +000030 import popen2
31 popen2._test()
32
Guido van Rossum59e4f371999-03-11 13:26:23 +000033
Fred Drake31f182e2000-08-28 17:20:05 +000034def _test():
35 # same test as popen2._test(), but using the os.popen*() API
36 print "Testing os module:"
37 import popen2
38 cmd = "cat"
Tim Peters36208572000-09-01 20:38:55 +000039 teststr = "ab cd\n"
Fred Drake31f182e2000-08-28 17:20:05 +000040 if os.name == "nt":
41 cmd = "more"
Tim Peters36208572000-09-01 20:38:55 +000042 # "more" doesn't act the same way across Windows flavors,
43 # sometimes adding an extra newline at the start or the
44 # end. So we strip whitespace off both ends for comparison.
45 expected = teststr.strip()
Fred Drake31f182e2000-08-28 17:20:05 +000046 print "testing popen2..."
47 w, r = os.popen2(cmd)
48 w.write(teststr)
49 w.close()
Tim Peters36208572000-09-01 20:38:55 +000050 got = r.read()
51 if got.strip() != expected:
Walter Dörwald70a6b492004-02-12 17:35:32 +000052 raise ValueError("wrote %r read %r" % (teststr, got))
Fred Drake31f182e2000-08-28 17:20:05 +000053 print "testing popen3..."
54 try:
55 w, r, e = os.popen3([cmd])
56 except:
57 w, r, e = os.popen3(cmd)
58 w.write(teststr)
59 w.close()
Tim Peters36208572000-09-01 20:38:55 +000060 got = r.read()
61 if got.strip() != expected:
Walter Dörwald70a6b492004-02-12 17:35:32 +000062 raise ValueError("wrote %r read %r" % (teststr, got))
Tim Peters36208572000-09-01 20:38:55 +000063 got = e.read()
64 if got:
Andrew M. Kuchlingbfd7d6a2005-02-10 13:24:50 +000065 raise ValueError("unexpected %r on stderr" % (got,))
Fred Drake31f182e2000-08-28 17:20:05 +000066 for inst in popen2._active[:]:
67 inst.wait()
Tim Peters36208572000-09-01 20:38:55 +000068 if popen2._active:
69 raise ValueError("_active not empty")
Fred Drake31f182e2000-08-28 17:20:05 +000070 print "All OK"
71
72main()
73_test()