Make test_pipes a little bit more robust.
[python.git] / Lib / test / test_pipes.py
blobd1053e8ae150a9342e1ff30dcaa07f3fd5c75647
1 import pipes
2 import os
3 import string
4 import unittest
5 from test.test_support import TESTFN, run_unittest, unlink, reap_children
7 if os.name != 'posix':
8 raise unittest.SkipTest('pipes module only works on posix')
10 TESTFN2 = TESTFN + "2"
12 # tr a-z A-Z is not portable, so make the ranges explicit
13 s_command = 'tr %s %s' % (string.ascii_lowercase, string.ascii_uppercase)
15 class SimplePipeTests(unittest.TestCase):
16 def tearDown(self):
17 for f in (TESTFN, TESTFN2):
18 unlink(f)
20 def testSimplePipe1(self):
21 t = pipes.Template()
22 t.append(s_command, pipes.STDIN_STDOUT)
23 f = t.open(TESTFN, 'w')
24 f.write('hello world #1')
25 f.close()
26 self.assertEqual(open(TESTFN).read(), 'HELLO WORLD #1')
28 def testSimplePipe2(self):
29 file(TESTFN, 'w').write('hello world #2')
30 t = pipes.Template()
31 t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT)
32 t.copy(TESTFN, TESTFN2)
33 self.assertEqual(open(TESTFN2).read(), 'HELLO WORLD #2')
35 def testSimplePipe3(self):
36 file(TESTFN, 'w').write('hello world #2')
37 t = pipes.Template()
38 t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT)
39 with t.open(TESTFN, 'r') as f:
40 self.assertEqual(f.read(), 'HELLO WORLD #2')
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)
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()
56 with t.open(TESTFN, 'r') as f:
57 self.assertEqual(f.read(), d)
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)
66 def testQuoting(self):
67 safeunquoted = string.ascii_letters + string.digits + '!@%_-+=:,./'
68 unsafe = '"`$\\'
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)
79 def testRepr(self):
80 t = pipes.Template()
81 self.assertEqual(repr(t), "<Template instance, steps=[]>")
82 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
83 self.assertEqual(repr(t),
84 "<Template instance, steps=[('tr a-z A-Z', '--')]>")
86 def testSetDebug(self):
87 t = pipes.Template()
88 t.debug(False)
89 self.assertEqual(t.debugging, False)
90 t.debug(True)
91 self.assertEqual(t.debugging, True)
93 def testReadOpenSink(self):
94 # check calling open('r') on a pipe ending with
95 # a sink raises ValueError
96 t = pipes.Template()
97 t.append('boguscmd', pipes.SINK)
98 self.assertRaises(ValueError, t.open, 'bogusfile', 'r')
100 def testWriteOpenSource(self):
101 # check calling open('w') on a pipe ending with
102 # a source raises ValueError
103 t = pipes.Template()
104 t.prepend('boguscmd', pipes.SOURCE)
105 self.assertRaises(ValueError, t.open, 'bogusfile', 'w')
107 def testBadAppendOptions(self):
108 t = pipes.Template()
110 # try a non-string command
111 self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)
113 # try a type that isn't recognized
114 self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')
116 # shouldn't be able to append a source
117 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)
119 # check appending two sinks
120 t = pipes.Template()
121 t.append('boguscmd', pipes.SINK)
122 self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)
124 # command needing file input but with no $IN
125 t = pipes.Template()
126 self.assertRaises(ValueError, t.append, 'boguscmd $OUT',
127 pipes.FILEIN_FILEOUT)
128 t = pipes.Template()
129 self.assertRaises(ValueError, t.append, 'boguscmd',
130 pipes.FILEIN_STDOUT)
132 # command needing file output but with no $OUT
133 t = pipes.Template()
134 self.assertRaises(ValueError, t.append, 'boguscmd $IN',
135 pipes.FILEIN_FILEOUT)
136 t = pipes.Template()
137 self.assertRaises(ValueError, t.append, 'boguscmd',
138 pipes.STDIN_FILEOUT)
141 def testBadPrependOptions(self):
142 t = pipes.Template()
144 # try a non-string command
145 self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)
147 # try a type that isn't recognized
148 self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')
150 # shouldn't be able to prepend a sink
151 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)
153 # check prepending two sources
154 t = pipes.Template()
155 t.prepend('boguscmd', pipes.SOURCE)
156 self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)
158 # command needing file input but with no $IN
159 t = pipes.Template()
160 self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',
161 pipes.FILEIN_FILEOUT)
162 t = pipes.Template()
163 self.assertRaises(ValueError, t.prepend, 'boguscmd',
164 pipes.FILEIN_STDOUT)
166 # command needing file output but with no $OUT
167 t = pipes.Template()
168 self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',
169 pipes.FILEIN_FILEOUT)
170 t = pipes.Template()
171 self.assertRaises(ValueError, t.prepend, 'boguscmd',
172 pipes.STDIN_FILEOUT)
174 def testBadOpenMode(self):
175 t = pipes.Template()
176 self.assertRaises(ValueError, t.open, 'bogusfile', 'x')
178 def testClone(self):
179 t = pipes.Template()
180 t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
182 u = t.clone()
183 self.assertNotEqual(id(t), id(u))
184 self.assertEqual(t.steps, u.steps)
185 self.assertNotEqual(id(t.steps), id(u.steps))
186 self.assertEqual(t.debugging, u.debugging)
188 def test_main():
189 run_unittest(SimplePipeTests)
190 reap_children()
192 if __name__ == "__main__":
193 test_main()