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