5
from test.support import TESTFN, run_unittest, unlink, TestSkipped
8
raise TestSkipped('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):
17
for f in (TESTFN, TESTFN2):
20
def testSimplePipe1(self):
22
t.append(s_command, pipes.STDIN_STDOUT)
23
f = t.open(TESTFN, 'w')
24
f.write('hello world #1')
26
self.assertEqual(open(TESTFN).read(), 'HELLO WORLD #1')
28
def testSimplePipe2(self):
29
open(TESTFN, 'w').write('hello world #2')
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
open(TESTFN, 'w').write('hello world #2')
38
t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT)
39
self.assertEqual(t.open(TESTFN, 'r').read(), 'HELLO WORLD #2')
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('')
47
t.copy(TESTFN, TESTFN2)
48
self.assertEqual(open(TESTFN2).read(), d)
50
def testEmptyPipeline2(self):
51
# read through empty pipe
52
d = 'empty pipeline test READ'
53
open(TESTFN, 'w').write(d)
55
self.assertEqual(t.open(TESTFN, 'r').read(), d)
57
def testEmptyPipeline3(self):
58
# write through empty pipe
59
d = 'empty pipeline test WRITE'
61
t.open(TESTFN, 'w').write(d)
62
self.assertEqual(open(TESTFN).read(), d)
64
def testQuoting(self):
65
safeunquoted = string.ascii_letters + string.digits + '!@%_-+=:,./'
68
self.assertEqual(pipes.quote(safeunquoted), safeunquoted)
69
self.assertEqual(pipes.quote('test file name'), "'test file name'")
71
self.assertEqual(pipes.quote('test%sname' % u),
74
self.assertEqual(pipes.quote("test%s'name'" % u),
75
'"test\\%s\'name\'"' % u)
79
self.assertEqual(repr(t), "<Template instance, steps=[]>")
80
t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
81
self.assertEqual(repr(t),
82
"<Template instance, steps=[('tr a-z A-Z', '--')]>")
84
def testSetDebug(self):
87
self.assertEqual(t.debugging, False)
89
self.assertEqual(t.debugging, True)
91
def testReadOpenSink(self):
92
# check calling open('r') on a pipe ending with
93
# a sink raises ValueError
95
t.append('boguscmd', pipes.SINK)
96
self.assertRaises(ValueError, t.open, 'bogusfile', 'r')
98
def testWriteOpenSource(self):
99
# check calling open('w') on a pipe ending with
100
# a source raises ValueError
102
t.prepend('boguscmd', pipes.SOURCE)
103
self.assertRaises(ValueError, t.open, 'bogusfile', 'w')
105
def testBadAppendOptions(self):
108
# try a non-string command
109
self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)
111
# try a type that isn't recognized
112
self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')
114
# shouldn't be able to append a source
115
self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)
117
# check appending two sinks
119
t.append('boguscmd', pipes.SINK)
120
self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)
122
# command needing file input but with no $IN
124
self.assertRaises(ValueError, t.append, 'boguscmd $OUT',
125
pipes.FILEIN_FILEOUT)
127
self.assertRaises(ValueError, t.append, 'boguscmd',
130
# command needing file output but with no $OUT
132
self.assertRaises(ValueError, t.append, 'boguscmd $IN',
133
pipes.FILEIN_FILEOUT)
135
self.assertRaises(ValueError, t.append, 'boguscmd',
139
def testBadPrependOptions(self):
142
# try a non-string command
143
self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)
145
# try a type that isn't recognized
146
self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')
148
# shouldn't be able to prepend a sink
149
self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)
151
# check prepending two sources
153
t.prepend('boguscmd', pipes.SOURCE)
154
self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)
156
# command needing file input but with no $IN
158
self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',
159
pipes.FILEIN_FILEOUT)
161
self.assertRaises(ValueError, t.prepend, 'boguscmd',
164
# command needing file output but with no $OUT
166
self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',
167
pipes.FILEIN_FILEOUT)
169
self.assertRaises(ValueError, t.prepend, 'boguscmd',
172
def testBadOpenMode(self):
174
self.assertRaises(ValueError, t.open, 'bogusfile', 'x')
178
t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
181
self.assertNotEqual(id(t), id(u))
182
self.assertEqual(t.steps, u.steps)
183
self.assertNotEqual(id(t.steps), id(u.steps))
184
self.assertEqual(t.debugging, u.debugging)
187
run_unittest(SimplePipeTests)
189
if __name__ == "__main__":