]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_popen2.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_popen2.py
CommitLineData
4710c53d 1#! /usr/bin/env python\r
2"""Test script for popen2.py"""\r
3\r
4import warnings\r
5warnings.filterwarnings("ignore", ".*popen2 module is deprecated.*",\r
6 DeprecationWarning)\r
7warnings.filterwarnings("ignore", "os\.popen. is deprecated.*",\r
8 DeprecationWarning)\r
9\r
10import os\r
11import sys\r
12import unittest\r
13import popen2\r
14\r
15from test.test_support import run_unittest, reap_children\r
16\r
17if 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
26try:\r
27 from os import popen\r
28 del popen\r
29except ImportError:\r
30 from os import fork\r
31 del fork\r
32\r
33class 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
126def test_main():\r
127 run_unittest(Popen2Test)\r
128\r
129if __name__ == "__main__":\r
130 test_main()\r