blob: 2d54eb008d00d7c8e8653aa891b8c353f473500c [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
Neal Norwitzb15ac312006-06-29 04:10:08 +00008from test.test_support import TestSkipped, reap_children
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
Martin v. Löwis478c82d2006-03-24 08:14:54 +000038 # When the test runs, there shouldn't be any open pipes
39 popen2._cleanup()
40 assert not popen2._active, "Active pipes when test starts " + repr([c.cmd for c in popen2._active])
Fred Drake31f182e2000-08-28 17:20:05 +000041 cmd = "cat"
Tim Peters36208572000-09-01 20:38:55 +000042 teststr = "ab cd\n"
Fred Drake31f182e2000-08-28 17:20:05 +000043 if os.name == "nt":
44 cmd = "more"
Tim Peters36208572000-09-01 20:38:55 +000045 # "more" doesn't act the same way across Windows flavors,
46 # sometimes adding an extra newline at the start or the
47 # end. So we strip whitespace off both ends for comparison.
48 expected = teststr.strip()
Fred Drake31f182e2000-08-28 17:20:05 +000049 print "testing popen2..."
50 w, r = os.popen2(cmd)
51 w.write(teststr)
52 w.close()
Tim Peters36208572000-09-01 20:38:55 +000053 got = r.read()
54 if got.strip() != expected:
Walter Dörwald70a6b492004-02-12 17:35:32 +000055 raise ValueError("wrote %r read %r" % (teststr, got))
Fred Drake31f182e2000-08-28 17:20:05 +000056 print "testing popen3..."
57 try:
58 w, r, e = os.popen3([cmd])
59 except:
60 w, r, e = os.popen3(cmd)
61 w.write(teststr)
62 w.close()
Tim Peters36208572000-09-01 20:38:55 +000063 got = r.read()
64 if got.strip() != expected:
Walter Dörwald70a6b492004-02-12 17:35:32 +000065 raise ValueError("wrote %r read %r" % (teststr, got))
Tim Peters36208572000-09-01 20:38:55 +000066 got = e.read()
67 if got:
Andrew M. Kuchlingbfd7d6a2005-02-10 13:24:50 +000068 raise ValueError("unexpected %r on stderr" % (got,))
Fred Drake31f182e2000-08-28 17:20:05 +000069 for inst in popen2._active[:]:
70 inst.wait()
Martin v. Löwis478c82d2006-03-24 08:14:54 +000071 popen2._cleanup()
Tim Peters36208572000-09-01 20:38:55 +000072 if popen2._active:
73 raise ValueError("_active not empty")
Fred Drake31f182e2000-08-28 17:20:05 +000074 print "All OK"
75
76main()
77_test()
Neal Norwitzb15ac312006-06-29 04:10:08 +000078reap_children()