Guido van Rossum | 9a744a9 | 1999-04-08 20:27:54 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test script for popen2.py |
| 3 | Christian Tismer |
| 4 | """ |
| 5 | |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +0000 | [diff] [blame] | 6 | import os |
| 7 | |
Guido van Rossum | 9a744a9 | 1999-04-08 20:27:54 +0000 | [diff] [blame] | 8 | # popen2 contains its own testing routine |
| 9 | # which is especially useful to see if open files |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +0000 | [diff] [blame] | 10 | # like stdin can be read successfully by a forked |
Guido van Rossum | 9a744a9 | 1999-04-08 20:27:54 +0000 | [diff] [blame] | 11 | # subprocess. |
| 12 | |
| 13 | def main(): |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +0000 | [diff] [blame] | 14 | print "Test popen2 module:" |
| 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 | 9a744a9 | 1999-04-08 20:27:54 +0000 | [diff] [blame] | 22 | import popen2 |
| 23 | popen2._test() |
| 24 | |
Guido van Rossum | 9a744a9 | 1999-04-08 20:27:54 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +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" |
Guido van Rossum | e2ab145 | 2000-09-05 04:49:50 +0000 | [diff] [blame^] | 31 | teststr = "ab cd\n" |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +0000 | [diff] [blame] | 32 | if os.name == "nt": |
| 33 | cmd = "more" |
Guido van Rossum | e2ab145 | 2000-09-05 04:49:50 +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() |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +0000 | [diff] [blame] | 38 | print "testing popen2..." |
| 39 | w, r = os.popen2(cmd) |
| 40 | w.write(teststr) |
| 41 | w.close() |
Guido van Rossum | e2ab145 | 2000-09-05 04:49:50 +0000 | [diff] [blame^] | 42 | got = r.read() |
| 43 | if got.strip() != expected: |
| 44 | raise ValueError("wrote %s read %s" % (`teststr`, `got`)) |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +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() |
Guido van Rossum | e2ab145 | 2000-09-05 04:49:50 +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`) |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +0000 | [diff] [blame] | 58 | for inst in popen2._active[:]: |
| 59 | inst.wait() |
Guido van Rossum | e2ab145 | 2000-09-05 04:49:50 +0000 | [diff] [blame^] | 60 | if popen2._active: |
| 61 | raise ValueError("_active not empty") |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +0000 | [diff] [blame] | 62 | print "All OK" |
| 63 | |
| 64 | main() |
| 65 | _test() |