blob: b816a3b81b44a12857036e0590ca59943cfd0445 [file] [log] [blame]
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001import pipes
2import os
3import string
4import unittest
Benjamin Petersone549ead2009-03-28 21:42:05 +00005from test.support import TESTFN, run_unittest, unlink
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 Pitrouea5d8272010-10-14 22:14:36 +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 Pitrouea5d8272010-10-14 22:14:36 +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 Pitrouea5d8272010-10-14 22:14:36 +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 Pitrouea5d8272010-10-14 22:14:36 +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)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000043 self.assertEqual(t.open(TESTFN, 'r').read(), 'HELLO WORLD #2')
44
45 def testEmptyPipeline1(self):
46 # copy through empty pipe
47 d = 'empty pipeline test COPY'
Antoine Pitrouea5d8272010-10-14 22:14:36 +000048 with open(TESTFN, 'w') as f:
49 f.write(d)
50 with open(TESTFN2, 'w') as f:
51 f.write('')
Thomas Wouters47b49bf2007-08-30 22:15:33 +000052 t=pipes.Template()
53 t.copy(TESTFN, TESTFN2)
Antoine Pitrouea5d8272010-10-14 22:14:36 +000054 with open(TESTFN2) as f:
55 self.assertEqual(f.read(), d)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000056
57 def testEmptyPipeline2(self):
58 # read through empty pipe
59 d = 'empty pipeline test READ'
Antoine Pitrouea5d8272010-10-14 22:14:36 +000060 with open(TESTFN, 'w') as f:
61 f.write(d)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000062 t=pipes.Template()
63 self.assertEqual(t.open(TESTFN, 'r').read(), d)
64
65 def testEmptyPipeline3(self):
66 # write through empty pipe
67 d = 'empty pipeline test WRITE'
68 t = pipes.Template()
Antoine Pitrouea5d8272010-10-14 22:14:36 +000069 with t.open(TESTFN, 'w') as f:
70 f.write(d)
71 with open(TESTFN) as f:
72 self.assertEqual(f.read(), d)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000073
74 def testQuoting(self):
Georg Brandl6c8583f2010-05-19 21:22:58 +000075 safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
76 unsafe = '"`$\\!'
Thomas Wouters47b49bf2007-08-30 22:15:33 +000077
Georg Brandl6c8583f2010-05-19 21:22:58 +000078 self.assertEqual(pipes.quote(''), "''")
Thomas Wouters47b49bf2007-08-30 22:15:33 +000079 self.assertEqual(pipes.quote(safeunquoted), safeunquoted)
80 self.assertEqual(pipes.quote('test file name'), "'test file name'")
81 for u in unsafe:
82 self.assertEqual(pipes.quote('test%sname' % u),
83 "'test%sname'" % u)
84 for u in unsafe:
85 self.assertEqual(pipes.quote("test%s'name'" % u),
Georg Brandl6c8583f2010-05-19 21:22:58 +000086 "'test%s'\"'\"'name'\"'\"''" % u)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000087
88 def testRepr(self):
89 t = pipes.Template()
90 self.assertEqual(repr(t), "<Template instance, steps=[]>")
91 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
92 self.assertEqual(repr(t),
93 "<Template instance, steps=[('tr a-z A-Z', '--')]>")
94
95 def testSetDebug(self):
96 t = pipes.Template()
97 t.debug(False)
98 self.assertEqual(t.debugging, False)
99 t.debug(True)
100 self.assertEqual(t.debugging, True)
101
102 def testReadOpenSink(self):
103 # check calling open('r') on a pipe ending with
104 # a sink raises ValueError
105 t = pipes.Template()
106 t.append('boguscmd', pipes.SINK)
107 self.assertRaises(ValueError, t.open, 'bogusfile', 'r')
108
109 def testWriteOpenSource(self):
110 # check calling open('w') on a pipe ending with
111 # a source raises ValueError
112 t = pipes.Template()
113 t.prepend('boguscmd', pipes.SOURCE)
114 self.assertRaises(ValueError, t.open, 'bogusfile', 'w')
115
116 def testBadAppendOptions(self):
117 t = pipes.Template()
118
119 # try a non-string command
120 self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)
121
122 # try a type that isn't recognized
123 self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')
124
125 # shouldn't be able to append a source
126 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)
127
128 # check appending two sinks
129 t = pipes.Template()
130 t.append('boguscmd', pipes.SINK)
131 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)
132
133 # command needing file input but with no $IN
134 t = pipes.Template()
135 self.assertRaises(ValueError, t.append, 'boguscmd $OUT',
136 pipes.FILEIN_FILEOUT)
137 t = pipes.Template()
138 self.assertRaises(ValueError, t.append, 'boguscmd',
139 pipes.FILEIN_STDOUT)
140
141 # command needing file output but with no $OUT
142 t = pipes.Template()
143 self.assertRaises(ValueError, t.append, 'boguscmd $IN',
144 pipes.FILEIN_FILEOUT)
145 t = pipes.Template()
146 self.assertRaises(ValueError, t.append, 'boguscmd',
147 pipes.STDIN_FILEOUT)
148
149
150 def testBadPrependOptions(self):
151 t = pipes.Template()
152
153 # try a non-string command
154 self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)
155
156 # try a type that isn't recognized
157 self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')
158
159 # shouldn't be able to prepend a sink
160 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)
161
162 # check prepending two sources
163 t = pipes.Template()
164 t.prepend('boguscmd', pipes.SOURCE)
165 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)
166
167 # command needing file input but with no $IN
168 t = pipes.Template()
169 self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',
170 pipes.FILEIN_FILEOUT)
171 t = pipes.Template()
172 self.assertRaises(ValueError, t.prepend, 'boguscmd',
173 pipes.FILEIN_STDOUT)
174
175 # command needing file output but with no $OUT
176 t = pipes.Template()
177 self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',
178 pipes.FILEIN_FILEOUT)
179 t = pipes.Template()
180 self.assertRaises(ValueError, t.prepend, 'boguscmd',
181 pipes.STDIN_FILEOUT)
182
183 def testBadOpenMode(self):
184 t = pipes.Template()
185 self.assertRaises(ValueError, t.open, 'bogusfile', 'x')
186
187 def testClone(self):
188 t = pipes.Template()
189 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
190
191 u = t.clone()
192 self.assertNotEqual(id(t), id(u))
193 self.assertEqual(t.steps, u.steps)
194 self.assertNotEqual(id(t.steps), id(u.steps))
195 self.assertEqual(t.debugging, u.debugging)
196
197def test_main():
198 run_unittest(SimplePipeTests)
199
200if __name__ == "__main__":
201 test_main()