5 from test
.test_support
import TESTFN
, run_unittest
, unlink
, reap_children
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
):
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 file(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 file(TESTFN
, 'w').write('hello world #2')
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('')
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
)
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'
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
+ '!@%_-+=:,./'
70 self
.assertEqual(pipes
.quote(safeunquoted
), safeunquoted
)
71 self
.assertEqual(pipes
.quote('test file name'), "'test file name'")
73 self
.assertEqual(pipes
.quote('test%sname' % u
),
76 self
.assertEqual(pipes
.quote("test%s'name'" % u
),
77 '"test\\%s\'name\'"' % u
)
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
):
89 self
.assertEqual(t
.debugging
, False)
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
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
104 t
.prepend('boguscmd', pipes
.SOURCE
)
105 self
.assertRaises(ValueError, t
.open, 'bogusfile', 'w')
107 def testBadAppendOptions(self
):
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
121 t
.append('boguscmd', pipes
.SINK
)
122 self
.assertRaises(ValueError, t
.append
, 'boguscmd', pipes
.SINK
)
124 # command needing file input but with no $IN
126 self
.assertRaises(ValueError, t
.append
, 'boguscmd $OUT',
127 pipes
.FILEIN_FILEOUT
)
129 self
.assertRaises(ValueError, t
.append
, 'boguscmd',
132 # command needing file output but with no $OUT
134 self
.assertRaises(ValueError, t
.append
, 'boguscmd $IN',
135 pipes
.FILEIN_FILEOUT
)
137 self
.assertRaises(ValueError, t
.append
, 'boguscmd',
141 def testBadPrependOptions(self
):
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
155 t
.prepend('boguscmd', pipes
.SOURCE
)
156 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd', pipes
.SOURCE
)
158 # command needing file input but with no $IN
160 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd $OUT',
161 pipes
.FILEIN_FILEOUT
)
163 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd',
166 # command needing file output but with no $OUT
168 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd $IN',
169 pipes
.FILEIN_FILEOUT
)
171 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd',
174 def testBadOpenMode(self
):
176 self
.assertRaises(ValueError, t
.open, 'bogusfile', 'x')
180 t
.append('tr a-z A-Z', pipes
.STDIN_STDOUT
)
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
)
189 run_unittest(SimplePipeTests
)
192 if __name__
== "__main__":