blob: f2b58d5e3d6484c88daee4b223e1912b3693a770 [file] [log] [blame]
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001import pipes
2import os
3import string
4import unittest
Antoine Pitrou9b14f602009-12-08 19:53:23 +00005from test.support import TESTFN, run_unittest, unlink, reap_children
Thomas Wouters47b49bf2007-08-30 22:15:33 +00006
7if os.name != 'posix':
Benjamin Petersone549ead2009-03-28 21:42:05 +00008 raise unittest.SkipTest('pipes module only works on posix')
Thomas Wouters47b49bf2007-08-30 22:15:33 +00009
10TESTFN2 = TESTFN + "2"
11
Thomas Woutersbca54802007-09-10 19:32:14 +000012# tr a-z A-Z is not portable, so make the ranges explicit
13s_command = 'tr %s %s' % (string.ascii_lowercase, string.ascii_uppercase)
14
Thomas Wouters47b49bf2007-08-30 22:15:33 +000015class SimplePipeTests(unittest.TestCase):
16 def tearDown(self):
17 for f in (TESTFN, TESTFN2):
18 unlink(f)
19
20 def testSimplePipe1(self):
21 t = pipes.Template()
Thomas Woutersbca54802007-09-10 19:32:14 +000022 t.append(s_command, pipes.STDIN_STDOUT)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000023 f = t.open(TESTFN, 'w')
24 f.write('hello world #1')
25 f.close()
Antoine Pitrou92f60ed2010-10-14 22:11:44 +000026 with open(TESTFN) as f:
27 self.assertEqual(f.read(), 'HELLO WORLD #1')
Thomas Wouters47b49bf2007-08-30 22:15:33 +000028
29 def testSimplePipe2(self):
Antoine Pitrou92f60ed2010-10-14 22:11:44 +000030 with open(TESTFN, 'w') as f:
31 f.write('hello world #2')
Thomas Wouters47b49bf2007-08-30 22:15:33 +000032 t = pipes.Template()
Thomas Woutersbca54802007-09-10 19:32:14 +000033 t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000034 t.copy(TESTFN, TESTFN2)
Antoine Pitrou92f60ed2010-10-14 22:11:44 +000035 with open(TESTFN2) as f:
36 self.assertEqual(f.read(), 'HELLO WORLD #2')
Thomas Wouters47b49bf2007-08-30 22:15:33 +000037
38 def testSimplePipe3(self):
Antoine Pitrou92f60ed2010-10-14 22:11:44 +000039 with open(TESTFN, 'w') as f:
40 f.write('hello world #2')
Thomas Wouters47b49bf2007-08-30 22:15:33 +000041 t = pipes.Template()
Thomas Woutersbca54802007-09-10 19:32:14 +000042 t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT)
Antoine Pitrou9b14f602009-12-08 19:53:23 +000043 f = t.open(TESTFN, 'r')
44 try:
45 self.assertEqual(f.read(), 'HELLO WORLD #2')
46 finally:
47 f.close()
Thomas Wouters47b49bf2007-08-30 22:15:33 +000048
49 def testEmptyPipeline1(self):
50 # copy through empty pipe
51 d = 'empty pipeline test COPY'
Antoine Pitrou92f60ed2010-10-14 22:11:44 +000052 with open(TESTFN, 'w') as f:
53 f.write(d)
54 with open(TESTFN2, 'w') as f:
55 f.write('')
Thomas Wouters47b49bf2007-08-30 22:15:33 +000056 t=pipes.Template()
57 t.copy(TESTFN, TESTFN2)
Antoine Pitrou92f60ed2010-10-14 22:11:44 +000058 with open(TESTFN2) as f:
59 self.assertEqual(f.read(), d)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000060
61 def testEmptyPipeline2(self):
62 # read through empty pipe
63 d = 'empty pipeline test READ'
Antoine Pitrou92f60ed2010-10-14 22:11:44 +000064 with open(TESTFN, 'w') as f:
65 f.write(d)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000066 t=pipes.Template()
Antoine Pitrou9b14f602009-12-08 19:53:23 +000067 f = t.open(TESTFN, 'r')
68 try:
69 self.assertEqual(f.read(), d)
70 finally:
71 f.close()
Thomas Wouters47b49bf2007-08-30 22:15:33 +000072
73 def testEmptyPipeline3(self):
74 # write through empty pipe
75 d = 'empty pipeline test WRITE'
76 t = pipes.Template()
Antoine Pitrou92f60ed2010-10-14 22:11:44 +000077 with t.open(TESTFN, 'w') as f:
78 f.write(d)
79 with open(TESTFN) as f:
80 self.assertEqual(f.read(), d)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000081
82 def testQuoting(self):
Georg Brandl8569e582010-05-19 20:57:08 +000083 safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
84 unsafe = '"`$\\!'
Thomas Wouters47b49bf2007-08-30 22:15:33 +000085
Georg Brandl8569e582010-05-19 20:57:08 +000086 self.assertEqual(pipes.quote(''), "''")
Thomas Wouters47b49bf2007-08-30 22:15:33 +000087 self.assertEqual(pipes.quote(safeunquoted), safeunquoted)
88 self.assertEqual(pipes.quote('test file name'), "'test file name'")
89 for u in unsafe:
90 self.assertEqual(pipes.quote('test%sname' % u),
91 "'test%sname'" % u)
92 for u in unsafe:
93 self.assertEqual(pipes.quote("test%s'name'" % u),
Georg Brandl8569e582010-05-19 20:57:08 +000094 "'test%s'\"'\"'name'\"'\"''" % u)
Benjamin Peterson21896a32010-03-21 22:03:03 +000095
Thomas Wouters47b49bf2007-08-30 22:15:33 +000096 def testRepr(self):
97 t = pipes.Template()
98 self.assertEqual(repr(t), "<Template instance, steps=[]>")
99 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
100 self.assertEqual(repr(t),
101 "<Template instance, steps=[('tr a-z A-Z', '--')]>")
102
103 def testSetDebug(self):
104 t = pipes.Template()
105 t.debug(False)
106 self.assertEqual(t.debugging, False)
107 t.debug(True)
108 self.assertEqual(t.debugging, True)
109
110 def testReadOpenSink(self):
111 # check calling open('r') on a pipe ending with
112 # a sink raises ValueError
113 t = pipes.Template()
114 t.append('boguscmd', pipes.SINK)
115 self.assertRaises(ValueError, t.open, 'bogusfile', 'r')
116
117 def testWriteOpenSource(self):
118 # check calling open('w') on a pipe ending with
119 # a source raises ValueError
120 t = pipes.Template()
121 t.prepend('boguscmd', pipes.SOURCE)
122 self.assertRaises(ValueError, t.open, 'bogusfile', 'w')
123
124 def testBadAppendOptions(self):
125 t = pipes.Template()
126
127 # try a non-string command
128 self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)
129
130 # try a type that isn't recognized
131 self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')
132
133 # shouldn't be able to append a source
134 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)
135
136 # check appending two sinks
137 t = pipes.Template()
138 t.append('boguscmd', pipes.SINK)
139 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)
140
141 # command needing file input but with no $IN
142 t = pipes.Template()
143 self.assertRaises(ValueError, t.append, 'boguscmd $OUT',
144 pipes.FILEIN_FILEOUT)
145 t = pipes.Template()
146 self.assertRaises(ValueError, t.append, 'boguscmd',
147 pipes.FILEIN_STDOUT)
148
149 # command needing file output but with no $OUT
150 t = pipes.Template()
151 self.assertRaises(ValueError, t.append, 'boguscmd $IN',
152 pipes.FILEIN_FILEOUT)
153 t = pipes.Template()
154 self.assertRaises(ValueError, t.append, 'boguscmd',
155 pipes.STDIN_FILEOUT)
156
157
158 def testBadPrependOptions(self):
159 t = pipes.Template()
160
161 # try a non-string command
162 self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)
163
164 # try a type that isn't recognized
165 self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')
166
167 # shouldn't be able to prepend a sink
168 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)
169
170 # check prepending two sources
171 t = pipes.Template()
172 t.prepend('boguscmd', pipes.SOURCE)
173 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)
174
175 # command needing file input but with no $IN
176 t = pipes.Template()
177 self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',
178 pipes.FILEIN_FILEOUT)
179 t = pipes.Template()
180 self.assertRaises(ValueError, t.prepend, 'boguscmd',
181 pipes.FILEIN_STDOUT)
182
183 # command needing file output but with no $OUT
184 t = pipes.Template()
185 self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',
186 pipes.FILEIN_FILEOUT)
187 t = pipes.Template()
188 self.assertRaises(ValueError, t.prepend, 'boguscmd',
189 pipes.STDIN_FILEOUT)
190
191 def testBadOpenMode(self):
192 t = pipes.Template()
193 self.assertRaises(ValueError, t.open, 'bogusfile', 'x')
194
195 def testClone(self):
196 t = pipes.Template()
197 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
198
199 u = t.clone()
200 self.assertNotEqual(id(t), id(u))
201 self.assertEqual(t.steps, u.steps)
202 self.assertNotEqual(id(t.steps), id(u.steps))
203 self.assertEqual(t.debugging, u.debugging)
204
205def test_main():
206 run_unittest(SimplePipeTests)
Antoine Pitrou9b14f602009-12-08 19:53:23 +0000207 reap_children()
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000208
209if __name__ == "__main__":
210 test_main()