Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test script for popen2.py |
| 3 | Christian Tismer |
| 4 | """ |
| 5 | |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 6 | import os |
Moshe Zadka | fc3fc33 | 2001-01-30 18:35:32 +0000 | [diff] [blame] | 7 | import sys |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 8 | from test.test_support import TestSkipped, reap_children |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 10 | # popen2 contains its own testing routine |
| 11 | # which is especially useful to see if open files |
Fredrik Lundh | 9407e55 | 2000-07-27 07:42:43 +0000 | [diff] [blame] | 12 | # like stdin can be read successfully by a forked |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 13 | # subprocess. |
| 14 | |
| 15 | def main(): |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 16 | print "Test popen2 module:" |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 17 | if (sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos') \ |
| 18 | and __name__ != '__main__': |
Moshe Zadka | fc3fc33 | 2001-01-30 18:35:32 +0000 | [diff] [blame] | 19 | # 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öwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 22 | raise TestSkipped, "popen2() doesn't work during import on " + sys.platform |
Fredrik Lundh | 9407e55 | 2000-07-27 07:42:43 +0000 | [diff] [blame] | 23 | 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 Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 30 | import popen2 |
| 31 | popen2._test() |
| 32 | |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 33 | |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 34 | def _test(): |
| 35 | # same test as popen2._test(), but using the os.popen*() API |
| 36 | print "Testing os module:" |
| 37 | import popen2 |
Martin v. Löwis | 478c82d | 2006-03-24 08:14:54 +0000 | [diff] [blame] | 38 | # 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 Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 41 | cmd = "cat" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 42 | teststr = "ab cd\n" |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 43 | if os.name == "nt": |
| 44 | cmd = "more" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 45 | # "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 Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 49 | print "testing popen2..." |
| 50 | w, r = os.popen2(cmd) |
| 51 | w.write(teststr) |
| 52 | w.close() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 53 | got = r.read() |
| 54 | if got.strip() != expected: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 55 | raise ValueError("wrote %r read %r" % (teststr, got)) |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 56 | 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 Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 63 | got = r.read() |
| 64 | if got.strip() != expected: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 65 | raise ValueError("wrote %r read %r" % (teststr, got)) |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 66 | got = e.read() |
| 67 | if got: |
Andrew M. Kuchling | bfd7d6a | 2005-02-10 13:24:50 +0000 | [diff] [blame] | 68 | raise ValueError("unexpected %r on stderr" % (got,)) |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 69 | for inst in popen2._active[:]: |
| 70 | inst.wait() |
Martin v. Löwis | 478c82d | 2006-03-24 08:14:54 +0000 | [diff] [blame] | 71 | popen2._cleanup() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 72 | if popen2._active: |
| 73 | raise ValueError("_active not empty") |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 74 | print "All OK" |
| 75 | |
| 76 | main() |
| 77 | _test() |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 78 | reap_children() |