blob: a6385984031e8779f8c2981e0f897b0e04dbcd63 [file] [log] [blame]
Georg Brandl27d9ee32007-08-30 10:38:56 +00001import pipes
2import os
3import string
4import unittest
Antoine Pitrou29dcdab2009-12-08 19:46:38 +00005from test.test_support import TESTFN, run_unittest, unlink, reap_children
Georg Brandl0226d852007-08-30 12:32:23 +00006
7if os.name != 'posix':
Benjamin Petersonbec087f2009-03-26 21:10:30 +00008 raise unittest.SkipTest('pipes module only works on posix')
Georg Brandl27d9ee32007-08-30 10:38:56 +00009
10TESTFN2 = TESTFN + "2"
11
Martin v. Löwis0a048192007-09-10 06:18:32 +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
Georg Brandl27d9ee32007-08-30 10:38:56 +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()
Martin v. Löwis0a048192007-09-10 06:18:32 +000022 t.append(s_command, pipes.STDIN_STDOUT)
Georg Brandl27d9ee32007-08-30 10:38:56 +000023 f = t.open(TESTFN, 'w')
24 f.write('hello world #1')
25 f.close()
26 self.assertEqual(open(TESTFN).read(), 'HELLO WORLD #1')
27
28 def testSimplePipe2(self):
29 file(TESTFN, 'w').write('hello world #2')
30 t = pipes.Template()
Martin v. Löwis0a048192007-09-10 06:18:32 +000031 t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT)
Georg Brandl27d9ee32007-08-30 10:38:56 +000032 t.copy(TESTFN, TESTFN2)
33 self.assertEqual(open(TESTFN2).read(), 'HELLO WORLD #2')
34
35 def testSimplePipe3(self):
36 file(TESTFN, 'w').write('hello world #2')
37 t = pipes.Template()
Martin v. Löwis0a048192007-09-10 06:18:32 +000038 t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT)
Antoine Pitrou29dcdab2009-12-08 19:46:38 +000039 with t.open(TESTFN, 'r') as f:
40 self.assertEqual(f.read(), 'HELLO WORLD #2')
Georg Brandl27d9ee32007-08-30 10:38:56 +000041
42 def testEmptyPipeline1(self):
43 # copy through empty pipe
44 d = 'empty pipeline test COPY'
45 file(TESTFN, 'w').write(d)
46 file(TESTFN2, 'w').write('')
47 t=pipes.Template()
48 t.copy(TESTFN, TESTFN2)
49 self.assertEqual(open(TESTFN2).read(), d)
50
51 def testEmptyPipeline2(self):
52 # read through empty pipe
53 d = 'empty pipeline test READ'
54 file(TESTFN, 'w').write(d)
55 t=pipes.Template()
Antoine Pitrou29dcdab2009-12-08 19:46:38 +000056 with t.open(TESTFN, 'r') as f:
57 self.assertEqual(f.read(), d)
Georg Brandl27d9ee32007-08-30 10:38:56 +000058
59 def testEmptyPipeline3(self):
60 # write through empty pipe
61 d = 'empty pipeline test WRITE'
62 t = pipes.Template()
63 t.open(TESTFN, 'w').write(d)
64 self.assertEqual(open(TESTFN).read(), d)
65
66 def testQuoting(self):
67 safeunquoted = string.ascii_letters + string.digits + '!@%_-+=:,./'
68 unsafe = '"`$\\'
69
70 self.assertEqual(pipes.quote(safeunquoted), safeunquoted)
71 self.assertEqual(pipes.quote('test file name'), "'test file name'")
72 for u in unsafe:
73 self.assertEqual(pipes.quote('test%sname' % u),
74 "'test%sname'" % u)
75 for u in unsafe:
76 self.assertEqual(pipes.quote("test%s'name'" % u),
77 '"test\\%s\'name\'"' % u)
78
Jack Diederich5cac46d2010-02-22 21:27:38 +000079 self.assertEqual(pipes.quote(''), "''")
80
Georg Brandl27d9ee32007-08-30 10:38:56 +000081 def testRepr(self):
82 t = pipes.Template()
83 self.assertEqual(repr(t), "<Template instance, steps=[]>")
84 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
85 self.assertEqual(repr(t),
86 "<Template instance, steps=[('tr a-z A-Z', '--')]>")
87
88 def testSetDebug(self):
89 t = pipes.Template()
90 t.debug(False)
91 self.assertEqual(t.debugging, False)
92 t.debug(True)
93 self.assertEqual(t.debugging, True)
94
95 def testReadOpenSink(self):
96 # check calling open('r') on a pipe ending with
97 # a sink raises ValueError
98 t = pipes.Template()
99 t.append('boguscmd', pipes.SINK)
100 self.assertRaises(ValueError, t.open, 'bogusfile', 'r')
101
102 def testWriteOpenSource(self):
103 # check calling open('w') on a pipe ending with
104 # a source raises ValueError
105 t = pipes.Template()
106 t.prepend('boguscmd', pipes.SOURCE)
107 self.assertRaises(ValueError, t.open, 'bogusfile', 'w')
108
109 def testBadAppendOptions(self):
110 t = pipes.Template()
111
112 # try a non-string command
113 self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)
114
115 # try a type that isn't recognized
116 self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')
117
118 # shouldn't be able to append a source
119 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)
120
121 # check appending two sinks
122 t = pipes.Template()
123 t.append('boguscmd', pipes.SINK)
124 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)
125
126 # command needing file input but with no $IN
127 t = pipes.Template()
128 self.assertRaises(ValueError, t.append, 'boguscmd $OUT',
129 pipes.FILEIN_FILEOUT)
130 t = pipes.Template()
131 self.assertRaises(ValueError, t.append, 'boguscmd',
132 pipes.FILEIN_STDOUT)
133
134 # command needing file output but with no $OUT
135 t = pipes.Template()
136 self.assertRaises(ValueError, t.append, 'boguscmd $IN',
137 pipes.FILEIN_FILEOUT)
138 t = pipes.Template()
139 self.assertRaises(ValueError, t.append, 'boguscmd',
140 pipes.STDIN_FILEOUT)
141
142
143 def testBadPrependOptions(self):
144 t = pipes.Template()
145
146 # try a non-string command
147 self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)
148
149 # try a type that isn't recognized
150 self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')
151
152 # shouldn't be able to prepend a sink
153 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)
154
155 # check prepending two sources
156 t = pipes.Template()
157 t.prepend('boguscmd', pipes.SOURCE)
158 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)
159
160 # command needing file input but with no $IN
161 t = pipes.Template()
162 self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',
163 pipes.FILEIN_FILEOUT)
164 t = pipes.Template()
165 self.assertRaises(ValueError, t.prepend, 'boguscmd',
166 pipes.FILEIN_STDOUT)
167
168 # command needing file output but with no $OUT
169 t = pipes.Template()
170 self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',
171 pipes.FILEIN_FILEOUT)
172 t = pipes.Template()
173 self.assertRaises(ValueError, t.prepend, 'boguscmd',
174 pipes.STDIN_FILEOUT)
175
176 def testBadOpenMode(self):
177 t = pipes.Template()
178 self.assertRaises(ValueError, t.open, 'bogusfile', 'x')
179
180 def testClone(self):
181 t = pipes.Template()
182 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
183
184 u = t.clone()
185 self.assertNotEqual(id(t), id(u))
186 self.assertEqual(t.steps, u.steps)
187 self.assertNotEqual(id(t.steps), id(u.steps))
188 self.assertEqual(t.debugging, u.debugging)
189
190def test_main():
191 run_unittest(SimplePipeTests)
Antoine Pitrou29dcdab2009-12-08 19:46:38 +0000192 reap_children()
Georg Brandl27d9ee32007-08-30 10:38:56 +0000193
194if __name__ == "__main__":
195 test_main()