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