blob: 3d31fceb4626946a464d5b6745d6540800d98e68 [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()
26 self.assertEqual(open(TESTFN).read(), 'HELLO WORLD #1')
27
28 def testSimplePipe2(self):
29 open(TESTFN, 'w').write('hello world #2')
30 t = pipes.Template()
Thomas Woutersbca54802007-09-10 19:32:14 +000031 t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT)
Thomas Wouters47b49bf2007-08-30 22:15:33 +000032 t.copy(TESTFN, TESTFN2)
33 self.assertEqual(open(TESTFN2).read(), 'HELLO WORLD #2')
34
35 def testSimplePipe3(self):
36 open(TESTFN, 'w').write('hello world #2')
37 t = pipes.Template()
Thomas Woutersbca54802007-09-10 19:32:14 +000038 t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT)
Antoine Pitrou9b14f602009-12-08 19:53:23 +000039 f = t.open(TESTFN, 'r')
40 try:
41 self.assertEqual(f.read(), 'HELLO WORLD #2')
42 finally:
43 f.close()
Thomas Wouters47b49bf2007-08-30 22:15:33 +000044
45 def testEmptyPipeline1(self):
46 # copy through empty pipe
47 d = 'empty pipeline test COPY'
48 open(TESTFN, 'w').write(d)
49 open(TESTFN2, 'w').write('')
50 t=pipes.Template()
51 t.copy(TESTFN, TESTFN2)
52 self.assertEqual(open(TESTFN2).read(), d)
53
54 def testEmptyPipeline2(self):
55 # read through empty pipe
56 d = 'empty pipeline test READ'
57 open(TESTFN, 'w').write(d)
58 t=pipes.Template()
Antoine Pitrou9b14f602009-12-08 19:53:23 +000059 f = t.open(TESTFN, 'r')
60 try:
61 self.assertEqual(f.read(), d)
62 finally:
63 f.close()
Thomas Wouters47b49bf2007-08-30 22:15:33 +000064
65 def testEmptyPipeline3(self):
66 # write through empty pipe
67 d = 'empty pipeline test WRITE'
68 t = pipes.Template()
69 t.open(TESTFN, 'w').write(d)
70 self.assertEqual(open(TESTFN).read(), d)
71
72 def testQuoting(self):
Georg Brandl8569e582010-05-19 20:57:08 +000073 safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
74 unsafe = '"`$\\!'
Thomas Wouters47b49bf2007-08-30 22:15:33 +000075
Georg Brandl8569e582010-05-19 20:57:08 +000076 self.assertEqual(pipes.quote(''), "''")
Thomas Wouters47b49bf2007-08-30 22:15:33 +000077 self.assertEqual(pipes.quote(safeunquoted), safeunquoted)
78 self.assertEqual(pipes.quote('test file name'), "'test file name'")
79 for u in unsafe:
80 self.assertEqual(pipes.quote('test%sname' % u),
81 "'test%sname'" % u)
82 for u in unsafe:
83 self.assertEqual(pipes.quote("test%s'name'" % u),
Georg Brandl8569e582010-05-19 20:57:08 +000084 "'test%s'\"'\"'name'\"'\"''" % u)
Benjamin Peterson21896a32010-03-21 22:03:03 +000085
Thomas Wouters47b49bf2007-08-30 22:15:33 +000086 def testRepr(self):
87 t = pipes.Template()
88 self.assertEqual(repr(t), "<Template instance, steps=[]>")
89 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
90 self.assertEqual(repr(t),
91 "<Template instance, steps=[('tr a-z A-Z', '--')]>")
92
93 def testSetDebug(self):
94 t = pipes.Template()
95 t.debug(False)
96 self.assertEqual(t.debugging, False)
97 t.debug(True)
98 self.assertEqual(t.debugging, True)
99
100 def testReadOpenSink(self):
101 # check calling open('r') on a pipe ending with
102 # a sink raises ValueError
103 t = pipes.Template()
104 t.append('boguscmd', pipes.SINK)
105 self.assertRaises(ValueError, t.open, 'bogusfile', 'r')
106
107 def testWriteOpenSource(self):
108 # check calling open('w') on a pipe ending with
109 # a source raises ValueError
110 t = pipes.Template()
111 t.prepend('boguscmd', pipes.SOURCE)
112 self.assertRaises(ValueError, t.open, 'bogusfile', 'w')
113
114 def testBadAppendOptions(self):
115 t = pipes.Template()
116
117 # try a non-string command
118 self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)
119
120 # try a type that isn't recognized
121 self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')
122
123 # shouldn't be able to append a source
124 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)
125
126 # check appending two sinks
127 t = pipes.Template()
128 t.append('boguscmd', pipes.SINK)
129 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)
130
131 # command needing file input but with no $IN
132 t = pipes.Template()
133 self.assertRaises(ValueError, t.append, 'boguscmd $OUT',
134 pipes.FILEIN_FILEOUT)
135 t = pipes.Template()
136 self.assertRaises(ValueError, t.append, 'boguscmd',
137 pipes.FILEIN_STDOUT)
138
139 # command needing file output but with no $OUT
140 t = pipes.Template()
141 self.assertRaises(ValueError, t.append, 'boguscmd $IN',
142 pipes.FILEIN_FILEOUT)
143 t = pipes.Template()
144 self.assertRaises(ValueError, t.append, 'boguscmd',
145 pipes.STDIN_FILEOUT)
146
147
148 def testBadPrependOptions(self):
149 t = pipes.Template()
150
151 # try a non-string command
152 self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)
153
154 # try a type that isn't recognized
155 self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')
156
157 # shouldn't be able to prepend a sink
158 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)
159
160 # check prepending two sources
161 t = pipes.Template()
162 t.prepend('boguscmd', pipes.SOURCE)
163 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)
164
165 # command needing file input but with no $IN
166 t = pipes.Template()
167 self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',
168 pipes.FILEIN_FILEOUT)
169 t = pipes.Template()
170 self.assertRaises(ValueError, t.prepend, 'boguscmd',
171 pipes.FILEIN_STDOUT)
172
173 # command needing file output but with no $OUT
174 t = pipes.Template()
175 self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',
176 pipes.FILEIN_FILEOUT)
177 t = pipes.Template()
178 self.assertRaises(ValueError, t.prepend, 'boguscmd',
179 pipes.STDIN_FILEOUT)
180
181 def testBadOpenMode(self):
182 t = pipes.Template()
183 self.assertRaises(ValueError, t.open, 'bogusfile', 'x')
184
185 def testClone(self):
186 t = pipes.Template()
187 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
188
189 u = t.clone()
190 self.assertNotEqual(id(t), id(u))
191 self.assertEqual(t.steps, u.steps)
192 self.assertNotEqual(id(t.steps), id(u.steps))
193 self.assertEqual(t.debugging, u.debugging)
194
195def test_main():
196 run_unittest(SimplePipeTests)
Antoine Pitrou9b14f602009-12-08 19:53:23 +0000197 reap_children()
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000198
199if __name__ == "__main__":
200 test_main()