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