blob: d5b886f87ef324b9b8fe55d842563ac8e76e94c1 [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 + '@%_-+=:,./'
Éric Araujoef1e94a82011-08-09 23:03:43 +020084 unicode_sample = '\xe9\xe0\xdf' # e + acute accent, a + grave, sharp s
85 unsafe = '"`$\\!' + unicode_sample
Thomas Wouters47b49bf2007-08-30 22:15:33 +000086
Georg Brandl8569e582010-05-19 20:57:08 +000087 self.assertEqual(pipes.quote(''), "''")
Thomas Wouters47b49bf2007-08-30 22:15:33 +000088 self.assertEqual(pipes.quote(safeunquoted), safeunquoted)
89 self.assertEqual(pipes.quote('test file name'), "'test file name'")
90 for u in unsafe:
91 self.assertEqual(pipes.quote('test%sname' % u),
92 "'test%sname'" % u)
93 for u in unsafe:
94 self.assertEqual(pipes.quote("test%s'name'" % u),
Georg Brandl8569e582010-05-19 20:57:08 +000095 "'test%s'\"'\"'name'\"'\"''" % u)
Benjamin Peterson21896a32010-03-21 22:03:03 +000096
Thomas Wouters47b49bf2007-08-30 22:15:33 +000097 def testRepr(self):
98 t = pipes.Template()
99 self.assertEqual(repr(t), "<Template instance, steps=[]>")
100 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
101 self.assertEqual(repr(t),
102 "<Template instance, steps=[('tr a-z A-Z', '--')]>")
103
104 def testSetDebug(self):
105 t = pipes.Template()
106 t.debug(False)
107 self.assertEqual(t.debugging, False)
108 t.debug(True)
109 self.assertEqual(t.debugging, True)
110
111 def testReadOpenSink(self):
112 # check calling open('r') on a pipe ending with
113 # a sink raises ValueError
114 t = pipes.Template()
115 t.append('boguscmd', pipes.SINK)
116 self.assertRaises(ValueError, t.open, 'bogusfile', 'r')
117
118 def testWriteOpenSource(self):
119 # check calling open('w') on a pipe ending with
120 # a source raises ValueError
121 t = pipes.Template()
122 t.prepend('boguscmd', pipes.SOURCE)
123 self.assertRaises(ValueError, t.open, 'bogusfile', 'w')
124
125 def testBadAppendOptions(self):
126 t = pipes.Template()
127
128 # try a non-string command
129 self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)
130
131 # try a type that isn't recognized
132 self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')
133
134 # shouldn't be able to append a source
135 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)
136
137 # check appending two sinks
138 t = pipes.Template()
139 t.append('boguscmd', pipes.SINK)
140 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)
141
142 # command needing file input but with no $IN
143 t = pipes.Template()
144 self.assertRaises(ValueError, t.append, 'boguscmd $OUT',
145 pipes.FILEIN_FILEOUT)
146 t = pipes.Template()
147 self.assertRaises(ValueError, t.append, 'boguscmd',
148 pipes.FILEIN_STDOUT)
149
150 # command needing file output but with no $OUT
151 t = pipes.Template()
152 self.assertRaises(ValueError, t.append, 'boguscmd $IN',
153 pipes.FILEIN_FILEOUT)
154 t = pipes.Template()
155 self.assertRaises(ValueError, t.append, 'boguscmd',
156 pipes.STDIN_FILEOUT)
157
158
159 def testBadPrependOptions(self):
160 t = pipes.Template()
161
162 # try a non-string command
163 self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)
164
165 # try a type that isn't recognized
166 self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')
167
168 # shouldn't be able to prepend a sink
169 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)
170
171 # check prepending two sources
172 t = pipes.Template()
173 t.prepend('boguscmd', pipes.SOURCE)
174 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)
175
176 # command needing file input but with no $IN
177 t = pipes.Template()
178 self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',
179 pipes.FILEIN_FILEOUT)
180 t = pipes.Template()
181 self.assertRaises(ValueError, t.prepend, 'boguscmd',
182 pipes.FILEIN_STDOUT)
183
184 # command needing file output but with no $OUT
185 t = pipes.Template()
186 self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',
187 pipes.FILEIN_FILEOUT)
188 t = pipes.Template()
189 self.assertRaises(ValueError, t.prepend, 'boguscmd',
190 pipes.STDIN_FILEOUT)
191
192 def testBadOpenMode(self):
193 t = pipes.Template()
194 self.assertRaises(ValueError, t.open, 'bogusfile', 'x')
195
196 def testClone(self):
197 t = pipes.Template()
198 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
199
200 u = t.clone()
201 self.assertNotEqual(id(t), id(u))
202 self.assertEqual(t.steps, u.steps)
203 self.assertNotEqual(id(t.steps), id(u.steps))
204 self.assertEqual(t.debugging, u.debugging)
205
206def test_main():
207 run_unittest(SimplePipeTests)
Antoine Pitrou9b14f602009-12-08 19:53:23 +0000208 reap_children()
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000209
210if __name__ == "__main__":
211 test_main()