blob: 6a7b45fb4666e75ff8f4c5c3122e455756cbb530 [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
Thomas Wouters47b49bf2007-08-30 22:15:33 +000082 def testRepr(self):
83 t = pipes.Template()
84 self.assertEqual(repr(t), "<Template instance, steps=[]>")
85 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
86 self.assertEqual(repr(t),
87 "<Template instance, steps=[('tr a-z A-Z', '--')]>")
88
89 def testSetDebug(self):
90 t = pipes.Template()
91 t.debug(False)
92 self.assertEqual(t.debugging, False)
93 t.debug(True)
94 self.assertEqual(t.debugging, True)
95
96 def testReadOpenSink(self):
97 # check calling open('r') on a pipe ending with
98 # a sink raises ValueError
99 t = pipes.Template()
100 t.append('boguscmd', pipes.SINK)
101 self.assertRaises(ValueError, t.open, 'bogusfile', 'r')
102
103 def testWriteOpenSource(self):
104 # check calling open('w') on a pipe ending with
105 # a source raises ValueError
106 t = pipes.Template()
107 t.prepend('boguscmd', pipes.SOURCE)
108 self.assertRaises(ValueError, t.open, 'bogusfile', 'w')
109
110 def testBadAppendOptions(self):
111 t = pipes.Template()
112
113 # try a non-string command
114 self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)
115
116 # try a type that isn't recognized
117 self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')
118
119 # shouldn't be able to append a source
120 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)
121
122 # check appending two sinks
123 t = pipes.Template()
124 t.append('boguscmd', pipes.SINK)
125 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)
126
127 # command needing file input but with no $IN
128 t = pipes.Template()
129 self.assertRaises(ValueError, t.append, 'boguscmd $OUT',
130 pipes.FILEIN_FILEOUT)
131 t = pipes.Template()
132 self.assertRaises(ValueError, t.append, 'boguscmd',
133 pipes.FILEIN_STDOUT)
134
135 # command needing file output but with no $OUT
136 t = pipes.Template()
137 self.assertRaises(ValueError, t.append, 'boguscmd $IN',
138 pipes.FILEIN_FILEOUT)
139 t = pipes.Template()
140 self.assertRaises(ValueError, t.append, 'boguscmd',
141 pipes.STDIN_FILEOUT)
142
143
144 def testBadPrependOptions(self):
145 t = pipes.Template()
146
147 # try a non-string command
148 self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)
149
150 # try a type that isn't recognized
151 self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')
152
153 # shouldn't be able to prepend a sink
154 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)
155
156 # check prepending two sources
157 t = pipes.Template()
158 t.prepend('boguscmd', pipes.SOURCE)
159 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)
160
161 # command needing file input but with no $IN
162 t = pipes.Template()
163 self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',
164 pipes.FILEIN_FILEOUT)
165 t = pipes.Template()
166 self.assertRaises(ValueError, t.prepend, 'boguscmd',
167 pipes.FILEIN_STDOUT)
168
169 # command needing file output but with no $OUT
170 t = pipes.Template()
171 self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',
172 pipes.FILEIN_FILEOUT)
173 t = pipes.Template()
174 self.assertRaises(ValueError, t.prepend, 'boguscmd',
175 pipes.STDIN_FILEOUT)
176
177 def testBadOpenMode(self):
178 t = pipes.Template()
179 self.assertRaises(ValueError, t.open, 'bogusfile', 'x')
180
181 def testClone(self):
182 t = pipes.Template()
183 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
184
185 u = t.clone()
186 self.assertNotEqual(id(t), id(u))
187 self.assertEqual(t.steps, u.steps)
188 self.assertNotEqual(id(t.steps), id(u.steps))
189 self.assertEqual(t.debugging, u.debugging)
190
191def test_main():
192 run_unittest(SimplePipeTests)
Antoine Pitrou9b14f602009-12-08 19:53:23 +0000193 reap_children()
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000194
195if __name__ == "__main__":
196 test_main()