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 |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 8 | from test.test_support import TestSkipped |
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 |
| 38 | cmd = "cat" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 39 | teststr = "ab cd\n" |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 40 | if os.name == "nt": |
| 41 | cmd = "more" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 42 | # "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 Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 46 | print "testing popen2..." |
| 47 | w, r = os.popen2(cmd) |
| 48 | w.write(teststr) |
| 49 | w.close() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 50 | got = r.read() |
| 51 | if got.strip() != expected: |
| 52 | raise ValueError("wrote %s read %s" % (`teststr`, `got`)) |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 53 | 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 Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 60 | got = r.read() |
| 61 | if got.strip() != expected: |
| 62 | raise ValueError("wrote %s read %s" % (`teststr`, `got`)) |
| 63 | got = e.read() |
| 64 | if got: |
| 65 | raise ValueError("unexected %s on stderr" % `got`) |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 66 | for inst in popen2._active[:]: |
| 67 | inst.wait() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 68 | if popen2._active: |
| 69 | raise ValueError("_active not empty") |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 70 | print "All OK" |
| 71 | |
| 72 | main() |
| 73 | _test() |