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