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