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 |
| 8 | from 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:" |
Moshe Zadka | fc3fc33 | 2001-01-30 18:35:32 +0000 | [diff] [blame] | 17 | if sys.platform[:4] == 'beos' and __name__ != '__main__': |
| 18 | # Locks get messed up or something. Generally we're supposed |
| 19 | # to avoid mixing "posix" fork & exec with native threads, and |
| 20 | # they may be right about that after all. |
| 21 | raise TestSkipped, "popen2() doesn't work during import on BeOS" |
Fredrik Lundh | 9407e55 | 2000-07-27 07:42:43 +0000 | [diff] [blame] | 22 | try: |
| 23 | from os import popen |
| 24 | except ImportError: |
| 25 | # if we don't have os.popen, check that |
| 26 | # we have os.fork. if not, skip the test |
| 27 | # (by raising an ImportError) |
| 28 | from os import fork |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 29 | import popen2 |
| 30 | popen2._test() |
| 31 | |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 32 | |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 33 | def _test(): |
| 34 | # same test as popen2._test(), but using the os.popen*() API |
| 35 | print "Testing os module:" |
| 36 | import popen2 |
| 37 | cmd = "cat" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 38 | teststr = "ab cd\n" |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 39 | if os.name == "nt": |
| 40 | cmd = "more" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 41 | # "more" doesn't act the same way across Windows flavors, |
| 42 | # sometimes adding an extra newline at the start or the |
| 43 | # end. So we strip whitespace off both ends for comparison. |
| 44 | expected = teststr.strip() |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 45 | print "testing popen2..." |
| 46 | w, r = os.popen2(cmd) |
| 47 | w.write(teststr) |
| 48 | w.close() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 49 | got = r.read() |
| 50 | if got.strip() != expected: |
| 51 | raise ValueError("wrote %s read %s" % (`teststr`, `got`)) |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 52 | print "testing popen3..." |
| 53 | try: |
| 54 | w, r, e = os.popen3([cmd]) |
| 55 | except: |
| 56 | w, r, e = os.popen3(cmd) |
| 57 | w.write(teststr) |
| 58 | w.close() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 59 | got = r.read() |
| 60 | if got.strip() != expected: |
| 61 | raise ValueError("wrote %s read %s" % (`teststr`, `got`)) |
| 62 | got = e.read() |
| 63 | if got: |
| 64 | raise ValueError("unexected %s on stderr" % `got`) |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 65 | for inst in popen2._active[:]: |
| 66 | inst.wait() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 67 | if popen2._active: |
| 68 | raise ValueError("_active not empty") |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 69 | print "All OK" |
| 70 | |
| 71 | main() |
| 72 | _test() |