Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 2 | """Test script for popen2.py""" |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 3 | |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 4 | import os |
Moshe Zadka | fc3fc33 | 2001-01-30 18:35:32 +0000 | [diff] [blame] | 5 | import sys |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | import popen2 |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 8 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 9 | from test.test_support import TestSkipped, run_unittest, reap_children |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 10 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 11 | if sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos': |
| 12 | # Locks get messed up or something. Generally we're supposed |
| 13 | # to avoid mixing "posix" fork & exec with native threads, and |
| 14 | # they may be right about that after all. |
| 15 | raise TestSkipped("popen2() doesn't work on " + sys.platform) |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 16 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 17 | # if we don't have os.popen, check that |
| 18 | # we have os.fork. if not, skip the test |
| 19 | # (by raising an ImportError) |
| 20 | try: |
| 21 | from os import popen |
| 22 | del popen |
| 23 | except ImportError: |
| 24 | from os import fork |
| 25 | del fork |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 26 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 27 | class Popen2Test(unittest.TestCase): |
| 28 | cmd = "cat" |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 29 | if os.name == "nt": |
| 30 | cmd = "more" |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 31 | teststr = "ab cd\n" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 32 | # "more" doesn't act the same way across Windows flavors, |
| 33 | # sometimes adding an extra newline at the start or the |
| 34 | # end. So we strip whitespace off both ends for comparison. |
| 35 | expected = teststr.strip() |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 36 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 37 | def setUp(self): |
| 38 | popen2._cleanup() |
| 39 | # When the test runs, there shouldn't be any open pipes |
| 40 | self.assertFalse(popen2._active, "Active pipes when test starts" + |
| 41 | repr([c.cmd for c in popen2._active])) |
| 42 | |
| 43 | def tearDown(self): |
| 44 | for inst in popen2._active: |
| 45 | inst.wait() |
| 46 | popen2._cleanup() |
| 47 | self.assertFalse(popen2._active, "_active not empty") |
| 48 | reap_children() |
| 49 | |
| 50 | def validate_output(self, teststr, expected_out, r, w, e=None): |
| 51 | w.write(teststr) |
| 52 | w.close() |
| 53 | got = r.read() |
| 54 | self.assertEquals(expected_out, got.strip(), "wrote %r read %r" % |
| 55 | (teststr, got)) |
| 56 | |
| 57 | if e is not None: |
| 58 | got = e.read() |
| 59 | self.assertFalse(got, "unexpected %r on stderr" % got) |
| 60 | |
| 61 | def test_popen2(self): |
| 62 | r, w = popen2.popen2(self.cmd) |
| 63 | self.validate_output(self.teststr, self.expected, r, w) |
| 64 | |
| 65 | def test_popen3(self): |
| 66 | if os.name == 'posix': |
| 67 | r, w, e = popen2.popen3([self.cmd]) |
| 68 | self.validate_output(self.teststr, self.expected, r, w, e) |
| 69 | |
| 70 | r, w, e = popen2.popen3(self.cmd) |
| 71 | self.validate_output(self.teststr, self.expected, r, w, e) |
| 72 | |
| 73 | def test_os_popen2(self): |
| 74 | # same test as test_popen2(), but using the os.popen*() API |
| 75 | w, r = os.popen2(self.cmd) |
| 76 | self.validate_output(self.teststr, self.expected, r, w) |
| 77 | |
| 78 | def test_os_popen3(self): |
| 79 | # same test as test_popen3(), but using the os.popen*() API |
| 80 | if os.name == 'posix': |
| 81 | w, r, e = os.popen3([self.cmd]) |
| 82 | self.validate_output(self.teststr, self.expected, r, w, e) |
| 83 | |
| 84 | w, r, e = os.popen3(self.cmd) |
| 85 | self.validate_output(self.teststr, self.expected, r, w, e) |
| 86 | |
| 87 | |
| 88 | def test_main(): |
| 89 | run_unittest(Popen2Test) |
| 90 | |
| 91 | if __name__ == "__main__": |
| 92 | test_main() |