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 |
| 7 | |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 8 | # popen2 contains its own testing routine |
| 9 | # which is especially useful to see if open files |
Fredrik Lundh | 9407e55 | 2000-07-27 07:42:43 +0000 | [diff] [blame] | 10 | # like stdin can be read successfully by a forked |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 11 | # subprocess. |
| 12 | |
| 13 | def main(): |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 14 | print "Test popen2 module:" |
Fredrik Lundh | 9407e55 | 2000-07-27 07:42:43 +0000 | [diff] [blame] | 15 | try: |
| 16 | from os import popen |
| 17 | except ImportError: |
| 18 | # if we don't have os.popen, check that |
| 19 | # we have os.fork. if not, skip the test |
| 20 | # (by raising an ImportError) |
| 21 | from os import fork |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 22 | import popen2 |
| 23 | popen2._test() |
| 24 | |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 25 | |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 26 | def _test(): |
| 27 | # same test as popen2._test(), but using the os.popen*() API |
| 28 | print "Testing os module:" |
| 29 | import popen2 |
| 30 | cmd = "cat" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame^] | 31 | teststr = "ab cd\n" |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 32 | if os.name == "nt": |
| 33 | cmd = "more" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame^] | 34 | # "more" doesn't act the same way across Windows flavors, |
| 35 | # sometimes adding an extra newline at the start or the |
| 36 | # end. So we strip whitespace off both ends for comparison. |
| 37 | expected = teststr.strip() |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 38 | print "testing popen2..." |
| 39 | w, r = os.popen2(cmd) |
| 40 | w.write(teststr) |
| 41 | w.close() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame^] | 42 | got = r.read() |
| 43 | if got.strip() != expected: |
| 44 | raise ValueError("wrote %s read %s" % (`teststr`, `got`)) |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 45 | print "testing popen3..." |
| 46 | try: |
| 47 | w, r, e = os.popen3([cmd]) |
| 48 | except: |
| 49 | w, r, e = os.popen3(cmd) |
| 50 | w.write(teststr) |
| 51 | w.close() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame^] | 52 | got = r.read() |
| 53 | if got.strip() != expected: |
| 54 | raise ValueError("wrote %s read %s" % (`teststr`, `got`)) |
| 55 | got = e.read() |
| 56 | if got: |
| 57 | raise ValueError("unexected %s on stderr" % `got`) |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 58 | for inst in popen2._active[:]: |
| 59 | inst.wait() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame^] | 60 | if popen2._active: |
| 61 | raise ValueError("_active not empty") |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 62 | print "All OK" |
| 63 | |
| 64 | main() |
| 65 | _test() |