]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_pipes.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_pipes.py
CommitLineData
4710c53d 1import pipes\r
2import os\r
3import string\r
4import unittest\r
5from test.test_support import TESTFN, run_unittest, unlink, reap_children\r
6\r
7if os.name != 'posix':\r
8 raise unittest.SkipTest('pipes module only works on posix')\r
9\r
10TESTFN2 = TESTFN + "2"\r
11\r
12# tr a-z A-Z is not portable, so make the ranges explicit\r
13s_command = 'tr %s %s' % (string.ascii_lowercase, string.ascii_uppercase)\r
14\r
15class SimplePipeTests(unittest.TestCase):\r
16 def tearDown(self):\r
17 for f in (TESTFN, TESTFN2):\r
18 unlink(f)\r
19\r
20 def testSimplePipe1(self):\r
21 t = pipes.Template()\r
22 t.append(s_command, pipes.STDIN_STDOUT)\r
23 f = t.open(TESTFN, 'w')\r
24 f.write('hello world #1')\r
25 f.close()\r
26 with open(TESTFN) as f:\r
27 self.assertEqual(f.read(), 'HELLO WORLD #1')\r
28\r
29 def testSimplePipe2(self):\r
30 with open(TESTFN, 'w') as f:\r
31 f.write('hello world #2')\r
32 t = pipes.Template()\r
33 t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT)\r
34 t.copy(TESTFN, TESTFN2)\r
35 with open(TESTFN2) as f:\r
36 self.assertEqual(f.read(), 'HELLO WORLD #2')\r
37\r
38 def testSimplePipe3(self):\r
39 with open(TESTFN, 'w') as f:\r
40 f.write('hello world #2')\r
41 t = pipes.Template()\r
42 t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT)\r
43 with t.open(TESTFN, 'r') as f:\r
44 self.assertEqual(f.read(), 'HELLO WORLD #2')\r
45\r
46 def testEmptyPipeline1(self):\r
47 # copy through empty pipe\r
48 d = 'empty pipeline test COPY'\r
49 with open(TESTFN, 'w') as f:\r
50 f.write(d)\r
51 with open(TESTFN2, 'w') as f:\r
52 f.write('')\r
53 t=pipes.Template()\r
54 t.copy(TESTFN, TESTFN2)\r
55 with open(TESTFN2) as f:\r
56 self.assertEqual(f.read(), d)\r
57\r
58 def testEmptyPipeline2(self):\r
59 # read through empty pipe\r
60 d = 'empty pipeline test READ'\r
61 with open(TESTFN, 'w') as f:\r
62 f.write(d)\r
63 t=pipes.Template()\r
64 with t.open(TESTFN, 'r') as f:\r
65 self.assertEqual(f.read(), d)\r
66\r
67 def testEmptyPipeline3(self):\r
68 # write through empty pipe\r
69 d = 'empty pipeline test WRITE'\r
70 t = pipes.Template()\r
71 with t.open(TESTFN, 'w') as f:\r
72 f.write(d)\r
73 with open(TESTFN) as f:\r
74 self.assertEqual(f.read(), d)\r
75\r
76 def testQuoting(self):\r
77 safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'\r
78 unsafe = '"`$\\!'\r
79\r
80 self.assertEqual(pipes.quote(''), "''")\r
81 self.assertEqual(pipes.quote(safeunquoted), safeunquoted)\r
82 self.assertEqual(pipes.quote('test file name'), "'test file name'")\r
83 for u in unsafe:\r
84 self.assertEqual(pipes.quote('test%sname' % u),\r
85 "'test%sname'" % u)\r
86 for u in unsafe:\r
87 self.assertEqual(pipes.quote("test%s'name'" % u),\r
88 "'test%s'\"'\"'name'\"'\"''" % u)\r
89\r
90 def testRepr(self):\r
91 t = pipes.Template()\r
92 self.assertEqual(repr(t), "<Template instance, steps=[]>")\r
93 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)\r
94 self.assertEqual(repr(t),\r
95 "<Template instance, steps=[('tr a-z A-Z', '--')]>")\r
96\r
97 def testSetDebug(self):\r
98 t = pipes.Template()\r
99 t.debug(False)\r
100 self.assertEqual(t.debugging, False)\r
101 t.debug(True)\r
102 self.assertEqual(t.debugging, True)\r
103\r
104 def testReadOpenSink(self):\r
105 # check calling open('r') on a pipe ending with\r
106 # a sink raises ValueError\r
107 t = pipes.Template()\r
108 t.append('boguscmd', pipes.SINK)\r
109 self.assertRaises(ValueError, t.open, 'bogusfile', 'r')\r
110\r
111 def testWriteOpenSource(self):\r
112 # check calling open('w') on a pipe ending with\r
113 # a source raises ValueError\r
114 t = pipes.Template()\r
115 t.prepend('boguscmd', pipes.SOURCE)\r
116 self.assertRaises(ValueError, t.open, 'bogusfile', 'w')\r
117\r
118 def testBadAppendOptions(self):\r
119 t = pipes.Template()\r
120\r
121 # try a non-string command\r
122 self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)\r
123\r
124 # try a type that isn't recognized\r
125 self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')\r
126\r
127 # shouldn't be able to append a source\r
128 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)\r
129\r
130 # check appending two sinks\r
131 t = pipes.Template()\r
132 t.append('boguscmd', pipes.SINK)\r
133 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)\r
134\r
135 # command needing file input but with no $IN\r
136 t = pipes.Template()\r
137 self.assertRaises(ValueError, t.append, 'boguscmd $OUT',\r
138 pipes.FILEIN_FILEOUT)\r
139 t = pipes.Template()\r
140 self.assertRaises(ValueError, t.append, 'boguscmd',\r
141 pipes.FILEIN_STDOUT)\r
142\r
143 # command needing file output but with no $OUT\r
144 t = pipes.Template()\r
145 self.assertRaises(ValueError, t.append, 'boguscmd $IN',\r
146 pipes.FILEIN_FILEOUT)\r
147 t = pipes.Template()\r
148 self.assertRaises(ValueError, t.append, 'boguscmd',\r
149 pipes.STDIN_FILEOUT)\r
150\r
151\r
152 def testBadPrependOptions(self):\r
153 t = pipes.Template()\r
154\r
155 # try a non-string command\r
156 self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)\r
157\r
158 # try a type that isn't recognized\r
159 self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')\r
160\r
161 # shouldn't be able to prepend a sink\r
162 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)\r
163\r
164 # check prepending two sources\r
165 t = pipes.Template()\r
166 t.prepend('boguscmd', pipes.SOURCE)\r
167 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)\r
168\r
169 # command needing file input but with no $IN\r
170 t = pipes.Template()\r
171 self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',\r
172 pipes.FILEIN_FILEOUT)\r
173 t = pipes.Template()\r
174 self.assertRaises(ValueError, t.prepend, 'boguscmd',\r
175 pipes.FILEIN_STDOUT)\r
176\r
177 # command needing file output but with no $OUT\r
178 t = pipes.Template()\r
179 self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',\r
180 pipes.FILEIN_FILEOUT)\r
181 t = pipes.Template()\r
182 self.assertRaises(ValueError, t.prepend, 'boguscmd',\r
183 pipes.STDIN_FILEOUT)\r
184\r
185 def testBadOpenMode(self):\r
186 t = pipes.Template()\r
187 self.assertRaises(ValueError, t.open, 'bogusfile', 'x')\r
188\r
189 def testClone(self):\r
190 t = pipes.Template()\r
191 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)\r
192\r
193 u = t.clone()\r
194 self.assertNotEqual(id(t), id(u))\r
195 self.assertEqual(t.steps, u.steps)\r
196 self.assertNotEqual(id(t.steps), id(u.steps))\r
197 self.assertEqual(t.debugging, u.debugging)\r
198\r
199def test_main():\r
200 run_unittest(SimplePipeTests)\r
201 reap_children()\r
202\r
203if __name__ == "__main__":\r
204 test_main()\r