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(''), "''")
71 self
.assertEqual(pipes
.quote(safeunquoted
), safeunquoted
)
72 self
.assertEqual(pipes
.quote('test file name'), "'test file name'")
74 self
.assertEqual(pipes
.quote('test%sname' % u
),
77 self
.assertEqual(pipes
.quote("test%s'name'" % u
),
78 "'test%s'\"'\"'name'\"'\"''" % u
)
82 self
.assertEqual(repr(t
), "<Template instance, steps=[]>")
83 t
.append('tr a-z A-Z', pipes
.STDIN_STDOUT
)
84 self
.assertEqual(repr(t
),
85 "<Template instance, steps=[('tr a-z A-Z', '--')]>")
87 def testSetDebug(self
):
90 self
.assertEqual(t
.debugging
, False)
92 self
.assertEqual(t
.debugging
, True)
94 def testReadOpenSink(self
):
95 # check calling open('r') on a pipe ending with
96 # a sink raises ValueError
98 t
.append('boguscmd', pipes
.SINK
)
99 self
.assertRaises(ValueError, t
.open, 'bogusfile', 'r')
101 def testWriteOpenSource(self
):
102 # check calling open('w') on a pipe ending with
103 # a source raises ValueError
105 t
.prepend('boguscmd', pipes
.SOURCE
)
106 self
.assertRaises(ValueError, t
.open, 'bogusfile', 'w')
108 def testBadAppendOptions(self
):
111 # try a non-string command
112 self
.assertRaises(TypeError, t
.append
, 7, pipes
.STDIN_STDOUT
)
114 # try a type that isn't recognized
115 self
.assertRaises(ValueError, t
.append
, 'boguscmd', 'xx')
117 # shouldn't be able to append a source
118 self
.assertRaises(ValueError, t
.append
, 'boguscmd', pipes
.SOURCE
)
120 # check appending two sinks
122 t
.append('boguscmd', pipes
.SINK
)
123 self
.assertRaises(ValueError, t
.append
, 'boguscmd', pipes
.SINK
)
125 # command needing file input but with no $IN
127 self
.assertRaises(ValueError, t
.append
, 'boguscmd $OUT',
128 pipes
.FILEIN_FILEOUT
)
130 self
.assertRaises(ValueError, t
.append
, 'boguscmd',
133 # command needing file output but with no $OUT
135 self
.assertRaises(ValueError, t
.append
, 'boguscmd $IN',
136 pipes
.FILEIN_FILEOUT
)
138 self
.assertRaises(ValueError, t
.append
, 'boguscmd',
142 def testBadPrependOptions(self
):
145 # try a non-string command
146 self
.assertRaises(TypeError, t
.prepend
, 7, pipes
.STDIN_STDOUT
)
148 # try a type that isn't recognized
149 self
.assertRaises(ValueError, t
.prepend
, 'tr a-z A-Z', 'xx')
151 # shouldn't be able to prepend a sink
152 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd', pipes
.SINK
)
154 # check prepending two sources
156 t
.prepend('boguscmd', pipes
.SOURCE
)
157 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd', pipes
.SOURCE
)
159 # command needing file input but with no $IN
161 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd $OUT',
162 pipes
.FILEIN_FILEOUT
)
164 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd',
167 # command needing file output but with no $OUT
169 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd $IN',
170 pipes
.FILEIN_FILEOUT
)
172 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd',
175 def testBadOpenMode(self
):
177 self
.assertRaises(ValueError, t
.open, 'bogusfile', 'x')
181 t
.append('tr a-z A-Z', pipes
.STDIN_STDOUT
)
184 self
.assertNotEqual(id(t
), id(u
))
185 self
.assertEqual(t
.steps
, u
.steps
)
186 self
.assertNotEqual(id(t
.steps
), id(u
.steps
))
187 self
.assertEqual(t
.debugging
, u
.debugging
)
190 run_unittest(SimplePipeTests
)
193 if __name__
== "__main__":