Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 1 | """Test script for popen2.py""" |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 2 | |
Neal Norwitz | 42dd86b | 2007-05-11 06:57:33 +0000 | [diff] [blame] | 3 | import warnings |
| 4 | warnings.filterwarnings("ignore", ".*popen2 module is deprecated.*", |
| 5 | DeprecationWarning) |
| 6 | warnings.filterwarnings("ignore", "os\.popen. is deprecated.*", |
| 7 | DeprecationWarning) |
| 8 | |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 9 | import os |
Moshe Zadka | fc3fc33 | 2001-01-30 18:35:32 +0000 | [diff] [blame] | 10 | import sys |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 11 | import unittest |
| 12 | import popen2 |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 13 | |
Benjamin Peterson | bec087f | 2009-03-26 21:10:30 +0000 | [diff] [blame] | 14 | from test.test_support import run_unittest, reap_children |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 15 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 16 | if sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos': |
| 17 | # Locks get messed up or something. Generally we're supposed |
| 18 | # to avoid mixing "posix" fork & exec with native threads, and |
| 19 | # they may be right about that after all. |
Benjamin Peterson | bec087f | 2009-03-26 21:10:30 +0000 | [diff] [blame] | 20 | raise unittest.SkipTest("popen2() doesn't work on " + sys.platform) |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 21 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 22 | # if we don't have os.popen, check that |
| 23 | # we have os.fork. if not, skip the test |
| 24 | # (by raising an ImportError) |
| 25 | try: |
| 26 | from os import popen |
| 27 | del popen |
| 28 | except ImportError: |
| 29 | from os import fork |
| 30 | del fork |
Guido van Rossum | 59e4f37 | 1999-03-11 13:26:23 +0000 | [diff] [blame] | 31 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 32 | class Popen2Test(unittest.TestCase): |
| 33 | cmd = "cat" |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 34 | if os.name == "nt": |
| 35 | cmd = "more" |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 36 | teststr = "ab cd\n" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 37 | # "more" doesn't act the same way across Windows flavors, |
| 38 | # sometimes adding an extra newline at the start or the |
| 39 | # end. So we strip whitespace off both ends for comparison. |
| 40 | expected = teststr.strip() |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 41 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 42 | def setUp(self): |
| 43 | popen2._cleanup() |
| 44 | # When the test runs, there shouldn't be any open pipes |
| 45 | self.assertFalse(popen2._active, "Active pipes when test starts" + |
| 46 | repr([c.cmd for c in popen2._active])) |
| 47 | |
| 48 | def tearDown(self): |
| 49 | for inst in popen2._active: |
| 50 | inst.wait() |
| 51 | popen2._cleanup() |
Florent Xicluna | a36e245 | 2010-03-04 15:57:20 +0000 | [diff] [blame] | 52 | self.assertFalse(popen2._active, "popen2._active not empty") |
| 53 | # The os.popen*() API delegates to the subprocess module (on Unix) |
| 54 | import subprocess |
| 55 | for inst in subprocess._active: |
| 56 | inst.wait() |
| 57 | subprocess._cleanup() |
| 58 | self.assertFalse(subprocess._active, "subprocess._active not empty") |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 59 | reap_children() |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 60 | |
| 61 | def validate_output(self, teststr, expected_out, r, w, e=None): |
| 62 | w.write(teststr) |
| 63 | w.close() |
| 64 | got = r.read() |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 65 | self.assertEqual(expected_out, got.strip(), "wrote %r read %r" % |
| 66 | (teststr, got)) |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 67 | |
| 68 | if e is not None: |
| 69 | got = e.read() |
| 70 | self.assertFalse(got, "unexpected %r on stderr" % got) |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 71 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 72 | def test_popen2(self): |
| 73 | r, w = popen2.popen2(self.cmd) |
| 74 | self.validate_output(self.teststr, self.expected, r, w) |
| 75 | |
| 76 | def test_popen3(self): |
| 77 | if os.name == 'posix': |
| 78 | r, w, e = popen2.popen3([self.cmd]) |
| 79 | self.validate_output(self.teststr, self.expected, r, w, e) |
| 80 | |
| 81 | r, w, e = popen2.popen3(self.cmd) |
| 82 | self.validate_output(self.teststr, self.expected, r, w, e) |
| 83 | |
| 84 | def test_os_popen2(self): |
| 85 | # same test as test_popen2(), but using the os.popen*() API |
Philip Jenvey | 8b90204 | 2009-09-29 19:10:15 +0000 | [diff] [blame] | 86 | if os.name == 'posix': |
| 87 | w, r = os.popen2([self.cmd]) |
| 88 | self.validate_output(self.teststr, self.expected, r, w) |
| 89 | |
| 90 | w, r = os.popen2(["echo", self.teststr]) |
| 91 | got = r.read() |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 92 | self.assertEqual(got, self.teststr + "\n") |
Philip Jenvey | 8b90204 | 2009-09-29 19:10:15 +0000 | [diff] [blame] | 93 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 94 | w, r = os.popen2(self.cmd) |
| 95 | self.validate_output(self.teststr, self.expected, r, w) |
| 96 | |
| 97 | def test_os_popen3(self): |
| 98 | # same test as test_popen3(), but using the os.popen*() API |
| 99 | if os.name == 'posix': |
| 100 | w, r, e = os.popen3([self.cmd]) |
| 101 | self.validate_output(self.teststr, self.expected, r, w, e) |
| 102 | |
Philip Jenvey | 8b90204 | 2009-09-29 19:10:15 +0000 | [diff] [blame] | 103 | w, r, e = os.popen3(["echo", self.teststr]) |
| 104 | got = r.read() |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 105 | self.assertEqual(got, self.teststr + "\n") |
Philip Jenvey | 8b90204 | 2009-09-29 19:10:15 +0000 | [diff] [blame] | 106 | got = e.read() |
| 107 | self.assertFalse(got, "unexpected %r on stderr" % got) |
| 108 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 109 | w, r, e = os.popen3(self.cmd) |
| 110 | self.validate_output(self.teststr, self.expected, r, w, e) |
| 111 | |
Philip Jenvey | 8b90204 | 2009-09-29 19:10:15 +0000 | [diff] [blame] | 112 | def test_os_popen4(self): |
| 113 | if os.name == 'posix': |
| 114 | w, r = os.popen4([self.cmd]) |
| 115 | self.validate_output(self.teststr, self.expected, r, w) |
| 116 | |
| 117 | w, r = os.popen4(["echo", self.teststr]) |
| 118 | got = r.read() |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 119 | self.assertEqual(got, self.teststr + "\n") |
Philip Jenvey | 8b90204 | 2009-09-29 19:10:15 +0000 | [diff] [blame] | 120 | |
| 121 | w, r = os.popen4(self.cmd) |
| 122 | self.validate_output(self.teststr, self.expected, r, w) |
| 123 | |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 124 | |
Collin Winter | 1b4145d | 2007-03-16 21:13:35 +0000 | [diff] [blame] | 125 | def test_main(): |
| 126 | run_unittest(Popen2Test) |
| 127 | |
| 128 | if __name__ == "__main__": |
| 129 | test_main() |